YuMenGuan.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. private Block[] blocks;
  85. public GameObject specialBlock;
  86. public float mostHeight;
  87. public override void Start()
  88. {
  89. base.Start();
  90. hasNum = new int[3];
  91. laser.headPos = eye;
  92. }
  93. //判断冲击波池中是否有多余的冲击波,没有则实例化新的
  94. private void ExpandPool(PoolType id, int num)
  95. {
  96. int need = num - hasNum[(int)id];
  97. if (need <= 0)
  98. {
  99. return;
  100. }
  101. GameObject prefab = null;
  102. List<Bullet> pool = null;
  103. List<Bullet> used = null;
  104. switch (id)
  105. {
  106. case PoolType.wave1:
  107. prefab = wave1;
  108. pool = wave1Pool;
  109. used = usedWave1;
  110. break;
  111. case PoolType.wave2:
  112. prefab = wave2;
  113. pool = wave2Pool;
  114. used = usedWave2;
  115. break;
  116. case PoolType.block:
  117. break;
  118. default:
  119. break;
  120. }
  121. foreach(Bullet b in used)
  122. {
  123. if (!b.gameObject.activeSelf)
  124. {
  125. transit.Add(b);
  126. pool.Add(b);
  127. need -= 1;
  128. hasNum[(int)id] += 1;
  129. }
  130. }
  131. for (int i = 0; i < need; i++)
  132. {
  133. GameObject g;
  134. g = Instantiate(prefab, Vector3.zero, new Quaternion(0, 0, 0, 0), null);
  135. g.SetActive(false);
  136. pool.Add(g.GetComponent<Bullet>());
  137. hasNum[(int)id] += 1;
  138. }
  139. foreach (Bullet bu in transit)
  140. {
  141. used.Remove(bu);
  142. transit = new List<Bullet>();
  143. }
  144. }
  145. //移动逻辑,作为随出来的移动时移动一定距离;作为无目标移动时始终捕捉是否有可攻击目标进入范围,有则切出移动状态
  146. public override void OnMove()
  147. {
  148. if (isAttackWalk && (!targetCharacter || targetCharacter.isDie))
  149. {
  150. CheckTarget();
  151. }
  152. if (Vector2.Distance(bodyTrans.position, targetCharacter.transform.position) <= 0.5f)
  153. {
  154. canMove = false;
  155. rb.velocity = new Vector3(0, 0, 0);
  156. }
  157. Vector3 dir;
  158. dir = bodyTrans.position.x <= targetCharacter.transform.position.x ? Vector3.right : Vector3.left;
  159. if (dir.x * bodyTrans.localScale.x > 0)
  160. {
  161. Turn();
  162. }
  163. rb.velocity = dir * moveSpeed;
  164. hasMoveDis = bodyTrans.position.x - origMoveX;
  165. if (isAttackWalk)
  166. {
  167. if (hasMoveDis >= curMoveDis)
  168. {
  169. isAttackWalk = false;
  170. EndCurAttackState(false);
  171. }
  172. }
  173. else
  174. {
  175. otherTarget = searchTrigger.GetMinDisTarget(attackController.targetTypes, attackController.canHitFly);
  176. if (otherTarget != null)
  177. {
  178. EndCurAttackState(false);
  179. }
  180. }
  181. }
  182. public override void Update()
  183. {
  184. base.Update();
  185. if (isWave1)
  186. {
  187. ContinueShockWave(0);
  188. }
  189. if (isWave2)
  190. {
  191. ContinueShockWave(1);
  192. }
  193. }
  194. //移动前逻辑
  195. public override void ToMove()
  196. {
  197. ChangeState(CharacterState.Run);
  198. curMoveDis = Random.Range(minMoveDis, maxMoveDis);
  199. origMoveX = bodyTrans.position.x;
  200. curTarget = TargetType.Tower;
  201. CheckTarget();
  202. ani.Play("yumenguan_walk", 0, 0);
  203. }
  204. //发射冲击波2号(3连)
  205. private void ToLaunchWave2()
  206. {
  207. LaunchWave(1, curWaveIndex);
  208. curWaveIndex++;
  209. if (curWaveIndex == 3)
  210. {
  211. curWaveIndex = 0;
  212. }
  213. }
  214. //发射冲击波1号
  215. private void ToLaunchWave1()
  216. {
  217. LaunchWave(0, 0);
  218. }
  219. //发射冲击波
  220. private void LaunchWave(int id, int index)
  221. {
  222. Vector3 dir = Vector3.zero;
  223. Vector3 dir1 = Vector3.left;
  224. Vector3 dir2 = Vector3.right;
  225. Bullet wv1, wv2;
  226. switch (id)
  227. {
  228. case 0:
  229. ExpandPool(PoolType.wave1, 2);
  230. wv1 = wave1Pool[hasNum[id] - 1];
  231. wv2 = wave1Pool[hasNum[id] - 2];
  232. wv1.transform.localScale = wave1Size;
  233. wv2.transform.localScale = wave1Size;
  234. wave1Pool.Remove(wv1);
  235. wave1Pool.Remove(wv2);
  236. usedWave1.Add(wv1);
  237. usedWave1.Add(wv2);
  238. wv1.GetComponent<WaveShockDir>().IsLeft = true;
  239. wv2.GetComponent<WaveShockDir>().IsLeft = false;
  240. wv1.GetComponent<WaveShockDir>().scale = waveEnlargeScale[curStateId];
  241. wv2.GetComponent<WaveShockDir>().scale = waveEnlargeScale[curStateId];
  242. wv1.GetComponent<WaveShockDir>().CheckTurn();
  243. wv2.GetComponent<WaveShockDir>().CheckTurn();
  244. wv1.BeShoot(this, wave1pos.position, dir1, wave1Damage.damage, wave1Damage.force, wave1Damage.changeHurt, wave1Damage.repelValue, false);
  245. wv2.BeShoot(this, wave2pos.position, dir2, wave1Damage.damage, wave1Damage.force, wave1Damage.changeHurt, wave1Damage.repelValue, false);
  246. wv1.transform.right = dir;
  247. wv2.transform.right = dir;
  248. break;
  249. case 1:
  250. ExpandPool(PoolType.wave2, 2);
  251. wv1 = wave2Pool[hasNum[id] - 1];
  252. wv2 = wave2Pool[hasNum[id] - 2];
  253. wv1.transform.localScale = wave2Size[index];
  254. wv2.transform.localScale = wave2Size[index];
  255. wave2Pool.Remove(wv1);
  256. wave2Pool.Remove(wv2);
  257. usedWave2.Add(wv1);
  258. usedWave2.Add(wv2);
  259. wv1.GetComponent<WaveShockDir>().IsLeft = true;
  260. wv2.GetComponent<WaveShockDir>().IsLeft = false;
  261. wv1.GetComponent<WaveShockDir>().scale = waveEnlargeScale[curStateId];
  262. wv2.GetComponent<WaveShockDir>().scale = waveEnlargeScale[curStateId];
  263. wv1.GetComponent<WaveShockDir>().CheckTurn();
  264. wv2.GetComponent<WaveShockDir>().CheckTurn();
  265. wv1.BeShoot(this, wave1pos.position, dir1, wave2Damage[index].damage, wave2Damage[index].force, wave2Damage[index].changeHurt, wave2Damage[index].repelValue, false);
  266. wv2.BeShoot(this, wave2pos.position, dir2, wave2Damage[index].damage, wave2Damage[index].force, wave2Damage[index].changeHurt, wave2Damage[index].repelValue, false);
  267. wv1.transform.right = dir;
  268. wv2.transform.right = dir;
  269. break;
  270. default:
  271. break;
  272. }
  273. hasNum[id] -= 2;
  274. }
  275. //连续发射冲击波
  276. private void ContinueShockWave(int id)
  277. {
  278. waveTime += Time.deltaTime;
  279. switch (id)
  280. {
  281. case 0:
  282. if (!isEndWave && ((!isContinuous && waveTime >= wave1aniTime)
  283. || (isContinuous && waveTime >= wave1loopTime)))
  284. {
  285. waveTime = 0;
  286. if (shockWaveTimes > 0)
  287. {
  288. shockWaveTimes--;
  289. isContinuous = true;
  290. ShockWave(0);
  291. }
  292. else
  293. {
  294. ani.Play("yumenguan_smash_r_end", 0, 0);
  295. isEndWave = true;
  296. }
  297. }
  298. if (isEndWave && waveTime >= waveEndTime)
  299. {
  300. isWave1 = false;
  301. isEndWave = false;
  302. isContinuous = false;
  303. waveTime = 0;
  304. EndCurAttackState(true);
  305. }
  306. break;
  307. case 1:
  308. if (waveTime >= wave2aniTime + waveEndTime)
  309. {
  310. isWave2 = false;
  311. waveTime = 0;
  312. EndCurAttackState(true);
  313. }
  314. break;
  315. default:
  316. break;
  317. }
  318. }
  319. //冲击波攻击
  320. public void ShockWave(int id)
  321. {
  322. switch (id)
  323. {
  324. case 0:
  325. if (!isContinuous)
  326. {
  327. isWave1 = true;
  328. waveTime = 0;
  329. ani.Play("yumenguan_smash_r", 0, 0);
  330. Invoke("ToLaunchWave1", launchDelayTime1);
  331. }
  332. else
  333. {
  334. waveTime = 0;
  335. ani.Play("yumenguan_smash_r_loop", 0, 0);
  336. Invoke("ToLaunchWave1", launchDelayTime1loop);
  337. }
  338. break;
  339. case 1:
  340. waveTime = 0;
  341. ani.Play("yumenguan_smash_double", 0, 0);
  342. Invoke("ToLaunchWave2", launchDelayTime2);
  343. Invoke("ToLaunchWave2", intervalTime[curStateId] + launchDelayTime2);
  344. Invoke("ToLaunchWave2", intervalTime[curStateId] * 2 + launchDelayTime2);
  345. isWave2 = true;
  346. break;
  347. default:
  348. break;
  349. }
  350. }
  351. public void ShootEyeLaser()
  352. {
  353. curTarget = TargetType.Player;
  354. CheckTarget();
  355. laser.endPos = targetCharacter.transform;
  356. laser.DrawLine();
  357. laser.gameObject.SetActive(true);
  358. }
  359. public void BlocksOut()
  360. {
  361. mostHeight = 0;
  362. print(mostHeight);
  363. curBlocks = Random.Range(minBlocks, maxBlocks);
  364. blocks = new Block[curBlocks];
  365. for(int i = curBlocks; i > 0; i--)
  366. {
  367. Vector3 pos = new Vector3(transform.position.x + Random.Range(minX, maxX), -2, 0);
  368. GameObject bl = Instantiate(block, pos, new Quaternion(0, 0, 0, 0), null);
  369. blocks[i - 1] = bl.GetComponent<Block>();
  370. blocks[i - 1].ymg = this;
  371. blocks[i - 1].Ready();
  372. }
  373. print(mostHeight);
  374. EndCurAttackState(true);
  375. }
  376. public void SpecialBlockOut(Block bl)
  377. {
  378. Vector3 pos = bl.transform.position;
  379. GameObject sb = Instantiate(specialBlock, pos, new Quaternion(0, 0, 0, 0), null);
  380. Block speblo = sb.GetComponent<Block>();
  381. speblo.ymg = this;
  382. speblo.height = bl.height;
  383. speblo.isSpecial = true;
  384. speblo.Ready();
  385. }
  386. public void AllBlocksDrop()
  387. {
  388. foreach(Block bl in blocks)
  389. {
  390. if (!bl.isDie)
  391. {
  392. bl.isSpecialDie = true;
  393. bl.ChangeAttackState(Block.State.drop);
  394. }
  395. }
  396. }
  397. //攻击
  398. public override void Attack()
  399. {
  400. switch (curAttackType)
  401. {
  402. case AttackMethods.move:
  403. ToMove();
  404. break;
  405. case AttackMethods.shockWave1:
  406. shockWaveTimes = Random.Range(minShockTimes, maxShockTimes);
  407. ShockWave(0);
  408. break;
  409. case AttackMethods.shockWave2:
  410. ShockWave(1);
  411. break;
  412. case AttackMethods.laser:
  413. ani.Play("yumenguan_eyelight", 0, 0);
  414. Invoke("ShootEyeLaser", waitTime);
  415. break;
  416. case AttackMethods.block:
  417. ani.Play("yumenguan_smash_double", 0, 0);
  418. Invoke("BlocksOut", aniTime);
  419. break;
  420. default:
  421. break;
  422. }
  423. }
  424. //根据当前大类随机攻击方式
  425. public override void RandomAttackType(AttackCategories cate)
  426. {
  427. AttackAssignment[] attack = attackConfigurations[(int)cate].attacks;
  428. switch (cate)
  429. {
  430. case AttackCategories.A:
  431. searchTrigger.enabled = true;
  432. otherTarget = null;
  433. otherTarget = searchTrigger.GetMinDisTarget(attackController.targetTypes, attackController.canHitFly);
  434. if (otherTarget == null)
  435. {
  436. isAttackWalk = false;
  437. curAttackType = AttackMethods.move;
  438. }
  439. else
  440. {
  441. curAttackType = RandomAttack(attackConfigurations[0].attacks);
  442. if (curAttackType == AttackMethods.move)
  443. {
  444. isAttackWalk = true;
  445. }
  446. }
  447. break;
  448. case AttackCategories.B:
  449. CheckTarget();
  450. if (weakEye.activeSelf && targetCharacter != null)
  451. {
  452. curAttackType = RandomAttack(attackConfigurations[1].attacks);
  453. }
  454. else
  455. {
  456. curAttackType = AttackMethods.block;
  457. }
  458. break;
  459. default:
  460. break;
  461. }
  462. }
  463. //在某个大类里按权重随机返回一个攻击类型
  464. public AttackMethods RandomAttack(AttackAssignment[] attackAssigns)
  465. {
  466. int weight = 0;
  467. AttackMethods last = new AttackMethods();
  468. foreach(AttackAssignment a in attackAssigns)
  469. {
  470. weight += a.weight;
  471. }
  472. int result = Random.Range(0, weight);
  473. foreach(AttackAssignment a in attackAssigns)
  474. {
  475. last = a.attack;
  476. result -= a.weight;
  477. if (result < 0)
  478. {
  479. return a.attack;
  480. }
  481. }
  482. return last;
  483. }
  484. }