GameController.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. import { AnimationComponent, CameraComponent, director, find, GraphicsComponent, instantiate, loader, Node, Prefab, Vec3, _decorator } from 'cc';
  2. import { EventDispatcher } from '../../../engines/events/EventDispatcher';
  3. import { GUIManager } from '../../../engines/gui/GUIManager';
  4. import { CameraUtils } from '../../../engines/utils/CameraUtils';
  5. import GameConfigManager from '../../models/GameConfigManager';
  6. import { GameModel } from '../../models/GameModel';
  7. import { FightingScene } from '../../scenes/FightingScene';
  8. import { UIConst } from '../UIConst';
  9. import { FenceBase } from './fences/FenceBase';
  10. import { FightingMediator } from './FightingMediator';
  11. import { MonsterBase } from './monsters/MonsterBase';
  12. import { MonsterHPUIPool } from './monsters/MonsterHPUIPool';
  13. import { SkillBase } from './skills/SkillBase';
  14. import SkillManager from './skills/SkillManager';
  15. import AutomaticFifleWeapon from './weapons/AutomaticFifleWeapon';
  16. import { SingleShotWeapon } from './weapons/SingleShotWeapon';
  17. import { WeaponBase } from './weapons/WeaponBase';
  18. const { ccclass, property } = _decorator;
  19. export class GameController extends EventDispatcher{
  20. public weapon:WeaponBase;
  21. public fence:FenceBase;
  22. public camera:CameraComponent;
  23. public leftEndNode:Node;
  24. public middleEndNode:Node;
  25. public rightEndNode:Node;
  26. public leftStartNode:Node;
  27. public middleStartNode:Node;
  28. public rightStartNode:Node;
  29. /**
  30. * 当前正在运行的技能
  31. */
  32. public currentSkill:SkillBase;
  33. /**
  34. * 游戏结束
  35. */
  36. public GameOver:boolean=true;
  37. public leftMonsterList:MonsterBase[];
  38. public middleMonsterList:MonsterBase[];
  39. public rightMonsterList:MonsterBase[];
  40. /**
  41. * 怪物血条层
  42. */
  43. public monsterHPUILayer:Node;
  44. /**
  45. * 关卡配置
  46. */
  47. public levelConfig:any;
  48. private monstersConfig:any[];
  49. /**
  50. * 这一关怪物数量
  51. */
  52. public monsterTotalCount:number;
  53. /**
  54. * 本局游戏时间
  55. */
  56. private runTime:number=0;
  57. private fightingMediator:FightingMediator;
  58. constructor()
  59. {
  60. super();
  61. this.leftMonsterList=[];
  62. this.middleMonsterList=[];
  63. this.rightMonsterList=[];
  64. }
  65. /**
  66. * 初始化
  67. * @param leftHand
  68. * @param rightHand
  69. */
  70. Init(uiMediator:FightingMediator):void{
  71. this.fightingMediator=uiMediator;
  72. let sceneNode:Node=find("FightingScene");
  73. if(sceneNode==null){
  74. throw new Error("找不到战斗场景节点!");
  75. }
  76. let sceneScript:FightingScene=sceneNode.getComponent(FightingScene);
  77. if(sceneNode==null){
  78. throw new Error("战斗场景上没有挂载FightingScene脚本!");
  79. }
  80. this.camera=sceneScript.camrea;
  81. this.leftStartNode=sceneScript.LeftStartPos;
  82. this.middleStartNode=sceneScript.MiddleStartPos;
  83. this.rightStartNode=sceneScript.RightStartPos;
  84. this.leftEndNode=sceneScript.LeftEndPos;
  85. this.middleEndNode=sceneScript.MiddleEndPos;
  86. this.rightEndNode=sceneScript.RightEndPos;
  87. this.weapon=this.CreateWeapon(sceneScript.leftHand,sceneScript.rightHand);
  88. this.StartGame();
  89. }
  90. /**
  91. * 改变栅栏
  92. */
  93. ChangeFence():void{
  94. this.GameOver=true;
  95. this.paused=true;
  96. let config:any=GameConfigManager.GetFenceConfig(GameModel.single.fenceId);
  97. // let prefab:Prefab=loader.getRes(config.prefab);
  98. // if(prefab==null){
  99. // throw new Error("栅栏资源未加载!");
  100. // }
  101. loader.loadRes(config.prefab,Prefab,(err,prefab:Prefab)=>{
  102. if(err!=null){
  103. throw new Error("栅栏加载出错!");
  104. }
  105. //如果老的存在,先清理
  106. if(this.fence&&this.fence.node!=null){
  107. this.fence.node.destroy();
  108. this.fence=null;
  109. }
  110. let fenceNode:Node=instantiate(prefab);
  111. this.fence=fenceNode.getComponent(FenceBase);
  112. this.fence.config=config;
  113. this.fence.node.setPosition(new Vec3(0,0,-2));
  114. this.scene.addChild(fenceNode);
  115. this.GameOver=false;
  116. this.paused=false;
  117. });
  118. }
  119. /**
  120. * 开始游戏
  121. */
  122. StartGame():void{
  123. GameModel.single.killCount=0;
  124. GameModel.single.angerCount=0;
  125. GameModel.single.integral=0;
  126. //重置技能
  127. this.SkillReset();
  128. this.levelConfig=GameConfigManager.GetLevelConfig(GameModel.single.currentLevel);
  129. this.runTime=0;
  130. //复制一份配置
  131. this.monstersConfig=GameConfigManager.CreateMonsterByList(this.levelConfig.monsters,this.runTime);
  132. this.monsterTotalCount=this.monstersConfig.length;
  133. //直接重置子弹数量
  134. this.weapon.bulletCount=GameConfigManager.GetWeaponConfig(GameModel.single.currentWeaponId).clip;
  135. //初始化栅栏
  136. this.ChangeFence();
  137. }
  138. /**
  139. * 游戏结束
  140. * @param state 0 胜利 1 失败 2正常退出
  141. */
  142. OnGameOver(state:number):void{
  143. //清理怪物
  144. this.ClearMonsters();
  145. //清理武器
  146. this.weapon.Destroy();
  147. //销毁栅栏
  148. if(this.fence.node){
  149. this.fence.node.destroy();
  150. }
  151. if(state==0){
  152. GUIManager.single.Show(UIConst.GAME_OVER_UI,true);
  153. }else if(state==1)
  154. {
  155. GUIManager.single.Show(UIConst.GAME_OVER_UI,false);
  156. }
  157. GUIManager.single.Hide(UIConst.FIGHTING_UI);
  158. this.GameOver=true;
  159. }
  160. paused:boolean;
  161. /**
  162. * 暂停游戏
  163. */
  164. PauseGame():void{
  165. this.paused=true;
  166. }
  167. /**
  168. * 继续游戏
  169. */
  170. PlayGame():void{
  171. this.paused=false;
  172. }
  173. /**
  174. * 重玩
  175. */
  176. Replay():void{
  177. this.StartGame();
  178. GUIManager.single.Show(UIConst.FIGHTING_UI);
  179. }
  180. /**
  181. * 开始下一关
  182. */
  183. PlayNextLevel():void{
  184. //清空试用栅栏
  185. GameModel.single.trialFenceId=-1;
  186. console.log(GameModel.single.currentLevel);
  187. this.StartGame();
  188. GUIManager.single.Show(UIConst.FIGHTING_UI);
  189. }
  190. /**
  191. * 清理怪物
  192. */
  193. ClearMonsters():void{
  194. this.clearMonsterList(this.leftMonsterList);
  195. this.clearMonsterList(this.middleMonsterList);
  196. this.clearMonsterList(this.rightMonsterList);
  197. }
  198. private clearMonsterList(list:MonsterBase[]):void{
  199. if(list.length>0){
  200. list.forEach(element => {
  201. if(element.node!=null&&element.node.isValid){
  202. element.node.destroy();
  203. }
  204. });
  205. }
  206. list.length=0;
  207. }
  208. TryFire(fireKey:number):void{
  209. if(this.currentSkill!=null&&this.currentSkill.unController){
  210. return;
  211. }
  212. if(fireKey==0){
  213. this.weapon.StopFire();
  214. }else{
  215. this.weapon.StartFire(fireKey);
  216. }
  217. }
  218. /**
  219. * 使用技能
  220. */
  221. UseSkill():void{
  222. if(this.currentSkill!=null){
  223. return;
  224. }
  225. //重置子弹数量
  226. this.weapon.bulletCount=this.weapon.maxBulletCount;
  227. this.currentSkill=SkillManager.single.Create(this.weapon.weaponConfig.skill);
  228. this.currentSkill.config=GameConfigManager.GetSkillConfig(this.weapon.weaponConfig.skill);
  229. this.currentSkill.AddEvent(SkillBase.SKILL_COMPLETE,this,this.SkillReset,0);
  230. this.currentSkill.UseSkill(this.weapon,this);
  231. GameModel.single.angerCount=0;
  232. }
  233. /**
  234. * 清除技能
  235. */
  236. private SkillReset():void{
  237. if(this.currentSkill!=null){
  238. this.currentSkill.RemoveEvent(SkillBase.SKILL_COMPLETE,this,this.SkillReset);
  239. this.currentSkill.Dispose();
  240. this.currentSkill=null;
  241. }
  242. }
  243. Update(dt:number):void{
  244. if(this.paused){
  245. return;
  246. }
  247. if(this.GameOver){
  248. return;
  249. }
  250. if(this.currentSkill!=null){
  251. this.currentSkill.Update(dt);
  252. }
  253. this.runTime+=dt*1000;
  254. this.weapon.Update(dt);
  255. this.CheckMonster();
  256. }
  257. get drawingBoard():GraphicsComponent{
  258. return this.fightingMediator.DrawingBoard;
  259. }
  260. /**
  261. * 检测怪物
  262. */
  263. CheckMonster():void{
  264. if(this.monstersConfig.length>0){
  265. let config:any;
  266. let monster:Node;
  267. for (let index = 0; index < this.monstersConfig.length; index++) {
  268. config = this.monstersConfig[index];
  269. if(this.runTime>=config.createTime){
  270. //根据配置创建怪物
  271. monster=this.CreateMonster(config);
  272. this.scene.addChild(monster);
  273. this.monstersConfig.splice(index,1);
  274. index--;
  275. }
  276. }
  277. }
  278. if(this.monstersConfig.length==0&&this.leftMonsterList.length==0&&this.middleMonsterList.length==0&&this.rightMonsterList.length==0){
  279. //全部怪物死亡
  280. console.log("通关");
  281. GameModel.single.currentLevel++;
  282. if(GameModel.single.currentLevel>=GameConfigManager.MaxLevel){
  283. GameModel.single.currentLevel=GameConfigManager.MaxLevel;
  284. }
  285. if(this.GameOver==false){
  286. this.OnGameOver(0);
  287. }
  288. }
  289. }
  290. /**
  291. * 怪物攻击
  292. * @param damage 伤害
  293. */
  294. MonsterAttack(attackType:number,damage:number):void{
  295. if(this.GameOver){
  296. return;
  297. }
  298. if(attackType==0){
  299. if(this.fence.IsDie==false){
  300. this.fence.Damage(damage);
  301. let cameraUtils:CameraUtils=this.camera.getComponent(CameraUtils);
  302. if(cameraUtils!=null){
  303. cameraUtils.shake(500,new Vec3(-0.01,-0.01,0),new Vec3(0.01,0.01,0));
  304. }
  305. }
  306. }else{
  307. if(this.GameOver){
  308. return;
  309. }else{
  310. this.OnGameOver(1);
  311. }
  312. }
  313. }
  314. /**
  315. * 是否有怪物可攻击 -1没有可攻击怪 0 左边 1中间 2右边
  316. */
  317. public get FindAttackMonsterPosType():number{
  318. let leftDis:number=this.NearestDistance(this.leftMonsterList);
  319. let middleDis:number=this.NearestDistance(this.middleMonsterList);
  320. let rightDis:number=this.NearestDistance(this.rightMonsterList);
  321. if(leftDis<middleDis&&leftDis<rightDis){
  322. return 0;
  323. }
  324. if(middleDis<leftDis&&middleDis<rightDis){
  325. return 1;
  326. }
  327. if(rightDis<leftDis&&rightDis<middleDis){
  328. return 2;
  329. }
  330. return -1;
  331. }
  332. /**
  333. * 就算这一路中可攻击怪的最近距离
  334. * @param monsterList
  335. */
  336. private NearestDistance(monsterList:MonsterBase[]):number{
  337. let monster:MonsterBase;
  338. let endNode:Node;
  339. let minDis:number=Number.MAX_VALUE;
  340. let dis:number;
  341. for (let index = 0; index < monsterList.length; index++) {
  342. monster = monsterList[index];
  343. if(monster.config.startPos==0){
  344. endNode=this.leftEndNode;
  345. }else if(monster.config.startPos==1){
  346. endNode=this.middleEndNode;
  347. }else{
  348. endNode=this.rightEndNode;
  349. }
  350. dis=Vec3.distance(monster.node.worldPosition,endNode.worldPosition);
  351. if(dis<=this.weapon.weaponConfig.attackDistance){
  352. if(dis<minDis){
  353. minDis=dis;
  354. }
  355. }
  356. }
  357. return minDis;
  358. }
  359. /**
  360. * 武器攻击
  361. */
  362. WeaponAttack():void{
  363. //只要开了第一枪就不算新玩家了
  364. GameModel.single.isNewPlayer=false;
  365. let monsterList:MonsterBase[];
  366. let monsterLists:Array<Array<MonsterBase>>=[];
  367. //攻击1条路
  368. if(this.weapon.weaponConfig.attackRange==1){
  369. if(this.weapon.fireKey==1){
  370. monsterList=this.leftMonsterList;
  371. }else if(this.weapon.fireKey==2){
  372. monsterList=this.middleMonsterList;
  373. }else{
  374. monsterList=this.rightMonsterList;
  375. }
  376. if(monsterList.length==0){
  377. return;
  378. }
  379. monsterList.sort(this.monsterSortFunc);
  380. monsterLists.push(monsterList);
  381. }else if(this.weapon.weaponConfig.attackRange==2){//攻击2条路
  382. //左
  383. if(this.weapon.fireKey==1){
  384. if(this.leftMonsterList.length>0){
  385. this.leftMonsterList.sort(this.monsterSortFunc);
  386. monsterLists.push(this.leftMonsterList);
  387. }
  388. if(this.middleMonsterList.length>0){
  389. this.middleMonsterList.sort(this.monsterSortFunc);
  390. monsterLists.push(this.middleMonsterList);
  391. }
  392. }else if(this.weapon.fireKey==2){//中
  393. if(this.middleMonsterList.length>0){
  394. this.middleMonsterList.sort(this.monsterSortFunc);
  395. monsterLists.push(this.middleMonsterList);
  396. }
  397. //随机左边/右边
  398. let random:number=Math.random();
  399. if(random>0.5){
  400. if(this.rightMonsterList.length>0){
  401. this.rightMonsterList.sort(this.monsterSortFunc);
  402. monsterLists.push(this.rightMonsterList);
  403. }
  404. }else{
  405. if(this.leftMonsterList.length>0){
  406. this.leftMonsterList.sort(this.monsterSortFunc);
  407. monsterLists.push(this.leftMonsterList);
  408. }
  409. }
  410. }else{//右
  411. if(this.rightMonsterList.length>0){
  412. this.rightMonsterList.sort(this.monsterSortFunc);
  413. monsterLists.push(this.rightMonsterList);
  414. }
  415. if(this.middleMonsterList.length>0){
  416. this.middleMonsterList.sort(this.monsterSortFunc);
  417. monsterLists.push(this.middleMonsterList);
  418. }
  419. }
  420. }else{//攻击3条路
  421. if(this.leftMonsterList.length>0){
  422. this.leftMonsterList.sort(this.monsterSortFunc);
  423. monsterLists.push(this.leftMonsterList);
  424. }
  425. if(this.middleMonsterList.length>0){
  426. this.middleMonsterList.sort(this.monsterSortFunc);
  427. monsterLists.push(this.middleMonsterList);
  428. }
  429. if(this.middleMonsterList.length>0){
  430. this.middleMonsterList.sort(this.monsterSortFunc);
  431. monsterLists.push(this.middleMonsterList);
  432. }
  433. }
  434. //没有任何怪物可以攻击
  435. if(monsterLists.length==0){
  436. return;
  437. }
  438. //攻击怪物
  439. let monster:MonsterBase;
  440. let endNode:Node;
  441. let index:number=0;
  442. let total:number=0;
  443. let dis:number=0;
  444. monsterLists.forEach(monsterList => {
  445. //攻击怪物
  446. index=0;
  447. total=Math.min(this.weapon.weaponConfig.attackCount,monsterList.length);
  448. while(index<total){
  449. monster=monsterList[index];
  450. if(monster.config.startPos==0){
  451. endNode=this.leftEndNode;
  452. }else if(monster.config.startPos==1){
  453. endNode=this.middleEndNode;
  454. }else{
  455. endNode=this.rightEndNode;
  456. }
  457. if(Vec3.distance(monster.node.worldPosition,endNode.worldPosition)<=this.weapon.weaponConfig.attackDistance){
  458. if(this.currentSkill!=null){
  459. monster.Damage(this.weapon.weaponConfig.damage*this.currentSkill.damage);
  460. }else{
  461. monster.Damage(this.weapon.weaponConfig.damage);
  462. }
  463. }else{
  464. console.log("超出枪械攻击距离Miss");
  465. }
  466. index++;
  467. total=Math.min(this.weapon.weaponConfig.attackCount,monsterList.length);
  468. }
  469. for (let index = 0; index < monsterList.length; index++) {
  470. monster = monsterList[index];
  471. //怪物死亡
  472. if(monster.isDie){
  473. //增加怒气
  474. GameModel.single.angerCount+=monster.config.anger;
  475. //增加击杀量
  476. GameModel.single.killCount++;
  477. //增加积分
  478. if(monster.config.integral>0){
  479. GameModel.single.integral+=monster.config.integral;
  480. }
  481. //从列表中删除
  482. monsterList.splice(index,1);
  483. index--;
  484. }
  485. }
  486. });
  487. }
  488. private monsterSortFunc(a:MonsterBase,b:MonsterBase):number{
  489. if(a.node.position.z>b.node.position.z){
  490. return -1;
  491. }else if(a.node.position.z<b.node.position.z){
  492. return 1;
  493. }
  494. return 0;
  495. }
  496. /**
  497. * 根据配置生成怪物
  498. * @param config
  499. */
  500. private CreateMonster(config:any):Node{
  501. let monsterConfig:any=GameConfigManager.GetMonsterConfig(config.monsterId);
  502. if(monsterConfig==null){
  503. throw new Error("找不到怪物配置:"+config.monsterId);
  504. }
  505. let prefab:Prefab=loader.getRes(monsterConfig.prefab);
  506. let monster:Node=instantiate(prefab);
  507. monster.name="Monster"+config.mId;
  508. let monsterScript=monster.addComponent(MonsterBase);
  509. monsterScript.config=config;
  510. //左
  511. if(config.startPos==0){
  512. monster.setPosition(this.leftStartNode.worldPosition);
  513. this.leftMonsterList.push(monsterScript);
  514. }else if(config.startPos==1){//中
  515. monster.setPosition(this.middleStartNode.worldPosition);
  516. this.middleMonsterList.push(monsterScript);
  517. }else{//右
  518. monster.setPosition(this.rightStartNode.worldPosition);
  519. this.rightMonsterList.push(monsterScript);
  520. }
  521. //创建血条
  522. monsterScript.hpView=MonsterHPUIPool.Create();
  523. monsterScript.hpView.monster=monsterScript;
  524. return monster;
  525. }
  526. private CreateWeapon(leftHand:AnimationComponent,rightHand:AnimationComponent):any{
  527. let config:any=GameConfigManager.GetWeaponConfig(GameModel.single.currentWeaponId);
  528. if(config.fireType==0){
  529. return new SingleShotWeapon(leftHand,rightHand);
  530. }else if(config.fireType==1){
  531. return new AutomaticFifleWeapon(leftHand,rightHand);
  532. }
  533. return new SingleShotWeapon(leftHand,rightHand);;
  534. }
  535. /**
  536. * 判断是否需要提示射击
  537. */
  538. FireTip(type:number):boolean{
  539. let isTrue:boolean=false;
  540. switch (type) {
  541. case 1:
  542. this.leftMonsterList.forEach(element => {
  543. if(element.config.fireTip>0){
  544. isTrue=true;
  545. }
  546. });
  547. break;
  548. case 2:
  549. this.middleMonsterList.forEach(element => {
  550. if(element.config.fireTip>0){
  551. isTrue=true;
  552. }
  553. });
  554. break;
  555. case 3:
  556. this.rightMonsterList.forEach(element => {
  557. if(element.config.fireTip>0){
  558. isTrue=true;
  559. }
  560. });
  561. break;
  562. }
  563. return isTrue;
  564. }
  565. get scene(){
  566. return director.getScene();
  567. }
  568. private static instance:GameController;
  569. public static get single(){
  570. if(this.instance==null){
  571. this.instance=new GameController();
  572. }
  573. return this.instance;
  574. }
  575. }