From: Ben Beltran Date: Mon, 9 Mar 2020 18:22:08 +0000 (-0500) Subject: Rewrite using modules X-Git-Tag: 3.0.0^2~7 X-Git-Url: https://git.r.bdr.sh/rbdr/serpentity/commitdiff_plain/19e91cddd138946a6e91aff6ec887ecf1461a803?ds=sidebyside;hp=724fec20a20bc50dec32cb0f3c461140daf51c05 Rewrite using modules --- diff --git a/lib/serpentity.js b/lib/serpentity.js index 60fb23c..ed233ba 100644 --- a/lib/serpentity.js +++ b/lib/serpentity.js @@ -1,11 +1,17 @@ -'use strict'; +// Export submodules +export { Component } from './serpentity/component.js'; +export { Entity } from './serpentity/entity.js'; +export { Node } from './serpentity/node.js'; +export { System } from './serpentity/system.js'; + +import { NodeCollection } from './serpentity/node_collection.js'; /* Serpentity is a simple entity framework inspired by Ash. Usage: - const Serpentity = require('serpentity'); + import Serpentity from '@serpentity/serpentity'; ## Instantiating an engine @@ -30,7 +36,8 @@ Remove entities or systems: Entities are the basic object of Serpentity, and they do nothing. - const entity = new Serpentity.Entity(); + import { Entity } from '@serpentity/serpentity'; + const entity = new Entity(); All the behavior is added through components @@ -39,7 +46,8 @@ 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" - const PositionComponent = class PositionComponent extends Serpentity.Component { + import { Component } from '@serpentity/serpentity'; + const PositionComponent = class PositionComponent extends Component { constructor(config) { this.x = 0; @@ -61,7 +69,8 @@ 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. - const MovementNode = class MovementNode extends Serpentity.Node; + import { Node } from '@serpentity/serpentity'; + const MovementNode = class MovementNode extends Node; MovementNode.position = PositionComponent; MovementNode.motion = MotionComponent; @@ -74,7 +83,8 @@ that comply with that API Systems are called on every update, and they use components through nodes. - const TestSystem = class TestSystem extends Serpentity.System { + import { System } from '@serpentity/serpentity'; + const TestSystem = class TestSystem extends System { added(engine){ this.nodeList = engine.getNodes(MovementNode); @@ -98,7 +108,7 @@ Systems are called on every update, and they use components through nodes. Just run `engine.update(dt)` in your game loop :D */ -const Serpentity = class Serpentity { +export default class Serpentity { constructor(config) { @@ -171,6 +181,7 @@ const Serpentity = class Serpentity { if (this.entities.indexOf(entity) >= 0) { return false; } + this.entities.push(entity); for (const collection of this._nodeCollections) { @@ -212,8 +223,8 @@ const Serpentity = class Serpentity { return this._nodeCollections[position].nodes; } - const nodeCollection = new Serpentity.NodeCollection({ - type : nodeType + const nodeCollection = new NodeCollection({ + type: nodeType }); this._nodeCollectionKeys.push(nodeType); @@ -235,13 +246,4 @@ const Serpentity = class Serpentity { system.update(dt); } } -}; - -// Add namespaced objects. -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; +} diff --git a/lib/serpentity/component.js b/lib/serpentity/component.js index 707da7a..32ae01f 100644 --- a/lib/serpentity/component.js +++ b/lib/serpentity/component.js @@ -1,5 +1,3 @@ -'use strict'; - /* * Components store data. Nothing to say here really, just * inherit and add a prototype, or don't even inherit, see? @@ -7,11 +5,9 @@ * components can be any class whatsoever. */ -const Component = class Component { +export class Component { constructor(config) { Object.assign(this, config); } -}; - -module.exports = Component; +} diff --git a/lib/serpentity/entity.js b/lib/serpentity/entity.js index 2a4b03f..e84b0cc 100644 --- a/lib/serpentity/entity.js +++ b/lib/serpentity/entity.js @@ -1,11 +1,9 @@ -'use strict'; - /* * The entity gives the entity framework its name. It exists only * to hold components. */ -const Entity = class Entity { +export class Entity { constructor(config) { this._componentKeys = []; @@ -24,6 +22,7 @@ const Entity = class Entity { if (this._componentKeys.indexOf(component.constructor) >= 0) { return false; } + this._componentKeys.push(component.constructor); this._components.push(component); return true; @@ -37,6 +36,7 @@ const Entity = class Entity { if (this._componentKeys.indexOf(componentClass) >= 0) { return true; } + return false; } @@ -50,6 +50,4 @@ const Entity = class Entity { return this._components[position]; } } -}; - -module.exports = Entity; +} diff --git a/lib/serpentity/node.js b/lib/serpentity/node.js index 796eee5..629e4ed 100644 --- a/lib/serpentity/node.js +++ b/lib/serpentity/node.js @@ -1,10 +1,8 @@ -'use strict'; - /* * A node describes a set of components in order to describe entities * that include them. */ -const Node = class Node { +export class Node { /* * Returns true if the given entity matches the defined protocol, @@ -37,6 +35,4 @@ const Node = class Node { Object.assign(this, config); } -}; - -module.exports = Node; +} diff --git a/lib/serpentity/node_collection.js b/lib/serpentity/node_collection.js index c603d6f..e615020 100644 --- a/lib/serpentity/node_collection.js +++ b/lib/serpentity/node_collection.js @@ -1,6 +1,4 @@ -'use strict'; - -const Events = require('events'); +import Events from 'events'; /* * Node Collections contain nodes, in order to keep the lists of nodes @@ -10,7 +8,7 @@ const Events = require('events'); * instances of that class. */ -const NodeCollection = class NodeCollection extends Events { +export class NodeCollection extends Events { constructor(config) { @@ -93,7 +91,7 @@ const NodeCollection = class NodeCollection extends Events { return found; } -}; +} /* @@ -103,5 +101,3 @@ NodeCollection.prototype[Symbol.iterator] = function * () { yield* this.nodes; }; - -module.exports = NodeCollection; diff --git a/lib/serpentity/system.js b/lib/serpentity/system.js index 76bf306..91c019c 100644 --- a/lib/serpentity/system.js +++ b/lib/serpentity/system.js @@ -1,5 +1,3 @@ -'use strict'; - /* * Systems contain most of the logic, and work with nodes in order to * act and change their values. @@ -8,7 +6,7 @@ * three methods. They are shown here to document the interface. */ -const System = class System { +export class System { /* * This will be run when the system is added to the engine @@ -33,6 +31,4 @@ const System = class System { // Override with update(dt) // Receives a delta of the time } -}; - -module.exports = System; +}