diff options
| author | Rubén Beltrán del Río <ben@nsovocal.com> | 2017-04-12 00:18:35 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-04-12 00:18:35 -0500 |
| commit | b3b840f89948d96aa28760fc5cf41c384488f6a6 (patch) | |
| tree | e6741096f29440a343323a5d7542f5caf1aceacb /lib | |
| parent | d071276e2426c4180e44debe8b92ad16cbbc414f (diff) | |
Feature/code update (#1)
* Updates code for new eslint config
* Remove gulp and babel files
* Remove dist
* Remove dist
* Add contributing file
* Fix references to collections / tests
* Convert tests to lab
* Add travis file
* Add missing line
* Test CI on node 6
* Removes browser test
* Properly includes local serpentity in test
* Rename to use orgs in npm
* Add changelog
* Add support files / removed dist to changelog
* Add yarn.lock file
* Iterate using object.keys instead of values
* Correct typo in typeName matching
* Add webpack to generate the dist files
* Add webpack config
* Add dist files
* Bump version
* Link to ash
* Updates CHANGELOG
* Add CI badge
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/serpentity.js | 101 | ||||
| -rw-r--r-- | lib/serpentity/component.js | 15 | ||||
| -rw-r--r-- | lib/serpentity/entity.js | 26 | ||||
| -rw-r--r-- | lib/serpentity/node.js | 38 | ||||
| -rw-r--r-- | lib/serpentity/node_collection.js | 51 | ||||
| -rw-r--r-- | lib/serpentity/system.js | 16 |
6 files changed, 113 insertions, 134 deletions
diff --git a/lib/serpentity.js b/lib/serpentity.js index 1360f92..0d932ac 100644 --- a/lib/serpentity.js +++ b/lib/serpentity.js @@ -5,11 +5,11 @@ Serpentity is a simple entity framework inspired by Ash. Usage: - let Serpentity = require('serpentity'); + const Serpentity = require('serpentity'); ## Instantiating an engine - let engine = Serpentity(); + const engine = new Serpentity(); Add entities or systems, systems are added with a priority (the smaller the number, the earlier it will be called): @@ -30,7 +30,7 @@ Remove entities or systems: Entities are the basic object of Serpentity, and they do nothing. - let entity = new Serpentity.Entity(); + const entity = new Serpentity.Entity(); All the behavior is added through components @@ -39,8 +39,9 @@ All the behavior is added through components Components define data that we can add to an entity. This data will eventually be consumed by "Systems" - let PositionComponent = class PositionComponent extends Serpentity.Component { - constructor (config) { + const PositionComponent = class PositionComponent extends Serpentity.Component { + constructor(config) { + this.x = 0; this.y = 0; @@ -60,7 +61,7 @@ Systems can refer to entities by requesting nodes. Nodes are sets of components that you define, so your system can require entities that always follow the API defined in the node. - let MovementNode = class MovementNode extends Serpentity.Node; + const MovementNode = class MovementNode extends Serpentity.Node; MovementNode.position = PositionComponent; MovementNode.motion = MotionComponent; @@ -73,16 +74,20 @@ that comply with that API Systems are called on every update, and they use components through nodes. - let TestSystem = class TestSystem extends Serpentity.System { - added (engine){ + const TestSystem = class TestSystem extends Serpentity.System { + added(engine){ + this.nodeList = engine.getNodes(MovementNode); - }, - removed (engine){ + } + + removed(engine){ + this.nodeList = undefined; } - update (dt){ - let node; - for (node of this.nodeList) { + + update(dt){ + + for (const node of this.nodeList) { console.log(`Current position is: ${node.position.x},${node.position.y}`); } } @@ -93,15 +98,16 @@ Systems are called on every update, and they use components through nodes. Just run `engine.update(dt)` in your game loop :D */ -let Serpentity = class Serpentity { +const Serpentity = class Serpentity { + + constructor(config) { - constructor (config) { this.systems = []; this.entities = []; this._nodeCollections = []; this._nodeCollectionKeys = []; - Object.assign(this, config || {}); + Object.assign(this, config); } /* @@ -110,8 +116,7 @@ let Serpentity = class Serpentity { * * returns true if added succesfully, false if already added */ - addSystem (system, priority) { - let lastIndex, found; + addSystem(system, priority) { if (this.systems.indexOf(system) >= 0) { return false; @@ -119,13 +124,12 @@ let Serpentity = class Serpentity { system.priority = priority; - found = false; - lastIndex = 0; + let lastIndex = 0; + + const found = this.systems.some((existingSystem, i) => { - this.systems.some(function findPriority(existingSystem, i) { lastIndex = i; if (existingSystem.priority >= system.priority) { - found = true; return true; } }); @@ -145,10 +149,9 @@ let Serpentity = class Serpentity { * * returns true if removed succesfully, false if already added */ - removeSystem (system) { - let position; + removeSystem(system) { - position = this.systems.indexOf(system); + const position = this.systems.indexOf(system); if (position >= 0) { this.systems[position].removed(this); this.systems.splice(position, 1); @@ -163,15 +166,16 @@ let Serpentity = class Serpentity { * * returns true if added, false if already there */ - addEntity (entity) { + addEntity(entity) { + if (this.entities.indexOf(entity) >= 0) { return false; } this.entities.push(entity); - this._nodeCollections.forEach(function (collection) { + for (const collection of this._nodeCollections) { collection.add(entity); - }); + } return true; } @@ -181,14 +185,13 @@ let Serpentity = class Serpentity { * * returns true if removed, false if not present */ - removeEntity (entity) { - let position; + removeEntity(entity) { - position = this.entities.indexOf(entity); + const position = this.entities.indexOf(entity); if (position >= 0) { - this._nodeCollections.forEach(function (collection) { + for (const collection of this._nodeCollections) { collection.remove(entity); - }); + } this.entities.splice(position, 1); return true; @@ -201,25 +204,24 @@ let Serpentity = class Serpentity { * Given a Node Class, retrieves a list of all the nodes for each * applicable entity. */ - getNodes (nodeType) { - let position, nodeCollection; + getNodes(nodeType) { - position = this._nodeCollectionKeys.indexOf(nodeType); + const position = this._nodeCollectionKeys.indexOf(nodeType); if (position >= 0) { return this._nodeCollections[position].nodes; } - nodeCollection = new Serpentity.NodeCollection({ + const nodeCollection = new Serpentity.NodeCollection({ type : nodeType }); this._nodeCollectionKeys.push(nodeType); this._nodeCollections.push(nodeCollection); - this.entities.forEach(function (entity) { + for (const entity of this.entities) { nodeCollection.add(entity); - }); + } return nodeCollection.nodes; } @@ -227,20 +229,19 @@ let Serpentity = class Serpentity { /* * Calls update for every loaded system. */ - update (dt) { - this.systems.forEach(function (system) { + update(dt) { + + for (const system of this.systems) { system.update(dt); - }); + } } }; // Add namespaced objects. -if (typeof module !== 'undefined' && this.module !== module) { - Serpentity.Component = require('./serpentity/component.js'); - Serpentity.Entity = require('./serpentity/entity.js'); - Serpentity.Node = require('./serpentity/node.js'); - Serpentity.NodeCollection = require('./serpentity/node_collection.js'); - Serpentity.System = require('./serpentity/system.js'); +Serpentity.Component = require('./serpentity/component.js'); +Serpentity.Entity = require('./serpentity/entity.js'); +Serpentity.Node = require('./serpentity/node.js'); +Serpentity.NodeCollection = require('./serpentity/node_collection.js'); +Serpentity.System = require('./serpentity/system.js'); - module.exports = Serpentity; -} +module.exports = Serpentity; diff --git a/lib/serpentity/component.js b/lib/serpentity/component.js index e9abcd1..707da7a 100644 --- a/lib/serpentity/component.js +++ b/lib/serpentity/component.js @@ -1,7 +1,5 @@ 'use strict'; -/* global Serpentity */ - /* * Components store data. Nothing to say here really, just * inherit and add a prototype, or don't even inherit, see? @@ -9,14 +7,11 @@ * components can be any class whatsoever. */ -let Component = class Component { - constructor (config) { - Object.assign(this, config || {}); +const Component = class Component { + constructor(config) { + + Object.assign(this, config); } }; -if (typeof module !== 'undefined' && this.module !== module) { - module.exports = Component; -} else { - Serpentity.Component = Component; -} +module.exports = Component; diff --git a/lib/serpentity/entity.js b/lib/serpentity/entity.js index add823e..2a4b03f 100644 --- a/lib/serpentity/entity.js +++ b/lib/serpentity/entity.js @@ -1,18 +1,17 @@ 'use strict'; -/* global Serpentity */ - /* * The entity gives the entity framework its name. It exists only * to hold components. */ -let Entity = class Entity { - constructor (config) { +const Entity = class Entity { + constructor(config) { + this._componentKeys = []; this._components = []; - Object.assign(this, config || {}); + Object.assign(this, config); } /* @@ -20,7 +19,8 @@ let Entity = class Entity { * * returns true if added, false if already present */ - addComponent (component) { + addComponent(component) { + if (this._componentKeys.indexOf(component.constructor) >= 0) { return false; } @@ -32,7 +32,8 @@ let Entity = class Entity { /* * returns true if component is included, false otherwise */ - hasComponent (componentClass) { + hasComponent(componentClass) { + if (this._componentKeys.indexOf(componentClass) >= 0) { return true; } @@ -42,16 +43,13 @@ let Entity = class Entity { /* * returns the component associated with that key */ - getComponent (componentClass) { - let position = this._componentKeys.indexOf(componentClass); + getComponent(componentClass) { + + const position = this._componentKeys.indexOf(componentClass); if (position >= 0) { return this._components[position]; } } }; -if (typeof module !== 'undefined' && this.module !== module) { - module.exports = Entity; -} else { - Serpentity.Entity = Entity; -} +module.exports = Entity; diff --git a/lib/serpentity/node.js b/lib/serpentity/node.js index fa0af75..796eee5 100644 --- a/lib/serpentity/node.js +++ b/lib/serpentity/node.js @@ -1,48 +1,42 @@ 'use strict'; -/* global Serpentity */ - /* * A node describes a set of components in order to describe entities * that include them. */ -let Node = class Node { +const Node = class Node { /* * Returns true if the given entity matches the defined protocol, * false otherwise */ - static matches (entity) { - let types = this.types; + static matches(entity) { + + const typeNames = Object.keys(this.types); - for (let typeName in types) { - if (types.hasOwnProperty(typeName)) { + for (const typeName of typeNames) { - let matched = false; - let type = types[typeName]; + const type = this.types[typeName]; + let matched = false; - if (entity.hasComponent(type)) { - matched = true; - } + if (entity.hasComponent(type)) { + matched = true; + } - if (!matched) { - return false; - } + if (!matched) { + return false; } } return true; } - constructor (config) { + constructor(config) { + this.types = {}; - Object.assign(this, config || {}); + Object.assign(this, config); } }; -if (typeof module !== 'undefined' && this.module !== module) { - module.exports = Node; -} else { - Serpentity.Node = Node; -} +module.exports = Node; diff --git a/lib/serpentity/node_collection.js b/lib/serpentity/node_collection.js index 7c51991..8da41da 100644 --- a/lib/serpentity/node_collection.js +++ b/lib/serpentity/node_collection.js @@ -1,7 +1,5 @@ 'use strict'; -/* global Serpentity */ - /* * Node Collections contain nodes, in order to keep the lists of nodes * that belong to each type. @@ -10,13 +8,14 @@ * instances of that class. */ -let NodeCollection = class NodeCollection { +const NodeCollection = class NodeCollection { + + constructor(config) { - constructor (config) { this.nodes = []; this.type = null; - Object.assign(this, config || {}); + Object.assign(this, config); } /* @@ -25,19 +24,18 @@ let NodeCollection = class NodeCollection { * * Returns true if added, false otherwise. */ - add (entity) { + add(entity) { if (this.type.matches(entity) && !this._entityExists(entity)) { - let node = new this.type({}); - let types = this.type.types; + const node = new this.type({}); + const types = this.type.types; + const typeNames = Object.keys(types); node.entity = entity; - for (let typeName in types) { - if (types.hasOwnProperty(typeName)) { - node[typeName] = entity.getComponent(types[typeName]); - } + for (const typeName of typeNames) { + node[typeName] = entity.getComponent(types[typeName]); } this.nodes.push(node); @@ -53,30 +51,33 @@ let NodeCollection = class NodeCollection { * * returns true if it was removed, false otherwise. */ - remove (entity) { - let found = -1; + remove(entity) { + + let foundIndex = -1; + + const found = this.nodes.some((node, i) => { - this.nodes.forEach(function (node, i) { if (node.entity === entity) { - found = i; + foundIndex = i; + return true; } }); - if (found >= 0) { - this.nodes.splice(found, 1); - return true; + if (found) { + this.nodes.splice(foundIndex, 1); } - return false; + return found; } /* * Checks whether we already have nodes for this entity. */ - _entityExists (entity) { + _entityExists(entity) { + let found = false; - for (let node of this.nodes) { + for (const node of this.nodes) { if (node.entity === entity) { found = true; } @@ -86,8 +87,4 @@ let NodeCollection = class NodeCollection { } }; -if (typeof module !== 'undefined' && this.module !== module) { - module.exports = NodeCollection; -} else { - Serpentity.NodeCollection = NodeCollection; -} +module.exports = NodeCollection; diff --git a/lib/serpentity/system.js b/lib/serpentity/system.js index 339f6e7..76bf306 100644 --- a/lib/serpentity/system.js +++ b/lib/serpentity/system.js @@ -1,7 +1,5 @@ 'use strict'; -/* global Serpentity */ - /* * Systems contain most of the logic, and work with nodes in order to * act and change their values. @@ -10,12 +8,12 @@ * three methods. They are shown here to document the interface. */ -let System = class System { +const System = class System { /* * This will be run when the system is added to the engine */ - added () { + added() { // Override with added(engine) // Receives an instance of the serpentity engine } @@ -23,7 +21,7 @@ let System = class System { /* * This will be run when the system is removed from the engine */ - removed () { + removed() { // Override with removed(engine) // Receives an instance of the serpentity engine } @@ -31,14 +29,10 @@ let System = class System { /* * This will run every time the engine's update method is called */ - update () { + update() { // Override with update(dt) // Receives a delta of the time } }; -if (typeof module !== 'undefined' && this.module !== module) { - module.exports = System; -} else { - Serpentity.System = System; -} +module.exports = System; |