Skip to content

Commit

Permalink
Merge pull request #29 from ayebear/hotfix-indexing
Browse files Browse the repository at this point in the history
Hotfix indexing
  • Loading branch information
ayebear authored Apr 9, 2018
2 parents 39739aa + b102442 commit 5ddc266
Show file tree
Hide file tree
Showing 14 changed files with 10,880 additions and 9,606 deletions.
8,460 changes: 4,476 additions & 3,984 deletions docs/ast/source/component_index.js.json

Large diffs are not rendered by default.

11,487 changes: 6,063 additions & 5,424 deletions docs/ast/source/entity.js.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 8 additions & 8 deletions docs/class/src/entity.js~Entity.html
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ <h3 data-ice="anchor" id="instance-method-attach">
<span class="right-info">


<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber310">source</a></span></span>
<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber315">source</a></span></span>
</span>
</h3>

Expand Down Expand Up @@ -966,7 +966,7 @@ <h3 data-ice="anchor" id="instance-method-destroy">
<span class="right-info">


<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber222">source</a></span></span>
<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber227">source</a></span></span>
</span>
</h3>

Expand Down Expand Up @@ -1018,7 +1018,7 @@ <h3 data-ice="anchor" id="instance-method-detach">
<span class="right-info">


<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber329">source</a></span></span>
<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber334">source</a></span></span>
</span>
</h3>

Expand Down Expand Up @@ -1072,7 +1072,7 @@ <h3 data-ice="anchor" id="instance-method-fromJSON">
<span class="right-info">


<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber285">source</a></span></span>
<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber290">source</a></span></span>
</span>
</h3>

Expand Down Expand Up @@ -1401,7 +1401,7 @@ <h3 data-ice="anchor" id="instance-method-removeAll">
<span class="right-info">


<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber209">source</a></span></span>
<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber210">source</a></span></span>
</span>
</h3>

Expand Down Expand Up @@ -1564,7 +1564,7 @@ <h3 data-ice="anchor" id="instance-method-toJSON">
<span class="right-info">


<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber270">source</a></span></span>
<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber275">source</a></span></span>
</span>
</h3>

Expand Down Expand Up @@ -1629,7 +1629,7 @@ <h3 data-ice="anchor" id="instance-method-toString">
<span class="right-info">


<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber257">source</a></span></span>
<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber262">source</a></span></span>
</span>
</h3>

Expand Down Expand Up @@ -1781,7 +1781,7 @@ <h3 data-ice="anchor" id="instance-method-valid">
<span class="right-info">


<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber243">source</a></span></span>
<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber248">source</a></span></span>
</span>
</h3>

Expand Down
13 changes: 6 additions & 7 deletions docs/coverage.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"coverage": "78.94%",
"expectCount": 57,
"coverage": "80.35%",
"expectCount": 56,
"actualCount": 45,
"files": {
"src/component_index.js": {
"expectCount": 13,
"expectCount": 12,
"actualCount": 1,
"undocumentLines": [
3,
Expand All @@ -15,10 +15,9 @@
25,
31,
49,
66,
73,
80,
85
54,
71,
86
]
},
"src/entity.js": {
Expand Down
53 changes: 27 additions & 26 deletions docs/file/src/component_index.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
}

// Uses an existing index or builds a new index, to return entities with the specified components
query(componentNames) {
query(componentNames = []) {
let hash = this.hashComponents(componentNames)

if (hash in this.index) {
Expand Down Expand Up @@ -83,40 +83,41 @@
}
}

// Generic way to apply an operation to matching component groups
apply(componentNames, callback) {
for (let hash in this.index) {
let group = this.index[hash]
// Checks if the current index group is supposed to match all entities
isMatchAllGroup(componentNames, group) {
return (componentNames.length === 0 &amp;&amp; group.components.size === 0)
}

// Check if index group has any of the components that the entity has
let hasAny = componentNames.some(name =&gt; group.components.has(name))
// Add an entity and all of its components to the index
addEntity(entity) {
const componentNames = Object.keys(entity.data)

// Check if the current index group is supposed to match all entities
let isMatchAllGroup = ((componentNames == undefined || componentNames.length === 0) &amp;&amp; group.components.size == 0)
for (let hash in this.index) {
const group = this.index[hash]

// Check if the entity has all of the components of the index group
const hasAll = entity.has(...group.components)

if (hasAny || isMatchAllGroup) {
callback(group.entities)
// Add the entity
if (hasAll || this.isMatchAllGroup(componentNames, group)) {
group.entities.set(entity.id, entity)
}
}
}

// Update an entity in the index (for creating components)
add(entity, ...componentNames) {
this.apply(componentNames, entities =&gt; {
entities.set(entity.id, entity)
})
}

// Update an entity in the index (for removing components)
// Remove certain components from the index for an entity
remove(entity, ...componentNames) {
this.apply(componentNames, entities =&gt; {
entities.delete(entity.id)
})
}
for (let hash in this.index) {
const group = this.index[hash]

// Add an entity and all of its components to the index
addEntity(entity) {
this.add(entity, ...Object.keys(entity.data))
// Check if index group has any of the components that the entity has
const hasAny = componentNames.some(name =&gt; group.components.has(name))

// Remove the entity
if (hasAny || this.isMatchAllGroup(componentNames, group)) {
group.entities.delete(entity.id)
}
}
}

// Remove an entity and all of its components from the index
Expand Down
23 changes: 14 additions & 9 deletions docs/file/src/entity.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@

// Add to the index, to update match all index
if (this.valid()) {
this.world.index.add(this)
this.world.index.addEntity(this)
}
}

Expand Down Expand Up @@ -163,9 +163,6 @@
// Create component and store in entity
// Note: The entity parameter is dangerous to use, since the component hasn&apos;t been added to the entity yet
this.data[component] = new this.world.components[component](this, ...args)

// Call custom onCreate to initialize component, pass the entity (this), and any additional arguments passed into set()
invoke(this.data[component], &apos;onCreate&apos;, this, ...args)
} else if (args.length &gt; 0) {
// Use first argument as component value
this.data[component] = args[0]
Expand All @@ -176,9 +173,12 @@

// Update the index with this new component
if (this.valid()) {
this.world.index.add(this, component)
this.world.index.addEntity(this)
}

// Call custom onCreate to initialize component, pass the entity (this), and any additional arguments passed into set()
invoke(this.data[component], &apos;onCreate&apos;, this, ...args)

return this
}

Expand Down Expand Up @@ -220,14 +220,15 @@
for (let component of components) {
if (component in this.data) {

// Call custom onRemove
invoke(this.data[component], &apos;onRemove&apos;)

// Remove from index
if (this.valid()) {
this.world.index.remove(this, component)
}

// Call custom onRemove
invoke(this.data[component], &apos;onRemove&apos;)

// Remove from entity
delete this.data[component]
}
}
Expand All @@ -246,7 +247,11 @@
*/
removeAll() {
this.remove(...Object.keys(this.data))
this.data = {}

if (Object.keys(this.data).length &gt; 0) {
throw new Error(&apos;Failed to remove all components. Components must have been added during the removeAll().&apos;)
}

return this
}

Expand Down
175 changes: 76 additions & 99 deletions docs/index.json

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions docs/source.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
</div>
</nav>

<div class="content" data-ice="content"><h1>Source <img data-ice="coverageBadge" src="./badge.svg"><span data-ice="totalCoverageCount" class="total-coverage-count">45/57</span></h1>
<div class="content" data-ice="content"><h1>Source <img data-ice="coverageBadge" src="./badge.svg"><span data-ice="totalCoverageCount" class="total-coverage-count">45/56</span></h1>

<table class="files-summary" data-ice="files" data-use-coverage="true">
<thead>
Expand All @@ -51,20 +51,20 @@
<tbody>

<tr data-ice="file">
<td data-ice="filePath"><span><a href="file/src/component_index.js.html#errorLines=10,14,25,3,31,49,66,73,8,80,85,9">src/component_index.js</a></span></td>
<td data-ice="filePath"><span><a href="file/src/component_index.js.html#errorLines=10,14,25,3,31,49,54,71,8,86,9">src/component_index.js</a></span></td>
<td data-ice="identifier" class="identifiers">-</td>
<td class="coverage"><span data-ice="coverage">7 %</span><span data-ice="coverageCount" class="coverage-count">1/13</span></td>
<td style="display: none;" data-ice="size">2428 byte</td>
<td style="display: none;" data-ice="lines">90</td>
<td style="display: none;" data-ice="updated">2018-04-01 03:55:21 (UTC)</td>
<td class="coverage"><span data-ice="coverage">8 %</span><span data-ice="coverageCount" class="coverage-count">1/12</span></td>
<td style="display: none;" data-ice="size">2456 byte</td>
<td style="display: none;" data-ice="lines">91</td>
<td style="display: none;" data-ice="updated">2018-04-02 04:30:46 (UTC)</td>
</tr>
<tr data-ice="file">
<td data-ice="filePath"><span><a href="file/src/entity.js.html">src/entity.js</a></span></td>
<td data-ice="identifier" class="identifiers"><span><a href="class/src/entity.js~Entity.html">Entity</a></span></td>
<td class="coverage"><span data-ice="coverage">100 %</span><span data-ice="coverageCount" class="coverage-count">22/22</span></td>
<td style="display: none;" data-ice="size">9428 byte</td>
<td style="display: none;" data-ice="lines">340</td>
<td style="display: none;" data-ice="updated">2018-04-01 04:50:38 (UTC)</td>
<td style="display: none;" data-ice="size">9596 byte</td>
<td style="display: none;" data-ice="lines">345</td>
<td style="display: none;" data-ice="updated">2018-04-02 04:32:35 (UTC)</td>
</tr>
<tr data-ice="file">
<td data-ice="filePath"><span><a href="file/src/utilities.js.html">src/utilities.js</a></span></td>
Expand All @@ -80,7 +80,7 @@
<td class="coverage"><span data-ice="coverage">100 %</span><span data-ice="coverageCount" class="coverage-count">20/20</span></td>
<td style="display: none;" data-ice="size">9908 byte</td>
<td style="display: none;" data-ice="lines">348</td>
<td style="display: none;" data-ice="updated">2018-04-01 04:53:54 (UTC)</td>
<td style="display: none;" data-ice="updated">2018-04-02 04:48:23 (UTC)</td>
</tr>
</tbody>
</table>
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.3.1",
"version": "0.3.2",
"description": "Pico Entity System for JavaScript (ES6).",
"main": "./index.js",
"scripts": {
Expand Down
53 changes: 27 additions & 26 deletions src/component_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ComponentIndex {
}

// Uses an existing index or builds a new index, to return entities with the specified components
query(componentNames) {
query(componentNames = []) {
let hash = this.hashComponents(componentNames)

if (hash in this.index) {
Expand Down Expand Up @@ -45,40 +45,41 @@ class ComponentIndex {
}
}

// Generic way to apply an operation to matching component groups
apply(componentNames, callback) {
for (let hash in this.index) {
let group = this.index[hash]
// Checks if the current index group is supposed to match all entities
isMatchAllGroup(componentNames, group) {
return (componentNames.length === 0 && group.components.size === 0)
}

// Check if index group has any of the components that the entity has
let hasAny = componentNames.some(name => group.components.has(name))
// Add an entity and all of its components to the index
addEntity(entity) {
const componentNames = Object.keys(entity.data)

// Check if the current index group is supposed to match all entities
let isMatchAllGroup = ((componentNames == undefined || componentNames.length === 0) && group.components.size == 0)
for (let hash in this.index) {
const group = this.index[hash]

// Check if the entity has all of the components of the index group
const hasAll = entity.has(...group.components)

if (hasAny || isMatchAllGroup) {
callback(group.entities)
// Add the entity
if (hasAll || this.isMatchAllGroup(componentNames, group)) {
group.entities.set(entity.id, entity)
}
}
}

// Update an entity in the index (for creating components)
add(entity, ...componentNames) {
this.apply(componentNames, entities => {
entities.set(entity.id, entity)
})
}

// Update an entity in the index (for removing components)
// Remove certain components from the index for an entity
remove(entity, ...componentNames) {
this.apply(componentNames, entities => {
entities.delete(entity.id)
})
}
for (let hash in this.index) {
const group = this.index[hash]

// Add an entity and all of its components to the index
addEntity(entity) {
this.add(entity, ...Object.keys(entity.data))
// Check if index group has any of the components that the entity has
const hasAny = componentNames.some(name => group.components.has(name))

// Remove the entity
if (hasAny || this.isMatchAllGroup(componentNames, group)) {
group.entities.delete(entity.id)
}
}
}

// Remove an entity and all of its components from the index
Expand Down
Loading

0 comments on commit 5ddc266

Please sign in to comment.