X-Git-Url: https://git.r.bdr.sh/rbdr/serpentity/blobdiff_plain/b3b840f89948d96aa28760fc5cf41c384488f6a6..3f00fec5fc38ff34000721903dd94c30ffc864f3:/lib/serpentity.js?ds=sidebyside diff --git a/lib/serpentity.js b/lib/serpentity.js index 0d932ac..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); @@ -223,7 +234,7 @@ const Serpentity = class Serpentity { nodeCollection.add(entity); } - return nodeCollection.nodes; + return nodeCollection; } /* @@ -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; +}