Turn scheduling

In turn-based games, actors (player, enemies, everything that moves or acts) often differ by their speed. However, since the game happens in turns, the speed is more accurately described as a frequency in which turns happen for a particular actor. ROT.Scheduler is a simple tool designed to simplify the task of selecting proper actor for a turn, based on the abstract speed of all actors.

First, you need to fill the scheduler instance with actors. There are very few requirements on actors: they need to be JS objects with method getSpeed(), which returns a number. An actor with speed=100 will get twice as many turns as actor with speed=50.
You can then repeatedly call the scheduler's next() method: it returns the actor whose turn should start.

var scheduler = new ROT.Scheduler(); var getSpeed = function() { return this.speed; } /* generate four actors */ for (var i=0;i<4;i++) { var actor = { speed: ROT.RNG.getPercentage(), number: i+1, getSpeed: getSpeed } scheduler.add(actor); SHOW("Object #%s has speed %s.".format(actor.number, actor.speed)); } /* simulate several turns */ var turns = []; for (var i=0;i<40;i++) { var next = scheduler.next(); turns.push(next.number); } SHOW("\nGenerated order of turns:"); SHOW(turns.join(" ") + " ...");