YuMenGuan.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 = PoolManager.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, attackController.curAttackMethod.searchMode);
  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>().CheckTurn();
  240. wv2.GetComponent<WaveShockDir>().CheckTurn();
  241. wv1.BeShoot(this, wave1pos.position, dir1,false);
  242. wv2.BeShoot(this, wave2pos.position, dir2, 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>().CheckTurn();
  259. wv2.GetComponent<WaveShockDir>().CheckTurn();
  260. wv1.BeShoot(this, wave1pos.position, dir1, false);
  261. wv2.BeShoot(this, wave2pos.position, dir2, false);
  262. wv1.transform.right = dir;
  263. wv2.transform.right = dir;
  264. break;
  265. default:
  266. break;
  267. }
  268. hasNum[id] -= 2;
  269. }
  270. //连续发射冲击波
  271. private void ContinueShockWave(int id)
  272. {
  273. waveTime += Time.deltaTime;
  274. switch (id)
  275. {
  276. case 0:
  277. if (!isEndWave && ((!isContinuous && waveTime >= wave1aniTime)
  278. || (isContinuous && waveTime >= wave1loopTime)))
  279. {
  280. waveTime = 0;
  281. if (shockWaveTimes > 0)
  282. {
  283. shockWaveTimes--;
  284. isContinuous = true;
  285. ShockWave(0);
  286. }
  287. else
  288. {
  289. ani.Play("yumenguan_smash_r_end", 0, 0);
  290. isEndWave = true;
  291. }
  292. }
  293. if (isEndWave && waveTime >= waveEndTime)
  294. {
  295. isWave1 = false;
  296. isEndWave = false;
  297. isContinuous = false;
  298. waveTime = 0;
  299. EndCurAttackState(true);
  300. }
  301. break;
  302. case 1:
  303. if (waveTime >= wave2aniTime + waveEndTime)
  304. {
  305. isWave2 = false;
  306. waveTime = 0;
  307. EndCurAttackState(true);
  308. }
  309. break;
  310. default:
  311. break;
  312. }
  313. }
  314. //冲击波攻击
  315. public void ShockWave(int id)
  316. {
  317. switch (id)
  318. {
  319. case 0:
  320. if (!isContinuous)
  321. {
  322. isWave1 = true;
  323. waveTime = 0;
  324. ani.Play("yumenguan_smash_r", 0, 0);
  325. Invoke("ToLaunchWave1", launchDelayTime1);
  326. }
  327. else
  328. {
  329. waveTime = 0;
  330. ani.Play("yumenguan_smash_r_loop", 0, 0);
  331. Invoke("ToLaunchWave1", launchDelayTime1loop);
  332. }
  333. break;
  334. case 1:
  335. waveTime = 0;
  336. ani.Play("yumenguan_smash_double", 0, 0);
  337. Invoke("ToLaunchWave2", launchDelayTime2);
  338. isWave2 = true;
  339. break;
  340. default:
  341. break;
  342. }
  343. }
  344. public void ShootEyeLaser()
  345. {
  346. curTarget = TargetType.Player;
  347. //CheckTarget();
  348. laser.endPos = targetCharacter.transform;
  349. laser.DrawLine();
  350. laser.gameObject.SetActive(true);
  351. }
  352. public void BlocksOut()
  353. {
  354. mostHeight = 0;
  355. print(mostHeight);
  356. curBlocks = Random.Range(minBlocks, maxBlocks);
  357. blocks = new Block[curBlocks];
  358. for(int i = curBlocks; i > 0; i--)
  359. {
  360. Vector3 pos = new Vector3(transform.position.x + Random.Range(minX, maxX), -2, 0);
  361. GameObject bl = PoolManager.Instantiate(block, pos, new Quaternion(0, 0, 0, 0), null);
  362. blocks[i - 1] = bl.GetComponent<Block>();
  363. blocks[i - 1].ymg = this;
  364. blocks[i - 1].Ready();
  365. }
  366. print(mostHeight);
  367. EndCurAttackState(true);
  368. }
  369. public void SpecialBlockOut(Block bl)
  370. {
  371. Vector3 pos = bl.transform.position;
  372. GameObject sb = PoolManager.Instantiate(specialBlock, pos, new Quaternion(0, 0, 0, 0), null);
  373. Block speblo = sb.GetComponent<Block>();
  374. speblo.ymg = this;
  375. speblo.height = bl.height;
  376. speblo.isSpecial = true;
  377. speblo.Ready();
  378. }
  379. public void AllBlocksDrop()
  380. {
  381. foreach(Block bl in blocks)
  382. {
  383. if (!bl.isDie)
  384. {
  385. bl.isSpecialDie = true;
  386. bl.ChangeAttackState(Block.State.drop);
  387. }
  388. }
  389. }
  390. //攻击
  391. public override void Attack()
  392. {
  393. switch (curAttackType)
  394. {
  395. case AttackMethods.move:
  396. ToMove();
  397. break;
  398. case AttackMethods.shockWave1:
  399. shockWaveTimes = Random.Range(minShockTimes, maxShockTimes);
  400. ShockWave(0);
  401. break;
  402. case AttackMethods.shockWave2:
  403. ShockWave(1);
  404. break;
  405. case AttackMethods.laser:
  406. ani.Play("yumenguan_eyelight", 0, 0);
  407. Invoke("ShootEyeLaser", waitTime);
  408. break;
  409. case AttackMethods.block:
  410. ani.Play("yumenguan_smash_double", 0, 0);
  411. Invoke("BlocksOut", aniTime);
  412. break;
  413. default:
  414. break;
  415. }
  416. }
  417. //根据当前大类随机攻击方式
  418. public override void RandomAttackType(AttackCategories cate)
  419. {
  420. AttackAssignment[] attack = attackConfigurations[(int)cate].attacks;
  421. switch (cate)
  422. {
  423. case AttackCategories.A:
  424. searchTrigger.enabled = true;
  425. otherTarget = null;
  426. otherTarget = searchTrigger.GetMinDisTarget(attackController.targetTypes, attackController.canHitFly, attackController.curAttackMethod.searchMode);
  427. if (otherTarget == null)
  428. {
  429. isAttackWalk = false;
  430. curAttackType = AttackMethods.move;
  431. }
  432. else
  433. {
  434. curAttackType = RandomAttack(attackConfigurations[0].attacks);
  435. if (curAttackType == AttackMethods.move)
  436. {
  437. isAttackWalk = true;
  438. }
  439. }
  440. break;
  441. case AttackCategories.B:
  442. //CheckTarget();
  443. if (weakEye.activeSelf && targetCharacter != null)
  444. {
  445. curAttackType = RandomAttack(attackConfigurations[1].attacks);
  446. }
  447. else
  448. {
  449. curAttackType = AttackMethods.block;
  450. }
  451. break;
  452. default:
  453. break;
  454. }
  455. }
  456. //在某个大类里按权重随机返回一个攻击类型
  457. public AttackMethods RandomAttack(AttackAssignment[] attackAssigns)
  458. {
  459. int weight = 0;
  460. AttackMethods last = new AttackMethods();
  461. foreach(AttackAssignment a in attackAssigns)
  462. {
  463. weight += a.weight;
  464. }
  465. int result = Random.Range(0, weight);
  466. foreach(AttackAssignment a in attackAssigns)
  467. {
  468. last = a.attack;
  469. result -= a.weight;
  470. if (result < 0)
  471. {
  472. return a.attack;
  473. }
  474. }
  475. return last;
  476. }
  477. }