import { Logic } from "../base/Logic" ; import { LogicEvent , LogicEventData , LogicType } from "../event/LogicEvent" ; import { Manager } from "./Manager" ; export class LogicManager { private _logTag = `[LogicManager]` ; private static _instance : LogicManager = null ; public static Instance ( ) { return this . _instance || ( this . _instance = new LogicManager ( ) ) ; } private _logics : Logic [ ] = [ ] ; private _logicTypes = [ ] ; private node : cc . Node = null ; public push ( logicType ) { for ( let i = 0 ; i < this . _logicTypes . length ; i ++ ) { if ( this . _logicTypes [ i ] == logicType ) { cc . error ( this . _logTag , `重复添加${ cc . js . getClassName ( logicType ) }` ) ; return ; } } if ( this . node ) { let logic = new logicType ; logic . init ( this . node ) ; this . _logics . push ( logic ) ; logic . onLoad ( ) ; } else { this . _logicTypes . push ( logicType ) ; } } public onLoad ( node ) { this . node = node ; Manager . eventDispatcher . addEventListener ( LogicEvent . ENTER_COMPLETE , this . onEnterComplete , this ) ; if ( this . _logics . length == 0 ) { for ( let i = 0 ; i < this . _logicTypes . length ; i ++ ) { let type = this . _logicTypes [ i ] ; cc . log ( this . _logTag , `添加Logic : ${ cc . js . getClassName ( type ) }` ) ; let logic = new type ; logic . init ( node ) ; this . _logics . push ( logic ) ; } } this . _logics . forEach ( ( data ) => { data . onLoad ( ) ; } ) ; } public onDestroy ( node ) { Manager . eventDispatcher . removeEventListener ( LogicEvent . ENTER_COMPLETE , this ) ; this . _logics . forEach ( ( data ) => { data . onDestroy ( ) ; } ) ; } protected onEnterComplete ( data ) { if ( data . type != LogicType . ROOM_LIST ) { if ( data && data . views && data . views . length > 0 ) { Manager . uiManager . closeExcept ( data . views ) ; } for ( let i = 0 ; i < this . _logics . length ; i ++ ) { let logic = this . _logics [ i ] ; if ( logic ) { logic . onEnterComplete ( data ) ; } } } } }