FF.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. import FMap from "./map/FMap";
  2. import ViewObject from "../../main/ViewObject";
  3. import FSprite from "./object/FSprite";
  4. import FLiteMap from "./map/FLiteMap";
  5. import CMath from "../../util/CMath";
  6. import FExitBox from "./box/FExitBox";
  7. import FObject, { GroupType } from "./object/FObject";
  8. import AIPet from "./object/AI/AIPet";
  9. import FControl from "./FControl";
  10. import { HttpStateType, ReveData } from "../../util/CHttp";
  11. import FFHeader from "./FFHeader";
  12. import FPanel from "./object/FPanel";
  13. import { RewardData } from "../data/sdata/SManage";
  14. import FObjectFly from "./FObjectFly";
  15. import MSprite from "./object/MSprite";
  16. import PSprite from "./object/PSprite";
  17. import FCountDown from "./FCountDown";
  18. import Joystick from "../../util/Joystick";
  19. import FFRes from "./FFRes";
  20. import FPanelIcon from "./object/FPanelIcon";
  21. import { EquipAttr, PetAttr } from "../data/udata/Player";
  22. import FFCalAttr from "../data/FFCalAttr";
  23. import { AudioMgr, GameViewType } from "../../main/ViewManage";
  24. import FTmpGood from "./evnet/FTmpGood";
  25. import TapSetMusic from "../setting/TapSetMusic";
  26. import RedPoint from "../data/RedPoint";
  27. import Joystick_mag from "../../joystick/Joystick_mag";
  28. /**
  29. * 战斗控制器
  30. */
  31. const { ccclass, property } = cc._decorator;
  32. export const FF_Result_Type = cc.Enum({
  33. none: 0,//还没有结果
  34. fail: 1,//被打败
  35. win: 2,//胜利
  36. });
  37. @ccclass
  38. export default class FF extends ViewObject {
  39. @property(cc.Node)
  40. mBlockInputEvents: cc.Node = null;//顶层遮罩
  41. @property(cc.Node)
  42. mMapLayer: cc.Node = null;//地图层
  43. // @property(Joystick_8d)
  44. // mJoystick: Joystick_8d = null;//操纵杆
  45. @property(Joystick_mag)
  46. mJoystick: Joystick_mag = null;//操纵杆
  47. @property(FLiteMap)
  48. mLietMap: FLiteMap = null;//小地图
  49. @property(cc.Node)
  50. mControl: cc.Node = null;//控制器
  51. @property(cc.Font)
  52. mRed: cc.Font = null;//红色字
  53. @property(cc.Font)
  54. mBjFont: cc.Font = null;//暴击掉血字
  55. @property(cc.Font)
  56. mSkillFont: cc.Font = null;//技能掉血字
  57. @property(cc.Node)
  58. mMu: cc.Node = null;//迷雾
  59. @property(cc.Prefab)
  60. mExitBox: cc.Prefab = null;//退出
  61. @property(cc.Prefab)
  62. mMapDialog: cc.Prefab = null;//地图中对话框
  63. @property(cc.SpriteFrame)
  64. mActiveIcon: cc.SpriteFrame = null;//怪物未激活头顶图片
  65. @property(cc.SpriteFrame)
  66. mDeadIcon: cc.SpriteFrame = null;//角色死亡怪物头顶图片
  67. @property(FFHeader)
  68. mFFheader: FFHeader = null;//表头显示
  69. @property(FPanel)
  70. mSmailHP: FPanel = null;//小怪
  71. @property(FPanel)
  72. mBossHP: FPanel = null;//boss
  73. @property(cc.Node)
  74. mTop: cc.Node = null;//顶部的菜单
  75. // @property(cc.Node)
  76. // mTmpTask: cc.Node = null;//临时任务道具显示
  77. @property(FCountDown)
  78. mCountDown: FCountDown = null;//倒计时
  79. @property(cc.AudioClip)
  80. mWalk: cc.AudioClip = null;//行走音乐
  81. @property(cc.AudioClip)
  82. mRun: cc.AudioClip = null;//跑步音乐
  83. /**
  84. * 控制器
  85. */
  86. public control: FControl = null;
  87. /**
  88. * 资源管理器
  89. */
  90. public fres: FFRes = null;
  91. /**
  92. * 主角
  93. */
  94. public mainSprite: MSprite = null;
  95. mMap: FMap = null;//地图
  96. /**
  97. * 战斗结果
  98. */
  99. public ffResultType = 0;
  100. /**
  101. * 顶层遮罩点击消息
  102. */
  103. public blockInputCallback: () => void;
  104. /**
  105. * 监听精灵移除事件,全局监听
  106. */
  107. public clearCallback: (result: FObject) => void;
  108. /**
  109. * 监听精灵移除事件,全局监听
  110. */
  111. public removeCallback: (result: FObject) => void;
  112. /**
  113. * 走到退出点回调
  114. */
  115. public exitCallback: () => void;
  116. /**
  117. * 是否不锁摄像机
  118. */
  119. public lockCamera = true;
  120. /**
  121. * 是否复活过
  122. */
  123. public isBack = false;
  124. onLoad() {
  125. cc.director.getPhysicsManager().enabled = true;
  126. this.control = this.mControl.getComponent(FControl);
  127. this.fres = this.node.getComponent(FFRes);
  128. this.mCountDown.setCallback(() => {
  129. this.clearCallback(this.mainSprite);
  130. });
  131. }
  132. onDestroy() {
  133. // this.main.stopAll();
  134. }
  135. public stopRuning() {
  136. this.mJoystick.stop();
  137. }
  138. public startRuning() {
  139. }
  140. /**
  141. * 监听精灵移除事件
  142. * @param resultCallback
  143. */
  144. public regClearCallback(clearCallback: (result: FObject) => void) {
  145. this.clearCallback = clearCallback;
  146. }
  147. /**
  148. * 监听事件中对象移除
  149. * @param resultCallback
  150. */
  151. public regRemoveCallback(clearCallback: (result: FObject) => void) {
  152. this.removeCallback = clearCallback;
  153. }
  154. /**
  155. * 退出副本监听
  156. * @param exitCallback
  157. */
  158. public regExitCallback(exitCallback: () => void) {
  159. this.exitCallback = exitCallback;
  160. }
  161. public loadMap(map: FMap) {
  162. this.mMap = map;
  163. this.mMap.mCamera = this.main.mMapCamera;
  164. this.mMap.mCamera.zoomRatio = 1;
  165. this.mMap.setFF(this);
  166. this.mMap.node.parent = this.mMapLayer;
  167. map.node.zIndex = 0;
  168. this.mMapLayer.x = -cc.winSize.width / 2
  169. this.mMapLayer.y = -cc.winSize.height / 2;
  170. this.mMu.zIndex = 9999;
  171. this.mMu.width = cc.winSize.width * 2;
  172. this.mMu.height = cc.winSize.height * 2;
  173. this.mMu.active = true;
  174. this.mLietMap.initMap(this);
  175. let nodes = this.mMap.getSprites();
  176. nodes.forEach(node => {
  177. let fobj = node.getComponent(FObject);
  178. if (fobj) {
  179. fobj.ff = this;
  180. fobj.map = this.mMap;
  181. }
  182. });
  183. }
  184. public update(dt) {
  185. //更新摄像机位置
  186. if (this.lockCamera && this.mainSprite) {
  187. let node = this.mainSprite.node;
  188. this.mMap.updateShoot(node.x, node.y);
  189. this.mLietMap.clearMask(node.getPosition());
  190. }
  191. }
  192. /**
  193. * 载入角色
  194. */
  195. public loadRole(sprites: Array<FSprite>) {
  196. for (let i = 0; i < sprites.length; i++) {
  197. const element = sprites[i];
  198. if (element instanceof MSprite) {
  199. this.mainSprite = element;
  200. element.setPosition(this.mMap.mInit.getPosition());
  201. }
  202. }
  203. this.mainSprite.setJoystick(this.mJoystick);
  204. let leader = this.mainSprite;
  205. for (let i = 0; i < sprites.length; i++) {
  206. const element = sprites[i];
  207. if (element instanceof PSprite) {
  208. (element as PSprite).setLeader(leader);
  209. leader.setNext(element);
  210. leader = element as any;
  211. element.addComponent(AIPet);
  212. }
  213. element.node.group = GroupType.A;
  214. this.mMap.addSprite(element);
  215. element.addPanelHUD();
  216. }
  217. let node = this.mainSprite.node;
  218. this.mMap.updateShoot(node.x, node.y);
  219. this.mLietMap.clearMask(node.getPosition());
  220. this.mMap.initCamera();
  221. this.control.updateSkill()
  222. }
  223. /**
  224. * 添加角色
  225. * @param sprite
  226. */
  227. public addRole(sprite: PSprite) {
  228. let sp = this.mainSprite as any;
  229. while (sp.nextSprite) {
  230. sp = sp.nextSprite;
  231. }
  232. sprite.setLeader(sp);
  233. sp.setNext(sprite);
  234. sprite.node.group = GroupType.A;
  235. this.mMap.addSprite(sprite);
  236. sprite.addPanelHUD();
  237. }
  238. /**
  239. * 更新面板中的角色
  240. */
  241. public updateRole() {
  242. let res = this.fres;
  243. let nodes = res.mHudNode.children
  244. for (let i = 0; i < nodes.length; i++) {
  245. const element = nodes[i];
  246. let panelIcon = element.getComponent(FPanelIcon)
  247. let sprite = panelIcon.sprite
  248. let ffAttr = sprite.attrData
  249. let petAttr: PetAttr = this.main.player.pet[ffAttr.id]
  250. let newAttr = FFCalAttr.getAttr(this.main, petAttr)
  251. sprite.flushAttrData(newAttr)
  252. panelIcon.setIcon()
  253. }
  254. if (this.mainSprite && this.mainSprite.isValid) {
  255. this.mainSprite.setAttrData(this.mainSprite.attrData)
  256. this.control.updateSkill()
  257. }
  258. }
  259. public getRandInit(): cc.Vec2 {
  260. let pos = cc.v2();
  261. pos.x = CMath.getRandom(this.mMap.mInit.x - 100, this.mMap.mInit.x + 100);
  262. pos.y = CMath.getRandom(this.mMap.mInit.y - 100, this.mMap.mInit.y + 100);
  263. return pos;
  264. }
  265. /**
  266. * 当前存活的数量
  267. */
  268. public activeCount(): number {
  269. let sprites = this.mMap.getSprites();
  270. let count = 0;
  271. for (let i = 0; i < sprites.length; i++) {
  272. const node = sprites[i];
  273. if (node.group == GroupType.A) {
  274. count++;
  275. }
  276. }
  277. return count;
  278. }
  279. public getGroupBy(group: string): Array<FSprite> {
  280. let ffs = [];
  281. let sprites = this.mMap.getSprites();
  282. for (let i = 0; i < sprites.length; i++) {
  283. const node = sprites[i];
  284. if (node.group == group) {
  285. ffs.push(node.getComponent(FSprite));
  286. }
  287. }
  288. return ffs;
  289. }
  290. /**
  291. * 点击退出按钮
  292. */
  293. public onclickExit() {
  294. let node = cc.instantiate(this.mExitBox);
  295. let exitBox = node.getComponent(FExitBox);
  296. exitBox.main = this.main;
  297. exitBox.endType = 1
  298. exitBox.init(this.mFFheader.stageData);
  299. exitBox.show();
  300. exitBox.setOKCallback(() => {
  301. this.exitDistroy();
  302. this.main.topNode.active = true;
  303. this.main.playMusicByPath(AudioMgr.homeMusic);
  304. });
  305. this.pauseSprite(true)
  306. exitBox.setCloseCallback(() => {
  307. this.pauseSprite(false)
  308. })
  309. }
  310. /**
  311. * 捡起地图上的物品
  312. * @param objectId
  313. */
  314. public getMapObject(objectId: string) {
  315. let msg = {
  316. objectId: objectId
  317. }
  318. this.main.gameHttp.sendJson('stage/v1/stageObject', msg, (state, reve: ReveData) => {
  319. this.main.stopLoad();
  320. if (state == HttpStateType.SUCCESS) {
  321. if (reve.retCode == 0) {
  322. //"_stage":{"good":{"1001":1}}
  323. let player = this.main.player;
  324. let stage = player.stage;
  325. stage.element.push(objectId);
  326. stage.data[this.mFFheader.stageData.id] = reve.data._stage;
  327. this.mFFheader.flush();
  328. this.main.showTips1(reve)
  329. } else {
  330. this.main.showTips(reve.message);
  331. }
  332. } else {
  333. this.main.showTips('网络异常');
  334. }
  335. });
  336. }
  337. /**
  338. * 暂停所有精灵
  339. */
  340. public pauseSprite(pause: boolean) {
  341. this.lockCamera = !pause;
  342. let nodes = this.mMap.getSprites();
  343. nodes.forEach(node => {
  344. let sprite = node.getComponent(FSprite);
  345. if (sprite) {
  346. sprite.setPause(pause)
  347. }
  348. });
  349. if (pause) {
  350. this.mJoystick.stop();
  351. if (this.mainSprite) {
  352. this.mainSprite.setShooting(false);
  353. }
  354. this.uiOutAction();
  355. cc.director.getPhysicsManager().enabled = false
  356. } else {
  357. this.uiInAction();
  358. cc.director.getPhysicsManager().enabled = true;
  359. }
  360. }
  361. /**
  362. * ui离场
  363. */
  364. private uiOutAction() {
  365. this.mJoystick.node.active = false;
  366. // this.mLietMap.node.active = false;
  367. this.mControl.active = false;
  368. this.mTop.active = false;
  369. // this.mTmpTask.active = false;
  370. }
  371. /**
  372. * ui显示
  373. */
  374. private uiInAction() {
  375. this.mJoystick.node.active = true;
  376. // this.mLietMap.node.active = true;
  377. this.mControl.active = true;
  378. this.mTop.active = true;
  379. // this.mTmpTask.active = true;
  380. }
  381. /**
  382. * 刷新当前攻击的对象
  383. * @param sprite
  384. */
  385. public flushHP(sprite: FSprite) {
  386. this.mBossHP.node.active = false;
  387. this.mSmailHP.node.active = false;
  388. if (sprite.hp <= 0) {
  389. return;
  390. }
  391. let panelHP: FPanel = null;
  392. if (sprite.attrData.type == 2) {//boss
  393. panelHP = this.mBossHP;
  394. } else {
  395. panelHP = this.mSmailHP;
  396. }
  397. panelHP.node.active = true;
  398. panelHP.sprite = sprite;
  399. panelHP.updatePanel();
  400. panelHP.setCancel();
  401. }
  402. /**
  403. * 更新气血
  404. * @param sprite
  405. */
  406. public updateHP(sprite: FSprite) {
  407. let panelHP: FPanel = null;
  408. if (this.mBossHP.node.active) {
  409. panelHP = this.mBossHP;
  410. } else if (this.mSmailHP.node.active) {
  411. panelHP = this.mSmailHP;
  412. }
  413. if (panelHP && sprite == panelHP.sprite) {
  414. panelHP.updatePanel();
  415. }
  416. }
  417. /**
  418. * 添加到地图上,飞到背包
  419. * @param icons
  420. */
  421. public addGoods(list: Array<RewardData>, pos: cc.Vec2) {
  422. list.forEach(element => {
  423. cc.resources.load(element.icon, cc.SpriteFrame, (err, spriteFrame: cc.SpriteFrame) => {
  424. if (err) {
  425. cc.error(err);
  426. } else {
  427. let node = new cc.Node();
  428. node.group = 'map';
  429. let sprite = node.addComponent(cc.Sprite);
  430. sprite.spriteFrame = spriteFrame;
  431. node.x = pos.x;
  432. node.y = pos.y;
  433. node.parent = this.mMap.mSprites;
  434. let fly = node.addComponent(FObjectFly);
  435. fly.ff = this;
  436. node.setScale(1.5);
  437. let jumpDis = CMath.getRandom(-100, 100);
  438. let jumpBy = cc.jumpBy(0.5, cc.v2(jumpDis, 0), 100, 1);
  439. cc.tween(node).sequence(
  440. jumpBy,
  441. cc.delayTime(0.5),
  442. cc.callFunc(() => {
  443. this.main.playerEffectByPath(AudioMgr.getCoin);
  444. fly.move = true;
  445. cc.tween(node).repeatForever(
  446. cc.rotateBy(1, 360)
  447. ).start()
  448. })
  449. ).start()
  450. }
  451. });
  452. });
  453. }
  454. /**
  455. * 道具掉地上
  456. * @param goodId
  457. * @param goodCount
  458. * @param pos
  459. */
  460. public addGoodToMap(goodId, pos: cc.Vec2) {
  461. let manage = this.main.sManage
  462. let _good = manage.getGoodById1(goodId)
  463. cc.resources.load('icon/good/' + _good.icon, cc.SpriteFrame, (err, spriteFrame: cc.SpriteFrame) => {
  464. if (err) {
  465. cc.error(err);
  466. } else {
  467. let node = new cc.Node();
  468. node.group = 'map';
  469. let sprite = node.addComponent(cc.Sprite);
  470. sprite.spriteFrame = spriteFrame;
  471. let tmpGood = node.addComponent(FTmpGood)
  472. tmpGood.goodId = goodId
  473. let rigidBody = node.addComponent(cc.RigidBody)
  474. rigidBody.enabledContactListener = true
  475. rigidBody.type = cc.RigidBodyType.Static
  476. let physicsBoxCollider = node.addComponent(cc.PhysicsBoxCollider)
  477. physicsBoxCollider.sensor = true
  478. physicsBoxCollider.size.width = spriteFrame.getRect().width
  479. physicsBoxCollider.size.height = spriteFrame.getRect().height
  480. node.x = pos.x;
  481. node.y = pos.y;
  482. node.parent = this.mMap.mSprites;
  483. node.setScale(1.5);
  484. let jumpDis = CMath.getRandom(-100, 100);
  485. let jumpBy = cc.jumpBy(0.5, cc.v2(jumpDis, 0), 100, 1);
  486. cc.tween(node).sequence(
  487. jumpBy,
  488. cc.delayTime(0.5),
  489. cc.callFunc(() => {
  490. cc.tween(node).repeatForever(
  491. cc.rotateBy(1, 360)
  492. ).start()
  493. })
  494. ).start()
  495. }
  496. });
  497. }
  498. public setBlockInputCallback(callback: () => void) {
  499. this.blockInputCallback = callback;
  500. }
  501. public blockInputEvents() {
  502. if (this.blockInputCallback) {
  503. this.blockInputCallback();
  504. }
  505. }
  506. /**
  507. * 地图震动
  508. */
  509. public shockMap() {
  510. // cc.tween(this.mMapLayer).sequence(
  511. // cc.moveBy(0.05, 0, 15).easing(cc.easeBackOut()),
  512. // cc.moveBy(0.05, 0, -15).easing(cc.easeBackOut())
  513. // ).start();
  514. }
  515. //移除伙伴
  516. public removePet(id) {
  517. let sprite = this.mainSprite.nextSprite as PSprite
  518. while (true) {
  519. if (!sprite) {
  520. return
  521. }
  522. if (sprite.attrData.id == id) {
  523. if (sprite.nextSprite) {
  524. let next = sprite.nextSprite as PSprite
  525. next.setLeader(sprite.inFront);
  526. }
  527. sprite.node.destroy();
  528. return
  529. } else {
  530. sprite = sprite.nextSprite as PSprite
  531. }
  532. }
  533. }
  534. public openSet() {
  535. this.pauseSprite(true)
  536. this.main.viewManage.loadFunc(GameViewType.tap_set, (viewObject: ViewObject) => {
  537. let setView = viewObject as TapSetMusic
  538. setView.setCloseCallback(() => {
  539. this.pauseSprite(false)
  540. })
  541. viewObject.show();
  542. });
  543. }
  544. /**
  545. * 复活所有角色
  546. */
  547. public fuhuo() {
  548. this.ffResultType = FF_Result_Type.none;
  549. let sprite: Array<FSprite> = this.getGroupBy('A')
  550. for (let i = 0; i < sprite.length; i++) {
  551. const element = sprite[i];
  552. if (element.hp <= 0) {//已经死亡的,进行复活
  553. element.isActive = true
  554. element.wudi(2)
  555. element.hp = element.attrData.hp
  556. let spriteNode = element.node.getChildByName('juese01');
  557. let spineNode0 = spriteNode.getChildByName('spineRight');
  558. let spine = spineNode0.getComponent(sp.Skeleton);
  559. if (!spine.node.active) {
  560. element.spine.destroy()
  561. element.spine = spine;
  562. spine.node.active = true
  563. }
  564. } else {
  565. element.hp = element.attrData.hp
  566. }
  567. element.updatePanel()
  568. }
  569. }
  570. /**
  571. * 第一次获得技能提示
  572. */
  573. firstGetSkillTips() {
  574. let tips = this.mControl.getChildByName("1");
  575. tips.active = true;
  576. this.mBlockInputEvents.active = true;
  577. this.setBlockInputCallback(() => {
  578. tips.active = false;
  579. this.mBlockInputEvents.active = false;
  580. })
  581. }
  582. /**
  583. * 第一次拾取物品提示
  584. */
  585. firstGetGoodsTips() {
  586. let tips = this.mControl.parent.getChildByName("goodAbout");
  587. console.log("======", tips)
  588. tips.active = true;
  589. this.mBlockInputEvents.active = true;
  590. this.setBlockInputCallback(() => {
  591. tips.active = false;
  592. this.mBlockInputEvents.active = false;
  593. })
  594. }
  595. /**
  596. * 监听网络
  597. * @param reveData
  598. */
  599. public httpEvent(reveData: ReveData) {
  600. // cc.log('ff reve : ',this);
  601. let data = reveData.data;
  602. //获得了新的装备
  603. if (data._equip != undefined) {
  604. if (data._equip.length > 0) {
  605. //刷新红点
  606. let res = this.fres;
  607. let nodes = res.mHudNode.children;
  608. for (let i = 0; i < nodes.length; i++) {
  609. const node = nodes[i];
  610. let fpi: FPanelIcon = node.getComponent(FPanelIcon)
  611. let petId = fpi.sprite.attrData.id
  612. let petAttr: PetAttr = this.main.player.getPet(petId)
  613. if (RedPoint.petRedPoint(this.main, petAttr)) {
  614. RedPoint.addRed(this.main, node, true)
  615. }
  616. }
  617. }
  618. }
  619. }
  620. }