-require("neon");
+if (typeof require !== "undefined") {
+ require("neon");
+}
/*
Serpentity is a simple entity framework inspired by Ash.
var engine = Serpentity();
-Add entities or systems:
+Add entities or systems, systems are added with a priority (the smaller
+the number, the earlier it will be called):
engine.addEntity(entityFactory());
- engine.addSystem(new GameSystem());
+ engine.addSystem(new GameSystem(), priority);
Update all systems:
You can add components to entities by using the add method:
- entity.add(new PositionComponent());
+ entity.addComponent(new PositionComponent());
Systems can refer to entities by requesting nodes.
Class("Serpentity")({
prototype : {
systems : null,
- nodeCollections : null,
entities : null,
+ _nodeCollections : null,
+ _nodeCollectionKeys : null,
init : function init(config) {
var property;
this.systems = [];
this.entities = [];
- this.nodeCollections = {};
+ this._nodeCollections = [];
+ this._nodeCollectionKeys = [];
for (property in config) {
if (config.hasOwnProperty(property)) {
*
* returns true if added succesfully, false if already added
*/
- addSystem : function addSystem(system) {
+ addSystem : function addSystem(system, priority) {
+ var lastIndex, found;
+
if (this.systems.indexOf(system) >= 0) {
return false;
}
- this.systems.push(system);
+
+ system.priority = priority;
+
+ found = false;
+ lastIndex = 0;
+
+ this.systems.some(function findPriority(existingSystem, i) {
+ lastIndex = i;
+ if (existingSystem.priority >= system.priority) {
+ found = true;
+ return true;
+ }
+ });
+
+ if (!found) {
+ lastIndex += 1
+ }
+
+ this.systems.splice(lastIndex, 0, system);
system.added(this);
return true;
},
* returns true if added, false if already there
*/
addEntity : function addEntity(entity) {
- var property;
-
if (this.entities.indexOf(entity) >= 0) {
return false;
}
this.entities.push(entity);
- for (property in this.nodeCollections) {
- if (this.nodeCollections.hasOwnProperty(property)) {
- this.nodeCollections[property].add(entity);
- }
- }
+ this._nodeCollections.forEach(function (collection) {
+ collection.add(entity);
+ });
+
return true;
},
position = this.entities.indexOf(entity);
if (position >= 0) {
- for (property in this.nodeCollections) {
- if (this.nodeCollections.hasOwnProperty(property)) {
- this.nodeCollections[property].remove(entity);
- }
- }
+ this._nodeCollections.forEach(function (collection) {
+ collection.remove(entity);
+ });
this.entities.splice(position, 1);
return true;
* applicable entity.
*/
getNodes : function getNodes(nodeType) {
- var nodeCollection;
+ var position, nodeCollection;
+
+ position = this._nodeCollectionKeys.indexOf(nodeType);
- if (this.nodeCollections.hasOwnProperty(nodeType)) {
- return this.nodeCollections[nodeType].nodes;
+ if (position >= 0) {
+ return this._nodeCollections[position].nodes;
}
nodeCollection = new Serpentity.NodeCollection({
type : nodeType,
});
- this.nodeCollections[nodeType] = nodeCollection;
+
+ this._nodeCollectionKeys.push(nodeType);
+ this._nodeCollections.push(nodeCollection);
this.entities.forEach(function (entity) {
nodeCollection.add(entity);
}
});
-require("./component.js");
-require("./entity.js");
-require("./node.js");
-require("./node_collection.js");
-require("./system.js");
+if (typeof require !== "undefined") {
+ require("./component.js");
+ require("./entity.js");
+ require("./node.js");
+ require("./node_collection.js");
+ require("./system.js");
+}