| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 | import { _decorator, Component, Label, Node, EventTarget } from 'cc';const { ccclass, property } = _decorator;import LevelData from '../Data/LevelData';import LevelManager from './LevelManager';import { Global } from '../Common/Global';import { AudioManage } from './AudioManage';import { GamePanel } from '../UiPanel/GamePanel';@ccclass('GameManager')export class GameManager extends Component {    private static _instance: GameManager = null;    static getInstance(): GameManager {        return GameManager._instance;    }    @property(GamePanel)    uiManager: GamePanel = null;    @property({ displayName: "是否为开发版本" })    public IsDevelop = false;    curLevel: number = 1;         //当前关卡    curLevelScore: number = 0;    //当前关卡分数    curLevelTime: number = 0;     //当前关卡所玩时间    curLevelData: LevelData = null;    curLevelTotalScore: number = 0;    gameTotalTime: number = 0;    //游戏总的时间    gameTotalScore: number = 0;   //游戏总分数    playClass: string;             //关卡分类 0代表可以继续下一关1代表只有1关    timer: number = 120;        // 2分钟,单位为秒    //游戏状态    isPaused: boolean = false;    isStartGame: boolean = false;    //消息收发     eventTarget = new EventTarget();    //gameDuration:number =undefined;    //  isSound:any =false;  //是否开启调用安卓的语音    // totalNum:number =0   //游戏总关卡    //怪兽的移动速度    speed: number = 300;    //怪兽刷新的频率(区间,最小刷新时间,最大刷新时间)毫秒    intervalTime = [1000, 3000]    //打一个怪兽增加的分数    addscoreNum = 5;    //放过一个怪兽减少的分数    minusscoreNum = 0;    onLoad() {        if (GameManager._instance) {            this.node.destroy();            return;        }        Global.IsDevelop = this.IsDevelop;        const isDevelopStr = this.getParameterByName('isDevelop') || true;        if (isDevelopStr === "true") {            Global.IsDevelop = true;        } else if (isDevelopStr === "false") {            Global.IsDevelop = false;        }        GameManager._instance = this;        if (!Global.IsDevelop) {            this.playClass = this.getParameterByName('playClass');            Global.StartLv = parseInt(this.getParameterByName('startLv')) || 1;            Global.MaxLv = parseInt(this.getParameterByName('maxLv')) || 10;            Global.GameSetTime = parseInt(this.getParameterByName('gameSetTime')) || 120;            Global.DifficultyRate = parseInt(this.getParameterByName('difficultyRate'));            //  this.gameTotalTime = parseInt(this.getParameterByName('totalTime'));            this.gameTotalScore = parseInt(this.getParameterByName('totalScore'));        }        let window1: any = window;        window1.CallNextLevel = () => {            this.NextLevel();        }        window1.CallRestartGame = () => {            this.RestartGame();        }        window1.CallPauseGame = () => {            this.PauseGame();        }        window1.CallContinueGame = () => {            this.ContinueGame();        }        window1.CallQuitGame = () => {            this.QuitGame();        }        window1.CallStartGame = () => {            this.StartGame();        }        window1.CallPlayBgMusic = () => {            this.PlayBgMusic();        }    }    start() {        this.initialize();        if (Global.IsDevelop) {            this.StartGame();        } else {            const levelManager = LevelManager.getInstance();        }    }    update(deltaTime: number) {    }    //初始化    initialize() {        this.timer = Global.GameSetTime;        this.curLevel = Global.StartLv;    }    //得到url参数值    getParameterByName(name) {        let url = window.location.href;        name = name.replace(/[\[\]]/g, '\\$&');        let regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),            results = regex.exec(url);        if (!results) return null;        if (!results[2]) return '';        return decodeURIComponent(results[2].replace(/\+/g, ' '));    }    //开始计时    startCountTime() {        this.schedule(this.updateTimer, 1);    }    updateTimer() {        if (this.isStartGame) {            this.timer--;            this.uiManager.RefreshTime(this.timer);            this.curLevelTime++;            this.gameTotalTime++;            if (this.timer <= 0) {                this.unschedule(this.updateTimer);                this.GameOver();                //保存数据                               const curTotalScore = this.gameTotalScore + this.curLevelTotalScore;                if (Global.IsDevelop) {                    AudioManage.instance.playSound(Global.Audio_TimeOutFail);                    this.uiManager.showGameEndTip(this.curLevel, this.curLevelScore, curTotalScore, false, true);                } else {                    if (this.playClass === 'A') {                        if (this.curLevelScore > this.curLevelData.targetScore) {                            this.saveDataToAndroid(0);                        } else {                            this.saveDataToAndroid(1);                        }                    } else {                        this.saveDataToAndroid(4);                    }                }                // 倒计时结束游戏失败            }        }    }    //重置时间    resetCountTime() {        this.timer = Global.GameSetTime;    }    //加分    addScore(score: number) {        if (!this.isStartGame)            return;        this.curLevelScore += score;        this.curLevelTotalScore += score;        this.uiManager.RefreshScore(this.curLevelScore);        if (this.curLevelScore >= this.curLevelData.targetScore) {            //保存数据            this.GameOver();                        if (this.curLevel === Global.MaxLv) {                this.saveDataToAndroid(3);            } else {                const curTotalScore = this.gameTotalScore + this.curLevelTotalScore;                if (Global.IsDevelop) {                    AudioManage.instance.playSound(Global.Audio_gameOver);                    this.uiManager.showGameEndTip(this.curLevel, this.curLevelScore, curTotalScore, true, false);                } else {                    this.saveDataToAndroid(0);                }            }            this.gameTotalScore += this.curLevelScore;        }    }    //减分    minusScore(score: number) {        if (!this.isStartGame)            return;        this.curLevelScore -= score;        if (this.curLevelScore <= 0) {            this.curLevelScore = 0        }        this.curLevelTotalScore -= score;        if (this.curLevelTotalScore <= 0) {            this.curLevelTotalScore = 0        }        this.uiManager.RefreshScore(this.curLevelScore);        // if (this.curLevelScore <= 0) {        //     this.GameOver();        //     const curTotalScore = this.gameTotalScore + this.curLevelTotalScore;        //     if (Global.IsDevelop) {        //         this.uiManager.showGameEndTip(this.curLevel, this.curLevelScore, curTotalScore, false);        //     } else {        //         if (this.playClass !== 'A'){        //             this.saveDataToAndroid(1);        //         }        //     }        // }    }    gameFail() {        this.GameOver();                const curTotalScore = this.gameTotalScore + this.curLevelTotalScore;        if (Global.IsDevelop) {            AudioManage.instance.playSound(Global.Audio_GameFail);            this.uiManager.showGameEndTip(this.curLevel, this.curLevelScore, curTotalScore, false, false);        } else {            this.saveDataToAndroid(1);        }    }    showTips(text: string) {        this.uiManager.ShowTips(text);    }    //进入下一关    NextLevel() {        this.curLevel++;        this.curLevelScore = 0;        this.curLevelTime = 0;        this.curLevelTotalScore = 0;        this.resetCountTime();        this.uiManager.RefreshLevel(this.curLevel);        this.uiManager.RefreshScore(this.curLevelScore);        this.unscheduleAllCallbacks();        this.StartGame();    }    //重新开始    RestartGame() {        this.curLevelScore = 0;        this.curLevelTime = 0        this.resetCountTime();        this.uiManager.RefreshScore(this.curLevelScore);        AudioManage.instance.playMusic();        this.StartGame();    }    //进入游戏场景    public EnterGameScene() {        this.StartGame();    }    //开始游戏    public StartGame() {        this.isStartGame = true;        const levelManager = LevelManager.getInstance();        if (Global.MaxLv > levelManager.levelData.length) {            Global.MaxLv = levelManager.levelData.length;        }        //得到当前关卡配置数据        this.curLevelData = levelManager.getLevelData(this.curLevel);        this.uiManager.RefreshLevel(this.curLevel);        this.uiManager.RefreshUI();        AudioManage.instance.playMusic();        this.startCountTime();    }    public ContinueGame() {        if (this.isPaused) {            this.ResumeGame();        } else {            this.RestartGame();        }    }    //暂停游戏    public PauseGame() {        this.isPaused = true;        this.isStartGame = false;        AudioManage.instance.pauseMusic();    }    //继续游戏    public ResumeGame() {        this.isPaused = false;        this.isStartGame = true;        AudioManage.instance.playMusic();    }    //游戏结束    public GameOver() {        this.isStartGame = false;        this.unscheduleAllCallbacks();        this.uiManager.GameOver();        AudioManage.instance.pauseMusic();    }    //退出游戏    public QuitGame() {        this.saveDataToAndroid(2);    }    public PlayBgMusic() {        AudioManage.instance.setBgVolume(1);    }    //保存数据到Android     saveDataToAndroid(ispass: number) {        if (Global.IsDevelop) {            return;        }        const obj = {            curLevel: this.curLevel,   //当前关卡            curLevelScore: this.curLevelScore,  //当前关卡分数            curLevelTime: this.curLevelTime,    //当前关卡时间            isPass: ispass    //0成功1失败2退出游戏3通关4超时失败5.暂停        };        const data = JSON.stringify(obj);        let ss: any = window;        if (typeof ss.AndroidInterface !== 'undefined') {            ss.AndroidInterface.performAndroidMethod('saveData:' + data);        } else {            console.log('安卓方法未定义');        }    }    getRand(min, max) {        return Math.floor(Math.random() * ((max + 1) - min)) + min;    }}
 |