-
Notifications
You must be signed in to change notification settings - Fork 115
typescript: add EntityManager types #246
base: dev
Are you sure you want to change the base?
Conversation
The |
Basically if you create a custom Entity class you need the type of the manager for the constructor... You thus need the typings of the manager. People can obviously just use a |
5a8a565
to
47a4dcb
Compare
@robertlong Since this library is IMO a really low-level one, I'd suggest to not constrain developers by hiding classes and/or methods of this library. I am currently working on serialization for ecsy and have to extend the EntityManager and ComponentManager Classes. export class SerializableWorld extends World<SerializableEntity> {
private _entities: Set<SerializableEntity> = new Set();
constructor(options?: WorldOptions) {
super({ ...DEFAULT_OPTIONS, ...options });
this._onEntityRemoved = this._onEntityRemoved.bind(this);
//
// ---- DON'T TRY THIS AT HOME ----
// we have to rewrite the ComponentManager on the fly since it is not public
// exposed by the package 🦹🏻♂️
type _ComponentsManager = new (world: World) => ComponentManager;
const ComponentsManager = this.componentsManager.constructor as _ComponentsManager;
class ExtComponentManager extends ComponentsManager {
constructor(world: SerializableWorld) {
super(world);
this._componentsRegister = new Map();
}
public registerComponent<C extends Component<any>>(
Component: ComponentConstructor<C>,
objectPool?: ObjectPool<C> | boolean,
): void {
super.registerComponent(Component, objectPool);
const name = Component.getName();
this._componentsRegister.set(name, Component);
}
public getComponentClassByClassName(name: string): ComponentConstructor<Component<any>> | undefined {
return this._componentsRegister.get(name);
}
private _componentsRegister: Map<string, ComponentConstructor<Component<any>>>;
}
this.componentsManager = new ExtComponentManager(this);
//
// ---- DON'T TRY THIS AT HOME ----
//
this.entityManager.eventDispatcher.addEventListener(
'EntityManager#ENTITY_REMOVED',
this._onEntityRemoved,
);
} Just my 2 cents and my 👍 for opening up the API and update the typings of it. 😃 |
EntityManager.d.ts
Entity
constructor (d.ts) to acceptEntityManager
Why?
When creating a custom
Entity
type, we may need to have access to theEntityManager
type.