fix replay after opening a game

This commit is contained in:
Zutatensuppe 2021-06-05 08:58:20 +02:00
parent 98acc0dcf6
commit 95a06972c7
6 changed files with 61 additions and 12 deletions

File diff suppressed because one or more lines are too long

View file

@ -4,7 +4,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>🧩 jigsaw.hyottoko.club</title> <title>🧩 jigsaw.hyottoko.club</title>
<script type="module" crossorigin src="/assets/index.ee8c98e9.js"></script> <script type="module" crossorigin src="/assets/index.ea488403.js"></script>
<link rel="modulepreload" href="/assets/vendor.684f7bc8.js"> <link rel="modulepreload" href="/assets/vendor.684f7bc8.js">
<link rel="stylesheet" href="/assets/index.6748df9f.css"> <link rel="stylesheet" href="/assets/index.6748df9f.css">
</head> </head>

View file

@ -1,11 +1,11 @@
"use strict" "use strict"
import {run} from './gameloop' import { GameLoopInstance, run } from './gameloop'
import Camera from './Camera' import Camera from './Camera'
import Graphics from './Graphics' import Graphics from './Graphics'
import Debug from './Debug' import Debug from './Debug'
import Communication from './Communication' import Communication from './Communication'
import Util from './../common/Util' import Util, { logger } from './../common/Util'
import PuzzleGraphics from './PuzzleGraphics' import PuzzleGraphics from './PuzzleGraphics'
import Game from './../common/GameCommon' import Game from './../common/GameCommon'
import fireworksController from './Fireworks' import fireworksController from './Fireworks'
@ -29,6 +29,8 @@ declare global {
} }
} }
const log = logger('game.ts')
// @ts-ignore // @ts-ignore
const images = import.meta.globEager('./*.png') const images = import.meta.globEager('./*.png')
@ -350,11 +352,20 @@ export async function main(
} }
const gameObject: GameType = Util.decodeGame(replay.game) const gameObject: GameType = Util.decodeGame(replay.game)
Game.setGame(gameObject.id, gameObject) Game.setGame(gameObject.id, gameObject)
REPLAY.requesting = false REPLAY.requesting = false
REPLAY.log = replay.log REPLAY.log = replay.log
REPLAY.lastRealTs = Time.timestamp() REPLAY.lastRealTs = Time.timestamp()
REPLAY.gameStartTs = parseInt(REPLAY.log[0][4], 10) REPLAY.gameStartTs = parseInt(REPLAY.log[0][4], 10)
REPLAY.lastGameTs = REPLAY.gameStartTs REPLAY.lastGameTs = REPLAY.gameStartTs
REPLAY.final = false
REPLAY.logPointer = 0
REPLAY.speeds = [0.5, 1, 2, 5, 10, 20, 50, 100, 250, 500]
REPLAY.speedIdx = 1
REPLAY.paused = false
REPLAY.dataOffset = 0
REPLAY.dataSize = 10000
TIME = () => REPLAY.lastGameTs TIME = () => REPLAY.lastGameTs
} else { } else {
throw '[ 2020-12-22 MODE invalid, must be play|replay ]' throw '[ 2020-12-22 MODE invalid, must be play|replay ]'
@ -491,8 +502,25 @@ export async function main(
doSetSpeedStatus() doSetSpeedStatus()
} }
const intervals: NodeJS.Timeout[] = []
const clearIntervals = () => {
intervals.forEach(inter => {
clearInterval(inter)
})
}
let gameLoopInstance: GameLoopInstance
const unload = () => {
clearIntervals()
if (gameLoopInstance) {
gameLoopInstance.stop()
}
}
if (MODE === MODE_PLAY) { if (MODE === MODE_PLAY) {
setInterval(updateTimerElements, 1000) intervals.push(setInterval(() => {
updateTimerElements()
}, 1000))
} else if (MODE === MODE_REPLAY) { } else if (MODE === MODE_REPLAY) {
doSetSpeedStatus() doSetSpeedStatus()
} }
@ -528,7 +556,7 @@ export async function main(
} else if (MODE === MODE_REPLAY) { } else if (MODE === MODE_REPLAY) {
// no external communication for replay mode, // no external communication for replay mode,
// only the REPLAY.log is relevant // only the REPLAY.log is relevant
const inter = setInterval(() => { intervals.push(setInterval(() => {
const realTs = Time.timestamp() const realTs = Time.timestamp()
if (REPLAY.requesting) { if (REPLAY.requesting) {
REPLAY.lastRealTs = realTs REPLAY.lastRealTs = realTs
@ -555,7 +583,7 @@ export async function main(
const nextIdx = REPLAY.logPointer + 1 const nextIdx = REPLAY.logPointer + 1
if (nextIdx >= REPLAY.log.length) { if (nextIdx >= REPLAY.log.length) {
if (REPLAY.final) { if (REPLAY.final) {
clearInterval(inter) clearIntervals()
} }
break break
} }
@ -592,7 +620,7 @@ export async function main(
REPLAY.lastRealTs = realTs REPLAY.lastRealTs = realTs
REPLAY.lastGameTs = maxGameTs REPLAY.lastGameTs = maxGameTs
updateTimerElements() updateTimerElements()
}, 50) }, 50))
} }
let _last_mouse_down: Point|null = null let _last_mouse_down: Point|null = null
@ -808,7 +836,7 @@ export async function main(
RERENDER = false RERENDER = false
} }
run({ gameLoopInstance = run({
update: onUpdate, update: onUpdate,
render: onRender, render: onRender,
}) })
@ -844,5 +872,6 @@ export async function main(
}, },
disconnect: Communication.disconnect, disconnect: Communication.disconnect,
connect: connect, connect: connect,
unload: unload,
} }
} }

View file

@ -6,7 +6,17 @@ interface GameLoopOptions {
update: (step: number) => void update: (step: number) => void
render: (passed: number) => void render: (passed: number) => void
} }
export const run = (options: GameLoopOptions): void => {
export interface GameLoopInstance {
stop: () => void
}
export const run = (options: GameLoopOptions): GameLoopInstance => {
let stopped = false
const stop = () => {
stopped = true
}
const fps = options.fps || 60 const fps = options.fps || 60
const slow = options.slow || 1 const slow = options.slow || 1
const update = options.update const update = options.update
@ -28,10 +38,15 @@ export const run = (options: GameLoopOptions): void => {
} }
render(dt / slow) render(dt / slow)
last = now last = now
if (!stopped) {
raf(frame) raf(frame)
} }
}
raf(frame) raf(frame)
return {
stop,
}
} }
export default { export default {

View file

@ -78,8 +78,9 @@ export default defineComponent({
onColorChange: (v: string) => {}, onColorChange: (v: string) => {},
onNameChange: (v: string) => {}, onNameChange: (v: string) => {},
onSoundsEnabledChange: (v: boolean) => {}, onSoundsEnabledChange: (v: boolean) => {},
disconnect: () => {},
connect: () => {}, connect: () => {},
disconnect: () => {},
unload: () => {},
}, },
} }
}, },
@ -121,6 +122,7 @@ export default defineComponent({
) )
}, },
unmounted () { unmounted () {
this.g.unload()
this.g.disconnect() this.g.disconnect()
}, },
methods: { methods: {

View file

@ -80,7 +80,9 @@ export default defineComponent({
replayOnSpeedUp: () => {}, replayOnSpeedUp: () => {},
replayOnSpeedDown: () => {}, replayOnSpeedDown: () => {},
replayOnPauseToggle: () => {}, replayOnPauseToggle: () => {},
connect: () => {},
disconnect: () => {}, disconnect: () => {},
unload: () => {},
}, },
replay: { replay: {
@ -129,6 +131,7 @@ export default defineComponent({
) )
}, },
unmounted () { unmounted () {
this.g.unload()
this.g.disconnect() this.g.disconnect()
}, },
methods: { methods: {