BigMonster.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import {GameEventEnum} from "../enum/GameEventEnum";
  2. import {GameScene} from "./GameScene";
  3. import {BigMonsterEventHandler} from "./BigMonsterEventHandler";
  4. const {ccclass, property} = cc._decorator;
  5. const RHYTHM_INTERVAL = 520
  6. @ccclass
  7. export class BigMonster extends cc.Component {
  8. @property(cc.Animation)
  9. animation: cc.Animation = null;
  10. @property(cc.Node)
  11. body: cc.Node = null;
  12. ready: boolean = false;
  13. private curAnim: string;
  14. private lastAnim: string;
  15. protected onLoad() {
  16. }
  17. start() {
  18. cc.director.on(GameEventEnum.RHYTHM_TICK, this.restartRhythmStand, this)
  19. // cc.director.on(GameEventEnum.MONSTER_READY, this.onReady, this)
  20. // cc.director.on(GameEventEnum.MONSTER_CANCEL, this.onCancel, this)
  21. // cc.director.on(GameEventEnum.STEP_CAN_MISS, this.onMiss, this)
  22. // cc.director.on(GameEventEnum.STEP_CAN_HIT, this.onHit, this)
  23. }
  24. restartRhythmStand() {
  25. if (this.ready) {
  26. return
  27. }
  28. if (!this.curAnim) {
  29. this.rhythmStand(true)
  30. }
  31. }
  32. onReady(complete: Function = null): void {
  33. if (this.ready) {
  34. return
  35. }
  36. this.ready = true
  37. this.stepUp(complete)
  38. }
  39. onCancel(complete: Function = null): void {
  40. if (!this.ready) {
  41. return
  42. }
  43. this.ready = false
  44. this.stepBack(complete)
  45. }
  46. onMiss() {
  47. this.sad()
  48. }
  49. onHit() {
  50. this.happy()
  51. }
  52. pause() {
  53. this.animation.pause(this.curAnim)
  54. this.animation.node.active = false
  55. this.body.active = true
  56. }
  57. resume() {
  58. this.body.active = false
  59. this.animation.node.active = true
  60. this.animation.resume(this.curAnim)
  61. }
  62. sad(speed: number = 1, complete: Function = null): void {
  63. this.playAnim("sad", false, speed, complete)
  64. }
  65. happy(speed: number = 1, complete: Function = null): void {
  66. this.playAnim("happy", false, speed, complete)
  67. }
  68. stepUp(complete: Function = null): void {
  69. this.playAnim("step_up", false, 1, complete)
  70. }
  71. stepBack(complete: Function = null): void {
  72. this.playAnim("step_back", false, 1, complete)
  73. }
  74. rhythmStand(loop: boolean, complete: Function = null): void {
  75. let speed = RHYTHM_INTERVAL / GameScene.instance.levelBeatInterval;
  76. this.playAnim("rhythm_stand", loop, speed, complete)
  77. }
  78. stepOnCan(life: number, nextLife: number, loop: boolean, speed: number = 1, complete: Function = null): void {
  79. if (this.curAnim && (this.curAnim == "step_ready" || this.curAnim == "step_not_ready")) {
  80. return
  81. }
  82. if (!this.ready) {
  83. this.playAnim("step_not_ready", loop, speed, complete)
  84. return
  85. }
  86. this.ready = false
  87. let eventHandler: BigMonsterEventHandler = this.animation.node.getComponent(BigMonsterEventHandler)
  88. eventHandler.targetLife = nextLife
  89. this.playAnim("step_ready", loop, speed, complete)
  90. }
  91. protected onDestroy() {
  92. cc.director.off(GameEventEnum.RHYTHM_TICK, this.restartRhythmStand, this)
  93. cc.director.off(GameEventEnum.MONSTER_READY, this.onReady, this)
  94. cc.director.off(GameEventEnum.MONSTER_CANCEL, this.onCancel, this)
  95. // cc.director.off(GameEventEnum.STEP_CAN_MISS, this.onMiss, this)
  96. // cc.director.off(GameEventEnum.STEP_CAN_HIT, this.onHit, this)
  97. }
  98. private playAnim(name: string, loop: boolean, speed: number = 1, complete: Function) {
  99. let animState: cc.AnimationState = this.animation.getAnimationState(name);
  100. animState.speed = speed
  101. let duration = animState.duration;
  102. this.animation.play(name)
  103. this.curAnim = name
  104. if (!loop) {
  105. this.scheduleOnce(() => {
  106. this.lastAnim = this.curAnim
  107. this.curAnim = null
  108. if (complete) {
  109. complete();
  110. }
  111. }, duration / speed);
  112. }
  113. }
  114. }