Files
youlegames/codes/games/server/games/gamehall/class.gamemanager.js
2026-02-04 23:47:45 +08:00

204 lines
4.7 KiB
JavaScript

function GameMgr(id,_stages){
this.id = id;
this.stageList = [];
this.delayList = [];
this.stageIndex = 0;
this.stageMtr = -1;
this.stage = -1;
this.timerId = null;
this.stages = _stages;
this.stagesList = [];
this.stageTime = 0;
}
GameMgr.prototype.init = function(){
this.stageIndex = 0;
this.stageMtr = -1;
if(this.timerId){
window.clearTimeout(this.timerId);
this.timerId = null;
}
}
GameMgr.prototype.newStage = function(_stage){
this.stage = _stage;
this.stageTime = new Date();
}
GameMgr.prototype.destroy = function(){
if(this.timerId){
window.clearTimeout(this.timerId);
this.timerId = null;
}
this.stageList = [];
this.delayList = [];
this.stagesList = [];
}
GameMgr.prototype.create = function(){
this.stagesList = [];
for(var i=0;i<this.stages;i++){
this.stagesList.push(new window['Stage_'+i](this));
}
}
GameMgr.prototype.setConfig = function(_stageList,_delayList){
this.stageList = _stageList;
this.delayList = _delayList;
}
//GameMgr.prototype.nextStep = function(_msg){
//if(this.timerId){
//window.clearTimeout(this.timerId);
//this.timerId = null;
//}
//if(this.delayList[this.stageIndex] == 0){
//if(this.stageIndex>=this.stageList.length){
//return;
//}
//if(this.stageMtr<this.stageIndex){
//this.stageMtr = this.stageIndex;
//this.stagesList[this.stageList[this.stageIndex]].onTimeOut(_msg);
//this.newStage(this.stageIndex);
//this.stageIndex++;
//this.finishStage(_msg);
//}else{
//this.stepStage("");
//}
//}else if(this.delayList[this.stageIndex] > 0){
//this.timerId = setTimeout(function(){
//if(this.stageMtr<this.stageIndex){
//if(this.stageIndex>=this.stageList.length){
//return;
//}
//this.stageMtr = this.stageIndex;
//this.stagesList[this.stageList[this.stageIndex]].onTimeOut(_msg);
//this.newStage(this.stageIndex);
//this.stageIndex++;
//this.finishStage(_msg);
//}else{
//this.stepStage("");
//}
//}.bind(this),this.delayList[this.stageIndex]);
//}else{
//}
//}
GameMgr.prototype.nextStep = function(_msg){
}
GameMgr.prototype.checkStep = function(){
if(this.stageIndex>=this.stageList.length){
return;
}
if(this.delayList[this.stageIndex] == 0){
if(this.stageMtr<this.stageIndex){
this.stageMtr = this.stageIndex;
this.stagesList[this.stageList[this.stageIndex]].onTimeOut();
this.newStage(this.stageIndex);
this.stageIndex++;
this.finishStage();
}else{
this.stepStage("");
}
}else if(this.delayList[this.stageIndex] > 0){
var deltaTime = new Date() - this.stageTime;
if(deltaTime > this.delayList[this.stageIndex]){
if(this.stageMtr<this.stageIndex){
if(this.stageIndex>=this.stageList.length){
return;
}
this.stageMtr = this.stageIndex;
this.stagesList[this.stageList[this.stageIndex]].onTimeOut();
this.newStage(this.stageIndex);
this.stageIndex++;
this.finishStage();
}else{
this.stepStage("");
}
}
}else{
}
}
GameMgr.prototype.onGame = function(_msg){
this.stageMtr = this.stageIndex;
if(this.delayList[this.stageIndex] == -1){
this.newStage(this.stageIndex);
this.stagesList[this.stageList[this.stageIndex]].onEnter(_msg);
this.stageIndex++;
if(this.stageIndex>=this.stageList.length){
return;
}
if(this.delayList[this.stageIndex] > -1){
this.nextStep(_msg);
}
}
}
GameMgr.prototype.finishStage = function(_msg){
this.stageMtr = this.stageIndex;
if(this.delayList[this.stageIndex]>-1){
this.nextStep(_msg);
}else{
}
}
GameMgr.prototype.stepStage = function(_msg){
this.newStage(this.stageIndex);
this.stagesList[this.stageList[this.stageIndex]].onEnter(_msg);
this.stageIndex++;
if(this.stageIndex>=this.stageList.length){
return;
}
if(this.delayList[this.stageIndex]>-1){
this.nextStep(_msg);
}
}
GameMgr.prototype.gameStart = function(_msg){
this.init();
this.stageIndex = 0;
this.stageMtr = 0;
if(this.stageIndex>=this.stageList.length){
return;
}
if(this.delayList[this.stageIndex] == 0){
this.newStage(this.stageIndex);
this.stagesList[this.stageList[this.stageIndex]].onEnter(_msg);
this.stageIndex++;
if(this.delayList[this.stageIndex]>-1){
this.nextStep(_msg);
}else{
}
}else if(this.delayList[this.stageIndex] > 0){
setTimeout(function(){
this.newStage(this.stageIndex);
this.stagesList[this.stageList[this.stageIndex]].onEnter(_msg);
this.stageIndex++;
if(this.delayList[this.stageIndex]>-1){
this.nextStep(_msg);
}else{
}
}.bind(this),this.delayList[this.stageIndex]);
}else{
}
}
GameMgr.prototype.doPack = function(_msg){
if(this.stage>=this.stageList.length || this.stage < 0){
return;
}
this.stagesList[this.stageList[this.stage]].doPack(_msg);
}