module entitas { "use strict" ; import Entity = entitas . Entity ; import Signal = entitas . utils . Signal ; import ISignal = entitas . utils . ISignal ; import IMatcher = entitas . IMatcher ; import IComponent = entitas . IComponent ; import GroupChanged = Group . GroupChanged ; import GroupUpdated = Group . GroupUpdated ; import GroupEventType = entitas . GroupEventType ; import SingleEntityException = entitas . exceptions . SingleEntityException ; /**
     * event delegate boilerplate:
     */ export module Group { export interface GroupChanged { ( group , entity , index , component ) : void ; } export interface IGroupChanged < T > extends ISignal < T > { dispatch ( group , entity , index , component ) : void ; } export interface GroupUpdated { ( group , entity , index , component , newComponent ) : void ; } export interface IGroupUpdated < T > extends ISignal < T > { dispatch ( group , entity , index , component , newComponent ) : void ; } } export class Group { type { entitas . utils . ISignal } public onEntityAdded : Group . IGroupChanged < GroupChanged > = null ; type { entitas . utils . ISignal } public onEntityRemoved : Group . IGroupChanged < GroupChanged > = null ; type { entitas . utils . ISignal } public onEntityUpdated : Group . IGroupUpdated < GroupUpdated > = null ; type { number } name public get count ( ) : number { return Object . keys ( this . _entities ) . length ; } type { entitas . IMatcher } name public get matcher ( ) : IMatcher { return this . _matcher ; } private _entities = { } ; private _matcher : IMatcher = null ; public _entitiesCache : Array < Entity > = null ; public _singleEntityCache : Entity = null ; public _toStringCache : string = '' ; constructor param matcher constructor ( matcher ) { this . _entities = { } ; this . onEntityAdded = new Signal < GroupChanged > ( this ) ; this . onEntityRemoved = new Signal < GroupChanged > ( this ) ; this . onEntityUpdated = new Signal < GroupUpdated > ( this ) ; this . _matcher = matcher ; } param eventType public createObserver ( eventType ) { if ( eventType === undefined ) eventType = GroupEventType . OnEntityAdded ; return new GroupObserver ( this , eventType ) ; } param entity public handleEntitySilently ( entity ) { if ( this . _matcher . matches ( entity ) ) { this . addEntitySilently ( entity ) ; } else { this . removeEntitySilently ( entity ) ; } } param entity param index param component public handleEntity ( entity , index , component ) { if ( this . _matcher . matches ( entity ) ) { this . addEntity ( entity , index , component ) ; } else { this . removeEntity ( entity , index , component ) ; } } param entity param index param previousComponent param newComponent public updateEntity ( entity , index , previousComponent , newComponent ) { if ( entity . id in this . _entities ) { const onEntityRemoved = this . onEntityRemoved ; if ( onEntityRemoved . active ) onEntityRemoved . dispatch ( this , entity , index , previousComponent ) ; const onEntityAdded = this . onEntityAdded ; if ( onEntityAdded . active ) onEntityAdded . dispatch ( this , entity , index , newComponent ) ; const onEntityUpdated = this . onEntityUpdated ; if ( onEntityUpdated . active ) onEntityUpdated . dispatch ( this , entity , index , previousComponent , newComponent ) ; } } param entity public addEntitySilently ( entity ) { if ( ! ( entity . id in this . _entities ) ) { this . _entities [ entity . id ] = entity ; this . _entitiesCache = null ; this . _singleEntityCache = null ; entity . addRef ( ) ; } } param entity param index param component public addEntity ( entity , index , component ) { if ( ! ( entity . id in this . _entities ) ) { this . _entities [ entity . id ] = entity ; this . _entitiesCache = null ; this . _singleEntityCache = null ; entity . addRef ( ) ; const onEntityAdded = this . onEntityAdded ; if ( onEntityAdded . active ) onEntityAdded . dispatch ( this , entity , index , component ) ; } } param entity public removeEntitySilently ( entity ) { if ( entity . id in this . _entities ) { delete this . _entities [ entity . id ] ; this . _entitiesCache = null ; this . _singleEntityCache = null ; entity . release ( ) ; } } param entity param index param component public removeEntity ( entity , index , component ) { if ( entity . id in this . _entities ) { delete this . _entities [ entity . id ] ; this . _entitiesCache = null ; this . _singleEntityCache = null ; let onEntityRemoved = this . onEntityRemoved ; if ( onEntityRemoved . active ) onEntityRemoved . dispatch ( this , entity , index , component ) ; entity . release ( ) ; } } param entity returns public containsEntity ( entity ) { return entity . id in this . _entities ; } returns public getEntities ( ) { if ( this . _entitiesCache == null ) { const entities = this . _entities ; const keys = Object . keys ( entities ) ; const length = keys . length ; const entitiesCache = this . _entitiesCache = new Array ( length ) ; for ( let i = 0 ; i < length ; i ++ ) { entitiesCache [ i ] = entities [ keys [ i ] ] ; } } return this . _entitiesCache ; } returns public getSingleEntity ( ) { if ( this . _singleEntityCache == null ) { const enumerator = Object . keys ( this . _entities ) ; const c = enumerator . length ; if ( c === 1 ) { this . _singleEntityCache = this . _entities [ enumerator [ 0 ] ] ; } else if ( c === 0 ) { return null ; } else { throw new SingleEntityException ( this . _matcher ) ; } } return this . _singleEntityCache ; } returns public toString ( ) { if ( this . _toStringCache == null ) { this . _toStringCache = "Group(" + this . _matcher + ")" ; } return this . _toStringCache ; } } }