BloodAndGore/Assets/Scripts/Effects/DebrisLauncher.cs

70 lines
3.2 KiB
C#
Raw Permalink Normal View History

2019-08-18 23:26:49 +02:00
using UnityEngine;
using UnityEngine.SceneManagement;
2019-08-14 16:17:48 +02:00
using Saltosion.OneWeapon.Utils;
2019-08-03 18:14:47 +02:00
2019-08-14 16:17:48 +02:00
namespace Saltosion.OneWeapon.Effects {
2019-08-14 23:27:14 +02:00
public enum DebrisType {
Blood, Structural, Wood
}
public class DebrisLauncher : MonoBehaviour {
private static DebrisLauncher Singleton;
2019-08-21 23:16:24 +02:00
private static int SingletonSceneIndex = -1;
2019-08-18 23:26:49 +02:00
2019-08-04 16:14:48 +02:00
public GameObject StructuralParticlePrefab;
2019-08-14 23:27:14 +02:00
public GameObject WoodParticlePrefab;
2019-08-03 18:14:47 +02:00
public GameObject BloodParticlePrefab;
2019-08-04 01:45:32 +02:00
public GameObject CensoredBloodParticlePrefab;
2019-08-03 18:14:47 +02:00
public Transform ParticleRoot;
public int MaxParticles;
2019-08-04 00:48:41 +02:00
public bool DebugLaunch = false;
2019-08-03 18:14:47 +02:00
2019-08-14 23:27:14 +02:00
public static void Splatter(DebrisType Type, Vector2 origin, Vector2 direction, int particleCount, float force, float degrees, bool sticky = true) {
int ParticleCount = (int)(particleCount * (Type == DebrisType.Blood ? Singleton.GetBloodParticleCountMultiplier() : 1));
LaunchParticles(Singleton.GetDebrisPrefab(Type), origin, direction, particleCount, force, degrees, sticky);
2019-08-04 16:14:48 +02:00
}
2019-08-07 21:48:34 +02:00
private static void LaunchParticles(GameObject prefab, Vector2 origin, Vector2 direction, int particleCount, float force, float degrees, bool sticky) {
2019-08-14 23:27:14 +02:00
for (int i = 0; i < particleCount; i++) {
2019-08-03 18:14:47 +02:00
if (Singleton.ParticleRoot.childCount >= Singleton.MaxParticles) {
Destroy(Singleton.ParticleRoot.GetChild((int)(Random.value * Singleton.ParticleRoot.childCount)).gameObject);
}
2019-08-04 16:14:48 +02:00
GameObject Obj = Instantiate(prefab, origin, new Quaternion(), Singleton.ParticleRoot);
2019-08-14 23:27:14 +02:00
DebrisParticle Particle = Obj.GetComponent<DebrisParticle>();
2019-08-07 20:48:44 +02:00
float Intensity = Mathf.Pow(Random.value, 0.8f) * force;
2019-08-03 18:14:47 +02:00
float Cone = degrees / 360.0f;
float Offset = Mathf.Atan2(direction.y, direction.x);
float Radians = Offset + Mathf.Pow(Cone * (Random.value - 0.5f) * 2f, 2f) * Mathf.PI * 2.0f;
2019-08-03 18:14:47 +02:00
Particle.LaunchForce = new Vector2(Mathf.Cos(Radians), Mathf.Sin(Radians)) * Intensity;
2019-08-07 21:48:34 +02:00
Particle.GetsStuck = sticky && Random.value < 0.5;
2019-08-03 18:14:47 +02:00
}
}
private void Start() {
2019-08-18 23:26:49 +02:00
int Index = SceneManager.GetActiveScene().buildIndex;
if (SingletonSceneIndex == Index) {
Debug.LogWarning($"The scene '{SceneManager.GetActiveScene().name}' has multiple DebrisLaunchers!\nOnly the last to run Start() will be used.");
}
SingletonSceneIndex = Index;
2019-08-03 18:14:47 +02:00
Singleton = this;
}
2019-08-04 01:45:32 +02:00
2019-08-14 23:27:14 +02:00
private GameObject GetDebrisPrefab(DebrisType Type) {
switch (Type) {
case DebrisType.Blood:
return Options.CensorGore ? CensoredBloodParticlePrefab : BloodParticlePrefab;
case DebrisType.Structural:
return StructuralParticlePrefab;
case DebrisType.Wood:
return WoodParticlePrefab;
default:
return CensoredBloodParticlePrefab;
}
2019-08-04 01:45:32 +02:00
}
2019-08-14 23:27:14 +02:00
private float GetBloodParticleCountMultiplier() {
2019-08-07 20:48:44 +02:00
return Options.CensorGore ? 0.1f : 1.5f;
2019-08-04 01:45:32 +02:00
}
2019-08-03 18:14:47 +02:00
}
}