YuMenGuan.cs 16 KB

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