Enemy.cs 5.2 KB

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