Skip to content

Commit

Permalink
Merge dev into master, 2.3.0 (#68)
Browse files Browse the repository at this point in the history
* fixes newEntity, which suffered from the same problem as registerSystem

* update changelog and package.json

* update bundles
  • Loading branch information
GGAlanSmithee authored May 31, 2018
1 parent ba433db commit b82be15
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 29 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

## v2.1.0 (2018-05-31)
## v2.3.0 (2018-05-31)

`EntityManager.newEntity` now properly registers a system using only the `components` array passed in

## v2.2.0 (2018-05-31)

`EntityManager.registerSystem` now properly registers a system using only the `components` array passed in

Expand Down
29 changes: 16 additions & 13 deletions dist/gg-entities.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/gg-entities.min.js

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions dist/gg-entities.module.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gg-entities",
"description": "easy-to-use Entity-Component System in JavaScript",
"version": "2.2.0",
"version": "2.3.0",
"author": {
"name": "alansmithee",
"email": "[email protected]"
Expand Down
16 changes: 10 additions & 6 deletions src/core/entity-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ class EntityManager {

this.entities = Array.from({ length : this.capacity }, () => ({ components: 0 }))
}

componentNamesToId(components) {
return Array
.from(this.componentLookup)
.filter(cl => components.some(c => c === cl[0]))
.map(cl => cl[1])
.reduce((curr, next) => curr | next, 0)
}

increaseCapacity() {
let oldCapacity = this.capacity
Expand Down Expand Up @@ -53,7 +61,7 @@ class EntityManager {

newEntity(components) {
if (Array.isArray(components)) {
components = Array.from(this.componentLookup).reduce((curr, next) => ['', curr[1] | next[1]], ['', 0])[1]
components = this.componentNamesToId(components)
}

if (!Number.isInteger(components) || components <= 0) {
Expand Down Expand Up @@ -180,11 +188,7 @@ class EntityManager {

registerSystem(type, components, callback) {
if (Array.isArray(components)) {
components = Array
.from(this.componentLookup)
.filter(cl => components.some(c => c === cl[0]))
.map(cl => cl[1])
.reduce((curr, next) => curr | next, 0)
components = this.componentNamesToId(components)
}

return this.systemManager.registerSystem(type, components, callback)
Expand Down
17 changes: 16 additions & 1 deletion test/entity-manager/new-entity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('EntityManager', function() {
expect(res.entity).to.deep.equal(this.entityManager.entities[res.id])
})

test('correctly creates an entity given an array of component names (as define in the [componentLookup])', () => {
test('correctly creates an entity given an array of component names (as defined in the [componentLookup])', () => {
let res = this.entityManager.newEntity(this.componentNames)

expect(res.id).to.equal(0)
Expand All @@ -60,6 +60,21 @@ describe('EntityManager', function() {
expect(res.id).to.equal(1)
expect(res.entity).to.deep.equal(this.entityManager.entities[res.id])
})

test('correctly creates an entity given an array of not all registered component names', () => {
const componentNames = [ this.positionName, this.statsName ]

let res = this.entityManager.newEntity(componentNames)

expect(res.id).to.equal(0)
expect(res.entity).to.deep.equal(this.entityManager.entities[res.id])

res = this.entityManager.newEntity(componentNames)

expect(res.id).to.equal(1)
expect(res.entity).to.deep.equal(this.entityManager.entities[res.id])
expect(res.entity.components).to.equal(this.position | this.stats)
})

test('does not add an entity and returns [capacity], given wrong [components] input', () => {
const capacity = this.entityManager.capacity
Expand Down

0 comments on commit b82be15

Please sign in to comment.