GGAlanSmithee / Entities

efficient and easy-to-use Entity-Component System in JavaScript
http://ggalansmithee.github.io/Entities/
MIT License
4 stars 1 forks source link

component initialization does not work for class components #92

Closed GGAlanSmithee closed 5 years ago

GGAlanSmithee commented 6 years ago

crashes here

GGAlanSmithee commented 6 years ago

Work-around until fixed:

const comp = 'comp'

class Component {
    constructor(x, y) {
        this.x = x
        this.y = y
    }

    x = 0
    y = 0
}

entityManager.registerComponent(comp, Component)

const entities = entityManager
    .build()
    .withComponent(comp, () => { return new Component(10, 5) })
    .create(3)

However, the below code (without a initializer) will fail, so this still needs to be fixed (preferably without the function returning a new instance hack)

const entities = entityManager
    .build()
    .withComponent(comp)
    .create(3)
GGAlanSmithee commented 5 years ago

This has been fixed in the dev branch. On top of this, the initialization API was changed to always require a function, to keep the API normalized.

Example:

entityManager.withComponent('myComp', () => new MyComp(12, 'hi'))