dungeonLogic.ts 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. import { USER } from "../constant/constant-user";
  2. import AccountModel from "../data/Account/AccountModel";
  3. import buffDtManager from "../dataManager/buffDtManager";
  4. import itemDtManager from "../dataManager/itemDtManager";
  5. import npcHelper from "../dataManager/npcHelper";
  6. import { AssetsHelper } from "../framework/asset/AssetsHelper";
  7. import { BenzAssetManager } from "../framework/asset/BenzAssetManager";
  8. import EventManager from "../framework/event/EventManager";
  9. import { Log, LOG_TAG } from "../framework/log/Log";
  10. import AudioManager from "../framework/music/AudioManager";
  11. import UIBase from "../framework/ui/UIBase";
  12. import UIHelp from "../framework/ui/UIHelp";
  13. import Utils from "../framework/utils/utils";
  14. import bundleManager from "../manager/bundleManager";
  15. import dungeonManager from "../manager/dungeonManager";
  16. import effectManager from "../manager/effectManager";
  17. import levelManager from "../manager/levelManager";
  18. import UIFailView from "../ui/uiview/Interface/UIFailView";
  19. import UITopMenu from "../ui/uiview/Interface/UITopMenu";
  20. import UIFallitem from "../ui/uiview/map/UIFallitem";
  21. import BulletObject from "./gameObject/bullet/bulletObject";
  22. import GameObject from "./gameObject/gameObject";
  23. import NodeMove from "./gameObject/player/nodeMove";
  24. import PlayerObject from "./gameObject/player/playerObject";
  25. import PlayerProp from "./gameObject/player/playerProp";
  26. import PlayerView from "./gameObject/player/playerView";
  27. import NpcObject from "./gameObject/tool/npcObject";
  28. import WeaponProp from "./gameObject/tool/weaponProp";
  29. import ZomBieObject from "./gameObject/zombie/zombieObject";
  30. import zombieView from "./gameObject/zombie/zombieView";
  31. import gameData from "./utrl/gameData";
  32. import { ATTACK_MODE, EVENT_TYPE, GAME_OBJECT_TYPE, ITEM_TYPE, ROLE_STATE, WEAPON_TYPE } from "./utrl/gameEnum";
  33. import gameEventManager from "./utrl/gameEventManager";
  34. const { ccclass, property } = cc._decorator;
  35. /**副本逻辑类 */
  36. @ccclass
  37. export default class dungeonLogic extends cc.Component {
  38. @property(cc.Node)
  39. /**地图父节点 */
  40. map_parent: cc.Node = null;
  41. @property(cc.Node)
  42. /**实体父节点 */
  43. object_parent: cc.Node = null;
  44. /**道具父节点 */
  45. @property(cc.Node)
  46. prop_parent: cc.Node = null;
  47. /**道具预制体 */
  48. @property(cc.Prefab)
  49. prop_pre: cc.Prefab = null;
  50. /**摄像机节点 */
  51. private camera: cc.Node = null;
  52. /**玩家预制体 */
  53. private player_pre: cc.Prefab = null;
  54. /**僵尸预制体 */
  55. private zombie_pre: cc.Prefab = null;
  56. /**子弹预制体 */
  57. private bullet_pre: cc.Prefab = null;
  58. game_over: boolean = false;
  59. isClose: boolean = false;
  60. /**buff间隔时间 */
  61. private buff_dur = 10;
  62. /**buff间隔计时器 */
  63. private buff_timer = 0;
  64. /**buff回复子弹血量体力值组 */
  65. private buffs: number[][] = [];
  66. /**角色组,0玩家组,1僵尸组 */
  67. roles: cc.Node[][] = [null, null];
  68. dtCount: number;
  69. playerDiePos: cc.Vec3 = null;
  70. //对象池
  71. playerBulletPool: cc.NodePool;
  72. zombieNodePool: cc.NodePool;
  73. playerNodePool: cc.NodePool;
  74. propsNodePool: cc.NodePool;
  75. onEnable() {
  76. this.isClose = false;
  77. cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, () => { this.randomLeader() })
  78. gameEventManager.on(EVENT_TYPE.GAME_OVER, this.gameOver, this);
  79. gameEventManager.on(EVENT_TYPE.random_leader, this.randomLeader, this);
  80. gameEventManager.on(EVENT_TYPE.add_zombie, this.addZombie, this);
  81. gameEventManager.on(EVENT_TYPE.bullet0, this.playerRemoteAttack0, this);
  82. gameEventManager.on(EVENT_TYPE.bullet1, this.playerRemoteAttack1, this);
  83. gameEventManager.on(EVENT_TYPE.zombie_boom, this.zombieRemoteBoom, this);
  84. gameEventManager.on(EVENT_TYPE.player_die, this.diePlayerCbk, this);
  85. gameEventManager.on(EVENT_TYPE.zombie_die, this.dieZombieCbk, this);
  86. gameEventManager.on(EVENT_TYPE.GAME_RELIVE, this.gameRelive, this);
  87. gameEventManager.on(EVENT_TYPE.RECOVER_ZOMBIE, this.recoverZombie, this)
  88. gameEventManager.on(EVENT_TYPE.RECOVER_PLAYER, this.recoverPlayer, this)
  89. gameEventManager.on(EVENT_TYPE.RECOVER_PLAYER_BULLET, this.recoverPlayerBullet, this)
  90. this.scheduleOnce(() => {
  91. this.iniPlayer();
  92. }, 0.02)
  93. }
  94. onDisable() {
  95. gameEventManager.off(EVENT_TYPE.GAME_RELIVE, this.gameRelive, this);
  96. gameEventManager.off(EVENT_TYPE.random_leader, this.randomLeader, this);
  97. gameEventManager.off(EVENT_TYPE.GAME_OVER, this.gameOver, this);
  98. gameEventManager.off(EVENT_TYPE.add_zombie, this.addZombie, this);
  99. gameEventManager.off(EVENT_TYPE.bullet0, this.playerRemoteAttack0, this);
  100. gameEventManager.off(EVENT_TYPE.bullet1, this.playerRemoteAttack1, this);
  101. gameEventManager.off(EVENT_TYPE.zombie_boom, this.zombieRemoteBoom, this);
  102. gameEventManager.off(EVENT_TYPE.player_die, this.diePlayerCbk, this);
  103. gameEventManager.off(EVENT_TYPE.zombie_die, this.dieZombieCbk, this);
  104. gameEventManager.off(EVENT_TYPE.RECOVER_ZOMBIE, this.recoverZombie, this)
  105. gameEventManager.off(EVENT_TYPE.RECOVER_PLAYER, this.recoverPlayer, this)
  106. gameEventManager.off(EVENT_TYPE.RECOVER_PLAYER_BULLET, this.recoverPlayerBullet, this)
  107. }
  108. /**初始化
  109. * @param camera 摄像机
  110. * @param 玩家预制体
  111. * @param 僵尸预制体
  112. */
  113. init(camera: cc.Node, player_pre: cc.Prefab, zombie_pre: cc.Prefab, bullet_pre: cc.Prefab) {
  114. this.camera = camera;
  115. this.player_pre = player_pre;
  116. this.zombie_pre = zombie_pre;
  117. this.bullet_pre = bullet_pre;
  118. this.initBulletPool();
  119. this.initZombiePool();
  120. this.initPlayerPool();
  121. this.initPropPool();
  122. this.iniPlayer();
  123. this.initZomBie();
  124. this.buffs = [];
  125. for (let i = 1; i <= 9; i++) {
  126. let d = buffDtManager.getDataByID(i);
  127. let b = [d.att, d.hp, d.food];
  128. this.buffs.push(b);
  129. }
  130. }
  131. initPlayerPool() {
  132. this.playerNodePool = Utils.initNodePool(this.player_pre, 20);
  133. }
  134. initZombiePool() {
  135. this.zombieNodePool = Utils.initNodePool(this.zombie_pre, 15);
  136. }
  137. initBulletPool() {
  138. this.playerBulletPool = Utils.initNodePool(this.bullet_pre, 10);
  139. }
  140. initPropPool() {
  141. this.propsNodePool = new cc.NodePool();
  142. }
  143. //回收僵尸节点
  144. recoverZombie(nd) {
  145. let zombiePool = this.zombieNodePool;
  146. if (zombiePool) {
  147. zombiePool.put(nd);
  148. Log.log(LOG_TAG.DEBUG, zombiePool);
  149. } else {
  150. nd.destroy();
  151. }
  152. }
  153. recoverPlayer(nd) {
  154. let playerPool = this.playerNodePool;
  155. if (playerPool) {
  156. playerPool.put(nd);
  157. Log.log(LOG_TAG.DEBUG, playerPool);
  158. } else {
  159. nd.destroy();
  160. }
  161. }
  162. recoverPlayerBullet(nd) {
  163. let playerBulletPool = this.playerBulletPool;
  164. if (playerBulletPool) {
  165. playerBulletPool.put(nd);
  166. Log.log(LOG_TAG.DEBUG, playerBulletPool);
  167. } else {
  168. nd.destroy();
  169. }
  170. }
  171. clearPropPool() {
  172. let poolLen = this.propsNodePool.size();
  173. if (poolLen > 0 && poolLen > 20) {
  174. this.propsNodePool.clear();
  175. Log.log(LOG_TAG.DEBUG, 'clearpropsNodePool');
  176. }
  177. }
  178. //初始化僵尸
  179. initZomBie() {
  180. let zombieBorn: cc.Node[] = this.node.$zombieBorn_parent.children;
  181. zombieBorn.forEach(node => {
  182. let name: string = node.name;
  183. let bornDt = this.getZombieBornDtByName(name);
  184. let bornNum: number = bornDt.num;
  185. for (let i = 0; i < bornNum; ++i) {
  186. let randomId = bornDt.type == "s" ? npcHelper.getRandomSeniorZombieId() : npcHelper.getRandomZombieId();
  187. let offsetX = Utils.random(-80, 80);
  188. let offsetY = Utils.random(-80, 80);
  189. let pos = cc.v3(node.x + offsetX, node.y + offsetY);
  190. this.addZombie(randomId, pos);
  191. }
  192. });
  193. this.addZombieCbk();
  194. }
  195. private getZombieBornDtByName(name: string): { num: number, type: string } {
  196. if (name.indexOf('s') >= 0) {
  197. let nameArr = name.split('');
  198. return { num: +nameArr[1], type: 's' };
  199. } else {
  200. return { num: +name, type: 'c' };
  201. }
  202. }
  203. //是否已经初始化过玩家
  204. isInitedPlayer() {
  205. if (this.getPlayerArr().length > 0) {
  206. return true;
  207. }
  208. return false;
  209. }
  210. //初始化玩家
  211. iniPlayer() {
  212. if (this.isInitedPlayer()) {
  213. return;
  214. }
  215. var initCamerepos = cc.v2(0, 0);
  216. let playerDt = AccountModel.getInstance().playerDtList;
  217. for (let i = 0; i < playerDt.length; i++) {
  218. let bornNode = this.node.$playerBornNode //cc.v2(Math.random(), Math.random());
  219. let nd = this.playerNodePool.size() > 0 ? this.playerNodePool.get() : cc.instantiate(this.player_pre);
  220. let playerViewCom = nd.getChildByName('view').getComponent(PlayerView);
  221. let playerObjCom = nd.getComponent(PlayerObject);
  222. let randomID = playerDt[i].player_ID;
  223. this.object_parent.addChild(nd);
  224. playerObjCom.init(randomID, this.roles);
  225. playerViewCom.init(randomID);
  226. this.addBuffItem(randomID, nd);
  227. let pos = cc.v2(Math.random(), Math.random());
  228. nd.setPosition(bornNode.x + pos.x, bornNode.y + pos.y);
  229. if (i == 0) {
  230. initCamerepos = nd.getPosition();
  231. }
  232. }
  233. this.camera.setPosition(initCamerepos);
  234. this.randomLeader();
  235. this.addPlayerCbk();
  236. this.resetIsCover();
  237. }
  238. resetIsCover() {
  239. let dt = AccountModel.getInstance().playerDtList;
  240. if (dt.length > 0) {
  241. dt.forEach(element => {
  242. element.isCover = false;
  243. });
  244. }
  245. }
  246. update(dt: number) {
  247. this.cameraTrack(dt);
  248. //this.runBullets(dt);
  249. this.checkPlayerWithProp();
  250. this.checkPlayerBulletCrashZombie();
  251. this.isCloseDungeon();
  252. this.playerRunBuff(dt);
  253. this.checkPlayerWithNpc();
  254. //this.fast();
  255. // this.updateRoleDt();
  256. }
  257. updateRoleDt() {
  258. this.dtCount++;
  259. if (this.dtCount >= 30) {
  260. this.dtCount = 0;
  261. this.layInPlayerDt();
  262. AccountModel.getInstance().playerDtList = gameData.playerDtList;
  263. }
  264. }
  265. propAction(props) {
  266. let itemID = props.getComponent(UIFallitem).getID();
  267. let node = new cc.Node;
  268. node.addComponent(cc.Sprite);
  269. node.parent = this.node;
  270. node.setPosition(props.x, props.y);
  271. let ResName = itemDtManager.getItemResName(itemID);
  272. this.propsNodePool.put(props);
  273. bundleManager.setBundleFrame('UI', ResName, node);
  274. cc.tween(node)
  275. .to(0.2, { scale: 2, position: cc.v3(node.x, node.y + 50) })
  276. .to(0.2, { scale: 0 })
  277. .call(() => {
  278. this.propsNodePool.put(node);
  279. })
  280. .start();
  281. }
  282. gameOver() {
  283. this.removeNode();
  284. this.setPlayerDiePos();
  285. this.game_over = true;
  286. UITopMenu.getInstance().node.active = false;
  287. gameEventManager.emit(EVENT_TYPE.CANT_JOY, false);
  288. AccountModel.getInstance().playerDtList = [];
  289. AccountModel.getInstance().curPower = USER.init_powerNum;
  290. AccountModel.getInstance().curBullet = USER.init_bulletNum;
  291. AccountModel.getInstance().curDay = USER.init_day;
  292. gameData.init();
  293. gameData.isSetZomBie = false;
  294. zjSdk?.gameEnd(() => {
  295. UIHelp.ShowUI(UIFailView);
  296. })
  297. }
  298. /*复活*/
  299. gameRelive() {
  300. }
  301. //记录玩家死亡位置 方便复活用
  302. setPlayerDiePos() {
  303. let playerDt = AccountModel.getInstance().playerDtList;
  304. if (playerDt && playerDt.length > 0) {
  305. for (let i = 0; i < playerDt.length; ++i) {
  306. if (playerDt[i].is_leader) {
  307. this.playerDiePos = playerDt[i].pos;
  308. return;
  309. }
  310. }
  311. }
  312. }
  313. /**切换攻击模式 */
  314. switchAttackMode() {
  315. if (gameData.cur_attack_mode == ATTACK_MODE.close) {
  316. gameData.cur_attack_mode = ATTACK_MODE.remote;
  317. }
  318. else {
  319. gameData.cur_attack_mode = ATTACK_MODE.close;
  320. }
  321. gameEventManager.emit(EVENT_TYPE.attack_mode);
  322. }
  323. /**生成玩家
  324. * @param id 视图id
  325. * @param pos 生成位置
  326. */
  327. private addPlayer(id: number, pos: cc.Vec3) {
  328. // cc.log('增加npc', id, pos);
  329. let nd = cc.instantiate(this.player_pre);
  330. let playerViewCom = nd.getChildByName('view').getComponent(PlayerView);
  331. let playerObjCom = nd.getComponent(PlayerObject);
  332. nd.setPosition(pos);
  333. this.addBuffItem(id, nd);
  334. this.object_parent.addChild(nd);
  335. playerObjCom.init(id, this.roles);
  336. playerObjCom.setLeader(false);
  337. playerViewCom.init(id);
  338. gameData.curPeopleNum++;
  339. EventManager.emit(EVENT_TYPE.UPDATE_PEOPLE_NUM);
  340. this.addPlayerCbk();
  341. }
  342. addBuffItem(playerID, parent) {
  343. var path = null;
  344. if (playerID == 13) {
  345. path = 'main/egg';
  346. } else if (playerID == 19) {
  347. path = 'main/cake';
  348. }
  349. if (path) {
  350. let effNode = BenzAssetManager.getInstance().getAsset(AssetsHelper.EFF_obtain);
  351. let Prefb = cc.instantiate(effNode);
  352. Prefb.parent = parent;
  353. Prefb.y = 100;
  354. let node = new cc.Node
  355. node.parent = Prefb;
  356. node.addComponent(cc.Sprite);
  357. bundleManager.setBundleFrame('UI', path, node);
  358. }
  359. }
  360. /**生成僵尸
  361. * @param id 视图id
  362. * @param pos 生成位置
  363. */
  364. private addZombie(id: number, pos: cc.Vec3) {
  365. let nd = null;
  366. if (this.zombieNodePool.size() > 0) {
  367. nd = this.zombieNodePool.get();
  368. } else {
  369. nd = cc.instantiate(this.zombie_pre);
  370. }
  371. let comp = nd.getComponent(ZomBieObject);
  372. this.object_parent.addChild(nd);
  373. comp.init(id, pos, this.roles);
  374. this.addZombieCbk();
  375. }
  376. /**随机从解锁人员里选出领队 */
  377. private randomLeader() {
  378. let nds: cc.Node[] = this.getPlayerArr();
  379. if (nds.length == 0) {
  380. //console.log('所有玩家已死亡');
  381. return;
  382. }
  383. let index = Math.round(Math.random() * nds.length) - 1;
  384. index = index < 0 ? 0 : index;
  385. for (let i = 0; i < nds.length; i++) {
  386. let comp = nds[i].getComponent(PlayerObject);
  387. if (i == index) {
  388. comp.setLeader(true);
  389. }
  390. else {
  391. comp.setLeader(false);
  392. }
  393. }
  394. }
  395. /**检测玩家子弹与僵尸碰撞 */
  396. checkPlayerBulletCrashZombie() {
  397. let bs = this.getBullets();
  398. let zs = this.getZombies();
  399. for (let i = bs.length - 1; i >= 0; i--) {
  400. for (let j = zs.length - 1; j >= 0; j--) {
  401. let dis = this.getDis(bs[i], zs[j]);
  402. if (dis < 30) {
  403. // console.log('击中僵尸')
  404. let bc = bs[i].getComponent(BulletObject);
  405. let zc = zs[j].getComponent(ZomBieObject);
  406. let zv = zc.zombie_view;
  407. zv.hitEff();
  408. let hp = zc.zombie_prop.beHurt(bc.damage);
  409. if (hp == 0) {
  410. // console.log('僵尸死亡')
  411. effectManager.zombieDieEff(zs[j].parent.parent.$eff_parent, zs[j].getPosition());
  412. zs[j].destroy();
  413. zs.splice(j, 1);
  414. gameEventManager.emit(EVENT_TYPE.zombie_die);
  415. }
  416. bs[i].destroy();
  417. bs.splice(i, 1);
  418. break;
  419. }
  420. }
  421. }
  422. }
  423. /**检测玩解锁npc */
  424. checkPlayerWithNpc() {
  425. if (!this.node.$npc_parent) {
  426. return;
  427. }
  428. let dis = 200;
  429. let ps = this.getPlayerArr();
  430. let ns = this.node.$npc_parent.children;
  431. for (let i = 0; i < ps.length; i++) {
  432. for (let j = ns.length - 1; j >= 0; j--) {
  433. if (this.getDis(ps[i], ns[j]) < dis) {
  434. let npc_id = ns[j].getComponent(NpcObject).view_id;
  435. this.addPlayer(npc_id, ns[j].position);
  436. ns[j].destroy();
  437. }
  438. }
  439. }
  440. }
  441. /**检测玩家 */
  442. checkPlayerWithProp() {//检测玩家与道具作用
  443. let len = 50;
  444. let players = this.roles[0];
  445. let props = this.prop_parent.children;
  446. for (let i = players.length - 1; i >= 0; i--) {
  447. if (!cc.isValid(players[i])) {
  448. continue;
  449. }
  450. for (let j = props.length - 1; j >= 0; j--) {
  451. if (!cc.isValid(props[j])) {
  452. continue;
  453. }
  454. let dis = this.getDis(players[i], props[j]);
  455. if (dis < len) {
  456. let p_comp = players[i].getComponent(PlayerObject);
  457. let comp = props[j].getComponent(UIFallitem);
  458. switch (comp.getType()) {
  459. case 2: {
  460. this.getHeal(comp.getValue(), props[j]);
  461. AudioManager.play('pickup');
  462. break;
  463. }
  464. case 3: {
  465. this.getBullet(comp.getValue(), props[j]);
  466. AudioManager.play('pickup');
  467. break;
  468. }
  469. case 1: {
  470. this.getFood(comp.getValue(), props[j]);
  471. AudioManager.play('pickup');
  472. break;
  473. }
  474. case 4: {
  475. if (p_comp.player_prop.player_ID > 10) {
  476. continue;
  477. }
  478. this.getWeapon(comp.getID(), comp.getValue(), p_comp, true, props[j]);
  479. AudioManager.play('pickup');
  480. break;
  481. }
  482. case 5: {
  483. if (p_comp.player_prop.player_ID > 10) {
  484. continue;
  485. }
  486. this.getWeapon(comp.getID(), comp.getValue(), p_comp, false, props[j]);
  487. AudioManager.play('pickup');
  488. break;
  489. }
  490. }
  491. }
  492. }
  493. }
  494. }
  495. /**获取僵尸组 */
  496. getZombies(): cc.Node[] {
  497. let zs: cc.Node[] = [];
  498. let ch = this.object_parent.children;
  499. ch.forEach(function (z: cc.Node) {
  500. let comp = z.getComponent(GameObject);
  501. if (comp.type == GAME_OBJECT_TYPE.zombie) {
  502. zs.push(z);
  503. }
  504. });
  505. return zs;
  506. }
  507. /**获取两实体间的距离 */
  508. getDis(obj0: cc.Node, obj1: cc.Node): number {
  509. let dis = obj0.position.sub(obj1.position).mag();
  510. return dis;
  511. }
  512. /**
  513. * 出去副本 储存玩家数据
  514. */
  515. layInPlayerDt(cb?) {
  516. let playerList = this.roles[0];
  517. gameData.playerDtList = [];
  518. playerList.forEach((node) => {
  519. let propCom = node.getComponent(PlayerObject).player_prop;
  520. let playerDt = propCom.getPlayerDt();
  521. if (playerDt) {
  522. gameData.playerDtList.push(propCom.getPlayerDt());
  523. }
  524. })
  525. AccountModel.getInstance().playerDtList = gameData.playerDtList;
  526. Log.log(LOG_TAG.DEBUG, AccountModel.getInstance().playerDtList)
  527. this.scheduleOnce(() => {
  528. if (cb) {
  529. cb();
  530. }
  531. }, 0.06)
  532. }
  533. /**获取食物道具事件回调 */
  534. getFood(food: number, props: cc.Node) {
  535. this.propAction(props);
  536. gameData.curPowerNum += food;
  537. }
  538. /**获取子弹道具事件回调 */
  539. getBullet(bullet: number, props: cc.Node) {
  540. this.propAction(props);
  541. gameData.curBulletNum += bullet;
  542. }
  543. /**获得药箱事件回调 */
  544. getHeal(heal: number, props: cc.Node) {
  545. this.propAction(props);
  546. let players = this.getPlayerArr();
  547. players.forEach(function (player: cc.Node) {
  548. let comp = player.getComponent(PlayerObject);
  549. comp.player_prop.healHp(heal, player.getChildByName('view'));
  550. });
  551. }
  552. //靠近出口
  553. isCloseDungeon() {
  554. if (this.isClose) { return };
  555. let playerArr = this.getPlayerArr();
  556. let exportNode = this.node.$export_parent.$exportNode;
  557. if (!exportNode) return;
  558. for (let j = 0; j < playerArr.length; ++j) {
  559. let offset = 150;
  560. let min = playerArr[j].x >= (exportNode.x - offset);
  561. let max = playerArr[j].x <= (exportNode.x + offset);
  562. let minY = playerArr[j].y <= exportNode.y
  563. if (minY && min && max) {
  564. this.isClose = true;
  565. this.layInPlayerDt(() => {
  566. levelManager.open(AccountModel.getInstance().curLevel, () => {
  567. // gameData.curInDungeonType = null;
  568. dungeonManager.remove();
  569. this.removeNode();
  570. })
  571. });
  572. }
  573. }
  574. return false;
  575. }
  576. removeNode() {
  577. let playerList = this.getPlayerArr();
  578. let dogList = this.dogNode()
  579. if (playerList.length <= 0) {
  580. return;
  581. }
  582. for (let i = playerList.length - 1; i >= 0; i--) {
  583. playerList[i].destroy();
  584. }
  585. for (let i = dogList.length - 1; i >= 0; i--) {
  586. dogList[i].destroy();
  587. }
  588. }
  589. dogNode() {
  590. let nds: cc.Node[] = [];
  591. for (let nd of this.object_parent.children) {
  592. if (nd.name == 'dog_obj') {
  593. nds.push(nd);
  594. }
  595. }
  596. return nds;
  597. }
  598. /**获取武器事件回调 */
  599. getWeapon(id: number, damage: number, player: PlayerObject, is_close: boolean, props: cc.Node) {
  600. let drop: WeaponProp = null;
  601. if (is_close) {
  602. if (player.player_prop.close_weapon != null) {
  603. if (player.player_prop.close_weapon.damage < damage) {
  604. //更改近战武器
  605. drop = player.player_prop.close_weapon;
  606. player.player_prop.setWeapon(id, true);
  607. this.propAction(props);
  608. }
  609. }
  610. else {
  611. //更改近战武器
  612. player.player_prop.setWeapon(id, true);
  613. this.propAction(props);
  614. }
  615. }
  616. else {
  617. if (player.player_prop.remote_weapon != null) {
  618. if (player.player_prop.remote_weapon.damage < damage) {
  619. //更改远程武器
  620. drop = player.player_prop.remote_weapon;
  621. player.player_prop.setWeapon(id, false);
  622. this.propAction(props);
  623. }
  624. }
  625. else {
  626. //更改远程武器
  627. player.player_prop.setWeapon(id, false);
  628. this.propAction(props);
  629. }
  630. }
  631. if (drop != null) {
  632. //生成对应id武器道具
  633. let nd = cc.instantiate(this.prop_pre);
  634. this.prop_parent.addChild(nd);
  635. nd.setPosition(player.node.position);
  636. let comp = nd.getComponent(UIFallitem);
  637. comp.initItem(drop.id);
  638. }
  639. }
  640. /**玩家生成回调 */
  641. private addPlayerCbk() {
  642. this.roles[0] = this.getPlayerArr();
  643. }
  644. /**玩家死亡回调 */
  645. private diePlayerCbk() {
  646. this.roles[0] = this.getPlayerArr();
  647. }
  648. /**僵尸生成回调 */
  649. private addZombieCbk() {
  650. this.roles[1] = this.getZombies();
  651. }
  652. /**僵尸死亡回调 */
  653. private dieZombieCbk() {
  654. this.roles[1] = this.getZombies();
  655. }
  656. /**玩家远程步枪手枪子弹攻击回调
  657. * @param rad 发射角度
  658. * @param damage 子弹伤害
  659. * @param player 发射者
  660. */
  661. private playerRemoteAttack0(rad: number, damage: number, player: PlayerObject) {
  662. if (gameData.curBulletNum == 0) {
  663. return;
  664. }
  665. player.player_view.fireEff();
  666. gameData.curBulletNum--;
  667. let b = null;
  668. if (this.playerBulletPool.size() > 0) {
  669. b = this.playerBulletPool.get();
  670. } else {
  671. b = cc.instantiate(this.bullet_pre);
  672. }
  673. this.object_parent.addChild(b);
  674. let com = b.getComponent(BulletObject);
  675. com.init(player.node.position.add(cc.v3(0, 30, 0)), rad, damage, player);
  676. }
  677. /**玩家远程霰弹枪子弹攻击回调
  678. * @param rad 发射角度
  679. * @param damage 子弹伤害
  680. * @param player 发射者
  681. */
  682. private playerRemoteAttack1(rad: number, damage: number, player: PlayerObject) {
  683. if (gameData.curBulletNum == 0) {
  684. return;
  685. }
  686. gameData.curBulletNum--;
  687. damage *= 4;//单发合成一发,攻击*4
  688. let b = cc.instantiate(this.bullet_pre);
  689. this.object_parent.addChild(b);
  690. let com = b.getComponent(BulletObject);
  691. com.init(player.node.position.add(cc.v3(0, 30, 0)), rad, damage, player);
  692. }
  693. /**远程肉弹攻击回调
  694. * @param start_pos 开始位置
  695. * @param target_pos 目标位置
  696. */
  697. private zombieRemoteBoom(start_pos: cc.Vec3, target_pos: cc.Vec3) {
  698. }
  699. //获取玩家列表
  700. getPlayerArr() {
  701. let nds: cc.Node[] = [];
  702. for (let nd of this.object_parent.children) {
  703. if (nd.getComponent(GameObject).type == GAME_OBJECT_TYPE.player) {
  704. if (nd.getComponent(PlayerObject).player_prop.cur_state != ROLE_STATE.die) {
  705. nds.push(nd);
  706. }
  707. }
  708. }
  709. return nds;
  710. }
  711. /**获取子弹组 */
  712. getBullets(): cc.Node[] {
  713. let bs: cc.Node[] = [];
  714. let ch = this.object_parent.children;
  715. ch.forEach(function (b: cc.Node) {
  716. let comp = b.getComponent(GameObject);
  717. if (comp.type == GAME_OBJECT_TYPE.bullet) {
  718. bs.push(b);
  719. }
  720. });
  721. return bs;
  722. }
  723. /**摄像机跟随 */
  724. private cameraTrack(dt: number) {
  725. let dis = 500;
  726. let target: cc.Node = null;
  727. for (let p of this.object_parent.children) {
  728. let comp = p.getComponent(NodeMove);
  729. if (comp != null && comp.enabled) {
  730. target = p;
  731. break;
  732. }
  733. }
  734. if (target != null) {
  735. let v_sub = target.position.sub(this.camera.position);
  736. // if (v_sub.mag() > dis) {
  737. // v_sub = v_sub.normalize().mul(dis);
  738. // }
  739. this.camera.setPosition(this.camera.position.add(v_sub.mul(dt * 2)));
  740. }
  741. }
  742. /**运行玩家buff */
  743. playerRunBuff(dt: number) {
  744. this.buff_timer += dt;
  745. if (this.buff_timer > this.buff_dur) {
  746. this.buff_timer = 0;
  747. let self = this;
  748. let players: PlayerObject[] = [];
  749. this.getPlayerArr().forEach(function (nd: cc.Node) {
  750. players.push(nd.getComponent(PlayerObject));
  751. });
  752. players.forEach(function (player: PlayerObject) {
  753. let b_id = player.player_prop.buff_id;
  754. if (b_id == 0) {
  755. return;
  756. }
  757. let buff = self.buffs[b_id - 1];
  758. players.forEach(function (i_player, idx, players) {
  759. i_player.player_prop.healHp(buff[1], players[idx].node.getChildByName('view'));
  760. });
  761. if (buff[0] > 0) {
  762. gameData.curBulletNum += buff[0];
  763. }
  764. if (buff[2] > 0) {
  765. gameData.curPowerNum += buff[2];
  766. }
  767. });
  768. }
  769. }
  770. /**性能优化 */
  771. fast() {
  772. let mapWall = this.node.$map_parent.$map.$wall.children;
  773. let mapDecoration = this.node.$map_parent.$map.$TheDecoration.children;
  774. this.nodeIsActive(mapWall);
  775. this.nodeIsActive(mapDecoration);
  776. }
  777. nodeIsActive(nodeArr: cc.Node[]) {
  778. if (!nodeArr) { return };
  779. let self = this;
  780. let sw = (cc.winSize.width / 2) + 120;
  781. nodeArr.forEach(function (nd: cc.Node) {
  782. let rl = Math.sqrt(Math.pow(nd.width, 2) + Math.pow(nd.height, 2)) / 2;
  783. if (self.getDis(nd, self.camera) > rl + sw) {
  784. if (nd.active == true) {
  785. nd.active = false;
  786. }
  787. }
  788. else {
  789. if (nd.active == false) {
  790. nd.active = true;
  791. }
  792. }
  793. });
  794. }
  795. }