新增全局管理器

This commit is contained in:
yhh
2020-06-18 12:14:06 +08:00
parent 231276e39b
commit 915056203d
8 changed files with 175 additions and 2 deletions

View File

@@ -0,0 +1,46 @@
class GlobalManager {
public static globalManagers: GlobalManager[] = [];
private _enabled: boolean;
public get enabled(){
return this._enabled;
}
public set enabled(value: boolean){
this.setEnabled(value);
}
public setEnabled(isEnabled: boolean){
if (this._enabled != isEnabled){
this._enabled = isEnabled;
if (this._enabled){
this.onEnabled();
} else {
this.onDisabled();
}
}
}
public onEnabled(){}
public onDisabled(){}
public update(){}
public static registerGlobalManager(manager: GlobalManager){
this.globalManagers.push(manager);
manager.enabled = true;
}
public static unregisterGlobalManager(manager: GlobalManager){
this.globalManagers.remove(manager);
manager.enabled = false;
}
public static getGlobalManager<T extends GlobalManager>(type){
for (let i = 0; i < this.globalManagers.length; i ++){
if (this.globalManagers[i] instanceof type)
return this.globalManagers[i] as T;
}
return null;
}
}