Now that I’m sure the basics are working the way I’d like I want to see if I can streamline things some more.
The first thing to do is recognize that the next item being pulled off the future events queue can always be referred to the same way. If we make the item a global variable then we can always have code refer to it by a known name without having to pass the reference around all over the place. This cleans things up quite a bit.
We’ll start by creating a variable called feqCurrent
, which is intended to hold a futureEventItem
object.
1 |
var feqCurrent; //global feqItem |
Next, we’ll copy the insertExistingItem
method in the futureEventsQueue
object so it eliminates the need to pass in a reference to an feqItem
, because it refers to the global variable instead.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
this.insertCurrent = function() { this.insertTime = feqCurrent.getActivationTime(); globalInsertTime = this.insertTime; if (this.feqSize == 0) { this.feq[0] = feqCurrent; this.feqSize++; } 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,feqCurrent); this.feqSize++; } }; |
We’ll follow that up by streamlining the advance
function in the same way.
1 2 3 4 |
function advance(byTime) { feqCurrent.update(byTime,"advance"); feq.insertCurrent(); }; |
Then we’ll do the same to the activate
function in the entity
object.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
this.activate = function() { if (this.nextState == "increment") { displayProgressText("entity "+this.entityID+" updated at time "+globalSimClock+"<br />"); if (globalSimClock + this.incrementTime >= this.endTime) { this.nextState = "destroy"; } advance(this.incrementTime); } else if (this.nextState == "destroy") { displayProgressText("entity "+this.entityID+" terminated at time "+globalSimClock+"<br />"); } else { displayProgressText("entity "+this.entityID+" in undefined state at time "+globalSimClock+"<br />"); } }; //this.activate |
Finally, we’ll do the same thing to the main event processing loop.
1 2 3 4 5 6 7 8 9 10 11 12 |
var keepRunning = true; while (keepRunning) { var itemList = feq.getFirstItem(); if (itemList) { feqCurrent = itemList[0]; feqCurrent.entity.activate(); } else { keepRunning = false; } } //while (keepRunning) |
The output remains the same as before, so we assume we haven’t broken anything.