ecsyjs / ecsy

Entity Component System for javascript
https://ecsyjs.github.io/ecsy/
MIT License
1.11k stars 115 forks source link

function like createEntity should return the entity defined by options.entityClass, not the default Entity. #213

Open iam-yan opened 4 years ago

iam-yan commented 4 years ago

Issue: In the library when an entity or entity[] is returned, e.g. createEntity function, the return types now are defined as Entity / Entity[]. But in the constructor of World class, one can pass in {entityClass: CustomEntity} to override the default entityClass, in which case all the relevant returned entity should be CustomEntity.

PS: I like how A-Frame is designed and wished to apply it in a wider use cases and found ecsy. Thanks a lot for bringing this fantastic project! !

fernandojsg commented 4 years ago

@robertlong do you have any thoughts on this?

robertlong commented 4 years ago

Maybe we could make the type of World accept an optional generic?

export class World<E = Entity> {
  createEntity(name?: string): E
}

Then you could do something like: World<ECSYThreeEntity>

I also need this in systems for query results: https://github.com/MozillaReality/ecsy-three/blob/dev/src/core/System.d.ts

It's a slightly larger change, but I think we could do it.