RoleItem.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. import { CCInteger, Collider2D, Component, Contact2DType, find, instantiate, Label, Node, Prefab, sp, tween, UITransform, v3, view, _decorator } from 'cc';
  2. import { BaseLayer } from '../../common/BaseLayer';
  3. import { cocosUtil } from '../../utils/cocosUtil';
  4. import { constants } from '../data/constants';
  5. import { msgac } from '../data/msgac';
  6. import { audioManager } from '../manager/audioManager';
  7. import { designManager } from '../manager/designManager';
  8. import { eventManager } from '../manager/eventManager';
  9. import { iconManager } from '../manager/iconManager';
  10. import { levelManager } from '../manager/levelManager';
  11. import { resManager } from '../manager/resManager';
  12. import { playerModel } from '../model/playerModel';
  13. const { ccclass, property } = _decorator;
  14. @ccclass('RoleItem')
  15. export class RoleItem extends Component {
  16. @property({
  17. tooltip: "是否是BOSS"
  18. })
  19. isBoss = false;
  20. @property({
  21. type: CCInteger,
  22. tooltip: "打败怪物后掉落的宝箱金币数量或者宝箱金币数量"
  23. })
  24. coin = 0;
  25. @property({
  26. type: CCInteger,
  27. tooltip: "类型 -1:玩家 0:怪物 1:金币宝箱 2:商店 3:弓箭 4:战力倍增 5:装备 6:技能 7:大炮 8:滚石"
  28. })
  29. roleType = 0;
  30. @property({
  31. type: CCInteger,
  32. tooltip: "类型参数1" +
  33. "\n-1|0) 角色id" +
  34. "\n2) 0:装备商店 1:技能商店" +
  35. "\n3) 0:向右射箭 1:向左射箭" +
  36. "\n4) 0:加法 1:乘法" +
  37. "\n5) 1:武器 2:衣服" +
  38. "\n6) 技能id" +
  39. "\n7) 0:向右发射 1:向左发射" +
  40. "\n8) 0:向右滚动 1:向左滚动"
  41. })
  42. roleTypeP1 = 0;
  43. @property({
  44. type: CCInteger,
  45. tooltip: "类型参数2" +
  46. "\n2) 商店id" +
  47. "\n5) 装备等级"
  48. })
  49. roleTypeP2 = 0;
  50. // 关卡主视图组件
  51. levelLayer: BaseLayer;
  52. // 战力相关数值
  53. atk: number = 0;
  54. valLabel: Label;
  55. // 所在层数
  56. floor: number = 0;
  57. // 地板节点
  58. groundNode: Node;
  59. // 战力变化特效支持
  60. atkChangeInfo: any;
  61. atkNode: Node;
  62. atkBgNode: Node;
  63. bodyNode: Node;
  64. bodySkeleton: sp.Skeleton;
  65. // 弓箭节点
  66. arrowsNode: Node;
  67. // 炮弹节点
  68. bulletsNode: Node;
  69. // 机关是否已触发了
  70. hasEffect: boolean = false;
  71. // 怪物是否已阵亡
  72. dead: boolean = false;
  73. // 视频图标
  74. adNode: Node;
  75. start() {
  76. if (this.roleType == constants.roleItemTypes.arrow) {
  77. this.arrowsNode = find("arrows", this.node);
  78. this.arrowsNode.active = false;
  79. let collider = this.arrowsNode.getComponent(Collider2D);
  80. collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
  81. } else if (this.roleType == constants.roleItemTypes.dapao) {
  82. this.bulletsNode = find("bullets", this.node);
  83. this.bulletsNode.active = false;
  84. let collider = this.bulletsNode.getComponent(Collider2D);
  85. collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
  86. } else if (this.roleType == constants.roleItemTypes.gunshi) {
  87. let collider = this.node.getComponent(Collider2D);
  88. collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
  89. let bodyNode = find("body", this.node);
  90. let bodyPos = bodyNode.getPosition();
  91. bodyPos.x = -15;
  92. if (this.roleTypeP1 == constants.arrowTypes.left) {
  93. bodyPos.x = 15;
  94. }
  95. this.bodyNode.position = bodyPos;
  96. }
  97. }
  98. init(groundNode: Node, floor: number, levelLayer: BaseLayer) {
  99. // 地板节点
  100. this.groundNode = groundNode;
  101. // 所在层数
  102. this.floor = floor;
  103. // 关卡主视图组件
  104. this.levelLayer = levelLayer;
  105. let bodyNode = find("body", this.node);
  106. this.bodyNode = bodyNode;
  107. if (bodyNode) {
  108. this.bodySkeleton = bodyNode.getComponent(sp.Skeleton);
  109. }
  110. this.node["isBoss"] = this.isBoss;
  111. this.adNode = find("ad", this.node);
  112. // 战力
  113. let atkNode = find("atk", this.node);
  114. if (atkNode) {
  115. this.valLabel = find("val", atkNode).getComponent(Label);
  116. let val = this.valLabel.string;
  117. if (val) {
  118. // 移除符号
  119. val = val.replace(/-|x|\+|X/g, "");
  120. this.atk = parseInt(val);
  121. }
  122. this.atkNode = atkNode;
  123. this.atkBgNode = find("bg", atkNode);
  124. }
  125. if (this.roleType == constants.roleItemTypes.equip) {
  126. // 装备图标替换
  127. let row = designManager.instance.getRowById(constants.tableName.equip, this.getEquipId());
  128. iconManager.instance.setSprite(bodyNode, row.icon);
  129. let bgNode = find("bg", this.node);
  130. bgNode.setPosition(0, bodyNode.getComponent(UITransform).height * 0.5);
  131. if (this.roleTypeP1 == constants.equipTypes.dress && this.roleTypeP2 == 1) {
  132. bodyNode.setScale(0.8, 0.8);
  133. }
  134. this.atkNode.setPosition(0, bodyNode.getComponent(UITransform).height + 20);
  135. // 上下动画
  136. cocosUtil.tweenUpDown(bodyNode);
  137. cocosUtil.tweenRotate(bgNode, 4);
  138. if (this.adNode && this.adNode.active) {
  139. cocosUtil.tweenUpDown(this.adNode);
  140. }
  141. }
  142. if (this.roleType == constants.roleItemTypes.atk) {
  143. if (this.roleTypeP1 == constants.atkChangeTypes.add) {
  144. this.playAnimation("a", true);
  145. } else if (this.roleTypeP1 == constants.atkChangeTypes.multi) {
  146. this.playAnimation("b", true);
  147. }
  148. } else if (this.roleType == constants.roleItemTypes.coinBox) {
  149. if (this.isBoss) {
  150. this.playAnimation(constants.animations.standBy2, true);
  151. } else {
  152. this.playAnimation(constants.animations.standBy1, true);
  153. }
  154. } else {
  155. this.playAnimation(constants.animations.standBy, true);
  156. }
  157. if (this.roleType == constants.roleItemTypes.skill) {
  158. // 技能图标替换
  159. let row = designManager.instance.getRowById(constants.tableName.skill, this.roleTypeP1);
  160. iconManager.instance.setSprite(bodyNode, row.icon);
  161. }
  162. levelLayer.addButtonListener(this.node);
  163. }
  164. onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D) {
  165. let roleItem = null;
  166. if (selfCollider.node.name == "arrows" && otherCollider.node.name != "arrows") {
  167. roleItem = otherCollider.node.getComponent(RoleItem);
  168. }
  169. if (selfCollider.node.name == "bullets" && otherCollider.node.name != "bullets") {
  170. roleItem = otherCollider.node.getComponent(RoleItem);
  171. }
  172. if (selfCollider.node.name == "role_gunshi" && otherCollider.node.name != "role_gunshi") {
  173. roleItem = otherCollider.node.getComponent(RoleItem);
  174. }
  175. if (!roleItem || roleItem.roleType != constants.roleItemTypes.monster) {
  176. return;
  177. }
  178. roleItem.playAnimation(constants.animations.hit, false, () => {
  179. roleItem.playAnimation(constants.animations.standBy, true);
  180. });
  181. roleItem.addAtk(-this.atk);
  182. }
  183. hasAd() {
  184. if (this.adNode && this.adNode.active) {
  185. return true;
  186. }
  187. return false;
  188. }
  189. hideAd() {
  190. if (this.adNode) {
  191. this.adNode.active = false;
  192. }
  193. }
  194. getEquipId() {
  195. let type = this.roleTypeP1;
  196. let lv = this.roleTypeP2;
  197. let roleId = playerModel.instance.getRoleId();
  198. let id = type * 1000 + roleId * 10 + lv;
  199. return id;
  200. }
  201. setBodyScale(scaleX: number, scaleY: number) {
  202. this.bodyNode.setScale(scaleX, scaleY);
  203. }
  204. playAnimation(name: string, isLoop?: boolean, cbComplete?: Function, cbEnd?: Function) {
  205. if (this.isDead() && name != constants.animations.die) {
  206. // 已阵亡
  207. return;
  208. }
  209. if (!this.bodySkeleton) {
  210. if (cbComplete) {
  211. cbComplete();
  212. }
  213. return;
  214. }
  215. if (!isLoop) {
  216. isLoop = false;
  217. }
  218. if (name == constants.animations.hit) {
  219. let obj: any = {};
  220. obj.pos = cocosUtil.convertToWorldSpace(this.node, true);
  221. eventManager.instance.send(msgac.showHitEffect, obj);
  222. }
  223. this.bodySkeleton.setAnimation(0, name, isLoop);
  224. this.bodySkeleton.setCompleteListener(() => {
  225. // 每次循环播放结束都会回调
  226. if (cbComplete) {
  227. cbComplete();
  228. }
  229. });
  230. this.bodySkeleton.setEndListener(() => {
  231. // 首次播放结束才会回调
  232. if (cbEnd) {
  233. cbEnd();
  234. }
  235. });
  236. audioManager.instance.onRolePlayAnimation(this, this.roleType, this.roleTypeP1, name);
  237. }
  238. // 玩家缩进传送门
  239. startOpenPortal(cb?: Function) {
  240. tween(this.bodyNode).to(0.2, {
  241. scale: v3(0, 0, 1)
  242. }).start();
  243. this.playAnimationPortal("1", cb);
  244. }
  245. // 玩家从传送门中跳出来
  246. playerAppearFromPortal(cb?: Function) {
  247. this.playAnimationPortal("2");
  248. this.playAnimation(constants.animations.chuansong, false, () => {
  249. if (cb) {
  250. cb();
  251. }
  252. });
  253. }
  254. playAnimationPortal(name: string, cb?: Function) {
  255. let portalNode = find("effect/portal", this.node);
  256. if (!portalNode) {
  257. if (cb) {
  258. cb();
  259. }
  260. return;
  261. }
  262. portalNode.active = true;
  263. let height = this.bodyNode.getComponent(UITransform).height;
  264. let s = 0.1;
  265. if (name == "2") {
  266. s = 0.7;
  267. }
  268. let pos = portalNode.getPosition();
  269. pos.y = height * s;
  270. portalNode.setPosition(pos);
  271. let portalSkeleton = portalNode.getComponent(sp.Skeleton);
  272. portalSkeleton.timeScale = 1.8;
  273. portalSkeleton.setAnimation(0, name, false);
  274. portalSkeleton.setCompleteListener(() => {
  275. if (cb) {
  276. cb();
  277. }
  278. });
  279. }
  280. // 金币宝箱掉落
  281. createCoinBox(cb?: Function, obj?: any) {
  282. if (this.coin <= 0) {
  283. if (cb) {
  284. cb();
  285. }
  286. return;
  287. }
  288. let bundle = constants.bundles.common;
  289. resManager.instance.loadAsset(bundle.name, bundle.roleBox, Prefab, () => { }, (err, prefab) => {
  290. let boxNode = instantiate(prefab);
  291. let parentNode = this.node.parent;
  292. let position = this.node.position;
  293. if (obj && obj.parentNode) {
  294. parentNode = obj.parentNode;
  295. }
  296. boxNode.parent = parentNode;
  297. if (obj && obj.worldPos) {
  298. position = cocosUtil.convertToNodeSpace(boxNode, obj.worldPos);
  299. }
  300. boxNode.position = position;
  301. let boxRoleItem = boxNode.getComponent(RoleItem);
  302. boxRoleItem.roleType = constants.roleItemTypes.coinBox;
  303. boxRoleItem.coin = this.coin;
  304. boxRoleItem.isBoss = this.isBoss;
  305. boxRoleItem.init(this.groundNode, this.floor, this.levelLayer);
  306. if (this.isBoss) {
  307. // 加个小手点击引导
  308. cocosUtil.addHand(boxNode, boxNode, (handNode) => {
  309. boxRoleItem["handNode"] = handNode;
  310. // 根据实际情况再进行微调
  311. let pos = handNode.getPosition();
  312. pos.x += 55;
  313. pos.y -= 40;
  314. handNode.setPosition(pos);
  315. if (levelManager.instance.isSkip) {
  316. levelManager.instance.onClickRoleItem(boxRoleItem.node);
  317. }
  318. });
  319. }
  320. if (cb) {
  321. cb();
  322. }
  323. });
  324. }
  325. // 获得玩家应该站立的世界坐标
  326. getPlayerWorldPos(playerNode?: Node) {
  327. let pos = cocosUtil.convertToWorldSpace(this.node);
  328. let transform = this.node.getComponent(UITransform);
  329. // 额外增加的间距
  330. let dx = 0;
  331. // 消除玩家不同角色立绘宽度差异
  332. let dp = 0;
  333. // if (playerNode) {
  334. // let playerTf = playerNode.getComponent(UITransform);
  335. // dp = playerTf.width * (1 - playerTf.anchorX);
  336. // }
  337. if (this.isBodyScalePositive()) {
  338. pos.x -= (transform.width * transform.anchorX + dx + dp);
  339. } else {
  340. pos.x += (transform.width * transform.anchorX + dx + dp);
  341. }
  342. let posY = cocosUtil.convertToWorldSpace(this.groundNode).y;
  343. return v3(pos.x, posY, 0);
  344. }
  345. // 获得立绘是否按照图片正常显示,没有翻转
  346. isBodyScalePositive() {
  347. let scale = this.bodyNode.getScale();
  348. if (scale.x > 0) {
  349. return true;
  350. }
  351. return false;
  352. }
  353. // 增加战力,val如果为负数,就是减少战力
  354. addAtk(val: number) {
  355. if (this.roleType != constants.roleItemTypes.player && this.roleType != constants.roleItemTypes.monster) {
  356. return;
  357. }
  358. if (this.atkChangeInfo) {
  359. // 上次还未完成的,直接刷新,并且丢弃掉,以最新的一个计数为准
  360. this.setAtkVal(this.atk);
  361. if (this.atk <= 0) {
  362. this.showDeadEffect();
  363. return;
  364. }
  365. }
  366. audioManager.instance.playEffect(constants.audioNames.figure);
  367. this.atkChangeInfo = {};
  368. this.atkChangeInfo.atkStart = this.atk;
  369. // 先加上去,避免快速点击计算错误
  370. this.atk += val;
  371. if (this.atk < 0) {
  372. this.atk = 0;
  373. }
  374. this.atkChangeInfo.change = 1;
  375. if (val < 0) {
  376. val = -val;
  377. this.atkChangeInfo.change = -1;
  378. }
  379. let dVal = 1;
  380. let duration = constants.atkChangeTimeGap * val;
  381. while (duration > constants.atkChangeTimeMax) {
  382. dVal++;
  383. duration = constants.atkChangeTimeGap * Math.ceil(val / dVal);
  384. }
  385. this.atkChangeInfo.change *= dVal;
  386. this.atkChangeInfo.val = val;
  387. this.atkChangeInfo.count = 0;
  388. }
  389. closeHand() {
  390. if (this["handNode"]) {
  391. this["handNode"].destroy();
  392. delete this["handNode"];
  393. }
  394. }
  395. openBoxAward(cb: Function) {
  396. if (this.isBoss) {
  397. this.playAnimation(constants.animations.open2, false, cb);
  398. } else {
  399. this.playAnimation(constants.animations.open1, false, cb);
  400. }
  401. }
  402. showDeadEffect(cb?: Function, obj?: any) {
  403. this.dead = true;
  404. this.playAnimation(constants.animations.die, false, () => {
  405. if (cb) {
  406. cb();
  407. }
  408. this.createCoinBox(() => {
  409. if (!this.isPlayer()) {
  410. // 移除敌人
  411. this.node.destroy();
  412. // 判断一下如果敌人全部阵亡,则弹出狩猎成功的界面
  413. eventManager.instance.send(msgac.judgeLevelSuccess);
  414. }
  415. }, obj);
  416. });
  417. }
  418. isPlayer() {
  419. return this.roleType == constants.roleItemTypes.player;
  420. }
  421. setAtkVal(val?: number) {
  422. if (val == undefined) {
  423. val = this.atk;
  424. }
  425. this.valLabel.string = "" + val;
  426. if (this.isPlayer()) {
  427. let tf = this.atkBgNode.getComponent(UITransform);
  428. if (val >= 1000) {
  429. this.atkNode.setPosition(0, 240);
  430. tf.width = 120;
  431. tf.height = 120;
  432. } else if (val >= 100) {
  433. this.atkNode.setPosition(0, 230);
  434. tf.width = 90;
  435. tf.height = 90;
  436. } else {
  437. this.atkNode.setPosition(0, 220);
  438. tf.width = 70;
  439. tf.height = 70;
  440. }
  441. }
  442. }
  443. isDead(): boolean {
  444. if (this.roleType != constants.roleItemTypes.player && this.roleType != constants.roleItemTypes.monster) {
  445. return false;
  446. }
  447. if (this.dead || this.atk <= 0) {
  448. return true;
  449. }
  450. return false;
  451. }
  452. startGunShi(cb?: Function) {
  453. if (this.hasEffect) {
  454. return;
  455. }
  456. this.hasEffect = true;
  457. let posX = this.groundNode.getComponent(UITransform).width * 0.5;
  458. let toPos = this.node.getPosition();
  459. toPos.x = posX;
  460. if (this.roleTypeP1 == constants.arrowTypes.left) {
  461. toPos.x = -posX;
  462. }
  463. tween(this.node).to(1.25, {
  464. position: toPos
  465. }).call(() => {
  466. this.node.destroy();
  467. if (cb) {
  468. cb();
  469. }
  470. }).start();
  471. }
  472. /**
  473. * 发射大炮
  474. */
  475. shootBullets(cb?: Function) {
  476. if (this.hasEffect) {
  477. return;
  478. }
  479. this.hasEffect = true;
  480. this.bulletsNode.active = false;
  481. this.scheduleOnce(() => {
  482. this.bulletsNode.active = true;
  483. let pos = this.bulletsNode.getPosition();
  484. let toPos = v3(view.getVisibleSize().width, 0, 0);
  485. if (this.roleTypeP1 == constants.arrowTypes.left) {
  486. toPos = v3(-view.getVisibleSize().width, 0, 0);
  487. pos.x = -190;
  488. } else {
  489. pos.x = 190;
  490. }
  491. this.bulletsNode.setPosition(pos);
  492. tween(this.bulletsNode).by(0.8, {
  493. position: toPos
  494. }).call(() => {
  495. this.node.destroy();
  496. if (cb) {
  497. cb();
  498. }
  499. }).start();
  500. }, 0.5);
  501. }
  502. /**
  503. * 射出弓箭
  504. */
  505. shootArrows(cb?: Function) {
  506. if (this.hasEffect) {
  507. return;
  508. }
  509. this.hasEffect = true;
  510. let arrowNode = find("arrows", this.node);
  511. arrowNode.active = false;
  512. this.scheduleOnce(() => {
  513. arrowNode.active = true;
  514. let pos = arrowNode.getPosition();
  515. pos.x += 50;
  516. arrowNode.setPosition(pos);
  517. let toPos = v3(view.getVisibleSize().width, 0, 0);
  518. if (this.roleTypeP1 == constants.arrowTypes.left) {
  519. toPos = v3(-view.getVisibleSize().width, 0, 0);
  520. }
  521. tween(this.arrowsNode).by(0.4, {
  522. position: toPos
  523. }).call(() => {
  524. this.node.destroy();
  525. if (cb) {
  526. cb();
  527. }
  528. }).start();
  529. }, 0.5);
  530. }
  531. update(dt: number) {
  532. let info = this.atkChangeInfo;
  533. if (info) {
  534. info.count += dt;
  535. if (info.count >= constants.atkChangeTimeGap) {
  536. info.count -= constants.atkChangeTimeGap;
  537. let changeVal = info.change;
  538. if (changeVal > info.val) {
  539. changeVal = info.val;
  540. }
  541. info.atkStart += changeVal;
  542. this.setAtkVal(info.atkStart);
  543. info.val -= Math.abs(changeVal);
  544. if (info.val <= 0) {
  545. this.atkChangeInfo = null;
  546. this.setAtkVal(this.atk);
  547. }
  548. if (info.atkStart <= 0) {
  549. this.atkChangeInfo = null;
  550. // 阵亡
  551. this.showDeadEffect();
  552. }
  553. }
  554. }
  555. }
  556. }