Support running the game at ludicrous speed

This commit is contained in:
Eevee (Evelyn Woods) 2020-12-11 21:31:07 -07:00
parent 0f0c7437a6
commit fec09c03ba
2 changed files with 26 additions and 13 deletions

View File

@ -205,14 +205,16 @@
</div>
<p>Game speed:
<select name="speed">
<option value="6">6× — 120 tics per second</option>
<option value="3">3× — 60 tics per second</option>
<option value="2">2× — 40 tics per second</option>
<option value="3/2">× — 30 tics per second</option>
<option value="1" selected>1× — 20 tics per second</option>
<option value="1/2">½× — 10 tics per second</option>
<option value="1/3">⅓× — 6⅔ tics per second</option>
<option value="1/4">¼× — 5 tics per second</option>
<option value="20">20× faster</option>
<option value="10">10× faster</option>
<option value="5">5× faster</option>
<option value="3">3× faster</option>
<option value="2">2× faster</option>
<option value="3/2">× faster</option>
<option value="1" selected>Normal speed</option>
<option value="1/2">2× slower</option>
<option value="1/3">3× slower</option>
<option value="1/4">4× slower</option>
</select>
</p>

View File

@ -1049,13 +1049,27 @@ class Player extends PrimaryView {
this.last_advance = performance.now();
// If the game is running faster than normal, we cap the timeout between game loops at 10ms
// and do multiple loops at a time
// (Note that this is a debug feature so precision is not a huge deal and I don't bother
// tracking fractional updates, but asking to run at 10× and only getting 2× would suck)
let num_advances = 1;
let dt = 1000 / (TICS_PER_SECOND * this.play_speed);
if (dt < 10) {
num_advances = Math.ceil(10 / dt);
dt = 10;
console.log(this.play_speed, dt, num_advances);
}
if (this.state === 'playing') {
this.advance_by(1);
this.advance_by(num_advances);
}
else if (this.state === 'rewinding') {
if (this.level.has_undo()) {
// Rewind by undoing one tic every tic
this.undo();
for (let i = 0; i < num_advances; i++) {
this.undo();
}
this.update_ui();
}
// If there are no undo entries left, freeze in place until the player stops rewinding,
@ -1064,9 +1078,6 @@ class Player extends PrimaryView {
// buffer dry) and change to 'waiting' instead?
}
// FIXME if play speed is sufficiently high, we need to advance multiple frames at once or
// the timeout will get capped
let dt = 1000 / (TICS_PER_SECOND * this.play_speed);
if (this.state === 'rewinding') {
// Rewind faster than normal time
dt *= 0.5;