import {GameEventEnum} from "../enum/GameEventEnum"; import {GameScene} from "./GameScene"; import {BigMonsterEventHandler} from "./BigMonsterEventHandler"; const {ccclass, property} = cc._decorator; const RHYTHM_INTERVAL = 520 @ccclass export class BigMonster extends cc.Component { @property(cc.Animation) animation: cc.Animation = null; @property(cc.Node) body: cc.Node = null; ready: boolean = false; private curAnim: string; private lastAnim: string; protected onLoad() { } start() { cc.director.on(GameEventEnum.RHYTHM_TICK, this.restartRhythmStand, this) // cc.director.on(GameEventEnum.MONSTER_READY, this.onReady, this) // cc.director.on(GameEventEnum.MONSTER_CANCEL, this.onCancel, this) // cc.director.on(GameEventEnum.STEP_CAN_MISS, this.onMiss, this) // cc.director.on(GameEventEnum.STEP_CAN_HIT, this.onHit, this) } restartRhythmStand() { if (this.ready) { return } if (!this.curAnim) { this.rhythmStand(true) } } onReady(complete: Function = null): void { if (this.ready) { return } this.ready = true this.stepUp(complete) } onCancel(complete: Function = null): void { if (!this.ready) { return } this.ready = false this.stepBack(complete) } onMiss() { this.sad() } onHit() { this.happy() } pause() { this.animation.pause(this.curAnim) this.animation.node.active = false this.body.active = true } resume() { this.body.active = false this.animation.node.active = true this.animation.resume(this.curAnim) } sad(speed: number = 1, complete: Function = null): void { this.playAnim("sad", false, speed, complete) } happy(speed: number = 1, complete: Function = null): void { this.playAnim("happy", false, speed, complete) } stepUp(complete: Function = null): void { this.playAnim("step_up", false, 1, complete) } stepBack(complete: Function = null): void { this.playAnim("step_back", false, 1, complete) } rhythmStand(loop: boolean, complete: Function = null): void { let speed = RHYTHM_INTERVAL / GameScene.instance.levelBeatInterval; this.playAnim("rhythm_stand", loop, speed, complete) } stepOnCan(life: number, nextLife: number, loop: boolean, speed: number = 1, complete: Function = null): void { if (this.curAnim && (this.curAnim == "step_ready" || this.curAnim == "step_not_ready")) { return } if (!this.ready) { this.playAnim("step_not_ready", loop, speed, complete) return } this.ready = false let eventHandler: BigMonsterEventHandler = this.animation.node.getComponent(BigMonsterEventHandler) eventHandler.targetLife = nextLife this.playAnim("step_ready", loop, speed, complete) } protected onDestroy() { cc.director.off(GameEventEnum.RHYTHM_TICK, this.restartRhythmStand, this) cc.director.off(GameEventEnum.MONSTER_READY, this.onReady, this) cc.director.off(GameEventEnum.MONSTER_CANCEL, this.onCancel, this) // cc.director.off(GameEventEnum.STEP_CAN_MISS, this.onMiss, this) // cc.director.off(GameEventEnum.STEP_CAN_HIT, this.onHit, this) } private playAnim(name: string, loop: boolean, speed: number = 1, complete: Function) { let animState: cc.AnimationState = this.animation.getAnimationState(name); animState.speed = speed let duration = animState.duration; this.animation.play(name) this.curAnim = name if (!loop) { this.scheduleOnce(() => { this.lastAnim = this.curAnim this.curAnim = null if (complete) { complete(); } }, duration / speed); } } }