Skip to content

Commit

Permalink
Now passes component constructor arguments to onCreate
Browse files Browse the repository at this point in the history
Updated readme, tests, and version
  • Loading branch information
ayebear committed May 29, 2017
1 parent 2b224c4 commit 395396d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,54 @@ world.component('position', class {

It is completely optional to register components. They may help structure your data, or provide basic methods to use with the components. Component definitions must be of type "function", which includes classes.

#### onRemove

This gets called when a component is removed:

```javascript
let removed = 0

world.component('sprite', class {
onRemove() {
removed++
}
})

let entity = world.entity().set('sprite')

assert(removed === 0)

entity.remove('sprite')

assert(removed === 1)
```

It also gets called when an entire entity is destroyed:

```javascript
let entity2 = world.entity().set('sprite')

assert(removed === 1)

entity2.destroy()

assert(removed === 2)
```

#### onCreate

This gets called immediately after the component is constructed. The first argument is always the parent entity, followed by the arguments passed into "set". It can be used as an alternative to the constructor, whenever you need access to the entity:

```javascript
world.component('player', class {
onCreate(entity, texture) {
entity.set('sprite', texture)
}
})

world.entity().set('player', 'player.png')
```

### Register systems

This registers a basic movement system. PicoES will create an instance of the class for you, so do not try to pass in an already instantiated object.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "picoes",
"version": "0.2.6",
"version": "0.2.7",
"description": "Pico Entity System for JavaScript (ES6).",
"main": "./src/picoes.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Entity {
this.data[component] = new this.world.components[component](...args)

// Call custom onCreate with this entity as a parameter
invoke(this.data[component], 'onCreate', this)
invoke(this.data[component], 'onCreate', this, ...args)
} else if (args.length > 0) {
// Use first argument as component value
this.data[component] = args[0]
Expand Down
21 changes: 17 additions & 4 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,30 @@ describe('World', function() {
assert(count === 0)
})
it('test entity creation with onCreate', function() {
let when = 0
let world = new World()
world.component('sprite', class {
onCreate(entity) {
constructor(texture, size) {
this.texture = texture
this.size = size
this.constructorCalled = ++when
}

onCreate(entity, texture, size) {
this.entity = entity
this.x = 1
this.onCreateCalled = ++when
assert(texture && texture === this.texture)
assert(size && size === this.size)
}
})

let ent = world.entity().set('sprite')
let ent = world.entity().set('sprite', 'test.png', 100)
assert(ent.get('sprite').constructorCalled === 1)
assert(ent.get('sprite').onCreateCalled === 2)
assert(ent.get('sprite').entity === ent)
assert(ent.get('sprite').x === 1)
assert(ent.get('sprite').texture === 'test.png')
assert(ent.get('sprite').size === 100)

})
it('test clearing with onRemove', function() {
let spriteCount = 0
Expand Down

0 comments on commit 395396d

Please sign in to comment.