levelLogic.ts 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  1. import { GameEvent } from "../constant/constant-game";
  2. import { USER } from "../constant/constant-user";
  3. import AccountModel from "../data/Account/AccountModel";
  4. import buffDtManager from "../dataManager/buffDtManager";
  5. import itemDtManager from "../dataManager/itemDtManager";
  6. import modelDtlManager from "../dataManager/modelDtlManager";
  7. import npcHelper from "../dataManager/npcHelper";
  8. import { AssetsHelper } from "../framework/asset/AssetsHelper";
  9. import { BenzAssetManager } from "../framework/asset/BenzAssetManager";
  10. import EventManager from "../framework/event/EventManager";
  11. import { Log, LOG_TAG } from "../framework/log/Log";
  12. import AudioManager from "../framework/music/AudioManager";
  13. import UIBase from "../framework/ui/UIBase";
  14. import UIHelp from "../framework/ui/UIHelp";
  15. import Utils from "../framework/utils/utils";
  16. import bundleManager from "../manager/bundleManager";
  17. import dungeonManager from "../manager/dungeonManager";
  18. import effectManager from "../manager/effectManager";
  19. import levelManager from "../manager/levelManager";
  20. import playerActionMgr from "../manager/playerActionMgr";
  21. import dungeonNode from "../ui/dungeonNode";
  22. import UIFailView from "../ui/uiview/Interface/UIFailView";
  23. import UIReliveView from "../ui/uiview/Interface/UIReliveView";
  24. import UITopMenu from "../ui/uiview/Interface/UITopMenu";
  25. import UIWinView from "../ui/uiview/Interface/UIWinView";
  26. import UIFallitem from "../ui/uiview/map/UIFallitem";
  27. import BulletObject from "./gameObject/bullet/bulletObject";
  28. import zomBiebullet from "./gameObject/bullet/zomBiebullet";
  29. import GameObject from "./gameObject/gameObject";
  30. import NodeMove from "./gameObject/player/nodeMove";
  31. import PlayerObject from "./gameObject/player/playerObject";
  32. import PlayerProp from "./gameObject/player/playerProp";
  33. import PlayerView from "./gameObject/player/playerView";
  34. import NpcObject from "./gameObject/tool/npcObject";
  35. import WeaponProp from "./gameObject/tool/weaponProp";
  36. import ZomBieObject from "./gameObject/zombie/zombieObject";
  37. import ZombieProp from "./gameObject/zombie/zombieProp";
  38. import zombieView from "./gameObject/zombie/zombieView";
  39. import gameData from "./utrl/gameData";
  40. import {
  41. ANI_STATE,
  42. ATTACK_MODE,
  43. EVENT_TYPE, FALLITEM_TYPE,
  44. GAME_OBJECT_TYPE, HERO_TYPE,
  45. HeroId,
  46. ITEM_TYPE,
  47. JOY_STATE,
  48. ROLE_STATE,
  49. WEAPON_TYPE
  50. } from "./utrl/gameEnum";
  51. import gameEventManager from "./utrl/gameEventManager";
  52. import UICrazyAwardView from "../ui/uiview/Interface/UICrazyAwardView";
  53. import UIMng from "../framework/ui/UIMng";
  54. const { ccclass, property } = cc._decorator;
  55. /**关卡逻辑类 */
  56. @ccclass
  57. export default class LevelLogic extends cc.Component {
  58. @property(cc.Node)
  59. /**地图父节点 */
  60. map_parent: cc.Node = null;
  61. @property(cc.Node)
  62. /**实体父节点 */
  63. object_parent: cc.Node = null;
  64. /**道具父节点 */
  65. @property(cc.Node)
  66. prop_parent: cc.Node = null;
  67. /**道具预制体 */
  68. @property(cc.Prefab)
  69. prop_pre: cc.Prefab = null;
  70. /**未解锁npc父节点 */
  71. @property(cc.Node)
  72. npc_parent: cc.Node = null;
  73. @property()
  74. playerNum: Number = 6;
  75. /**摄像机节点 */
  76. private camera: cc.Node = null;
  77. /**玩家预制体 */
  78. private player_pre: cc.Prefab = null;
  79. /**僵尸预制体 */
  80. private zombie_pre: cc.Prefab = null;
  81. /**子弹预制体 */
  82. private bullet_pre: cc.Prefab = null;
  83. game_over: boolean = false; //是否失败
  84. isGoDungeon: boolean = false;
  85. isWin: boolean = false; //是否通关成功
  86. playerDiePos: cc.Vec3 = null; //玩家死亡位置
  87. playerIsMove: boolean = false;
  88. /**buff间隔时间 */
  89. private buff_dur = 10;
  90. /**buff间隔计时器 */
  91. private buff_timer = 0;
  92. /**buff回复子弹血量体力值组 */
  93. private buffs: number[][] = [];
  94. /**地图包围框节点 */
  95. private map_frame: cc.Node = null;
  96. /**迷你地图 */
  97. private mini_map: cc.Node = null;
  98. zombieBornNodeArr: cc.Node[];
  99. /**玩家组 */
  100. /**角色组,0玩家组,1僵尸组 */
  101. roles: cc.Node[][] = [null, null];
  102. _isInit: boolean = false;
  103. _initPlayer: boolean = false;
  104. dtCount: number = 0;
  105. isPause = false;
  106. //对象池
  107. playerBulletPool: cc.NodePool;
  108. zombieNodePool: cc.NodePool;
  109. playerNodePool: cc.NodePool;
  110. propsNodePool: cc.NodePool;
  111. start() {
  112. // gameData.curPeopleNum = this.getPlayerArr().length;
  113. gameEventManager.emit(EVENT_TYPE.CANT_JOY, true);
  114. Utils.tipsSprite('main/jiangshilaixi', cc.director.getScene().getChildByName('Canvas'));
  115. gameEventManager.emit(EVENT_TYPE.attack_mode);
  116. if (AccountModel.getInstance().curLevel == 1) { zjSdk?.sendEvent('进入新手引导') };
  117. this.initBulletPool();
  118. this.initZombiePool();
  119. this.initPlayerPool();
  120. this.initPropPool();
  121. }
  122. initPlayerPool() {
  123. this.playerNodePool = Utils.initNodePool(this.player_pre, 20);
  124. }
  125. initZombiePool() {
  126. this.zombieNodePool = Utils.initNodePool(this.zombie_pre, 15);
  127. }
  128. initBulletPool() {
  129. this.playerBulletPool = Utils.initNodePool(this.bullet_pre, 10);
  130. }
  131. initPropPool() {
  132. this.propsNodePool = new cc.NodePool();
  133. }
  134. clearPropPool() {
  135. let poolLen = this.propsNodePool.size();
  136. if (poolLen > 0 && poolLen > 20) {
  137. this.propsNodePool.clear();
  138. Log.log(LOG_TAG.DEBUG, 'clearpropsNodePool');
  139. }
  140. }
  141. onEnable() {
  142. this._isInit = false;
  143. this._initPlayer = false;
  144. gameEventManager.on(EVENT_TYPE.GAME_RELIVE, this.gameRelive, this);
  145. gameEventManager.on(EVENT_TYPE.GAME_OVER, this.gameOver, this);
  146. gameEventManager.on(EVENT_TYPE.joy_stick, this.playerMove, this);
  147. gameEventManager.on(EVENT_TYPE.random_leader, this.randomLeader, this);
  148. gameEventManager.on(EVENT_TYPE.add_zombie, this.addZombie, this);
  149. gameEventManager.on(EVENT_TYPE.bullet0, this.playerRemoteAttack0, this);
  150. gameEventManager.on(EVENT_TYPE.bullet1, this.playerRemoteAttack1, this);
  151. gameEventManager.on(EVENT_TYPE.zombie_boom, this.zombieRemoteBoom, this);
  152. gameEventManager.on(EVENT_TYPE.player_die, this.diePlayerCbk, this);
  153. gameEventManager.on(EVENT_TYPE.zombie_die, this.dieZombieCbk, this);
  154. gameEventManager.on(EVENT_TYPE.RESET_ZOMBIE, this.reSetZombie, this);
  155. gameEventManager.on(EVENT_TYPE.zombieDieForCrazy, this.zombieDie, this);
  156. gameEventManager.on(EVENT_TYPE.ADD_HERO, this.addHero, this)
  157. gameEventManager.on(EVENT_TYPE.ADD_HERO_BY_ID, this.addHeroById, this)
  158. gameEventManager.on(EVENT_TYPE.ADD_WEAPONS, this.addWeapons, this)
  159. gameEventManager.on(EVENT_TYPE.VIOLENT_ZOMBIE, this.zombieViolent, this);
  160. gameEventManager.on(EVENT_TYPE.BOSS_SKIN_ADDZOMBIE, this.bossAddZombie, this)
  161. gameEventManager.on(EVENT_TYPE.ADD_BOSS, this.addBoss, this)
  162. gameEventManager.on(EVENT_TYPE.CHANGE_STATUS, this.changeStatue, this)
  163. gameEventManager.on(EVENT_TYPE.RECOVER_ZOMBIE, this.recoverZombie, this)
  164. gameEventManager.on(EVENT_TYPE.RECOVER_PLAYER, this.recoverPlayer, this)
  165. gameEventManager.on(EVENT_TYPE.RECOVER_PLAYER_BULLET, this.recoverPlayerBullet, this)
  166. this.isGoDungeon = false;
  167. if (this.mini_map) { this.mini_map.active = true; }
  168. this.scheduleOnce(() => {
  169. this.iniPlayer();
  170. }, 0.04)
  171. gameEventManager.emit(EVENT_TYPE.INIT_SWITCH_BTN);
  172. }
  173. isAddBoos() {
  174. if (gameData.bAddBoss) {
  175. let bossList = this.getBossList();
  176. if (bossList.length <= 0) {
  177. this.addBoss();
  178. }
  179. }
  180. }
  181. onDisable() {
  182. gameEventManager.off(EVENT_TYPE.GAME_RELIVE, this.gameRelive, this);
  183. gameEventManager.off(EVENT_TYPE.ADD_BOSS, this.addBoss, this)
  184. gameEventManager.off(EVENT_TYPE.BOSS_SKIN_ADDZOMBIE, this.bossAddZombie, this)
  185. gameEventManager.off(EVENT_TYPE.ADD_HERO, this.addHero, this)
  186. gameEventManager.off(EVENT_TYPE.ADD_HERO_BY_ID, this.addHeroById, this)
  187. gameEventManager.off(EVENT_TYPE.ADD_WEAPONS, this.addWeapons, this)
  188. gameEventManager.off(EVENT_TYPE.VIOLENT_ZOMBIE, this.zombieViolent, this);
  189. gameEventManager.off(EVENT_TYPE.RESET_ZOMBIE, this.reSetZombie, this);
  190. gameEventManager.off(EVENT_TYPE.joy_stick, this.playerMove, this);
  191. gameEventManager.off(EVENT_TYPE.random_leader, this.randomLeader, this);
  192. gameEventManager.off(EVENT_TYPE.GAME_OVER, this.gameOver, this);
  193. gameEventManager.off(EVENT_TYPE.add_zombie, this.addZombie, this);
  194. gameEventManager.off(EVENT_TYPE.bullet0, this.playerRemoteAttack0, this);
  195. gameEventManager.off(EVENT_TYPE.bullet1, this.playerRemoteAttack1, this);
  196. gameEventManager.off(EVENT_TYPE.zombie_boom, this.zombieRemoteBoom, this);
  197. gameEventManager.off(EVENT_TYPE.player_die, this.diePlayerCbk, this);
  198. gameEventManager.off(EVENT_TYPE.zombie_die, this.dieZombieCbk, this);
  199. gameEventManager.off(EVENT_TYPE.CHANGE_STATUS, this.changeStatue, this)
  200. gameEventManager.off(EVENT_TYPE.zombieDieForCrazy, this.zombieDie, this);
  201. gameEventManager.off(EVENT_TYPE.RECOVER_ZOMBIE, this.recoverZombie, this);
  202. gameEventManager.off(EVENT_TYPE.RECOVER_PLAYER, this.recoverPlayer, this)
  203. gameEventManager.off(EVENT_TYPE.RECOVER_PLAYER_BULLET, this.recoverPlayerBullet, this)
  204. }
  205. changeStatue(flag) {
  206. this.isPause = flag;
  207. }
  208. update(dt: number) {
  209. if (this.isPause) {
  210. return;
  211. }
  212. if (!this.playerIsMove || !this._isInit) {
  213. return;
  214. }
  215. if (this.isWin || this.game_over) {
  216. return;
  217. }
  218. // this.runBullets(dt);
  219. this.checkPlayerWithProp();
  220. this.checkPlayerBulletCrashZombie();
  221. this.checkBulletCrashPlayer();
  222. this.playerRunBuff(dt);
  223. this.isCloseDungeon();
  224. this.checkPlayerWithNpc();
  225. this.isCloseWinNode();
  226. this.createZombie();
  227. this.fast();
  228. this.updateRoleDt();
  229. }
  230. lateUpdate(dt) {
  231. this.cameraTrack(dt);
  232. }
  233. updateRoleDt() {
  234. if (this.game_over) { return }
  235. this.dtCount++;
  236. if (this.dtCount >= 15) {
  237. this.dtCount = 0;
  238. this.layInPlayerDt();
  239. AccountModel.getInstance().playerDtList = gameData.playerDtList;
  240. }
  241. }
  242. bossAddZombie(addNum: number, targetNode: cc.Node) {
  243. let randomID = npcHelper.getRandomZombieId();
  244. this.schedule(() => {
  245. if (!targetNode || !cc.isValid(targetNode)) {
  246. return;
  247. }
  248. let randomX = 0//Utils.random(-100, 100);
  249. let randomY = Utils.random(-50, -20)
  250. let pos = cc.v3(targetNode.position.x + randomX, targetNode.position.y + randomY);
  251. this.addZombie(randomID, pos);
  252. }, 0.5, addNum)
  253. }
  254. //回收僵尸节点
  255. recoverZombie(nd) {
  256. let zombiePool = this.zombieNodePool;
  257. if (zombiePool) {
  258. zombiePool.put(nd);
  259. Log.log(LOG_TAG.DEBUG, zombiePool);
  260. } else {
  261. nd.destroy();
  262. }
  263. }
  264. recoverPlayer(nd) {
  265. let playerPool = this.playerNodePool;
  266. if (playerPool) {
  267. playerPool.put(nd);
  268. Log.log(LOG_TAG.DEBUG, playerPool);
  269. } else {
  270. nd.destroy();
  271. }
  272. }
  273. recoverPlayerBullet(nd) {
  274. let playerBulletPool = this.playerBulletPool;
  275. if (playerBulletPool) {
  276. playerBulletPool.put(nd);
  277. Log.log(LOG_TAG.DEBUG, playerBulletPool);
  278. } else {
  279. nd.destroy();
  280. }
  281. }
  282. //僵尸狂暴状态
  283. zombieViolent() {
  284. // console.log('僵尸狂暴')
  285. let zombieList = this.getZombies();
  286. if (zombieList.length > 0) {
  287. for (let i = 0; i < zombieList.length; ++i) {
  288. if (cc.isValid(zombieList[i]) && zombieList[i].active == true) {
  289. zombieList[i].getComponent(zombieView).zombieViolent();
  290. }
  291. }
  292. }
  293. }
  294. //僵尸重生
  295. reSetZombie(pos) {
  296. gameData.isSetZomBie = true;
  297. this.scheduleOnce(() => {
  298. if (gameData.isSetZomBie == false) {
  299. return;
  300. }
  301. let offsetX = Utils.random(-150, 150);
  302. let offsetY = Utils.random(-150, 150);
  303. let newPos = cc.v3(pos.x + offsetX, pos.y + offsetY);
  304. if (!gameData.isDayTime) {
  305. this.addZombie(npcHelper.getRandomZombieId(), newPos);
  306. }
  307. this.addZombie(npcHelper.getRandomZombieId(), newPos);
  308. }, 10)
  309. }
  310. zombieDie() {
  311. if (!GameEvent.isShowInnerCrazyAward) {
  312. return;
  313. }
  314. if (GameEvent.crazyAwardPopMaxTimes - AccountModel.getInstance().show_crazy_in_level <= 0) {
  315. return;
  316. }
  317. //第一滴血
  318. if (gameData.curKillZomNum == GameEvent.firstCrazyByZombieNum) {
  319. this.isPause = true;
  320. gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS, true)
  321. UIHelp.ShowUI(UICrazyAwardView);
  322. }
  323. if (gameData.curKillZomNum != 0 &&
  324. gameData.curKillZomNum % GameEvent.crazyByZombieNumTimes == 0) {
  325. //濒临状态不弹
  326. if (this.checkVerge()) {
  327. return;
  328. }
  329. gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS, true)
  330. this.isPause = true;
  331. UIHelp.ShowUI(UICrazyAwardView)
  332. }
  333. }
  334. checkVerge() {
  335. let result = 0;
  336. const threshold = 20;
  337. const player = this.roles[0];
  338. for (let node of player) {
  339. if (!cc.isValid(node)) {
  340. continue;
  341. }
  342. result += node.getComponent(PlayerObject).player_prop.cur_hp
  343. if (result >= threshold) {
  344. return false;
  345. }
  346. }
  347. return true;
  348. }
  349. playerMove(state) {
  350. if (state = JOY_STATE.move) {
  351. this.playerIsMove = true
  352. } else {
  353. this.playerIsMove = false;
  354. }
  355. }
  356. /**初始化
  357. * @param camera 摄像机
  358. * @param player_pre
  359. * @param zombie_pre
  360. * @param bullet_pre
  361. * @param isNew 是否新开局
  362. */
  363. init(camera: cc.Node, player_pre: cc.Prefab, zombie_pre: cc.Prefab, bullet_pre: cc.Prefab, isNew = false) {
  364. this.camera = camera;
  365. this.player_pre = player_pre;
  366. this.zombie_pre = zombie_pre;
  367. this.bullet_pre = bullet_pre;
  368. this.zombieBornNodeArr = this.node.$zombieBorn_parent.children;
  369. isNew = this.iniPlayer()
  370. //新开局
  371. if (isNew) {
  372. this.initUIData();
  373. this.tryLoadPermanentHero();
  374. }
  375. this.initZomBie();
  376. this.map_frame = this.map_parent.getChildByName('frame');
  377. gameEventManager.emit(EVENT_TYPE.refresh_mini_map, levelManager.getCurOpenId() - 1, this.map_frame);
  378. gameEventManager.emit(EVENT_TYPE.refresh_mini_map_pos, cc.v2());
  379. this.buffs = [];
  380. for (let i = 1; i <= 9; i++) {
  381. let d = buffDtManager.getDataByID(i);
  382. let b = [d.att, d.hp, d.food];
  383. this.buffs.push(b);
  384. }
  385. }
  386. private initUIData() {
  387. AccountModel.getInstance().first_dec_power = false;
  388. AccountModel.getInstance().first_dec_bullet = false;
  389. }
  390. private tryLoadPermanentHero() {
  391. const ids = AccountModel.singleInstance.fixed_role_ids;
  392. //微信平台加载不出来的临时解决方案
  393. if (!ids.indexOf(HERO_TYPE.wuliuqi)
  394. && AccountModel.singleInstance.sign_times >= 7) {
  395. ids.push(HERO_TYPE.wuliuqi);
  396. }
  397. Log.log(LOG_TAG.DEBUG, "加载英雄:", ids);
  398. if (ids.length != 0) {
  399. ids.forEach((value, index) => {
  400. this.scheduleOnce(() => {
  401. const playerObject = this.addHeroById(value);
  402. //护士写死算了,
  403. if (value == 14) {
  404. const data = itemDtManager.getDataByID(FALLITEM_TYPE.futou);
  405. const gunData = itemDtManager.getDataByID(FALLITEM_TYPE.chongfengqiang);
  406. this.addWeapons(data.id, data.value, data.type == 4, playerObject);
  407. this.addWeapons(gunData.id, gunData.value, data.gunData == 4, playerObject);
  408. }
  409. // 保证有players对象
  410. }, (index + 3) * 0.2);
  411. })
  412. }
  413. }
  414. //初始化僵尸
  415. initZomBie() {
  416. this.createZombie();
  417. this.addZombieCbk();
  418. }
  419. createZombie() {
  420. for (let j = this.zombieBornNodeArr.length - 1; j >= 0; --j) {
  421. let dis = this.getDis(this.camera, this.zombieBornNodeArr[j]);
  422. let limit = (cc.winSize.width / 2) + 150;
  423. if (this.zombieBornNodeArr[j].active == true && dis <= limit) {
  424. let name: string = this.zombieBornNodeArr[j].name;
  425. let bornDt = this.getZombieBornDtByName(name);
  426. let bornNum: number = bornDt.num;
  427. this.schedule(() => {
  428. let randomId = bornDt.type == "s" ? npcHelper.getRandomSeniorZombieId() : npcHelper.getRandomZombieId();
  429. let offsetX = Utils.random(-150, 150);
  430. let offsetY = Utils.random(-150, 150);
  431. let pos = cc.v3(this.zombieBornNodeArr[j].x + offsetX, this.zombieBornNodeArr[j].y + offsetY);
  432. this.addZombie(randomId, pos);
  433. }, 0.04, bornNum - 1)
  434. this.zombieBornNodeArr[j].active = false;
  435. }
  436. }
  437. }
  438. addBoss() {
  439. if (gameData.bAddBoss) {
  440. gameData.bAddBoss = false;
  441. }
  442. let bossId = npcHelper.getRandomBossId();
  443. let cb = () => {
  444. let randomX = Utils.random(150, 200);
  445. let randomY = Utils.random(-10, 80);
  446. let pos = cc.v3(this.roles[0][0].position.x + randomX, this.roles[0][0].position.y + randomY, 0)
  447. this.addZombie(bossId, pos);
  448. }
  449. this.addBossTips(bossId, cb)
  450. }
  451. //添加BOSS警告
  452. addBossTips(bossId, cb) {
  453. let framesName = modelDtlManager.getDataByID(bossId).modelname;
  454. let winSize = cc.winSize;
  455. let node = new cc.Node();
  456. let bossNode = new cc.Node();
  457. node.addComponent(cc.Sprite);
  458. bossNode.addComponent(cc.Sprite)
  459. bundleManager.setBundleFrame('UI', 'main/' + framesName, bossNode);
  460. bundleManager.setBundleFrame('UI', 'main/jinggao', node);
  461. node.parent = cc.director.getScene().getChildByName('Canvas');
  462. bossNode.parent = node
  463. bossNode.x += 150;
  464. node.x = -winSize.width / 2 - 400;
  465. node.y += 80;
  466. cc.tween(node)
  467. .delay(1)
  468. .to(1, { x: 0 }, { easing: 'smooth' })
  469. .delay(2)
  470. .by(1, { x: winSize.width / 2 + 350 }, { easing: 'smooth' })
  471. .call(() => { node.destroy(); cb() })
  472. .start()
  473. }
  474. getBossList() {
  475. let bossList: cc.Node[] = [];
  476. let zombieList = this.getZombies();
  477. for (let nd of zombieList) {
  478. if (npcHelper.isBoss(nd.getComponent(ZomBieObject).zombie_prop.zombie_ID)) {
  479. bossList.push(nd);
  480. }
  481. }
  482. return bossList;
  483. }
  484. private getZombieBornDtByName(name: string): { num: number, type: string } {
  485. if (name.indexOf('s') >= 0) {
  486. let nameArr = name.split('');
  487. return { num: +nameArr[1], type: 's' };
  488. } else {
  489. return { num: +name, type: 'c' };
  490. }
  491. }
  492. //是否已经初始化过玩家
  493. isInitedPlayer() {
  494. if (this.getPlayerArr().length > 0) {
  495. return true;
  496. }
  497. return false;
  498. }
  499. //初始化玩家
  500. iniPlayer() {
  501. if (this._initPlayer) {
  502. return;
  503. }
  504. this._initPlayer = true;
  505. let isNew = false;
  506. let playerDt = AccountModel.getInstance().playerDtList;
  507. // 为零就是初始化
  508. if (playerDt.length == 0) {
  509. isNew = true;
  510. }
  511. let playerNum = playerDt.length > 0 ? playerDt.length : this.playerNum;
  512. gameData.curPeopleNum = playerNum;
  513. EventManager.emit(EVENT_TYPE.UPDATE_PEOPLE_NUM);
  514. var initCameraPos = cc.v2();
  515. let playerBornNode = this.node.$playerBornNode;
  516. if (this.mini_map) { this.mini_map.active = true; }
  517. let idx = 0;
  518. this.schedule(() => {
  519. let nd = this.playerNodePool.size() > 0 ? this.playerNodePool.get() : cc.instantiate(this.player_pre);
  520. let playerViewCom = nd.getChildByName('view').getComponent(PlayerView);
  521. let playerObjCom = nd.getComponent(PlayerObject);
  522. let randomID = null;
  523. if (playerDt.length > 0) {
  524. randomID = playerDt[idx].player_ID;
  525. let offset = 50;
  526. if (playerDt[idx].pos) {
  527. nd.setPosition(playerDt[idx].pos.x, playerDt[idx].pos.y - offset);
  528. } else {
  529. if (playerDt[0].pos) {
  530. nd.setPosition(playerDt[0].pos.x, playerDt[0].pos.y - offset);
  531. } else {
  532. let random = Math.random();
  533. nd.setPosition(playerBornNode.x + random, playerBornNode.y + random);
  534. }
  535. }
  536. } else {
  537. randomID = npcHelper.getRandomComPlayerId();
  538. let offset = cc.v2(Math.random(), Math.random());
  539. nd.setPosition(playerBornNode.x + offset.x, playerBornNode.y + offset.y);
  540. }
  541. if (idx == 0) {
  542. initCameraPos = nd.getPosition();
  543. this.camera.setPosition(initCameraPos);
  544. }
  545. this.addBuffItem(randomID, nd);
  546. this.object_parent.addChild(nd);
  547. playerObjCom.init(randomID, this.roles);
  548. playerViewCom.init(randomID);
  549. if (idx >= (playerNum - 1)) {
  550. this.addPlayerCbk();
  551. this.randomLeader(); //随机一个领导
  552. this.resetIsCover();
  553. this._isInit = true
  554. this.isAddBoos();
  555. return;
  556. }
  557. idx++;
  558. }, 0.02, playerNum - 1);
  559. return isNew;
  560. }
  561. resetIsCover() {
  562. let dt = AccountModel.getInstance().playerDtList;
  563. if (dt.length > 0) {
  564. dt.forEach(element => {
  565. element.isCover = false;
  566. });
  567. }
  568. }
  569. gameOver() {
  570. if (AccountModel.getInstance().curLevel == 1) { zjSdk?.sendEvent('新手引导-失败') };
  571. this.removeNode();
  572. this.setPlayerDiePos();
  573. this.game_over = true;
  574. UITopMenu.getInstance().node.active = false;
  575. gameEventManager.emit(EVENT_TYPE.CANT_JOY, false);
  576. gameData.isSetZomBie = false;
  577. AccountModel.getInstance().curPower = USER.init_powerNum;
  578. AccountModel.getInstance().curBullet = USER.init_bulletNum;
  579. AccountModel.getInstance().curDay = USER.init_day;
  580. AccountModel.getInstance().playerDtList = [];
  581. AccountModel.getInstance().curWelfare = [];
  582. gameData.init();
  583. if (AccountModel.getInstance().reliveNum <= 0) {
  584. zjSdk?.gameEnd(() => {
  585. UIHelp.ShowUI(UIFailView);
  586. })
  587. } else {
  588. UIHelp.ShowUI(UIReliveView);
  589. }
  590. }
  591. /*复活*/
  592. gameRelive(reLiveType) {
  593. if (reLiveType == 'restart') {
  594. zjSdk?.gameEnd(() => {
  595. UIHelp.ShowUI(UIFailView);
  596. })
  597. } else {
  598. Log.log(LOG_TAG.DEBUG, '复活');
  599. AccountModel.getInstance().reliveNum -= 1;
  600. this.relivePlayer();
  601. }
  602. }
  603. //玩家复活
  604. relivePlayer() {
  605. let playerNum = 5;
  606. for (let i = 0; i < playerNum; ++i) {
  607. let randomId = npcHelper.getRandomComPlayerId();
  608. let randomPos = cc.v3(this.playerDiePos.x + Utils.random(-20, 20), this.playerDiePos.y + Utils.random(-20, 20));
  609. this.addPlayer(randomId, randomPos)
  610. }
  611. for (let j = 0; j < this.roles[0].length; ++j) {
  612. let closeWeaponID = 21;
  613. let romoteWeaponID = 39;
  614. let remoteValue = itemDtManager.getDataByID(romoteWeaponID).value;
  615. let closeValue = itemDtManager.getDataByID(closeWeaponID).value;
  616. let playerObj = this.roles[0][j].getComponent(PlayerObject);
  617. this.getWeapon(closeWeaponID, closeValue, playerObj, true);
  618. this.getWeapon(romoteWeaponID, remoteValue, playerObj, false);
  619. }
  620. this.randomLeader();
  621. this.game_over = false;
  622. UITopMenu.getInstance().node.active = true;
  623. gameData.isSetZomBie = true;
  624. gameEventManager.emit(EVENT_TYPE.CANT_JOY, true);
  625. gameData.curBulletNum += 200; //奖励定死
  626. gameData.curPowerNum = 200
  627. let bs = this.roles[0];
  628. let zs = this.getZombies();
  629. for (let i = bs.length - 1; i >= 0; i--) {
  630. for (let j = zs.length - 1; j >= 0; j--) {
  631. let dis = this.getDis(bs[i], zs[j]);
  632. if (dis < 100) {
  633. zs[j].destroy();
  634. }
  635. }
  636. }
  637. }
  638. //记录玩家死亡位置 方便复活用
  639. setPlayerDiePos() {
  640. let playerDt = AccountModel.getInstance().playerDtList;
  641. if (playerDt && playerDt.length > 0) {
  642. for (let i = 0; i < playerDt.length; ++i) {
  643. if (playerDt[i].is_leader) {
  644. this.playerDiePos = playerDt[i].pos;
  645. return;
  646. }
  647. }
  648. }
  649. }
  650. /**切换攻击模式 */
  651. switchAttackMode() {
  652. if (gameData.cur_attack_mode == ATTACK_MODE.close) {
  653. gameData.cur_attack_mode = ATTACK_MODE.remote;
  654. }
  655. else {
  656. gameData.cur_attack_mode = ATTACK_MODE.close;
  657. }
  658. gameEventManager.emit(EVENT_TYPE.attack_mode);
  659. }
  660. /**设置迷你地图 */
  661. setMiniMap(map: cc.Node) {
  662. this.mini_map = map;
  663. }
  664. /**玩家生成回调 */
  665. private addPlayerCbk() {
  666. this.roles[0] = this.getPlayerArr();
  667. }
  668. /**玩家死亡回调 */
  669. private diePlayerCbk() {
  670. this.roles[0] = this.getPlayerArr();
  671. }
  672. /**僵尸生成回调 */
  673. private addZombieCbk() {
  674. this.roles[1] = this.getZombies();
  675. }
  676. /**僵尸死亡回调 */
  677. private dieZombieCbk() {
  678. this.roles[1] = this.getZombies();
  679. }
  680. /**生成玩家
  681. * @param id 视图id
  682. * @param pos 生成位置
  683. */
  684. private addPlayer(id: number, pos: cc.Vec3) {
  685. // cc.log('增加npc', id, pos);
  686. let nd = cc.instantiate(this.player_pre);
  687. let playerViewCom = nd.getChildByName('view').getComponent(PlayerView);
  688. let playerObjCom = nd.getComponent(PlayerObject);
  689. nd.setPosition(pos);
  690. this.addBuffItem(id, nd);
  691. this.object_parent.addChild(nd);
  692. playerObjCom.init(id, this.roles);
  693. playerObjCom.setLeader(false);
  694. playerViewCom.init(id);
  695. gameData.curPeopleNum++;
  696. EventManager.emit(EVENT_TYPE.UPDATE_PEOPLE_NUM);
  697. this.addPlayerCbk();
  698. return playerObjCom;
  699. }
  700. addBuffItem(playerID, parent) {
  701. var path = null;
  702. if (playerID == 13) {
  703. path = 'main/egg';
  704. } else if (playerID == 19) {
  705. path = 'main/cake';
  706. }
  707. if (path) {
  708. let effNode = BenzAssetManager.getInstance().getAsset(AssetsHelper.EFF_obtain);
  709. let Prefb = cc.instantiate(effNode) as unknown as cc.Node;
  710. Prefb.parent = parent;
  711. Prefb.y = 100;
  712. let node = new cc.Node
  713. node.parent = Prefb;
  714. node.addComponent(cc.Sprite);
  715. bundleManager.setBundleFrame('UI', path, node);
  716. }
  717. }
  718. /**生成僵尸
  719. * @param id 视图id
  720. * @param pos 生成位置
  721. */
  722. private addZombie(id: number, pos: cc.Vec3) {
  723. let nd = null;
  724. if (this.zombieNodePool.size() > 0) {
  725. nd = this.zombieNodePool.get();
  726. } else {
  727. nd = cc.instantiate(this.zombie_pre);
  728. }
  729. let comp = nd.getComponent(ZomBieObject);
  730. this.object_parent.addChild(nd);
  731. comp.init(id, pos, this.roles, this.isPause);
  732. this.addZombieCbk();
  733. }
  734. /**随机从解锁人员里选出领队 */
  735. private randomLeader() {
  736. let newArr: cc.Node[] = [];
  737. let playerList = this.getPlayerArr();
  738. if (playerList.length == 0) {
  739. console.log('所有玩家已死亡');
  740. return;
  741. }
  742. for (let i = 0; i < playerList.length; i++) {
  743. if (playerList[i].getComponent(PlayerObject).isDie == false) {
  744. newArr.push(playerList[i]);
  745. }
  746. }
  747. let index = Math.round(Math.random() * newArr.length) - 1;
  748. index = index < 0 ? 0 : index;
  749. for (let i = 0; i < newArr.length; i++) {
  750. let comp = newArr[i].getComponent(PlayerObject);
  751. if (i == index) {
  752. comp.setLeader(true);
  753. }
  754. else {
  755. comp.setLeader(false);
  756. }
  757. }
  758. console.log('随机生成领队');
  759. }
  760. /**检测玩家子弹与僵尸碰撞 */
  761. checkPlayerBulletCrashZombie() {
  762. if (gameData.cur_attack_mode == ATTACK_MODE.close) {
  763. return;
  764. };
  765. let bs = this.getBullets();
  766. let zs = this.getZombies();
  767. for (let i = bs.length - 1; i >= 0; i--) {
  768. for (let j = zs.length - 1; j >= 0; j--) {
  769. let dis = this.getDis(bs[i], zs[j]);
  770. if (dis < 40) {
  771. if (!zs[j] || !cc.isValid(zs[j])) {
  772. break;
  773. }
  774. let bc = bs[i].getComponent(BulletObject);
  775. let zc = zs[j].getComponent(ZomBieObject);
  776. let zv = zc.zombie_view;
  777. zv.hitEff();
  778. let hp = zc.zombie_prop.beHurt(bc.damage);
  779. if (hp == 0) {
  780. //玩家加经验
  781. if (bc.from_p && bc.from_p.isValid) {
  782. let curExp = zc.zombie_prop.exp
  783. bc.from_p.addExp(curExp);
  784. }
  785. var curLevel = AccountModel.getInstance().curLevel;
  786. if (curLevel > 1) {
  787. let x = zs[j].x;
  788. let y = zs[j].y;
  789. let pos = cc.v3(x, y);
  790. gameEventManager.emit(EVENT_TYPE.RESET_ZOMBIE, pos);
  791. }
  792. this.recoverZombie(zs[j]);
  793. zs.splice(j, 1);
  794. gameData.curKillZomNum += 1;
  795. gameEventManager.emit(EVENT_TYPE.zombie_die);
  796. }
  797. this.recoverPlayerBullet(bs[i]);
  798. bs.splice(i, 1);
  799. break;
  800. }
  801. }
  802. }
  803. }
  804. /** 检测玩家跟僵尸子弹的碰撞 */
  805. checkBulletCrashPlayer() {
  806. let bs = this.getZombieBullets();
  807. if (bs.length <= 0) {
  808. return;
  809. }
  810. let wj = this.roles[0];
  811. for (let i = bs.length - 1; i >= 0; i--) {
  812. for (let j = wj.length - 1; j >= 0; j--) {
  813. let dis = this.getBulletToplayerDis(bs[i], wj[j]);
  814. if (dis < 30) {
  815. let bc = bs[i].getComponent(zomBiebullet);
  816. let zc = wj[j].getComponent(PlayerObject);
  817. let zv = zc.player_view;
  818. zv.playBeHurt();
  819. let hp = zc.player_prop.beHurt(bc.damage);
  820. if (hp == 0) {
  821. zc.die();
  822. }
  823. bs[i].destroy();
  824. bs.splice(i, 1);
  825. break;
  826. }
  827. }
  828. }
  829. }
  830. /*获取玩家与僵尸子弹的距离**/
  831. getBulletToplayerDis(zombieBullet: cc.Node, player: cc.Node): number {
  832. let bulletView = zombieBullet.getComponent(zomBiebullet).view;
  833. let wp = bulletView.parent.convertToWorldSpaceAR(cc.v3(bulletView.x, bulletView.y));
  834. let np = zombieBullet.parent.convertToNodeSpaceAR(wp);
  835. return np.sub(player.position).mag();
  836. }
  837. //检测玩家与道具作用
  838. checkPlayerWithProp() {
  839. let len = 100;
  840. let players = this.roles[0];
  841. let props = this.prop_parent.children;
  842. if (!players || !props || players.length <= 0 || props.length <= 0) {
  843. return;
  844. }
  845. for (let i = players.length - 1; i >= 0; i--) {
  846. if (!cc.isValid(players[i])) {
  847. continue;
  848. }
  849. let p_comp = players[i].getComponent(PlayerObject);
  850. for (let j = props.length - 1; j >= 0; j--) {
  851. if (!cc.isValid(props[j])) {
  852. continue
  853. }
  854. let dis = this.getDis(players[i], props[j]);
  855. if (dis < len) {
  856. let comp = props[j].getComponent(UIFallitem);
  857. switch (comp.getType()) {
  858. case 2: {
  859. this.getHeal(comp.getValue(), props[j]);
  860. AudioManager.play('pickup');
  861. break;
  862. }
  863. case 3: {
  864. this.getBullet(comp.getValue(), props[j]);
  865. AudioManager.play('pickup');
  866. break;
  867. }
  868. case 1: {
  869. this.getFood(comp.getValue(), props[j]);
  870. AudioManager.play('pickup');
  871. break;
  872. }
  873. case 4: {
  874. if (p_comp.player_prop.player_ID > 10) {
  875. continue;
  876. }
  877. if (this.getWeapon(comp.getID(), comp.getValue(), p_comp, true, props[j])) {
  878. AudioManager.play('pickup');
  879. }
  880. break;
  881. }
  882. case 5: {
  883. if (p_comp.player_prop.player_ID > 10) {
  884. continue;
  885. }
  886. //获得枪引导
  887. if (AccountModel.getInstance().is_new == 1) {
  888. if (AccountModel.getInstance().curLevel == 1 && gameData.cur_attack_mode == ATTACK_MODE.close) {
  889. gameEventManager.emit(EVENT_TYPE.getGun_guide);
  890. }
  891. }
  892. if (this.getWeapon(comp.getID(), comp.getValue(), p_comp, false, props[j])) {
  893. AudioManager.play('pickup');
  894. }
  895. break;
  896. }
  897. }
  898. }
  899. }
  900. }
  901. }
  902. propAction(props) {
  903. let itemID = props.getComponent(UIFallitem).getID();
  904. let node = new cc.Node;
  905. node.addComponent(cc.Sprite);
  906. node.parent = this.node;
  907. node.setPosition(props.x, props.y);
  908. let ResName = itemDtManager.getItemResName(itemID);
  909. this.propsNodePool.put(props);
  910. bundleManager.setBundleFrame('UI', ResName, node);
  911. cc.tween(node)
  912. .to(0.2, { scale: 2, position: cc.v3(node.x, node.y + 50) })
  913. .to(0.2, { scale: 0 })
  914. .call(() => {
  915. this.propsNodePool.put(node);
  916. this.clearPropPool();
  917. })
  918. .start();
  919. }
  920. /**检测玩解锁npc */
  921. checkPlayerWithNpc() {
  922. let dis = 200;
  923. let ps = this.getPlayerArr();
  924. let ns = this.npc_parent.children;
  925. for (let j = ns.length - 1; j >= 0; j--) {
  926. if (!ps[0]) { return }
  927. if (this.getDis(ps[0], ns[j]) < dis) {
  928. let npc_id = ns[j].getComponent(NpcObject).view_id;
  929. this.addPlayer(npc_id, ns[j].position);
  930. ns[j].destroy();
  931. AudioManager.play('getrole');
  932. }
  933. }
  934. }
  935. addHero() {
  936. let randomID = Utils.random(49, 52);
  937. let players = this.roles[0];
  938. if (players[0]) {
  939. this.addPlayer(randomID, players[0].position);
  940. }
  941. }
  942. addHeroById(roleId: number) {
  943. let players = this.roles[0];
  944. const randomX = Utils.random(-0.5, 0.5);
  945. const randomY = Utils.random(-0.5, 0.5);
  946. if (players[0]) {
  947. const position = players[0].position;
  948. return this.addPlayer(roleId, new cc.Vec3(position.x + randomX, position.y + randomY, 0));
  949. }
  950. }
  951. /**
  952. * 给英雄增加装备
  953. */
  954. addWeapons(id: number, damage: number, is_close: boolean, player?: PlayerObject) {
  955. if (player) {
  956. this.getWeapon(id, damage, player, is_close);
  957. return;
  958. }
  959. let players = this.roles[0];
  960. let props = this.prop_parent.children;
  961. if (!players || !props || players.length <= 0 || props.length <= 0) {
  962. return;
  963. }
  964. // 直到有人拿起武器
  965. for (let i = players.length - 1; i >= 0; i--) {
  966. if (!cc.isValid(players[i])) {
  967. continue;
  968. }
  969. let p_comp = players[i].getComponent(PlayerObject);
  970. const isEquip = this.getWeapon(id, damage, p_comp, is_close);
  971. if (isEquip) {
  972. return;
  973. }
  974. }
  975. }
  976. /**获取僵尸组 */
  977. getZombies(): cc.Node[] {
  978. let zs: cc.Node[] = [];
  979. let ch = this.object_parent.children;
  980. ch.forEach(function (z: cc.Node) {
  981. let comp = z.getComponent(GameObject);
  982. if (comp.type == GAME_OBJECT_TYPE.zombie) {
  983. zs.push(z);
  984. }
  985. });
  986. return zs;
  987. }
  988. /**获取两实体间的距离 */
  989. getDis(obj0: cc.Node, obj1: cc.Node): number {
  990. let dis = obj0.position.sub(obj1.position).mag();
  991. return dis;
  992. }
  993. /**获取食物道具事件回调 */
  994. getFood(food: number, props: cc.Node) {
  995. this.propAction(props);
  996. gameData.curPowerNum += food;
  997. }
  998. /**
  999. * 进入副本 储存玩家数据
  1000. */
  1001. layInPlayerDt() {
  1002. gameData.playerDtList = [];
  1003. this.roles[0].forEach((node) => {
  1004. if (!node || !cc.isValid(node)) {
  1005. return;
  1006. }
  1007. let propCom = node.getComponent(PlayerObject).player_prop;
  1008. let playerDt = propCom.getPlayerDt();
  1009. if (playerDt) {
  1010. gameData.playerDtList.push(propCom.getPlayerDt());
  1011. }
  1012. })
  1013. }
  1014. /**
  1015. *
  1016. * @param name 房间名
  1017. * @param type 房间类型
  1018. */
  1019. goDungeon(name: string, type: number) {
  1020. if (this.mini_map) { this.mini_map.active = false; }
  1021. // this.layInPlayerDt(type);
  1022. dungeonManager.open(name, type);
  1023. levelManager.remove();
  1024. }
  1025. //是否靠近房间
  1026. isCloseDungeon() {
  1027. let playerArr = this.getPlayerArr();
  1028. let dungeonList = this.node.$dungeon_parent.children;
  1029. if (!dungeonList) return;
  1030. let leave = true;
  1031. for (let i = 0; i < dungeonList.length; ++i) {
  1032. for (let j = 0; j < playerArr.length; ++j) {
  1033. if (playerArr[j].group == 'leader') {
  1034. let offset = 100;
  1035. let min = playerArr[j].x >= (dungeonList[i].x - offset);
  1036. let max = playerArr[j].x <= (dungeonList[i].x + offset);
  1037. let minY = dungeonList[i].y + 50 >= playerArr[j].y && playerArr[j].y >= dungeonList[i].y - 50
  1038. if (minY && min && max) {
  1039. this.isGoDungeon = true;
  1040. let name = dungeonList[i].getComponent(dungeonNode).getName();
  1041. let type = dungeonList[i].getComponent(dungeonNode).getType();
  1042. this.goDungeon(name, type);
  1043. this.removeNode();
  1044. }
  1045. }
  1046. }
  1047. }
  1048. return false;
  1049. }
  1050. removeNode() {
  1051. let playerList = this.getPlayerArr();
  1052. let dogList = this.dogNode()
  1053. if (playerList.length <= 0) {
  1054. return;
  1055. }
  1056. for (let i = playerList.length - 1; i >= 0; i--) {
  1057. playerList[i].destroy();
  1058. }
  1059. for (let i = dogList.length - 1; i >= 0; i--) {
  1060. dogList[i].destroy();
  1061. }
  1062. }
  1063. dogNode() {
  1064. let nds: cc.Node[] = [];
  1065. for (let nd of this.object_parent.children) {
  1066. if (nd.name == 'dog_obj') {
  1067. nds.push(nd);
  1068. }
  1069. }
  1070. return nds;
  1071. }
  1072. //是否靠近拯救点
  1073. isCloseWinNode() {
  1074. let playerArr = this.getPlayerArr();
  1075. let dungeonList = this.node.$winNode;
  1076. if (!dungeonList) return;
  1077. for (let j = 0; j < playerArr.length; ++j) {
  1078. let dis = this.getDis(dungeonList, playerArr[j]);
  1079. if (dis > 600) {
  1080. return;
  1081. }
  1082. if (dis < 50 && !this.isWin) {
  1083. this.isWin = true;
  1084. UITopMenu.getInstance().node.active = false;
  1085. gameEventManager.emit(EVENT_TYPE.CANT_JOY, false);
  1086. zjSdk?.gameEnd(() => {
  1087. UIHelp.ShowUI(UIWinView);
  1088. })
  1089. gameData.playerDtList = [];
  1090. playerArr.forEach((node) => {
  1091. let propCom = node.getComponent(PlayerObject).player_prop;
  1092. let playerDt = propCom.getPlayerDt();
  1093. if (playerDt) {
  1094. gameData.playerDtList.push(propCom.getPlayerDt());
  1095. }
  1096. })
  1097. }
  1098. }
  1099. return false;
  1100. }
  1101. /**获取子弹道具事件回调 */
  1102. getBullet(bullet: number, props: cc.Node) {
  1103. this.propAction(props);
  1104. gameData.curBulletNum += bullet;
  1105. this.roles[0].forEach(function (player: cc.Node) {
  1106. if (player && cc.isValid(player)) {
  1107. let isleader = player.getComponent(PlayerObject).player_prop.is_leader;
  1108. if (isleader) {
  1109. let bulletCom = player.getChildByName('addbulletNum');
  1110. bulletCom.active = true;
  1111. bulletCom.getComponent(cc.Label).string = '+' + bullet
  1112. setTimeout(() => {
  1113. bulletCom.active = false;
  1114. }, 500)
  1115. return;
  1116. }
  1117. }
  1118. });
  1119. }
  1120. /**获得药箱事件回调 */
  1121. getHeal(heal: number, props: cc.Node) {
  1122. this.propAction(props);
  1123. let players = this.getPlayerArr();
  1124. players.forEach(function (player: cc.Node) {
  1125. let comp = player.getComponent(PlayerObject);
  1126. comp.player_prop.healHp(heal, player.getChildByName('view'));
  1127. });
  1128. }
  1129. /**
  1130. * 获取武器事件回调
  1131. *
  1132. * props 可能为空
  1133. * */
  1134. getWeapon(id: number, damage: number, player: PlayerObject, is_close: boolean, props?: cc.Node) {
  1135. let del = false;
  1136. let drop: WeaponProp = null;
  1137. if (is_close) {
  1138. if (player.player_prop.close_weapon != null) {
  1139. if (player.player_prop.close_weapon.damage < damage) {
  1140. //更改近战武器
  1141. drop = player.player_prop.close_weapon;
  1142. player.player_prop.setWeapon(id, true);
  1143. props && this.propAction(props);
  1144. del = true;
  1145. }
  1146. } else {
  1147. //更改近战武器
  1148. player.player_prop.setWeapon(id, true);
  1149. props && this.propAction(props);
  1150. del = true;
  1151. }
  1152. } else {
  1153. if (player.player_prop.remote_weapon != null) {
  1154. if (player.player_prop.remote_weapon.damage < damage) {
  1155. //更改远程武器
  1156. drop = player.player_prop.remote_weapon;
  1157. player.player_prop.setWeapon(id, false);
  1158. props && this.propAction(props);
  1159. del = true;
  1160. }
  1161. } else {
  1162. //更改远程武器
  1163. player.player_prop.setWeapon(id, false);
  1164. props && this.propAction(props);
  1165. del = true;
  1166. }
  1167. }
  1168. if (drop != null) {
  1169. //生成对应id武器道具
  1170. let nd = cc.instantiate(this.prop_pre);
  1171. this.prop_parent.addChild(nd);
  1172. nd.setPosition(player.node.position);
  1173. let comp = nd.getComponent(UIFallitem);
  1174. comp.initItem(drop.id);
  1175. }
  1176. return del;
  1177. }
  1178. /**玩家远程步枪手枪子弹攻击回调
  1179. * @param rad 发射角度
  1180. * @param damage 子弹伤害
  1181. * @param player 发射者
  1182. */
  1183. private playerRemoteAttack0(rad: number, damage: number, player: PlayerObject) {
  1184. if (gameData.curBulletNum == 0) {
  1185. return;
  1186. }
  1187. player.player_view.fireEff();
  1188. gameData.curBulletNum--;
  1189. let b = null;
  1190. if (this.playerBulletPool.size() > 0) {
  1191. b = this.playerBulletPool.get();
  1192. } else {
  1193. b = cc.instantiate(this.bullet_pre);
  1194. }
  1195. this.object_parent.addChild(b);
  1196. let com = b.getComponent(BulletObject);
  1197. let pos = playerActionMgr.getGunHeadPos();
  1198. com.init(cc.v3(pos.x, pos.y), rad, damage, player);
  1199. }
  1200. /**玩家远程霰弹枪子弹攻击回调
  1201. * @param rad 发射角度
  1202. * @param damage 子弹伤害
  1203. * @param player 发射者
  1204. */
  1205. private playerRemoteAttack1(rad: number, damage: number, player: PlayerObject) {
  1206. if (gameData.curBulletNum == 0) {
  1207. return;
  1208. }
  1209. gameData.curBulletNum--;
  1210. damage *= 4;//单发合成一发,攻击*4
  1211. let b = cc.instantiate(this.bullet_pre);
  1212. this.object_parent.addChild(b);
  1213. let com = b.getComponent(BulletObject);
  1214. let pos = playerActionMgr.getGunHeadPos();
  1215. com.init(cc.v3(pos.x, pos.y), rad, damage, player);
  1216. }
  1217. /**远程肉弹攻击回调
  1218. * @param start_pos 开始位置
  1219. * @param target_pos 目标位置
  1220. */
  1221. private zombieRemoteBoom(start_pos: cc.Vec3, target_pos: cc.Vec3, addNum: number, damage: number) {
  1222. this.schedule(() => {
  1223. let bullet = BenzAssetManager.getInstance().getAsset(AssetsHelper.PREFAB_ZOMBIE_BULLET);
  1224. let b = cc.instantiate(bullet) as unknown as cc.Node;
  1225. this.object_parent.addChild(b);
  1226. let com = b.getComponent(zomBiebullet);
  1227. com.init(start_pos, target_pos, damage);
  1228. }, 0.3, addNum)
  1229. }
  1230. //获取玩家列表
  1231. getPlayerArr() {
  1232. let nds: cc.Node[] = [];
  1233. for (let nd of this.object_parent.children) {
  1234. if (nd.getComponent(GameObject).type == GAME_OBJECT_TYPE.player) {
  1235. if (nd.getComponent(PlayerObject).player_prop.cur_state != ROLE_STATE.die) {
  1236. nds.push(nd);
  1237. }
  1238. }
  1239. }
  1240. return nds;
  1241. }
  1242. /**获取子弹组 */
  1243. getBullets(): cc.Node[] {
  1244. let bs: cc.Node[] = [];
  1245. let ch = this.object_parent.children;
  1246. ch.forEach(function (b: cc.Node) {
  1247. let comp = b.getComponent(GameObject);
  1248. if (comp.type == GAME_OBJECT_TYPE.bullet) {
  1249. bs.push(b);
  1250. }
  1251. });
  1252. return bs;
  1253. }
  1254. /** 僵尸子弹组 */
  1255. getZombieBullets(): cc.Node[] {
  1256. let bs: cc.Node[] = [];
  1257. let ch = this.object_parent.children;
  1258. ch.forEach(function (b: cc.Node) {
  1259. let comp = b.getComponent(GameObject);
  1260. if (comp.type == GAME_OBJECT_TYPE.zombieBullet) {
  1261. bs.push(b);
  1262. }
  1263. });
  1264. return bs;
  1265. }
  1266. /**摄像机跟随 */
  1267. private cameraTrack(dt: number) {
  1268. // setTimeout(() => {
  1269. let multiple = 1;
  1270. let dis = 500 * multiple;
  1271. let target: cc.Node = null;
  1272. for (let p of this.object_parent.children) {
  1273. let comp = p.getComponent(NodeMove);
  1274. if (comp != null && comp.enabled) {
  1275. target = p;
  1276. break;
  1277. }
  1278. }
  1279. if (target != null) {
  1280. let v_sub = target.position.sub(this.camera.position);
  1281. // if (v_sub.mag() > dis) {
  1282. // v_sub = v_sub.normalize().mul(dis);
  1283. // }
  1284. this.camera.setPosition(this.camera.position.add(v_sub.mul(0.02 * 2.5)));
  1285. }
  1286. // }, 0.1)
  1287. }
  1288. /**运行玩家buff */
  1289. playerRunBuff(dt: number) {
  1290. this.buff_timer += dt;
  1291. if (this.buff_timer > this.buff_dur) {
  1292. this.buff_timer = 0;
  1293. let self = this;
  1294. let players: PlayerObject[] = [];
  1295. this.getPlayerArr().forEach(function (nd: cc.Node) {
  1296. players.push(nd.getComponent(PlayerObject));
  1297. });
  1298. players.forEach(function (player: PlayerObject) {
  1299. let b_id = player.player_prop.buff_id;
  1300. if (b_id == 0) {
  1301. return;
  1302. }
  1303. let buff = self.buffs[b_id - 1];
  1304. players.forEach(function (i_player, idx, players) {
  1305. i_player.player_prop.healHp(buff[1], players[idx].node.getChildByName('view'));
  1306. });
  1307. if (buff[0] > 0) {
  1308. gameData.curBulletNum += buff[0];
  1309. }
  1310. if (buff[2] > 0) {
  1311. gameData.curPowerNum += buff[2];
  1312. }
  1313. });
  1314. }
  1315. }
  1316. /**性能优化 */
  1317. fast() {
  1318. let zs = this.getZombies();
  1319. let mapBg = this.node.$map_parent.$map.$bg.children;
  1320. let mapCar = this.node.$map_parent.$map.$car.children;
  1321. let mapUi = this.node.$map_parent.$map.$ui.children;
  1322. let mapBuild = this.node.$map_parent.$map.$buildding.children;
  1323. this.zombieActive(zs);
  1324. this.nodeIsActive(mapBg);
  1325. this.nodeIsActive(mapCar);
  1326. this.nodeIsActive(mapUi);
  1327. this.nodeIsActive(mapBuild);
  1328. }
  1329. zombieActive(zs) {
  1330. if (!zs) { return };
  1331. let self = this;
  1332. let winSize = cc.winSize.width;
  1333. zs.forEach(function (nd: cc.Node) {
  1334. let dis = self.getDis(nd, self.camera);
  1335. if (dis > winSize) {
  1336. if (nd.active == false) { return }
  1337. nd.active = false;
  1338. }
  1339. else {
  1340. if (nd.active == true) { return }
  1341. nd.active = true;
  1342. }
  1343. })
  1344. }
  1345. nodeIsActive(nodeArr: cc.Node[]) {
  1346. if (!nodeArr) { return };
  1347. let self = this;
  1348. let sw = (cc.winSize.width / 2) + 120;
  1349. nodeArr.forEach(function (nd: cc.Node) {
  1350. let rl = Math.sqrt(Math.pow(nd.width, 2) + Math.pow(nd.height, 2)) / 2;
  1351. if (self.getDis(nd, self.camera) > rl + sw) {
  1352. if (nd.active == true) {
  1353. nd.active = false;
  1354. }
  1355. }
  1356. else {
  1357. if (nd.active == false) {
  1358. nd.active = true;
  1359. }
  1360. }
  1361. });
  1362. }
  1363. }