From 8005b41f5959d1dbf8431265d0d598d422b33d32 Mon Sep 17 00:00:00 2001 From: Jens Pitkanen Date: Wed, 7 Aug 2019 22:48:34 +0300 Subject: [PATCH] Add blood drips while walking --- Assets/Scripts/Effects/BloodDripper.cs | 38 +++++++++++++++++++++ Assets/Scripts/Effects/BloodDripper.cs.meta | 11 ++++++ Assets/Scripts/Effects/BloodLauncher.cs | 11 +++--- Assets/Scripts/Effects/BloodParticle.cs | 7 ++-- 4 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 Assets/Scripts/Effects/BloodDripper.cs create mode 100644 Assets/Scripts/Effects/BloodDripper.cs.meta diff --git a/Assets/Scripts/Effects/BloodDripper.cs b/Assets/Scripts/Effects/BloodDripper.cs new file mode 100644 index 0000000..608fff6 --- /dev/null +++ b/Assets/Scripts/Effects/BloodDripper.cs @@ -0,0 +1,38 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace Saltosion.OneWeapon { + public class BloodDripper : MonoBehaviour { + public Vector2 DripOffset = new Vector2(0, -0.5f); + public int DripCount = 5; + public float DropInterval = 0.5f; + + private Rigidbody2D ParentBody; + private int Drips = 0; + private bool Step = false; + private float LastDropTime; + + private void Start() { + ParentBody = transform.parent.GetComponent(); + DropInterval *= Random.value + 0.5f; + LastDropTime = Random.value * DropInterval + Time.time; + } + + private void Update() { + if (ParentBody.velocity.magnitude > 0.1 && Time.time - LastDropTime > DropInterval) { + Step = !Step; + bool WalkingSideWays = Mathf.Abs(Vector2.Dot(ParentBody.velocity, new Vector2(1, 0))) > 0.5; + Vector2 Offset = DripOffset + new Vector2(-ParentBody.velocity.y, ParentBody.velocity.x).normalized * + (WalkingSideWays ? 0.1f : 0.2f) * + (Step ? 1f : -1f); + BloodLauncher.Splatter(ParentBody.position + Offset, Vector2.zero, 1 + (int)(Random.value * 5), 0f, 360f, false); + Drips++; + if (Drips >= DripCount) { + Destroy(gameObject); + } + LastDropTime = Time.time; + } + } + } +} diff --git a/Assets/Scripts/Effects/BloodDripper.cs.meta b/Assets/Scripts/Effects/BloodDripper.cs.meta new file mode 100644 index 0000000..caaf630 --- /dev/null +++ b/Assets/Scripts/Effects/BloodDripper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4d1575b7aca5970469125348cb12707d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Effects/BloodLauncher.cs b/Assets/Scripts/Effects/BloodLauncher.cs index b62a57b..02b4b8d 100644 --- a/Assets/Scripts/Effects/BloodLauncher.cs +++ b/Assets/Scripts/Effects/BloodLauncher.cs @@ -12,15 +12,15 @@ namespace Saltosion.OneWeapon { public int MaxParticles; public bool DebugLaunch = false; - public static void Splatter(Vector2 origin, Vector2 direction, int particleCount, float force, float degrees) { - LaunchParticles(Singleton.GetGorePrefab(), origin, direction, particleCount, force, degrees); + public static void Splatter(Vector2 origin, Vector2 direction, int particleCount, float force, float degrees, bool sticky = true) { + LaunchParticles(Singleton.GetGorePrefab(), origin, direction, particleCount, force, degrees, sticky); } - public static void DebrisExplode(Vector2 origin, Vector2 direction, int particleCount, float force, float degrees) { - LaunchParticles(Singleton.StructuralParticlePrefab, origin, direction, particleCount, force, degrees); + public static void DebrisExplode(Vector2 origin, Vector2 direction, int particleCount, float force, float degrees, bool sticky = true) { + LaunchParticles(Singleton.StructuralParticlePrefab, origin, direction, particleCount, force, degrees, sticky); } - private static void LaunchParticles(GameObject prefab, Vector2 origin, Vector2 direction, int particleCount, float force, float degrees) { + private static void LaunchParticles(GameObject prefab, Vector2 origin, Vector2 direction, int particleCount, float force, float degrees, bool sticky) { for (int i = 0; i < (int)(particleCount * Singleton.GetParticleCountMultiplier()); i++) { if (Singleton.ParticleRoot.childCount >= Singleton.MaxParticles) { Destroy(Singleton.ParticleRoot.GetChild((int)(Random.value * Singleton.ParticleRoot.childCount)).gameObject); @@ -32,6 +32,7 @@ namespace Saltosion.OneWeapon { float Offset = Mathf.Atan2(direction.y, direction.x); float Radians = Offset + Mathf.Pow(Cone * (Random.value - 0.5f) * 2f, 2f) * Mathf.PI * 2.0f; Particle.LaunchForce = new Vector2(Mathf.Cos(Radians), Mathf.Sin(Radians)) * Intensity; + Particle.GetsStuck = sticky && Random.value < 0.5; } } diff --git a/Assets/Scripts/Effects/BloodParticle.cs b/Assets/Scripts/Effects/BloodParticle.cs index 39d6413..00af2a8 100644 --- a/Assets/Scripts/Effects/BloodParticle.cs +++ b/Assets/Scripts/Effects/BloodParticle.cs @@ -8,16 +8,15 @@ namespace Saltosion.OneWeapon { public Vector2 LaunchForce = new Vector2(); public bool Settled = false; + public bool GetsStuck = true; public Sprite[] Sprites; private Rigidbody2D CurrentlyStuckOn = null; private Vector2 Velocity; - private bool GetsStuck; private void Start() { transform.localScale = new Vector2(0.02f, 0.02f) + Random.value * new Vector2(0.10f, 0.10f); Velocity = LaunchForce + new Vector2(Random.value - 0.5f, Random.value - 0.5f); - GetsStuck = Random.value > 0.5; if (Sprites.Length > 0) { Renderer.sprite = Sprites[(int)(Random.value * int.MaxValue) % Sprites.Length]; Renderer.flipX = Random.value < 0.5; @@ -35,6 +34,10 @@ namespace Saltosion.OneWeapon { if (CurrentlyStuckOn != null && GetsStuck) { transform.parent = CurrentlyStuckOn.transform; GetComponentInChildren().sortingLayerName = "Blood Particles (On characters)"; + if (transform.parent.tag == "Player" && Random.value < 0.5) { + // Half of all blood particles are "drippy" so they drip away over time on the floor + gameObject.AddComponent(typeof(BloodDripper)); + } } Destroy(GetComponent()); Destroy(this);