TrapAttack.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using GameWorld;
  3. using LitJson;
  4. using UnityEngine;
  5. public class TrapAttack : MonoBehaviour
  6. {
  7. private void OnEnable()
  8. {
  9. this.atkData = JsonMapper.ToObject(this.data);
  10. }
  11. private void OnTriggerEnter2D(Collider2D other)
  12. {
  13. if (other.name == "PlayerHurtBox")
  14. {
  15. this.currentTime = this.interval;
  16. this.PlayerHurt(other);
  17. }
  18. }
  19. public void SetInfo(TrapAttack.AttackType type, int atkDamage, float atkInterval)
  20. {
  21. this.attackType = type;
  22. this.damage = atkDamage;
  23. this.interval = atkInterval;
  24. }
  25. private void OnTriggerStay2D(Collider2D other)
  26. {
  27. if (this.attackType == TrapAttack.AttackType.Once)
  28. {
  29. return;
  30. }
  31. this.currentTime -= Time.deltaTime;
  32. if (this.currentTime <= 0f)
  33. {
  34. this.currentTime = this.interval;
  35. if (other.name == "PlayerHurtBox")
  36. {
  37. this.PlayerHurt(other);
  38. }
  39. }
  40. }
  41. private void PlayerHurt(Collider2D other)
  42. {
  43. PlayerHurtAtkEventArgs args = new PlayerHurtAtkEventArgs(other.transform.parent.gameObject, base.transform.gameObject, base.transform.gameObject, this.damage, Incrementor.GetNextId(), this.atkData, false);
  44. EventManager.PostEvent<Transform, PlayerHurtAtkEventArgs>("PlayerHurtAtk", base.transform, args);
  45. }
  46. public TrapAttack.AttackType attackType;
  47. public int damage;
  48. public float interval;
  49. [SerializeField]
  50. private string data;
  51. private JsonData atkData;
  52. private float currentTime;
  53. public enum AttackType
  54. {
  55. Once,
  56. Mulit
  57. }
  58. }