diff options
Diffstat (limited to 'Assets/Scripts/Traps/TrapDamage.cs')
-rw-r--r-- | Assets/Scripts/Traps/TrapDamage.cs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/Assets/Scripts/Traps/TrapDamage.cs b/Assets/Scripts/Traps/TrapDamage.cs new file mode 100644 index 0000000..a61df7b --- /dev/null +++ b/Assets/Scripts/Traps/TrapDamage.cs @@ -0,0 +1,59 @@ +using MontanaJohns.Actors; +using System.Collections; +using UnityEngine; + +public class TrapDamage : MonoBehaviour +{ + [SerializeField] + protected int damage; + + [Header("TrapDamage")] + [SerializeField] protected float stayDamageDelay; + + protected bool stay; + protected Hashtable justEntered = new Hashtable(); + + protected void OnTriggerEnter2D(Collider2D collision) + { + if (justEntered.Contains(collision.gameObject)) + justEntered[collision.gameObject] = true; + else + justEntered.Add(collision.gameObject, true); + + DealDamage(collision); + } + protected void OnTriggerStay2D(Collider2D collision) + { + if (!stay) + { + StartCoroutine(StayDamage(collision)); + } + } + + protected void OnTriggerExit2D(Collider2D collision) + { + justEntered[collision.gameObject] = false; + } + + protected IEnumerator StayDamage(Collider2D collision) + { + stay = true; + if (justEntered.Contains(collision.gameObject) && justEntered[collision.gameObject].Equals(true)) + justEntered[collision.gameObject] = false; + else if (justEntered.Contains(collision.gameObject)) + DealDamage(collision); + yield return new WaitForSeconds(stayDamageDelay); + stay = false; + } + + protected void DealDamage(Collider2D collision) + { + if (collision.tag == "Player" || collision.tag == "Enemy") + { + collision.GetComponent<Actor>().TakeDamage(damage); + Debug.Log("Damage"); + } + } + + +} |