Skip to content

Commit

Permalink
Update ecs.js
Browse files Browse the repository at this point in the history
  • Loading branch information
babak-karimi-asl authored Sep 1, 2020
1 parent 275e99b commit 4fde9b3
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions ecs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

const clone = a=>JSON.parse(JSON.stringify(a))
const ecs = {typeBuilders:{},UUID:1,systemsForType:{},objectsById:{},objects:[],objectsByType:{}}
const ecs = {typeBuilders:{},UUID:1,systemsForType:{},systems:[],objectsById:{},objects:[],objectsByType:{}}
ecs.addType = function(type,structure){
if(type in this.typeBuilders){
throw `CREATE TYPE ERROR: type "${type}" already exists`
Expand Down Expand Up @@ -37,9 +37,12 @@ ecs.addSystem = function(inputType,fnc){
throw `CREATE OBJECT ERROR: type '${inputType}' does not exist`
return 0
}
/*
if(!this.systemsForType[inputType])
this.systemsForType[inputType]=[]
this.systemsForType[inputType].push(fnc)
*/
this.systems.push({inputType,fnc})
}

ecs.createObject = function(type,init={}){
Expand All @@ -50,6 +53,21 @@ ecs.createObject = function(type,init={}){
return this.typeBuilders[type](init)
}

ecs.getObjectById(id){
let obj = this.objectsById[id]
if(!obj){
throw `object of type "${type}" with id "${id}" does not exist`
return 0
}
/*
if(obj._type!==type){
throw `object with id "${id}" is not of type "${type}" , it is a "${obj._type}"`
return 0
}
*/
return obj
}

ecs.clearObjects =function(){
this.objects=[]
this.objectsById={}
Expand Down Expand Up @@ -80,15 +98,20 @@ ecs.deleteObject=function(object){
}

ecs.tickSystems = function(){
/*
for(let k in this.systemsForType)
for(let s of this.systemsForType[k])
for(let o of this.objectsByType[k])
s(this,o)
*/
for(let s of this.systems)
for(let o of this.objectsByType[s.inputType])
s.fnc(this,o)
this.refreshObjects()
}

//////////////////////////////////

/*
ecs.addType('player',{name:'player',health:0,damage:0})
ecs.addType('card',{place:'hands',size:'small'})
Expand Down Expand Up @@ -122,4 +145,8 @@ console.log(ecs)
ecs.refreshObjects()
console.log(ecs)
*/

//exports {ecs}
module.exports = ecs

0 comments on commit 4fde9b3

Please sign in to comment.