EnemyTower.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. public class EnemyTower : Character
  6. {
  7. [Header("µÐ·½Ëþ")]
  8. public string name;
  9. public float height = 8;
  10. private void Awake()
  11. {
  12. TowerMap.enemyTowers.Add(gameObject);
  13. Init();
  14. //attackController.ChooseAttack(0);
  15. attackController.curAttackMethod = attackController.attackMethod_march[0];
  16. }
  17. public void Attack_March()
  18. {
  19. GameObject bulletObj = PoolManager.Instantiate(attackController.curAttackMethod.bulletPrefab);
  20. attackController.attackTime = attackController.attackKeys[0].totalTime;
  21. Bullet bullet = bulletObj.GetComponent<Bullet>();
  22. AttackInfo attackInfo = attackController.curAttackMethod.attackInfo;
  23. Vector3 attackDir = attackInfo.attackDir.normalized;
  24. if (bodyTrans.localScale.x < 0)
  25. {
  26. attackDir.x = -attackDir.x;
  27. }
  28. bullet.BeShoot(this, attackController.curAttackMethod.shootPos[0].position, attackDir, attackController.curAttackMethod.shootTrack,
  29. attackController.curAttackMethod.shootAlwaysTrack, attackTarget ? attackTarget : null);
  30. }
  31. public bool GetAttack()
  32. {
  33. if (targetCharacter != null)
  34. {
  35. return true;
  36. }
  37. return false;
  38. }
  39. public void SearchTarget()
  40. {
  41. targetCharacter = searchTrigger.GetMinDisTarget(attackController.targetTypes, true, attackController.curAttackMethod.searchMode);
  42. }
  43. public override void OnState()
  44. {
  45. base.OnState();
  46. SearchTarget();
  47. attackController.attackTime -= Time.deltaTime;
  48. dieKeepTime -= Time.deltaTime;
  49. bool isAttack = GetAttack();
  50. switch (state)
  51. {
  52. case CharacterState.Idle:
  53. if (isAttack)
  54. {
  55. ChangeState(CharacterState.Attack);
  56. break;
  57. }
  58. break;
  59. case CharacterState.Attack:
  60. if (attackController.attackTime <= 0)
  61. {
  62. ChangeState(CharacterState.Idle);
  63. break;
  64. }
  65. break;
  66. case CharacterState.Die:
  67. if (dieKeepTime <= 0)
  68. {
  69. TowerMap.enemyTowers.Remove(gameObject);
  70. if(TowerMap.enemyTowers.Count ==0)
  71. {
  72. GameManager.instance.GameEnd();
  73. }
  74. gameObject.SetActive(false);
  75. break;
  76. }
  77. break;
  78. default:
  79. break;
  80. }
  81. }
  82. public override void ChangeState(CharacterState newState)
  83. {
  84. switch (state)
  85. {
  86. case CharacterState.Idle:
  87. break;
  88. case CharacterState.Attack:
  89. attackTarget = null;
  90. break;
  91. case CharacterState.Die:
  92. isDie = false;
  93. break;
  94. default:
  95. break;
  96. }
  97. CharacterState oldState = state;
  98. state = newState;
  99. switch (newState)
  100. {
  101. case CharacterState.Idle:
  102. rb.velocity = Vector3.zero;
  103. break;
  104. case CharacterState.Attack:
  105. attackTarget = targetCharacter;
  106. Attack_March();
  107. break;
  108. case CharacterState.Die:
  109. isDie = true;
  110. dieKeepTime = totalDieKeepTime;
  111. TowerMap.enemyTowers.Remove(gameObject);
  112. break;
  113. default:
  114. break;
  115. }
  116. }
  117. public override void BeHit(int damage)
  118. {
  119. base.BeHit(damage);
  120. }
  121. public override void BeHit(AttackInfo attackInfo, Character attackFrom, int damage = -1)
  122. {
  123. base.BeHit(attackInfo.damage);
  124. }
  125. }