Block.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. public class Block : Character
  6. {
  7. public enum State
  8. {
  9. outGround = 0, //出土
  10. charge = 1, //蓄力
  11. sprint = 2, //冲刺
  12. bomb = 3, //爆炸
  13. none = 4,
  14. back = 5, //被击飞后退
  15. warning = 6, //预警
  16. drop = 7, //坠落
  17. }
  18. [Header("属性")]
  19. public YuMenGuan ymg;
  20. public bool isSpecial;
  21. public bool isSpecialDie;
  22. [Header("掉落魂")]
  23. public GameObject soulPrefab;
  24. public float soulStartSpeed = 1f;
  25. public int dropSoul;
  26. public float dropSoulAngle = 60f;
  27. [Header("砖头怪攻击状态")]
  28. public State curState;
  29. public float chargeTime; //空中蓄力时长
  30. private float hasChargeTime;
  31. [Header("冲出地面")]
  32. public float outSpeed;
  33. public float maxHeight, minHeight; //随即高度
  34. public float height;
  35. public AttackInfo outDamage;
  36. [Header("向前冲刺")]
  37. public float sprintAccSpeed; //冲刺加速度
  38. private float curSpeed; //当前速度
  39. [Header("警告")]
  40. public GameObject warning;
  41. public Vector3 posYZ;
  42. private GameObject curWarning;
  43. public float warningTime;
  44. private float hasWarningTime;
  45. [Header("爆炸")]
  46. public GameObject bombPrefab;
  47. private bool isTowerBomb;
  48. private GameObject curBomb;
  49. [Header("攻击来源")]
  50. public BeHitTrigger hitTrigger;
  51. public int attackerID;
  52. [Header("击飞轨迹")]
  53. public float backForceRate; //击飞的力的倍数
  54. public void Ready()
  55. {
  56. Vector3 ps = transform.position;
  57. ps.y = posYZ.y;
  58. ps.z = posYZ.z;
  59. curWarning = PoolManager.Instantiate(warning, ps, new Quaternion(0, 0, 0, 0), null);
  60. hp = totalHp;
  61. if (!isSpecial)
  62. {
  63. height = Random.Range(minHeight, maxHeight);
  64. if (height > ymg.mostHeight)
  65. {
  66. ymg.mostHeight = height;
  67. }
  68. ChangeAttackState(State.warning);
  69. }
  70. else
  71. {
  72. ChangeAttackState(State.outGround);
  73. }
  74. }
  75. public override void ChangeState(CharacterState newState)
  76. {
  77. if (state == newState)
  78. {
  79. return;
  80. }
  81. switch (state)
  82. {
  83. default:
  84. break;
  85. }
  86. state = newState;
  87. switch (newState)
  88. {
  89. case CharacterState.Die:
  90. if (isSpecial)
  91. {
  92. ymg.AllBlocksDrop();
  93. }
  94. if (!isTowerBomb)
  95. {
  96. ChangeAttackState(State.none);
  97. }
  98. curWarning.SetActive(false);
  99. dieKeepTime = totalDieKeepTime;
  100. break;
  101. default:
  102. break;
  103. }
  104. }
  105. public override void OnState()
  106. {
  107. switch (state)
  108. {
  109. case CharacterState.Die:
  110. dieKeepTime -= Time.deltaTime;
  111. if (!isDie && dieKeepTime <= 0)
  112. {
  113. if (curBomb != null || isSpecialDie)
  114. {
  115. DropSouls();
  116. }
  117. isDie = true;
  118. bodyTrans.gameObject.SetActive(false);
  119. }
  120. if (curBomb != null && !curBomb.activeSelf ||isDie && curBomb == null)
  121. {
  122. gameObject.SetActive(false);
  123. }
  124. break;
  125. default:
  126. break;
  127. }
  128. }
  129. public void DropSouls()
  130. {
  131. if (dropSoul > 1)
  132. {
  133. for (int i = 0; i < dropSoul; i++)
  134. {
  135. float angleInterval = dropSoulAngle / (float)(dropSoul - 1);
  136. float angle = 90 + ((float)i - (float)(dropSoul - 1) / 2) * angleInterval;
  137. angle = angle / 180 * Mathf.PI;
  138. GameObject soulObj = PoolManager.Instantiate(soulPrefab, transform.position);
  139. Vector3 dir = new Vector3(Mathf.Cos(angle), Mathf.Sin(angle), 0);
  140. soulObj.GetComponent<Soul>().Burst(dir * soulStartSpeed);
  141. }
  142. }
  143. else
  144. {
  145. GameObject soulObj = PoolManager.Instantiate(soulPrefab, transform.position);
  146. Vector3 dir = Vector3.up;
  147. soulObj.GetComponent<Soul>().Burst(dir * soulStartSpeed);
  148. }
  149. }
  150. public override void BeHit(int damage)
  151. {
  152. if (hitTrigger.attackerID == 2)
  153. {
  154. rb.velocity = Vector3.zero;
  155. ChangeAttackState(State.back);
  156. return;
  157. }
  158. else
  159. {
  160. hp -= damage;
  161. //伤害跳字
  162. if (showInjuryNum)
  163. {
  164. GameObject injuryNum = PoolManager.Instantiate(injuryNumText);
  165. injuryNum.transform.position = new Vector3(transform.position.x + Random.Range(-1f, 1f), transform.position.y + 1, transform.position.z);
  166. TextMeshProUGUI text = injuryNum.GetComponentInChildren<TextMeshProUGUI>();
  167. text.text = damage.ToString();
  168. if (gameObject.CompareTag("Player"))
  169. {
  170. text.color = Color.red;
  171. }
  172. }
  173. if (hp <= 0)
  174. {
  175. ChangeState(CharacterState.Die);
  176. return;
  177. }
  178. }
  179. }
  180. public void ChangeAttackState(State newState)
  181. {
  182. switch (curState)
  183. {
  184. case State.outGround:
  185. rb.velocity = Vector3.zero;
  186. break;
  187. case State.charge:
  188. break;
  189. case State.sprint:
  190. break;
  191. case State.bomb:
  192. break;
  193. default:
  194. break;
  195. }
  196. curState = newState;
  197. switch (newState)
  198. {
  199. case State.warning:
  200. curWarning.SetActive(true);
  201. hasWarningTime = warningTime;
  202. break;
  203. case State.outGround:
  204. if (!isSpecial && height == ymg.mostHeight)
  205. {
  206. ymg.SpecialBlockOut(this);
  207. gameObject.SetActive(false);
  208. }
  209. ani.Play("show", 0, 0);
  210. break;
  211. case State.charge:
  212. ani.Play("idle", 0, 0);
  213. hasChargeTime = 0;
  214. break;
  215. case State.sprint:
  216. //ani.Play("move_start", 0, 0);
  217. break;
  218. case State.bomb:
  219. ani.Play("charge", 0, 0);
  220. if (isTowerBomb)
  221. {
  222. rb.velocity = Vector3.zero;
  223. }
  224. curBomb = PoolManager.Instantiate(bombPrefab, transform.position, new Quaternion(0, 0, 0, 0), transform);
  225. ChangeState(CharacterState.Die);
  226. break;
  227. case State.none:
  228. rb.velocity = Vector3.zero;
  229. break;
  230. case State.back:
  231. DropSouls();
  232. rb.useGravity = true;
  233. break;
  234. case State.drop:
  235. GetComponentInChildren<Collider>().enabled = false;
  236. rb.velocity = Vector3.zero;
  237. rb.useGravity = true;
  238. break;
  239. default:
  240. break;
  241. }
  242. }
  243. public void OnAttackState()
  244. {
  245. switch (curState)
  246. {
  247. case State.warning:
  248. hasWarningTime -= Time.deltaTime;
  249. if (hasWarningTime <= 0)
  250. {
  251. ChangeAttackState(State.outGround);
  252. }
  253. break;
  254. case State.outGround:
  255. Vector3 ve = rb.velocity;
  256. ve.y = outSpeed;
  257. rb.velocity = ve;
  258. if (transform.position.y >= height)
  259. {
  260. curWarning.SetActive(false);
  261. ChangeAttackState(State.charge);
  262. }
  263. break;
  264. case State.charge:
  265. hasChargeTime += Time.deltaTime;
  266. if (hasChargeTime >= chargeTime)
  267. {
  268. ChangeAttackState(State.sprint);
  269. }
  270. break;
  271. case State.sprint:
  272. if (targetCharacter == null || targetCharacter.isDie)
  273. {
  274. CheckTarget();
  275. }
  276. Vector3 dir;
  277. if (targetCharacter.transform.position.x >= transform.position.x)
  278. {
  279. dir = Vector3.right;
  280. }
  281. else
  282. {
  283. dir = Vector3.left;
  284. }
  285. curSpeed += sprintAccSpeed * Time.deltaTime;
  286. rb.velocity = dir * curSpeed;
  287. if (transform.position.x >= targetCharacter.transform.position.x)
  288. {
  289. isTowerBomb = true;
  290. ChangeAttackState(State.bomb);
  291. }
  292. break;
  293. case State.back:
  294. if (rb.useGravity && transform.position.y <= 1f)
  295. {
  296. rb.useGravity = false;
  297. Vector3 vee = rb.velocity;
  298. vee.y = 0;
  299. rb.velocity = vee;
  300. Vector3 poss = transform.position;
  301. poss.y = 1;
  302. transform.position = poss;
  303. ChangeState(CharacterState.Die);
  304. }
  305. break;
  306. case State.drop:
  307. if(rb.useGravity && transform.position.y <= 1f)
  308. {
  309. rb.useGravity = false;
  310. Vector3 vee = rb.velocity;
  311. vee.y = 0;
  312. rb.velocity = vee;
  313. Vector3 poss = transform.position;
  314. poss.y = 1;
  315. transform.position = poss;
  316. ChangeState(CharacterState.Die);
  317. }
  318. break;
  319. default:
  320. break;
  321. }
  322. }
  323. private void CheckTarget()
  324. {
  325. float minDis = -1;
  326. foreach (GameObject g in TowerMap.myTowers)
  327. {
  328. float dis = Vector2.Distance(g.transform.position, transform.position);
  329. if (minDis == -1 || dis < minDis)
  330. {
  331. minDis = dis;
  332. targetCharacter = g.GetComponent<Character>();
  333. }
  334. }
  335. }
  336. public override void FixedUpdate()
  337. {
  338. base.FixedUpdate();
  339. OnAttackState();
  340. }
  341. }