mb.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  7. import { cocosz } from "../Framework/CocosZ";
  8. import { ZindexLayer } from "../Framework/Constant";
  9. import { gameMgr } from "./gameMgr";
  10. const { ccclass, property } = cc._decorator;
  11. @ccclass
  12. export default class MB extends cc.Component {
  13. @property({ type: cc.AudioClip, tooltip: "音效" })
  14. clip: cc.AudioClip = null;
  15. protected _spAni: sp.Skeleton = null;
  16. onLoad() {
  17. // spine动画
  18. this._spAni = this.node.getChildByName("ani").getComponent(sp.Skeleton);
  19. }
  20. start() {
  21. this.node.zIndex = ZindexLayer.zindex_mb;
  22. this._spAni.setAnimation(0, "daiji", false);
  23. }
  24. time: number = -1;
  25. isAtk: boolean = false;
  26. isHart: boolean = false;
  27. canChangeState: boolean = true;
  28. update(dt) {
  29. if (cocosz.isPause || !gameMgr.isGameStart || gameMgr.isWin || gameMgr.isFail) {
  30. return;
  31. }
  32. if (++this.time % 15 == 0) {
  33. if (gameMgr && gameMgr.playerTs && gameMgr.playerTs.isDeath == false) {
  34. let p1 = this.node.getPosition();
  35. let p2 = gameMgr.playerTs.node.getPosition();
  36. let dis = p1.subSelf(p2).mag();
  37. if (dis < 400) {
  38. this.atkStart();
  39. if (dis < 300) {
  40. this.atkEnemy();
  41. }
  42. } else {
  43. this.atkEnd();
  44. }
  45. } else {
  46. this.atkEnd();
  47. }
  48. }
  49. }
  50. atkEnemy() {
  51. if (this.isHart && gameMgr && gameMgr.playerTs) {
  52. gameMgr.playerTs.hart(1, null);
  53. }
  54. }
  55. atkStart() {
  56. if (this.canChangeState && this.isAtk == false) {
  57. this.canChangeState = false;
  58. this.isAtk = true;
  59. this.node.stopAllActions();
  60. cc.tween(this.node)
  61. .call(() => {
  62. this._spAni.setAnimation(0, "doudong", true);
  63. })
  64. .delay(0.5)
  65. .call(() => {
  66. if (this.clip && this.clip.isValid) gameMgr.playClip(this.clip, this.node);
  67. this._spAni.setAnimation(0, "zheng", false);
  68. this._spAni.addAnimation(0, "zheng2", true);
  69. this.isHart = true;
  70. })
  71. .delay(3)
  72. .call(() => {
  73. this.canChangeState = true;
  74. })
  75. .start();
  76. }
  77. }
  78. atkEnd() {
  79. if (this.canChangeState && this.isAtk == true) {
  80. this.isAtk = false;
  81. this.isHart = false;
  82. this._spAni.setAnimation(0, "daiji", false);
  83. }
  84. }
  85. }