BloodAndGore/Assets/Scripts/Effects/BloodParticle.cs

64 lines
2.8 KiB
C#
Raw Normal View History

2019-08-03 18:14:47 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2019-08-14 16:17:48 +02:00
namespace Saltosion.OneWeapon.Effects {
2019-08-03 18:14:47 +02:00
public class BloodParticle : MonoBehaviour {
public SpriteRenderer Renderer;
2019-08-03 18:14:47 +02:00
public Vector2 LaunchForce = new Vector2();
public bool Settled = false;
2019-08-07 21:48:34 +02:00
public bool GetsStuck = true;
public Sprite[] Sprites;
2019-08-03 18:14:47 +02:00
private Rigidbody2D CurrentlyStuckOn = null;
private Vector2 Velocity;
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);
if (Sprites.Length > 0) {
Renderer.sprite = Sprites[(int)(Random.value * int.MaxValue) % Sprites.Length];
Renderer.flipX = Random.value < 0.5;
Renderer.flipY = Random.value < 0.5;
}
2019-08-03 18:14:47 +02:00
}
private void FixedUpdate() {
Velocity *= 1.0f - Time.fixedDeltaTime * 20.0f;
if (CurrentlyStuckOn != null) {
bool IsStatic = CurrentlyStuckOn.bodyType == RigidbodyType2D.Static;
2019-08-04 00:48:41 +02:00
Velocity *= 1.0f - Time.fixedDeltaTime * (IsStatic ? 50.0f : 15.0f);
2019-08-03 18:14:47 +02:00
}
if (Velocity.magnitude < 0.1) {
if (CurrentlyStuckOn != null && GetsStuck) {
transform.parent = CurrentlyStuckOn.transform;
2019-08-04 00:48:41 +02:00
GetComponentInChildren<SpriteRenderer>().sortingLayerName = "Blood Particles (On characters)";
2019-08-07 21:48:34 +02:00
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));
}
2019-08-03 18:14:47 +02:00
}
Destroy(GetComponent<BoxCollider2D>());
Destroy(this);
}
Vector2 AppliedMovement = Velocity * Time.fixedDeltaTime;
RaycastHit2D Hit = Physics2D.Raycast(transform.position, AppliedMovement, AppliedMovement.magnitude, LayerMask.NameToLayer("Bloodphobic"));
if (Hit.rigidbody != null && Hit.rigidbody.gameObject == Hit.collider.gameObject) {
2019-08-03 18:14:47 +02:00
bool IsStatic = Hit.rigidbody.bodyType == RigidbodyType2D.Static;
Velocity = Velocity.normalized * Mathf.Min(IsStatic ? 15.0f : 60.0f, Velocity.magnitude);
CurrentlyStuckOn = Hit.rigidbody;
AppliedMovement = Velocity * Time.fixedDeltaTime;
2019-08-04 00:48:41 +02:00
} else if (CurrentlyStuckOn != null) {
CurrentlyStuckOn = null;
2019-08-03 18:14:47 +02:00
}
Vector2 NewPosition = (Vector2)transform.position + AppliedMovement;
transform.position = NewPosition;
}
}
}