Enemy.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. public WeaponEnemy weapon;
  29. void OnCollisionEnter(Collision collision)
  30. {
  31. if (!dead)
  32. {
  33. if (collision.collider.tag == "Bullet")
  34. {
  35. health -= 1f;
  36. }
  37. }
  38. if (health <= 0)
  39. Death();
  40. }
  41. // Use this for initialization
  42. void Start()
  43. {
  44. health = maxHealth;
  45. dayTimeManager = GameObject.FindObjectOfType<DayTimeManager>();
  46. behaviorController = new EnemyAI.BehaviorController();
  47. behaviorController.blackboard.Weapon = weapon;
  48. behaviorController.blackboard.navMeshAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
  49. }
  50. // Update is called once per frame
  51. void Update()
  52. {
  53. if (!dead)
  54. {
  55. // AI
  56. EnemyAI.Blackboard blackboard = behaviorController.blackboard;
  57. if (dayTimeManager.dayTimeLabel == DayTimeLabel.Night)
  58. {
  59. blackboard.Night = true;
  60. }
  61. else
  62. {
  63. blackboard.Night = false;
  64. }
  65. blackboard.PlayerPosition = GameObject.FindGameObjectWithTag("Player").transform.position;
  66. blackboard.EnemyPosition = transform.position;
  67. behaviorController.Update();
  68. // Energy collection
  69. if (dayTimeManager.dayTimeLabel != lastCharging)
  70. {
  71. switch (dayTimeManager.dayTimeLabel)
  72. {
  73. case (DayTimeLabel.Night):
  74. powerLevel = PowerLevels.Full;
  75. power = 16;
  76. lastCharging = dayTimeManager.dayTimeLabel;
  77. IndicatorCoverVeryLow.SetActive(false);
  78. IndicatorCoverLow.SetActive(false);
  79. IndicatorCoverMedium.SetActive(false);
  80. IndicatorCoverHigh.SetActive(false);
  81. IndicatorCoverFull.SetActive(false);
  82. break;
  83. case (DayTimeLabel.Morning):
  84. powerLevel = PowerLevels.VeryLow;
  85. power = 1;
  86. lastCharging = dayTimeManager.dayTimeLabel;
  87. IndicatorCoverVeryLow.SetActive(false);
  88. IndicatorCoverLow.SetActive(true);
  89. IndicatorCoverMedium.SetActive(true);
  90. IndicatorCoverHigh.SetActive(true);
  91. IndicatorCoverFull.SetActive(true);
  92. break;
  93. case (DayTimeLabel.Noon):
  94. powerLevel = PowerLevels.Low;
  95. power = 2;
  96. lastCharging = dayTimeManager.dayTimeLabel;
  97. IndicatorCoverVeryLow.SetActive(false);
  98. IndicatorCoverLow.SetActive(false);
  99. IndicatorCoverMedium.SetActive(true);
  100. IndicatorCoverHigh.SetActive(true);
  101. IndicatorCoverFull.SetActive(true);
  102. break;
  103. case (DayTimeLabel.Evening):
  104. powerLevel = PowerLevels.Medium;
  105. power = 4;
  106. lastCharging = dayTimeManager.dayTimeLabel;
  107. IndicatorCoverVeryLow.SetActive(false);
  108. IndicatorCoverLow.SetActive(false);
  109. IndicatorCoverMedium.SetActive(false);
  110. IndicatorCoverHigh.SetActive(true);
  111. IndicatorCoverFull.SetActive(true);
  112. break;
  113. }
  114. }
  115. }
  116. }
  117. private void Death()
  118. {
  119. dead = true;
  120. weapon.gameObject.SetActive(false);
  121. GetComponent<UnityEngine.AI.NavMeshAgent>().isStopped = true;
  122. GameObject go = GameObject.Instantiate(powerDrop);
  123. go.GetComponent<PowerDrop>().powerAmount = power;
  124. go.transform.position = gameObject.transform.position;
  125. GameObject.Destroy(gameObject);
  126. }
  127. }