diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2022-11-11 12:05:10 +0100 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2022-11-11 12:05:10 +0100 |
commit | 347ec6972f34bb95564f92c36830b4e1956bfcfa (patch) | |
tree | f4792dc645ad7994d1b3354d010356df92d6a148 /web/pw-visualizer/src | |
parent | 949844b957055f2a45438db68c638ffbe218acda (diff) | |
download | planetwars.dev-347ec6972f34bb95564f92c36830b4e1956bfcfa.tar.xz planetwars.dev-347ec6972f34bb95564f92c36830b4e1956bfcfa.zip |
visualizer: ensure that turn number remains within bounds
Diffstat (limited to 'web/pw-visualizer/src')
-rw-r--r-- | web/pw-visualizer/src/index.ts | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/web/pw-visualizer/src/index.ts b/web/pw-visualizer/src/index.ts index f97bd2b..74b2319 100644 --- a/web/pw-visualizer/src/index.ts +++ b/web/pw-visualizer/src/index.ts @@ -51,6 +51,16 @@ export function set_loading(loading: boolean) { } } +function clamp(min: number, max: number, value: number): number { + if (value < min) { + return min; + } + if (value > max) { + return max; + } + return value; +} + const ELEMENTS: any = {}; var CANVAS: any; var RESOLUTION: any; @@ -561,7 +571,7 @@ export class GameInstance { } updateTurn(turn: number) { - this.frame = Math.max(0, turn); + this.frame = clamp(0, this.turn_count-1, turn); this.game.update_turn(this.frame); this._update_state(); this.updateTurnCounters(); |