Skip to content

Commit

Permalink
Systems can now be created without a component name array
Browse files Browse the repository at this point in the history
Restructured the documentation so the parameters would match up
Updated/added tests
  • Loading branch information
ayebear committed Apr 7, 2018
1 parent 901552c commit 74ab931
Show file tree
Hide file tree
Showing 11 changed files with 15,823 additions and 13,337 deletions.
9,488 changes: 4,744 additions & 4,744 deletions docs/ast/source/entity.js.json

Large diffs are not rendered by default.

19,339 changes: 10,867 additions & 8,472 deletions docs/ast/source/world.js.json

Large diffs are not rendered by default.

44 changes: 28 additions & 16 deletions docs/class/src/entity.js~Entity.html
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ <h3 data-ice="anchor" id="instance-set-id">
<span class="right-info">


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

Expand All @@ -770,7 +770,19 @@ <h3 data-ice="anchor" id="instance-set-id">




<div data-ice="throwWrap">
<h4>Throw:</h4>
<table>
<tbody>

<tr class="throw" data-ice="throw">
<td><p data-ice="throwName"><span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error">Error</a></span></p></td>
<td data-ice="throwDesc"><p>Cannot set entity id</p>
</td>
</tr>
</tbody>
</table>
</div>



Expand All @@ -796,7 +808,7 @@ <h3 data-ice="anchor" id="instance-method-access">
<span class="right-info">


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

Expand Down Expand Up @@ -884,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#lineNumber303">source</a></span></span>
<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber305">source</a></span></span>
</span>
</h3>

Expand Down Expand Up @@ -954,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#lineNumber215">source</a></span></span>
<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber217">source</a></span></span>
</span>
</h3>

Expand Down Expand Up @@ -1006,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#lineNumber322">source</a></span></span>
<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber324">source</a></span></span>
</span>
</h3>

Expand Down Expand Up @@ -1060,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#lineNumber278">source</a></span></span>
<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber280">source</a></span></span>
</span>
</h3>

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


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

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


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

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


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

Expand Down Expand Up @@ -1389,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#lineNumber202">source</a></span></span>
<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber204">source</a></span></span>
</span>
</h3>

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


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

Expand Down Expand Up @@ -1551,7 +1563,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#lineNumber263">source</a></span></span>
<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber265">source</a></span></span>
</span>
</h3>

Expand Down Expand Up @@ -1616,7 +1628,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#lineNumber250">source</a></span></span>
<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber252">source</a></span></span>
</span>
</h3>

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


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

Expand Down Expand Up @@ -1768,7 +1780,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#lineNumber236">source</a></span></span>
<span data-ice="source"><span><a href="file/src/entity.js.html#lineNumber238">source</a></span></span>
</span>
</h3>

Expand Down
61 changes: 32 additions & 29 deletions docs/class/src/world.js~World.html
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ <h1 data-ice="name">World</h1>



<span class="code" data-ice="name"><span><a href="class/src/world.js~World.html#instance-method-system">system</a></span></span><span class="code" data-ice="signature">(components: <span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array">Array</a></span>, systemClass: <span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function">Function</a></span>, args: ...<span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a></span>): <span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a></span></span>
<span class="code" data-ice="name"><span><a href="class/src/world.js~World.html#instance-method-system">system</a></span></span><span class="code" data-ice="signature">(args: ...<span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a></span>): <span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a></span></span>
</p>
</div>
<div>
Expand Down Expand Up @@ -686,7 +686,7 @@ <h3 data-ice="anchor" id="instance-method-every">
<span class="right-info">


<span data-ice="source"><span><a href="file/src/world.js.html#lineNumber230">source</a></span></span>
<span data-ice="source"><span><a href="file/src/world.js.html#lineNumber259">source</a></span></span>
</span>
</h3>

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


<span data-ice="source"><span><a href="file/src/world.js.html#lineNumber267">source</a></span></span>
<span data-ice="source"><span><a href="file/src/world.js.html#lineNumber296">source</a></span></span>
</span>
</h3>

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


<span data-ice="source"><span><a href="file/src/world.js.html#lineNumber171">source</a></span></span>
<span data-ice="source"><span><a href="file/src/world.js.html#lineNumber200">source</a></span></span>
</span>
</h3>

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


<span data-ice="source"><span><a href="file/src/world.js.html#lineNumber287">source</a></span></span>
<span data-ice="source"><span><a href="file/src/world.js.html#lineNumber316">source</a></span></span>
</span>
</h3>

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


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

Expand Down Expand Up @@ -1079,8 +1079,8 @@ <h4>Example:</h4>

<pre class="prettyprint source-code"><code data-ice="exampleCode">// Example flow of method call order:
// Setup systems:
world.system([], systemA)
world.system([], systemB)
world.system(systemA)
world.system(systemB)
// During world.run():
// systemA.pre()
// systemA.every() * number of entities
Expand All @@ -1104,11 +1104,11 @@ <h3 data-ice="anchor" id="instance-method-system">



<span class="code" data-ice="name">system</span><span class="code" data-ice="signature">(components: <span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array">Array</a></span>, systemClass: <span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function">Function</a></span>, args: ...<span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a></span>): <span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a></span></span>
<span class="code" data-ice="name">system</span><span class="code" data-ice="signature">(args: ...<span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a></span>): <span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a></span></span>
<span class="right-info">


<span data-ice="source"><span><a href="file/src/world.js.html#lineNumber150">source</a></span></span>
<span data-ice="source"><span><a href="file/src/world.js.html#lineNumber167">source</a></span></span>
</span>
</h3>

Expand All @@ -1130,26 +1130,13 @@ <h4 data-ice="title">Params:</h4>
<tbody>

<tr data-ice="property" data-depth="0">
<td data-ice="name" class="code" data-depth="0">components</td>
<td data-ice="type" class="code"><span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array">Array</a></span></td>
<td data-ice="appendix"></td>
<td data-ice="description"><p>The list of components the system will process in every(). This follows the same logic as entity.has() and world.every().</p>
</td>
</tr>
<tr data-ice="property" data-depth="0">
<td data-ice="name" class="code" data-depth="0">systemClass</td>
<td data-ice="type" class="code"><span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function">Function</a></span></td>
<td data-ice="appendix"></td>
<td data-ice="description"><p>The system class to instantiate. Can contain the following methods: constructor, initialize,
pre, every, post. Pre() and post() get called before and after every(), for each of the independent systems. See world.run()
for an example of the call order.</p>
</td>
</tr>
<tr data-ice="property" data-depth="0">
<td data-ice="name" class="code" data-depth="0">args</td>
<td data-ice="type" class="code">...<span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a></span></td>
<td data-ice="appendix"><ul><li>optional</li></ul></td>
<td data-ice="description"><p>The arguments to forward to the system&apos;s constructors</p>
<td data-ice="appendix"></td>
<td data-ice="description"><p>Both signatures are accepted: (components, systemClass, ...args) or (systemClass, ...args).</p>
<p><strong>[components]</strong>: The list of components the system will process in every(). This follows the same logic as entity.has() and world.every().</p>
<p><strong>{systemClass}</strong>: The system class to instantiate. Can contain the following methods: constructor, initialize, pre, every, post. Pre() and post() get called before and after every(), for each of the independent systems. See world.run() for an example of the call order.</p>
<p><strong>[...args]</strong>: The arguments to forward to the system&apos;s constructors.</p>
</td>
</tr>
</tbody>
Expand Down Expand Up @@ -1187,11 +1174,27 @@ <h4>Example:</h4>

<pre class="prettyprint source-code"><code data-ice="exampleCode">// Movement system
world.system([&apos;position&apos;, &apos;velocity&apos;], class {
constructor(context) {
// This is showing how you can optionally pass parameters to the system&apos;s constructor
this.context = context
}
every(position, velocity, entity) {
position.x += velocity.x
position.y += velocity.y
}
})</code></pre>
}, context)</code></pre>
</div>
<div class="example-doc" data-ice="exampleDoc">

<pre class="prettyprint source-code"><code data-ice="exampleCode">// System that doesn&apos;t use every()
world.system(class {
constructor(context) {
this.context = context
}
pre() {
// Handle events or something
}
}, context)</code></pre>
</div>
</div>

Expand Down
2 changes: 2 additions & 0 deletions docs/file/src/entity.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
* ID is read-only, attempting to set it will throw an error.
*
* @private
*
* @throws {Error} Cannot set entity id
*/
set id(id) {
throw new Error(&apos;Cannot set entity id&apos;)
Expand Down
51 changes: 40 additions & 11 deletions docs/file/src/world.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -171,25 +171,54 @@
* @example
* // Movement system
* world.system([&apos;position&apos;, &apos;velocity&apos;], class {
* constructor(context) {
* // This is showing how you can optionally pass parameters to the system&apos;s constructor
* this.context = context
* }
* every(position, velocity, entity) {
* position.x += velocity.x
* position.y += velocity.y
* }
* })
* }, context)
*
* @example
* // System that doesn&apos;t use every()
* world.system(class {
* constructor(context) {
* this.context = context
* }
* pre() {
* // Handle events or something
* }
* }, context)
*
* @param {Array} components - The list of components the system will process in every(). This follows the same logic as entity.has() and world.every().
* @param {Function} systemClass - The system class to instantiate. Can contain the following methods: constructor, initialize,
* pre, every, post. Pre() and post() get called before and after every(), for each of the independent systems. See world.run()
* for an example of the call order.
* @param {...Object} [args] - The arguments to forward to the system&apos;s constructors
* @param {...Object} args - Both signatures are accepted: (components, systemClass, ...args) or (systemClass, ...args).
*
* **[components]**: The list of components the system will process in every(). This follows the same logic as entity.has() and world.every().
*
* **{systemClass}**: The system class to instantiate. Can contain the following methods: constructor, initialize, pre, every, post. Pre() and post() get called before and after every(), for each of the independent systems. See world.run() for an example of the call order.
*
* **[...args]**: The arguments to forward to the system&apos;s constructors.
*
* @return {number} Unique ID of the system on success or undefined on failure
*/
system(components, systemClass, ...args) {
// TODO: Make components optional, and parameters more dynamic by only using ...args
system(...args) {
// Get components and systemClass from arguments
let components = []
let systemClass, rest
if (Array.isArray(args[0])) {
components = args[0]
systemClass = args[1]
rest = args.slice(2)
} else {
systemClass = args[0]
rest = args.slice(1)
}

// Make sure the system is valid
if (isFunction(systemClass)) {
// Create the system, and set the component array query
let newSystem = new systemClass(...args)
let newSystem = new systemClass(...rest)
newSystem.components = components

// Add the system, return its ID
Expand Down Expand Up @@ -221,8 +250,8 @@
* @example
* // Example flow of method call order:
* // Setup systems:
* world.system([], systemA)
* world.system([], systemB)
* world.system(systemA)
* world.system(systemB)
* // During world.run():
* // systemA.pre()
* // systemA.every() * number of entities
Expand Down
Loading

0 comments on commit 74ab931

Please sign in to comment.