import { MapData } from "../Common/JsonData/JsonData" ; import { Util } from "../Common/Utils/Util" ; import { Debug } from "../Common/Debug" ; export class PoolManager { private pool : Set < cc . Node > = new Set ( ) ; private currentePool : Map < string , cc . Node [ ] > = new Map ( ) ; private instantToPrefab : Map < number , cc . Prefab > = new Map ( ) ; private urlToPrefab : Map < string , cc . Prefab > = new Map ( ) ; public addUrlToPrefab ( url , prefab ) { if ( url && prefab ) { this . urlToPrefab . set ( url , prefab ) ; } } public requestInstant ( prefab , args = null ) { if ( prefab == null ) { return ; } this . addPrefabPool ( prefab ) ; var uuid = prefab . data . uuid ; var currentList = this . currentePool . get ( uuid ) ; var instant = currentList . pop ( ) ; if ( ! instant ) { instant = this . addAndPopInstant ( prefab ) ; } instant . active = true ; if ( args != null && args . length != 0 ) { var uiComponent = instant . getComponent ( "UIComponent" ) ; if ( uiComponent ) { uiComponent . loadCompleteCall ( args ) ; } } return instant ; } public requestInstantWithArgs ( prefab , position , rotation , parent ) { if ( prefab == null ) { return ; } this . addPrefabPool ( prefab ) ; var uuid = prefab . data . uuid ; var currentList = this . currentePool . get ( uuid ) ; var instant = currentList . pop ( ) ; if ( ! instant ) { instant = this . addAndPopInstant ( prefab ) ; instant . active = false ; } instant . angle = - rotation ; instant . position = position ; instant . active = true ; instant . parent = parent ; return instant ; } public addPrefabPool ( prefab ) { if ( prefab == null ) { Debug . error ( "addPrefabPool prefab is null" ) ; return ; } if ( prefab . data == null ) { Debug . error ( "prefab name:" + prefab . name + "data is null" ) ; } var uuid = prefab . data . uuid ; if ( this . currentePool . has ( uuid ) ) { return ; } var currentList = new Array ( ) ; this . currentePool . set ( uuid , currentList ) ; } public addAndPopInstant ( prefab ) { var instant = cc . instantiate ( prefab ) ; ( instant as any ) . id = Util . uuid ( ) ; this . pool . add ( instant ) ; this . instantToPrefab . set ( ( instant as any ) . id , prefab ) ; return instant ; } public returnInstant ( instant ) { if ( ! instant ) { return ; } if ( ! instant . isValid ) { return ; } instant . active = false ; instant . removeFromParent ( ) ; var prefab = this . instantToPrefab . get ( ( instant as any ) . id ) ; if ( ! prefab ) { return ; } if ( ! prefab . data . uuid ) { return ; } var currentList = this . currentePool . get ( prefab . data . uuid ) ; currentList . push ( instant ) ; } public reset ( ) { for ( var [ key , instant ] of this . currentePool ) { if ( instant . isValid ) { instant . destroy ( ) ; } } this . pool . clear ( ) ; this . currentePool . clear ( ) ; this . instantToPrefab . clear ( ) ; this . urlToPrefab . clear ( ) ; } public requestInstantByUrl ( url , callBack , args ) { if ( url == null ) { callBack ( "null url" , null ) ; return ; } if ( this . urlToPrefab . has ( url ) ) { var prefab = this . urlToPrefab . get ( url ) ; var ins = this . requestInstant ( prefab , args ) ; callBack ( null , ins ) ; return ; } let self = this ; cc . loader . loadRes ( url , function ( err , prefab ) { self . addUrlToPrefab ( url , prefab ) ; var ins = self . requestInstant ( prefab , args ) ; callBack ( err , ins ) ; } ) ; } public setSpriteFrameByUrl ( iconName , sprite = null , caller = null , callback = null ) { let url = "UI/Icon/" + iconName . toString ( ) ; cc . loader . loadRes ( url , cc . SpriteFrame , function ( err , spriteFrame ) { if ( sprite ) { sprite . spriteFrame = spriteFrame ; } if ( caller && callback ) { callback . call ( caller , spriteFrame ) ; } } ) ; } public setAtlasByUrl ( atlas_id , animation , caller = null , callback = null ) { let url = "clip/" + atlas_id . toString ( ) ; cc . loader . loadRes ( url , cc . AnimationClip , function ( err , clip ) { clip . name = atlas_id ; clip . wrapMode = cc . WrapMode . Normal ; animation . addClip ( clip ) ; if ( caller && callback ) { callback . call ( caller ) ; } } ) ; } }