import Component from './Component' ; import { Is } from '../misc/Is' ; import EntityRepository from './EntityRepository' ; import Config from './Config' ; import { ComponentTID , ComponentSID , EntityUID } from '../../types/CommonTypes' ; /**
 * The class that generates and manages all kinds of components.
 */ export default class ComponentRepository { private static __instance : ComponentRepository ; private __component_sid_count_map : Map < ComponentTID , number > ; private __components : Map < ComponentTID , Array < Component > > ; static __componentClasses : Map < ComponentTID , typeof Component > = new Map ( ) ; constructor ( ) { this . __component_sid_count_map = new Map ( ) ; this . __components = new Map ( ) ; } static registerComponentClass ( componentClass ) { const thisClass = ComponentRepository ; thisClass . __componentClasses . set ( componentClass . componentTID , componentClass ) ; } static unregisterComponentClass ( componentTID ) { const thisClass = ComponentRepository ; thisClass . __componentClasses . delete ( componentTID ) ; } /**
     * Gets the singleton instance of the ComponentRepository.
     */ static getInstance ( ) { if ( ! this . __instance ) { this . __instance = new ComponentRepository ( ) ; } return this . __instance ; } param componentTid static getComponentClass ( componentTid ) { return this . __componentClasses . get ( componentTid ) ; } param componentTid param entityUid param entityRepository createComponent ( componentTid , entityUid , entityRepository ) { const thisClass = ComponentRepository ; const componentClass = thisClass . __componentClasses . get ( componentTid ) ; if ( Is . exist ( componentClass ) ) { let component_sid_count = this . __component_sid_count_map . get ( componentTid ) ; if ( Is . not . exist ( component_sid_count ) ) { this . __component_sid_count_map . set ( componentTid , 0 ) ; component_sid_count = Component . invalidComponentSID ; } this . __component_sid_count_map . set ( componentTid , ++ component_sid_count ! ) ; const component = new componentClass ( entityUid , component_sid_count ! , entityRepository ) as Component ; if ( ! this . __components . has ( componentTid ) ) { this . __components . set ( componentTid , [ ] ) ; } const array = this . __components . get ( componentTid ) ; if ( array != null ) { array [ component . componentSID ] = component ; return component ; } } else { console . error ( 'The Component Class object is invalid.' ) ; } return null ; } param componentClass param componentSid getComponent ( componentClass , componentSid ) { return this . getComponentFromComponentTID ( componentClass . componentTID , componentSid ) ; } param componentTid param componentSid getComponentFromComponentTID ( componentTid , componentSid ) { const map = this . __components . get ( componentTid ) ; if ( map != null ) { const component = map [ componentSid ] ; if ( component != null ) { return map [ componentSid ] ; } else { return null ; } } return null ; } private param componentClass _getComponents ( componentClass ) { return this . __components . get ( componentClass . componentTID ) ; } static getMemoryBeginIndex ( componentTid ) { let memoryBeginIndex = 0 ; for ( let i = 0 ; i < componentTid ; i ++ ) { const componentClass = ComponentRepository . __componentClasses . get ( i ) ; if ( componentClass != null ) { const sizeOfComponent = ( componentClass as any ) . sizeOfThisComponent ; const maxEntityNumber = Config . maxEntityNumber ; memoryBeginIndex += sizeOfComponent * maxEntityNumber ; } } return memoryBeginIndex ; } param componentType getComponentsWithType ( componentType ) { const components = this . __components . get ( componentType . componentTID ) ; if ( components == null ) { return [ ] ; } return components ; } /**
     * Gets all componentTIDs.
     */ getComponentTIDs ( ) { const indices = [ ] ; for ( const type of this . __components . keys ( ) ) { indices . push ( type ) ; } indices . sort ( ( a , b ) => { if ( a < b ) return - 1 ; if ( a > b ) return 1 ; return 0 ; } ) ; return indices ; } }