// Learn TypeScript: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html // Learn Attribute: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html import EventName, { PopName } from "../EventName/EventName"; import { GlobalCountDownTime } from "../GameLogic/DataConfig"; import MyComponent from "../Template/MyComponent"; import GameLevel from "./GameLevel"; import GamePause from "./GamePause"; import HonorManger from "./HonorManger"; import PopManger from "./PopManger"; const { ccclass, property } = cc._decorator; @ccclass export default class GameTime extends MyComponent { //人物预制体 @property(cc.Label) countdown: cc.Label = null @property(cc.Sprite) sp_progress: cc.Sprite = null //当前死亡次数 CurrentDieCount = 0 onLoad(): void { super.onLoad(); this.CurrentDieCount = 0 this.regEvent(EventName.InitCountDown, this.InitCountDown, this) this.regEvent(EventName.StartCountDown, this.StartCountDown, this) this.regEvent(EventName.PauseCountDown, this.PauseCountDown, this) this.regEvent(EventName.RestoreCountDown, this.RestoreCountDown, this) this.regEvent(EventName.ADDCountDown, this.ADDCountDown, this) this.regEvent(EventName.RestartCurrentLevel, this.RestartCurrentLevel, this) this.regEvent(EventName.GameOver, this.GameOver, this) this.regEvent(EventName.NextCoustomGame, this.NextCoustomGame, this) this.regEvent(EventName.NextCoustomGameStop, this.NextCoustomGameStop, this) } ADDCountDown() { this.CountDownTime = 20 //恢复暂停 GamePause.GamePause = false } RestartCurrentLevel() { //恢复暂停 GamePause.GamePause = false } NextCoustomGameStop() { this.unschedule(this.updateCountdown); } InitCountDown() { this.CountDownTime = GlobalCountDownTime this.sp_progress.fillRange = -1 * (this.CountDownTime / GlobalCountDownTime) this.setTime() this.begin = true } begin = false StartCountDown() { if (this.begin == true) { this.begin = false /// this.CountDownTime = GlobalCountDownTime this.schedule(this.updateCountdown, 1, cc.macro.REPEAT_FOREVER, 0.01); this.Shake() if (GameLevel.gameLevel > 1) { // // 暂时先关闭 // this.timeShow = 2 } } } PauseCountDown() { this.PauseTime = true this.updateCountdown() } RestoreCountDown() { this.PauseTime = false } NextCoustomGame() { HonorManger.getInstance().Game602() HonorManger.getInstance().Game604() // HonorManger.getInstance().Game605() this.unschedule(this.updateCountdown); //恢复 this.RestoreCountDown() } GameOver() { HonorManger.getInstance().Game603() this.unschedule(this.updateCountdown); //恢复 this.RestoreCountDown() } //展示几秒闯关时间已重置 timeShow = -1; //更新截止时间 CountDownTime: number = 30 //是否暂停 PauseTime: boolean = false setTime() { const minutes = Math.floor(this.CountDownTime / 60); const remainingSeconds = this.CountDownTime % 60; this.countdown.string = `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`; } private updateCountdown() { if (GamePause.GamePause == true) { return } this.setTime() HonorManger.getInstance().remainder = this.CountDownTime HonorManger.getInstance().ElapsedTime = GlobalCountDownTime - this.CountDownTime if (!this.PauseTime) { if (this.CountDownTime > 0) { let Step = 0 this.schedule(() => { Step = Step + 0.2 this.sp_progress.fillRange = -1 * ((this.CountDownTime + 1 - Step) / GlobalCountDownTime) }, 0.2, 4, 0.1)//3 } // this.sp_progress.fillRange = -1 * (this.CountDownTime / GlobalCountDownTime) this.CountDownTime-- } if (this.CountDownTime < 0) { this.countdown.string = "00:00"; //先暂停 GamePause.GamePause = true switch (this.CurrentDieCount++) { case 0: //推送延迟20s PopManger.getInstance().Pop(PopName.TimeOver_1) break; case 1: //再次挑战本关卡 PopManger.getInstance().Pop(PopName.TimeOver_2) break; default: cc.systemEvent.emit(EventName.GameOver) PopManger.getInstance().Pop(PopName.GameOverMid) break; } } if (this.CountDownTime == 20) { this.Shake() } if (this.timeShow >= 0) { this.timeShow-- this.countdown.string = '已重置' } } Shake() { let x = this.node.x; let y = this.node.y; let action = cc.repeatForever( cc.sequence( cc.scaleTo(0.018, 1.05, 1.05), cc.scaleTo(0.018, 1.08, 1.08), cc.scaleTo(0.018, 1.1, 1.1), cc.scaleTo(0.018, 1.08, 1.08), cc.scaleTo(0.018, 1.05, 1.05), cc.scaleTo(0.018, 1, 1), ) ) this.node.runAction(action); this.scheduleOnce(() => { this.node.stopAction(action); this.node.x = x; this.node.y = y; }, 1) } }