Enemy.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Enemy : MonoBehaviour
  5. {
  6. [SerializeField]
  7. private GameObject powerDrop;
  8. [SerializeField]
  9. private PowerLevels powerLevel;
  10. [SerializeField]
  11. private GameObject IndicatorCoverVeryLow;
  12. [SerializeField]
  13. private GameObject IndicatorCoverLow;
  14. [SerializeField]
  15. private GameObject IndicatorCoverMedium;
  16. [SerializeField]
  17. private GameObject IndicatorCoverHigh;
  18. [SerializeField]
  19. private GameObject IndicatorCoverFull;
  20. private float power = 1.0f;
  21. private enum PowerLevels { VeryLow, Low, Medium, High, Full }
  22. private EnemyAI.BehaviorController behaviorController;
  23. private DayTimeManager dayTimeManager;
  24. private float health;
  25. private DayTimeLabel lastCharging;
  26. public float maxHealth = 2.5f;
  27. private bool dead;
  28. void OnCollisionEnter(Collision collision)
  29. {
  30. if (!dead)
  31. {
  32. if (collision.collider.tag == "Bullet")
  33. {
  34. health -= 1f;
  35. }
  36. }
  37. if (health <= 0)
  38. Death();
  39. }
  40. // Use this for initialization
  41. void Start()
  42. {
  43. health = maxHealth;
  44. dayTimeManager = GameObject.FindObjectOfType<DayTimeManager>();
  45. behaviorController = new EnemyAI.BehaviorController();
  46. behaviorController.blackboard.navMeshAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
  47. }
  48. // Update is called once per frame
  49. void Update()
  50. {
  51. if (!dead)
  52. {
  53. // AI
  54. EnemyAI.Blackboard blackboard = behaviorController.blackboard;
  55. if (dayTimeManager.dayTimeLabel == DayTimeLabel.Night)
  56. {
  57. blackboard.Night = true;
  58. }
  59. else
  60. {
  61. blackboard.Night = false;
  62. }
  63. blackboard.PlayerPosition = GameObject.FindGameObjectWithTag("Player").transform.position;
  64. blackboard.EnemyPosition = transform.position;
  65. behaviorController.Update();
  66. // Energy collection
  67. if (dayTimeManager.dayTimeLabel != lastCharging)
  68. {
  69. switch (dayTimeManager.dayTimeLabel)
  70. {
  71. case (DayTimeLabel.Night):
  72. powerLevel = PowerLevels.Full;
  73. power = 16;
  74. lastCharging = dayTimeManager.dayTimeLabel;
  75. IndicatorCoverVeryLow.SetActive(false);
  76. IndicatorCoverLow.SetActive(false);
  77. IndicatorCoverMedium.SetActive(false);
  78. IndicatorCoverHigh.SetActive(false);
  79. IndicatorCoverFull.SetActive(false);
  80. break;
  81. case (DayTimeLabel.Morning):
  82. powerLevel = PowerLevels.VeryLow;
  83. power = 1;
  84. lastCharging = dayTimeManager.dayTimeLabel;
  85. IndicatorCoverVeryLow.SetActive(false);
  86. IndicatorCoverLow.SetActive(true);
  87. IndicatorCoverMedium.SetActive(true);
  88. IndicatorCoverHigh.SetActive(true);
  89. IndicatorCoverFull.SetActive(true);
  90. break;
  91. case (DayTimeLabel.Noon):
  92. powerLevel = PowerLevels.Low;
  93. power = 2;
  94. lastCharging = dayTimeManager.dayTimeLabel;
  95. IndicatorCoverVeryLow.SetActive(false);
  96. IndicatorCoverLow.SetActive(false);
  97. IndicatorCoverMedium.SetActive(true);
  98. IndicatorCoverHigh.SetActive(true);
  99. IndicatorCoverFull.SetActive(true);
  100. break;
  101. case (DayTimeLabel.Evening):
  102. powerLevel = PowerLevels.Medium;
  103. power = 4;
  104. lastCharging = dayTimeManager.dayTimeLabel;
  105. IndicatorCoverVeryLow.SetActive(false);
  106. IndicatorCoverLow.SetActive(false);
  107. IndicatorCoverMedium.SetActive(false);
  108. IndicatorCoverHigh.SetActive(true);
  109. IndicatorCoverFull.SetActive(true);
  110. break;
  111. }
  112. }
  113. }
  114. }
  115. private void Death()
  116. {
  117. dead = true;
  118. GetComponent<UnityEngine.AI.NavMeshAgent>().isStopped = true;
  119. GameObject go = GameObject.Instantiate(powerDrop);
  120. go.GetComponent<PowerDrop>().powerAmount = power;
  121. go.transform.position = gameObject.transform.position;
  122. GameObject.Destroy(gameObject);
  123. }
  124. }