I clone the ruby elevator simulator in javascript using async and await for scheduling rather than an explicit event queue and clock.
See Elevator Simulator Revisited for ruby version.
Here elevator E takes person P to work W.
//code.fed.wiki.org/assets/page/async-elevator-simulator/index.html HEIGHT 80
// E L E V A T O R var floor = 1 var going = +1 var riding = false async function elevator () { while (true) { if (floor+going >= 1 && floor+going <= 30) { floor += going if (riding) at = floor await delay(2) } else { going = -going await delay(5) } } } // P A S S E N G E R var at = 2 var want = 5 async function passenger () { const heading = () => want > at ? +1 : -1 while (true) { while (at != floor || heading() != going) { await delay(1) // waiting } while (riding = (at != want)) { await delay(1) // riding } do { await delay(60) // working want = 1 + Math.floor(Math.random()*30) } while (want == at) } }
Programming seemed rather flat to me before I was introduced to Don Knuth's elevator simulator. A simulator has a scheduler and a model. The scheduler is a nerdy little thing that feels kinda like an operating system. But the model, well, it can be anything. wiki
Don describes an elevator simulator as an example application of a doubly-linked list in the Art of Computer Programming, Volume I. google