replay stuff encapsulated in one object
This commit is contained in:
parent
23e3ce4a1d
commit
6cecba5aa1
1 changed files with 46 additions and 43 deletions
89
game/game.js
89
game/game.js
|
|
@ -347,14 +347,16 @@ async function main() {
|
||||||
|
|
||||||
// stuff only available in replay mode...
|
// stuff only available in replay mode...
|
||||||
// TODO: refactor
|
// TODO: refactor
|
||||||
let GAME_LOG
|
const REPLAY = {
|
||||||
let GAME_LOG_IDX = 0
|
log: null,
|
||||||
let REPLAY_SPEEDS = [0.5, 1, 2, 5, 10, 20, 50]
|
logIdx: 0,
|
||||||
let REPLAY_SPEED_IDX = 1
|
speeds: [0.5, 1, 2, 5, 10, 20, 50],
|
||||||
let REPLAY_PAUSED = false
|
speedIdx: 1,
|
||||||
let lastRealTime = null
|
paused: false,
|
||||||
let lastGameTime = null
|
lastRealTs: null,
|
||||||
let GAME_START_TS = null
|
lastGameTs: null,
|
||||||
|
gameStartTs: null,
|
||||||
|
}
|
||||||
|
|
||||||
if (MODE === 'play') {
|
if (MODE === 'play') {
|
||||||
const game = await Communication.connect(gameId, CLIENT_ID)
|
const game = await Communication.connect(gameId, CLIENT_ID)
|
||||||
|
|
@ -362,11 +364,11 @@ async function main() {
|
||||||
} else if (MODE === 'replay') {
|
} else if (MODE === 'replay') {
|
||||||
const {game, log} = await Communication.connectReplay(gameId, CLIENT_ID)
|
const {game, log} = await Communication.connectReplay(gameId, CLIENT_ID)
|
||||||
Game.newGame(Util.decodeGame(game))
|
Game.newGame(Util.decodeGame(game))
|
||||||
GAME_LOG = log
|
REPLAY.log = log
|
||||||
lastRealTime = Time.timestamp()
|
REPLAY.lastRealTs = Time.timestamp()
|
||||||
GAME_START_TS = GAME_LOG[0][GAME_LOG[0].length - 1]
|
REPLAY.gameStartTs = REPLAY.log[0][REPLAY.log[0].length - 1]
|
||||||
lastGameTime = GAME_START_TS
|
REPLAY.lastGameTs = REPLAY.gameStartTs
|
||||||
TIME = () => lastGameTime
|
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 ]'
|
||||||
}
|
}
|
||||||
|
|
@ -383,9 +385,9 @@ async function main() {
|
||||||
const {bgColorPickerEl, playerColorPickerEl, nameChangeEl, updateScoreBoard, replayControl} = addMenuToDom(gameId)
|
const {bgColorPickerEl, playerColorPickerEl, nameChangeEl, updateScoreBoard, replayControl} = addMenuToDom(gameId)
|
||||||
updateScoreBoard(TIME())
|
updateScoreBoard(TIME())
|
||||||
|
|
||||||
const longFinished = Game.getFinishTs(gameId)
|
const longFinished = !! Game.getFinishTs(gameId)
|
||||||
let finished = longFinished ? true : false
|
let finished = longFinished
|
||||||
const justFinished = () => !!(finished && !longFinished)
|
const justFinished = () => finished && !longFinished
|
||||||
|
|
||||||
const fireworks = new fireworksController(canvas, Game.getRng(gameId))
|
const fireworks = new fireworksController(canvas, Game.getRng(gameId))
|
||||||
fireworks.init(canvas)
|
fireworks.init(canvas)
|
||||||
|
|
@ -440,23 +442,25 @@ async function main() {
|
||||||
})
|
})
|
||||||
} else if (MODE === 'replay') {
|
} else if (MODE === 'replay') {
|
||||||
const setSpeedStatus = () => {
|
const setSpeedStatus = () => {
|
||||||
replayControl.speed.innerText = 'Replay-Speed: ' + (REPLAY_SPEEDS[REPLAY_SPEED_IDX] + 'x') + (REPLAY_PAUSED ? ' Paused' : '')
|
replayControl.speed.innerText = 'Replay-Speed: ' +
|
||||||
|
(REPLAY.speeds[REPLAY.speedIdx] + 'x') +
|
||||||
|
(REPLAY.paused ? ' Paused' : '')
|
||||||
}
|
}
|
||||||
setSpeedStatus()
|
setSpeedStatus()
|
||||||
replayControl.speedUp.addEventListener('click', () => {
|
replayControl.speedUp.addEventListener('click', () => {
|
||||||
if (REPLAY_SPEED_IDX + 1 < REPLAY_SPEEDS.length) {
|
if (REPLAY.speedIdx + 1 < REPLAY.speeds.length) {
|
||||||
REPLAY_SPEED_IDX++
|
REPLAY.speedIdx++
|
||||||
setSpeedStatus()
|
setSpeedStatus()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
replayControl.speedDown.addEventListener('click', () => {
|
replayControl.speedDown.addEventListener('click', () => {
|
||||||
if (REPLAY_SPEED_IDX >= 1) {
|
if (REPLAY.speedIdx >= 1) {
|
||||||
REPLAY_SPEED_IDX--
|
REPLAY.speedIdx--
|
||||||
setSpeedStatus()
|
setSpeedStatus()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
replayControl.pause.addEventListener('click', () => {
|
replayControl.pause.addEventListener('click', () => {
|
||||||
REPLAY_PAUSED = !REPLAY_PAUSED
|
REPLAY.paused = !REPLAY.paused
|
||||||
setSpeedStatus()
|
setSpeedStatus()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -487,55 +491,54 @@ async function main() {
|
||||||
} break;
|
} break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finished = Game.getFinishTs(gameId)
|
finished = !! Game.getFinishTs(gameId)
|
||||||
})
|
})
|
||||||
} else if (MODE === 'replay') {
|
} else if (MODE === 'replay') {
|
||||||
// no external communication for replay mode,
|
// no external communication for replay mode,
|
||||||
// only the GAME_LOG is relevant
|
// only the REPLAY.log is relevant
|
||||||
let inter = setInterval(() => {
|
let inter = setInterval(() => {
|
||||||
let realTime = Time.timestamp()
|
const realTs = Time.timestamp()
|
||||||
if (REPLAY_PAUSED) {
|
if (REPLAY.paused) {
|
||||||
lastRealTime = realTime
|
REPLAY.lastRealTs = realTs
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let timePassedReal = realTime - lastRealTime
|
const timePassedReal = realTs - REPLAY.lastRealTs
|
||||||
|
const timePassedGame = timePassedReal * REPLAY.speeds[REPLAY.speedIdx]
|
||||||
let timePassedGame = timePassedReal * REPLAY_SPEEDS[REPLAY_SPEED_IDX]
|
const maxGameTs = REPLAY.lastGameTs + timePassedGame
|
||||||
let maxGameTs = lastGameTime + timePassedGame
|
|
||||||
do {
|
do {
|
||||||
if (REPLAY_PAUSED) {
|
if (REPLAY.paused) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
let nextIdx = GAME_LOG_IDX + 1
|
const nextIdx = REPLAY.logIdx + 1
|
||||||
if (nextIdx >= GAME_LOG.length) {
|
if (nextIdx >= REPLAY.log.length) {
|
||||||
clearInterval(inter)
|
clearInterval(inter)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
let logEntry = GAME_LOG[nextIdx]
|
const logEntry = REPLAY.log[nextIdx]
|
||||||
let nextTs = GAME_START_TS + logEntry[logEntry.length - 1]
|
const nextTs = REPLAY.gameStartTs + logEntry[logEntry.length - 1]
|
||||||
if (nextTs > maxGameTs) {
|
if (nextTs > maxGameTs) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
let entryWithTs = logEntry.slice()
|
const entryWithTs = logEntry.slice()
|
||||||
entryWithTs[entryWithTs.length - 1] = nextTs
|
entryWithTs[entryWithTs.length - 1] = nextTs
|
||||||
if (entryWithTs[0] === Protocol.LOG_ADD_PLAYER) {
|
if (entryWithTs[0] === Protocol.LOG_ADD_PLAYER) {
|
||||||
Game.addPlayer(gameId, ...entryWithTs.slice(1))
|
Game.addPlayer(gameId, ...entryWithTs.slice(1))
|
||||||
RERENDER = true
|
RERENDER = true
|
||||||
} else if (entryWithTs[0] === Protocol.LOG_UPDATE_PLAYER) {
|
} else if (entryWithTs[0] === Protocol.LOG_UPDATE_PLAYER) {
|
||||||
let playerId = Game.getPlayerIdByIndex(gameId, entryWithTs[1])
|
const playerId = Game.getPlayerIdByIndex(gameId, entryWithTs[1])
|
||||||
Game.addPlayer(gameId, playerId, ...entryWithTs.slice(2))
|
Game.addPlayer(gameId, playerId, ...entryWithTs.slice(2))
|
||||||
RERENDER = true
|
RERENDER = true
|
||||||
} else if (entryWithTs[0] === Protocol.LOG_HANDLE_INPUT) {
|
} else if (entryWithTs[0] === Protocol.LOG_HANDLE_INPUT) {
|
||||||
let playerId = Game.getPlayerIdByIndex(gameId, entryWithTs[1])
|
const playerId = Game.getPlayerIdByIndex(gameId, entryWithTs[1])
|
||||||
Game.handleInput(gameId, playerId, ...entryWithTs.slice(2))
|
Game.handleInput(gameId, playerId, ...entryWithTs.slice(2))
|
||||||
RERENDER = true
|
RERENDER = true
|
||||||
}
|
}
|
||||||
GAME_LOG_IDX = nextIdx
|
REPLAY.logIdx = nextIdx
|
||||||
} while (true)
|
} while (true)
|
||||||
lastRealTime = realTime
|
REPLAY.lastRealTs = realTs
|
||||||
lastGameTime = maxGameTs
|
REPLAY.lastGameTs = maxGameTs
|
||||||
}, 50)
|
}, 50)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -620,7 +623,7 @@ async function main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
finished = Game.getFinishTs(gameId)
|
finished = !! Game.getFinishTs(gameId)
|
||||||
if (justFinished()) {
|
if (justFinished()) {
|
||||||
fireworks.update()
|
fireworks.update()
|
||||||
RERENDER = true
|
RERENDER = true
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue