playcanvas
Version:
PlayCanvas WebGL game engine
86 lines (83 loc) • 3.31 kB
JavaScript
import { EventHandler } from '../../core/event-handler.js';
/**
* @import { AnimComponentSystem } from './anim/system.js'
* @import { AnimationComponentSystem } from './animation/system.js'
* @import { AudioListenerComponentSystem } from './audio-listener/system.js'
* @import { ButtonComponentSystem } from './button/system.js'
* @import { CameraComponentSystem } from './camera/system.js'
* @import { CollisionComponentSystem } from './collision/system.js'
* @import { ElementComponentSystem } from './element/system.js'
* @import { JointComponentSystem } from './joint/system.js'
* @import { LayoutChildComponentSystem } from './layout-child/system.js'
* @import { LayoutGroupComponentSystem } from './layout-group/system.js'
* @import { LightComponentSystem } from './light/system.js'
* @import { ModelComponentSystem } from './model/system.js'
* @import { ParticleSystemComponentSystem } from './particle-system/system.js'
* @import { RenderComponentSystem } from './render/system.js'
* @import { RigidBodyComponentSystem } from './rigid-body/system.js'
* @import { ScreenComponentSystem } from './screen/system.js'
* @import { ScriptComponentSystem } from './script/system.js'
* @import { ScrollViewComponentSystem } from './scroll-view/system.js'
* @import { ScrollbarComponentSystem } from './scrollbar/system.js'
* @import { SoundComponentSystem } from './sound/system.js'
* @import { SpriteComponentSystem } from './sprite/system.js'
* @import { ZoneComponentSystem } from './zone/system.js'
*/ /**
* The ComponentSystemRegistry manages the instances of an application's {@link ComponentSystem}s.
* {@link AppBase} maintains a single instance of this class which can be accessed via
* {@link AppBase#systems}.
*
* ```javascript
* // Set the gravity to zero
* app.systems.rigidbody.gravity = new pc.Vec3(0, 0, 0);
*
* // Set the volume to 50%
* app.systems.sound.volume = 0.5;
* ```
*/ class ComponentSystemRegistry extends EventHandler {
/**
* Create a new ComponentSystemRegistry instance.
*/ constructor(){
super();
// An array of pc.ComponentSystem objects
this.list = [];
}
/**
* Add a component system to the registry.
*
* @param {object} system - The {@link ComponentSystem} instance.
* @ignore
*/ add(system) {
const id = system.id;
if (this[id]) {
throw new Error(`ComponentSystem name '${id}' already registered or not allowed`);
}
this[id] = system;
// Update the component system array
this.list.push(system);
}
/**
* Remove a component system from the registry.
*
* @param {object} system - The {@link ComponentSystem} instance.
* @ignore
*/ remove(system) {
const id = system.id;
if (!this[id]) {
throw new Error(`No ComponentSystem named '${id}' registered`);
}
delete this[id];
// Update the component system array
const index = this.list.indexOf(this[id]);
if (index !== -1) {
this.list.splice(index, 1);
}
}
destroy() {
this.off();
for(let i = 0; i < this.list.length; i++){
this.list[i].destroy();
}
}
}
export { ComponentSystemRegistry };