Here is an entity object that knows how to be created and activated from external calls. It updates itself and puts itself back into the future events queue is appropriate. This is in keeping with my original concept of how an entity would work. The key, of course, is to keep rewriting and simplifying this thing until more and more of the mechanics are hidden from the coder. Languages like SLX do this implicitly, but if we’re building a framework from scratch, given the limitations imposed by JavaScript, there’s only so far we’re going to be able to go with that. Still, it’ll be fun to see how far we get.
In the meantime the forms here mean I’ll have to streamline some of the existing code. Since the state information is all kept within the entity itself it no longer has to be kept with the future events queue elements (since those elements contain a reference to the entity), so those data structures and calls will be pruned.
I’ll make this all run tomorrow, but here’s the rough code for now:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
//entity item function entity(entityID,initialTime,incrementTime,endTime) { //initialize on creation var this.entityID = entityID; var this.initialTime = initialTime; var this.incrementTime = incrementTime; var this.endTime = endTime; var this.nextState = "increment"; //insert an event element into the FEQ //FEQ element has reference to this entity forever feq.insertItem(initalTime,"absolute",this); //an external call will pull the element from the FEQ //"item" will be a reference to the FEQ element so it can be put back in if needed this.activate = function(item) { if (this.nextState == "increment") { //do stuff based on state console.log("entity "+this.entityID+" updated at time "+globalSimClock); if (globalSimClock + this.incrementTime >= this.endTime) { //set item to termminate when appropriate this.nextState = "destroy"; } //if item survives it goes back into the FEQ item.update(this.incrementTime,"advance"); feq.insertExistingItem(item); } else if (this.nextState == "destroy") { //if it doesn't put a reference back into the FEQ... //...it will eventually be garbage collected console.log("entity "+this.entityID+" terminated at time "+globalSimClock) } else { //this, of course, shouldn't happen alert("entity "+this.entityID+" went into undefined state"); } }; //this.activate }; //entity |