YuMenGuan.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class YuMenGuan : Boss
  5. {
  6. public enum AttackMethods
  7. {
  8. none = 0,
  9. move = 1, //移动
  10. shockWave1 = 2, //单手砸地,两侧冲击波
  11. shockWave2 = 3, //双手砸地,两侧冲击波*3
  12. laser = 4, //眼睛激光
  13. block = 5, //砖头怪
  14. blackHand = 6, //黑手
  15. }
  16. public enum PoolType
  17. {
  18. wave1 = 0,
  19. wave2 = 1,
  20. block = 2,
  21. }
  22. [System.Serializable]
  23. public struct AttackAssignment
  24. {
  25. public AttackMethods attack;
  26. public int weight; //权重
  27. }
  28. [System.Serializable]
  29. public struct BossAttackType
  30. {
  31. public AttackCategories category;
  32. public AttackAssignment[] attacks;
  33. }
  34. [Header("攻击配置")]
  35. public BossAttackType[] attackConfigurations;
  36. private AttackMethods curAttackType;
  37. [Header("移动")]
  38. public float maxMoveDis, minMoveDis;
  39. private float curMoveDis;
  40. private float hasMoveDis;
  41. private float origMoveX;
  42. private bool isAttackWalk; //作为随机到的攻击而移动
  43. private Character otherTarget;
  44. [Header("冲击波")]
  45. public GameObject wave1;
  46. public GameObject wave2;
  47. public AttackInfo wave1Damage;
  48. public AttackInfo[] wave2Damage;
  49. public Transform wave1pos;
  50. public Transform wave2pos;
  51. public float launchDelayTime1, launchDelayTime1loop, launchDelayTime2;
  52. public float[] waveEnlargeScale;
  53. public Vector3 wave1Size;
  54. public Vector3[] wave2Size;
  55. public float[] intervalTime; //三连冲击波,每个冲击波发射的间隔时间
  56. private List<Bullet> wave1Pool = new List<Bullet>();
  57. private List<Bullet> wave2Pool = new List<Bullet>();
  58. private int[] hasNum; //池中已有的数量
  59. private List<Bullet> usedWave1 = new List<Bullet>();
  60. private List<Bullet> usedWave2 = new List<Bullet>();
  61. private int curWaveIndex = 0;
  62. private List<Bullet> transit = new List<Bullet>(); //中转
  63. private int shockWaveTimes; //连续n次单手冲击波
  64. private bool isContinuous; //连续多次单手冲击波
  65. public int maxShockTimes, minShockTimes;
  66. public float wave1aniTime, wave1loopTime;
  67. public float wave2aniTime;
  68. public float waveEndTime;
  69. private float waveTime; //冲击波进行时长
  70. private bool isWave1; //冲击波1进行中
  71. private bool isWave2; //冲击波2进行中
  72. private bool isEndWave;
  73. [Header("眼睛激光")]
  74. public EyeLaser laser;
  75. public Transform eye; //从哪里射出
  76. public GameObject weakEye; //哪个弱点被打爆后不再发射
  77. public float waitTime; //蓄力多久后开始发射激光
  78. [Header("砖头怪")]
  79. public int minBlocks, maxBlocks;
  80. public float aniTime;
  81. private int curBlocks;
  82. public GameObject block;
  83. public float minX, maxX;
  84. public override void Start()
  85. {
  86. base.Start();
  87. hasNum = new int[3];
  88. laser.headPos = eye;
  89. }
  90. //判断冲击波池中是否有多余的冲击波,没有则实例化新的
  91. private void ExpandPool(PoolType id, int num)
  92. {
  93. int need = num - hasNum[(int)id];
  94. if (need <= 0)
  95. {
  96. return;
  97. }
  98. GameObject prefab = null;
  99. List<Bullet> pool = null;
  100. List<Bullet> used = null;
  101. switch (id)
  102. {
  103. case PoolType.wave1:
  104. prefab = wave1;
  105. pool = wave1Pool;
  106. used = usedWave1;
  107. break;
  108. case PoolType.wave2:
  109. prefab = wave2;
  110. pool = wave2Pool;
  111. used = usedWave2;
  112. break;
  113. case PoolType.block:
  114. break;
  115. default:
  116. break;
  117. }
  118. foreach(Bullet b in used)
  119. {
  120. if (!b.gameObject.activeSelf)
  121. {
  122. transit.Add(b);
  123. pool.Add(b);
  124. need -= 1;
  125. hasNum[(int)id] += 1;
  126. }
  127. }
  128. for (int i = 0; i < need; i++)
  129. {
  130. GameObject g;
  131. g = Instantiate(prefab, Vector3.zero, new Quaternion(0, 0, 0, 0), null);
  132. g.SetActive(false);
  133. pool.Add(g.GetComponent<Bullet>());
  134. hasNum[(int)id] += 1;
  135. }
  136. foreach (Bullet bu in transit)
  137. {
  138. used.Remove(bu);
  139. transit = new List<Bullet>();
  140. }
  141. }
  142. //移动逻辑,作为随出来的移动时移动一定距离;作为无目标移动时始终捕捉是否有可攻击目标进入范围,有则切出移动状态
  143. public override void OnMove()
  144. {
  145. if (isAttackWalk && (!targetCharacter || targetCharacter.isDie))
  146. {
  147. CheckTarget();
  148. }
  149. if (Vector2.Distance(bodyTrans.position, targetCharacter.transform.position) <= 0.5f)
  150. {
  151. canMove = false;
  152. rb.velocity = new Vector3(0, 0, 0);
  153. }
  154. Vector3 dir;
  155. dir = bodyTrans.position.x <= targetCharacter.transform.position.x ? Vector3.right : Vector3.left;
  156. if (dir.x * bodyTrans.localScale.x > 0)
  157. {
  158. Turn();
  159. }
  160. rb.velocity = dir * moveSpeed;
  161. hasMoveDis = bodyTrans.position.x - origMoveX;
  162. if (isAttackWalk)
  163. {
  164. if (hasMoveDis >= curMoveDis)
  165. {
  166. isAttackWalk = false;
  167. EndCurAttackState(false);
  168. }
  169. }
  170. else
  171. {
  172. otherTarget = searchTrigger.GetMinDisTarget(targetTypes, canHitFly);
  173. if (otherTarget != null)
  174. {
  175. EndCurAttackState(false);
  176. }
  177. }
  178. }
  179. public override void Update()
  180. {
  181. base.Update();
  182. if (isWave1)
  183. {
  184. ContinueShockWave(0);
  185. }
  186. if (isWave2)
  187. {
  188. ContinueShockWave(1);
  189. }
  190. }
  191. //移动前逻辑
  192. public override void ToMove()
  193. {
  194. ChangeState(CharacterState.Run);
  195. curMoveDis = Random.Range(minMoveDis, maxMoveDis);
  196. origMoveX = bodyTrans.position.x;
  197. curTarget = TargetType.Tower;
  198. CheckTarget();
  199. ani.Play("yumenguan_walk", 0, 0);
  200. }
  201. //发射冲击波2号(3连)
  202. private void ToLaunchWave2()
  203. {
  204. LaunchWave(1, curWaveIndex);
  205. curWaveIndex++;
  206. if (curWaveIndex == 3)
  207. {
  208. curWaveIndex = 0;
  209. }
  210. }
  211. //发射冲击波1号
  212. private void ToLaunchWave1()
  213. {
  214. LaunchWave(0, 0);
  215. }
  216. //发射冲击波
  217. private void LaunchWave(int id, int index)
  218. {
  219. Vector3 dir = Vector3.zero;
  220. Vector3 dir1 = Vector3.left;
  221. Vector3 dir2 = Vector3.right;
  222. Bullet wv1, wv2;
  223. switch (id)
  224. {
  225. case 0:
  226. ExpandPool(PoolType.wave1, 2);
  227. wv1 = wave1Pool[hasNum[id] - 1];
  228. wv2 = wave1Pool[hasNum[id] - 2];
  229. wv1.transform.localScale = wave1Size;
  230. wv2.transform.localScale = wave1Size;
  231. wave1Pool.Remove(wv1);
  232. wave1Pool.Remove(wv2);
  233. usedWave1.Add(wv1);
  234. usedWave1.Add(wv2);
  235. wv1.GetComponent<WaveShockDir>().IsLeft = true;
  236. wv2.GetComponent<WaveShockDir>().IsLeft = false;
  237. wv1.GetComponent<WaveShockDir>().scale = waveEnlargeScale[curStateId];
  238. wv2.GetComponent<WaveShockDir>().scale = waveEnlargeScale[curStateId];
  239. wv1.GetComponent<WaveShockDir>().CheckTurn();
  240. wv2.GetComponent<WaveShockDir>().CheckTurn();
  241. wv1.BeShoot(this, wave1pos.position, dir1, wave1Damage.damage, wave1Damage.force, wave1Damage.changeHurt, wave1Damage.repelValue, false);
  242. wv2.BeShoot(this, wave2pos.position, dir2, wave1Damage.damage, wave1Damage.force, wave1Damage.changeHurt, wave1Damage.repelValue, false);
  243. wv1.transform.right = dir;
  244. wv2.transform.right = dir;
  245. break;
  246. case 1:
  247. ExpandPool(PoolType.wave2, 2);
  248. wv1 = wave2Pool[hasNum[id] - 1];
  249. wv2 = wave2Pool[hasNum[id] - 2];
  250. wv1.transform.localScale = wave2Size[index];
  251. wv2.transform.localScale = wave2Size[index];
  252. wave2Pool.Remove(wv1);
  253. wave2Pool.Remove(wv2);
  254. usedWave2.Add(wv1);
  255. usedWave2.Add(wv2);
  256. wv1.GetComponent<WaveShockDir>().IsLeft = true;
  257. wv2.GetComponent<WaveShockDir>().IsLeft = false;
  258. wv1.GetComponent<WaveShockDir>().scale = waveEnlargeScale[curStateId];
  259. wv2.GetComponent<WaveShockDir>().scale = waveEnlargeScale[curStateId];
  260. wv1.GetComponent<WaveShockDir>().CheckTurn();
  261. wv2.GetComponent<WaveShockDir>().CheckTurn();
  262. wv1.BeShoot(this, wave1pos.position, dir1, wave2Damage[index].damage, wave2Damage[index].force, wave2Damage[index].changeHurt, wave2Damage[index].repelValue, false);
  263. wv2.BeShoot(this, wave2pos.position, dir2, wave2Damage[index].damage, wave2Damage[index].force, wave2Damage[index].changeHurt, wave2Damage[index].repelValue, false);
  264. wv1.transform.right = dir;
  265. wv2.transform.right = dir;
  266. break;
  267. default:
  268. break;
  269. }
  270. hasNum[id] -= 2;
  271. }
  272. //连续发射冲击波
  273. private void ContinueShockWave(int id)
  274. {
  275. waveTime += Time.deltaTime;
  276. switch (id)
  277. {
  278. case 0:
  279. if (!isEndWave && ((!isContinuous && waveTime >= wave1aniTime)
  280. || (isContinuous && waveTime >= wave1loopTime)))
  281. {
  282. waveTime = 0;
  283. if (shockWaveTimes > 0)
  284. {
  285. shockWaveTimes--;
  286. isContinuous = true;
  287. ShockWave(0);
  288. }
  289. else
  290. {
  291. ani.Play("yumenguan_smash_r_end", 0, 0);
  292. isEndWave = true;
  293. }
  294. }
  295. if (isEndWave && waveTime >= waveEndTime)
  296. {
  297. isWave1 = false;
  298. isEndWave = false;
  299. isContinuous = false;
  300. waveTime = 0;
  301. EndCurAttackState(true);
  302. }
  303. break;
  304. case 1:
  305. if (waveTime >= wave2aniTime + waveEndTime)
  306. {
  307. isWave2 = false;
  308. waveTime = 0;
  309. EndCurAttackState(true);
  310. }
  311. break;
  312. default:
  313. break;
  314. }
  315. }
  316. //冲击波攻击
  317. public void ShockWave(int id)
  318. {
  319. switch (id)
  320. {
  321. case 0:
  322. if (!isContinuous)
  323. {
  324. isWave1 = true;
  325. waveTime = 0;
  326. ani.Play("yumenguan_smash_r", 0, 0);
  327. Invoke("ToLaunchWave1", launchDelayTime1);
  328. }
  329. else
  330. {
  331. waveTime = 0;
  332. ani.Play("yumenguan_smash_r_loop", 0, 0);
  333. Invoke("ToLaunchWave1", launchDelayTime1loop);
  334. }
  335. break;
  336. case 1:
  337. waveTime = 0;
  338. ani.Play("yumenguan_smash_double", 0, 0);
  339. Invoke("ToLaunchWave2", launchDelayTime2);
  340. Invoke("ToLaunchWave2", intervalTime[curStateId] + launchDelayTime2);
  341. Invoke("ToLaunchWave2", intervalTime[curStateId] * 2 + launchDelayTime2);
  342. isWave2 = true;
  343. break;
  344. default:
  345. break;
  346. }
  347. }
  348. public void ShootEyeLaser()
  349. {
  350. curTarget = TargetType.Player;
  351. CheckTarget();
  352. laser.endPos = targetCharacter.transform;
  353. laser.DrawLine();
  354. laser.gameObject.SetActive(true);
  355. }
  356. public void BlocksOut()
  357. {
  358. curBlocks = Random.Range(minBlocks, maxBlocks);
  359. for(int i = curBlocks; i > 0; i--)
  360. {
  361. Vector3 pos = new Vector3(transform.position.x + Random.Range(minX, maxX), -2, 0);
  362. Instantiate(block, pos, new Quaternion(0, 0, 0, 0), null);
  363. }
  364. EndCurAttackState(true);
  365. }
  366. //攻击
  367. public override void Attack()
  368. {
  369. switch (curAttackType)
  370. {
  371. case AttackMethods.move:
  372. ToMove();
  373. break;
  374. case AttackMethods.shockWave1:
  375. shockWaveTimes = Random.Range(minShockTimes, maxShockTimes);
  376. ShockWave(0);
  377. break;
  378. case AttackMethods.shockWave2:
  379. ShockWave(1);
  380. break;
  381. case AttackMethods.laser:
  382. ani.Play("yumenguan_eyelight", 0, 0);
  383. Invoke("ShootEyeLaser", waitTime);
  384. break;
  385. case AttackMethods.block:
  386. ani.Play("yumenguan_smash_double", 0, 0);
  387. Invoke("BlocksOut", aniTime);
  388. break;
  389. default:
  390. break;
  391. }
  392. }
  393. //根据当前大类随机攻击方式
  394. public override void RandomAttackType(AttackCategories cate)
  395. {
  396. AttackAssignment[] attack = attackConfigurations[(int)cate].attacks;
  397. switch (cate)
  398. {
  399. case AttackCategories.A:
  400. searchTrigger.enabled = true;
  401. otherTarget = null;
  402. otherTarget = searchTrigger.GetMinDisTarget(targetTypes, canHitFly);
  403. if (otherTarget == null)
  404. {
  405. isAttackWalk = false;
  406. curAttackType = AttackMethods.move;
  407. }
  408. else
  409. {
  410. curAttackType = RandomAttack(attackConfigurations[0].attacks);
  411. if (curAttackType == AttackMethods.move)
  412. {
  413. isAttackWalk = true;
  414. }
  415. }
  416. break;
  417. case AttackCategories.B:
  418. if (weakEye.activeSelf)
  419. {
  420. curAttackType = RandomAttack(attackConfigurations[1].attacks);
  421. }
  422. else
  423. {
  424. curAttackType = AttackMethods.block;
  425. }
  426. break;
  427. default:
  428. break;
  429. }
  430. }
  431. //在某个大类里按权重随机返回一个攻击类型
  432. public AttackMethods RandomAttack(AttackAssignment[] attackAssigns)
  433. {
  434. int weight = 0;
  435. AttackMethods last = new AttackMethods();
  436. foreach(AttackAssignment a in attackAssigns)
  437. {
  438. weight += a.weight;
  439. }
  440. int result = Random.Range(0, weight);
  441. foreach(AttackAssignment a in attackAssigns)
  442. {
  443. last = a.attack;
  444. result -= a.weight;
  445. if (result < 0)
  446. {
  447. return a.attack;
  448. }
  449. }
  450. return last;
  451. }
  452. }