Skip to content
This repository has been archived by the owner on Jun 1, 2022. It is now read-only.

typescript: add EntityManager types #246

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

DavidPeicho
Copy link
Contributor

@DavidPeicho DavidPeicho commented Aug 17, 2020

  • Adds EntityManager.d.ts
  • Update Entity constructor (d.ts) to accept EntityManager

Why?

When creating a custom Entity type, we may need to have access to the EntityManager type.

src/EntityManager.d.ts Outdated Show resolved Hide resolved
@robertlong
Copy link
Member

The EntityManager type definition needs typed function arguments and return parameters. Also I personally don't really want to expose EntityManager as a public API as I want to refactor it and possibly get rid of this abstraction entirely. What methods are exposed from EntityManager that you don't currently have access to and need for your project?

@DavidPeicho
Copy link
Contributor Author

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 any type in this case, but it makes sense to have access to the manager API when extending Entity.

@martinemmert
Copy link

martinemmert commented Oct 21, 2020

@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.
This is my current solution to extend the ComponentManager, since it is not exposed by the package. 😅

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. 😃

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants