Today we’ll do some more streamlining.
We’ll start by adding a new function to the futureEventsQueue
object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
this.newItem = function(time,entity) { //create futureEventItem var feqItem = new futureEventItem(time,"absolute",entity); this.insertTime = feqItem.getActivationTime(); globalInsertTime = this.insertTime; if (this.feqSize == 0) { this.feq[0] = feqItem; this.feqSize++; console.log("Array size: "+this.feq.length); } else { //find index of feq item to insert before var insertIndex = this.feq.findIndex(this.findLaterTime); //insert the element if (insertIndex < 0) { insertIndex = this.feq.length; } this.feq.splice(insertIndex,0,feqItem); this.feqSize++; console.log("Array size: "+this.feq.length); } }; |
Then, instead of having to specify the “absolute” (vs. “advance”) flag in the initialization of a new entity
object, we know we’ll be inserting it into the future events queue at a specific time (which might, in fact, be the current time, but that is a different discussion). Look for the streamlined and specific newItem
call in place of the more general insertItem
call.
1 2 3 4 5 6 7 8 |
function entity(entityID,initialTime,incrementTime,endTime) { this.entityID = entityID; this.initialTime = initialTime; this.incrementTime = incrementTime; this.endTime = endTime; this.nextState = "increment"; //feq.insertItem(initialTime,"absolute",this); feq.newItem(initialTime,this); |
Again the output is unchanged so we’re still pretty sure we haven’t broken anything.