less context switch between drawing text and image
This commit is contained in:
parent
0164d197e1
commit
06cfb7e256
1 changed files with 43 additions and 32 deletions
71
game/game.js
71
game/game.js
|
|
@ -17,6 +17,9 @@ if (typeof MODE === 'undefined') throw '[ MODE not set ]'
|
||||||
|
|
||||||
if (typeof DEBUG === 'undefined') window.DEBUG = false
|
if (typeof DEBUG === 'undefined') window.DEBUG = false
|
||||||
|
|
||||||
|
const MODE_PLAY = 'play'
|
||||||
|
const MODE_REPLAY = 'replay'
|
||||||
|
|
||||||
let RERENDER = true
|
let RERENDER = true
|
||||||
|
|
||||||
let TIME = () => Time.timestamp()
|
let TIME = () => Time.timestamp()
|
||||||
|
|
@ -216,7 +219,7 @@ function addMenuToDom(gameId) {
|
||||||
timerEl.appendChild(timerCountdownEl)
|
timerEl.appendChild(timerCountdownEl)
|
||||||
|
|
||||||
let replayControl = null
|
let replayControl = null
|
||||||
if (MODE === 'replay') {
|
if (MODE === MODE_REPLAY) {
|
||||||
const replayControlEl = ELEMENTS.DIV.cloneNode(true)
|
const replayControlEl = ELEMENTS.DIV.cloneNode(true)
|
||||||
const speedUp = btn('⏫')
|
const speedUp = btn('⏫')
|
||||||
const speedDown = btn('⏬')
|
const speedDown = btn('⏬')
|
||||||
|
|
@ -331,6 +334,12 @@ async function main() {
|
||||||
const cursorGrabMask = await Graphics.loadImageToBitmap('/grab_mask.png')
|
const cursorGrabMask = await Graphics.loadImageToBitmap('/grab_mask.png')
|
||||||
const cursorHandMask = await Graphics.loadImageToBitmap('/hand_mask.png')
|
const cursorHandMask = await Graphics.loadImageToBitmap('/hand_mask.png')
|
||||||
|
|
||||||
|
// all cursors must be of the same dimensions
|
||||||
|
const CURSOR_W = cursorGrab.width
|
||||||
|
const CURSOR_W_2 = Math.round(CURSOR_W / 2)
|
||||||
|
const CURSOR_H = cursorGrab.height
|
||||||
|
const CURSOR_H_2 = Math.round(CURSOR_H / 2)
|
||||||
|
|
||||||
const cursors = {}
|
const cursors = {}
|
||||||
const getPlayerCursor = async (p) => {
|
const getPlayerCursor = async (p) => {
|
||||||
const key = p.color + ' ' + p.d
|
const key = p.color + ' ' + p.d
|
||||||
|
|
@ -359,10 +368,10 @@ async function main() {
|
||||||
gameStartTs: null,
|
gameStartTs: null,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MODE === 'play') {
|
if (MODE === MODE_PLAY) {
|
||||||
const game = await Communication.connect(gameId, CLIENT_ID)
|
const game = await Communication.connect(gameId, CLIENT_ID)
|
||||||
Game.newGame(Util.decodeGame(game))
|
Game.newGame(Util.decodeGame(game))
|
||||||
} else if (MODE === 'replay') {
|
} else if (MODE === 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))
|
||||||
REPLAY.log = log
|
REPLAY.log = log
|
||||||
|
|
@ -423,7 +432,7 @@ async function main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
const evts = new EventAdapter(canvas, viewport)
|
const evts = new EventAdapter(canvas, viewport)
|
||||||
if (MODE === 'play') {
|
if (MODE === MODE_PLAY) {
|
||||||
bgColorPickerEl.value = playerBgColor()
|
bgColorPickerEl.value = playerBgColor()
|
||||||
evts.addEvent([Protocol.INPUT_EV_BG_COLOR, bgColorPickerEl.value])
|
evts.addEvent([Protocol.INPUT_EV_BG_COLOR, bgColorPickerEl.value])
|
||||||
bgColorPickerEl.addEventListener('change', () => {
|
bgColorPickerEl.addEventListener('change', () => {
|
||||||
|
|
@ -443,7 +452,7 @@ async function main() {
|
||||||
evts.addEvent([Protocol.INPUT_EV_PLAYER_NAME, nameChangeEl.value])
|
evts.addEvent([Protocol.INPUT_EV_PLAYER_NAME, nameChangeEl.value])
|
||||||
})
|
})
|
||||||
setInterval(updateTimer, 1000)
|
setInterval(updateTimer, 1000)
|
||||||
} else if (MODE === 'replay') {
|
} else if (MODE === MODE_REPLAY) {
|
||||||
const setSpeedStatus = () => {
|
const setSpeedStatus = () => {
|
||||||
replayControl.speed.innerText = 'Replay-Speed: ' +
|
replayControl.speed.innerText = 'Replay-Speed: ' +
|
||||||
(REPLAY.speeds[REPLAY.speedIdx] + 'x') +
|
(REPLAY.speeds[REPLAY.speedIdx] + 'x') +
|
||||||
|
|
@ -468,7 +477,7 @@ async function main() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MODE === 'play') {
|
if (MODE === MODE_PLAY) {
|
||||||
Communication.onServerChange((msg) => {
|
Communication.onServerChange((msg) => {
|
||||||
const msgType = msg[0]
|
const msgType = msg[0]
|
||||||
const evClientId = msg[1]
|
const evClientId = msg[1]
|
||||||
|
|
@ -496,7 +505,7 @@ async function main() {
|
||||||
}
|
}
|
||||||
finished = !! Game.getFinishTs(gameId)
|
finished = !! Game.getFinishTs(gameId)
|
||||||
})
|
})
|
||||||
} else if (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
|
||||||
let inter = setInterval(() => {
|
let inter = setInterval(() => {
|
||||||
|
|
@ -549,7 +558,7 @@ async function main() {
|
||||||
let _last_mouse_down = null
|
let _last_mouse_down = null
|
||||||
const onUpdate = () => {
|
const onUpdate = () => {
|
||||||
for (let evt of evts.consumeAll()) {
|
for (let evt of evts.consumeAll()) {
|
||||||
if (MODE === 'play') {
|
if (MODE === MODE_PLAY) {
|
||||||
// LOCAL ONLY CHANGES
|
// LOCAL ONLY CHANGES
|
||||||
// -------------------------------------------------------------
|
// -------------------------------------------------------------
|
||||||
const type = evt[0]
|
const type = evt[0]
|
||||||
|
|
@ -592,7 +601,7 @@ async function main() {
|
||||||
RERENDER = true
|
RERENDER = true
|
||||||
}
|
}
|
||||||
Communication.sendClientEvent(evt)
|
Communication.sendClientEvent(evt)
|
||||||
} else if (MODE === 'replay') {
|
} else if (MODE === MODE_REPLAY) {
|
||||||
// LOCAL ONLY CHANGES
|
// LOCAL ONLY CHANGES
|
||||||
// -------------------------------------------------------------
|
// -------------------------------------------------------------
|
||||||
const type = evt[0]
|
const type = evt[0]
|
||||||
|
|
@ -695,33 +704,35 @@ async function main() {
|
||||||
// DRAW PLAYERS
|
// DRAW PLAYERS
|
||||||
// ---------------------------------------------------------------
|
// ---------------------------------------------------------------
|
||||||
const ts = TIME()
|
const ts = TIME()
|
||||||
|
const texts = []
|
||||||
|
// Cursors
|
||||||
for (let player of Game.getActivePlayers(gameId, ts)) {
|
for (let player of Game.getActivePlayers(gameId, ts)) {
|
||||||
const cursor = await getPlayerCursor(player)
|
const cursor = await getPlayerCursor(player)
|
||||||
const pos = viewport.worldToViewportRaw(player)
|
const pos = viewport.worldToViewport(player)
|
||||||
ctx.drawImage(cursor,
|
ctx.drawImage(cursor, pos.x - CURSOR_W_2, pos.y - CURSOR_H_2)
|
||||||
Math.round(pos.x - cursor.width/2),
|
if (
|
||||||
Math.round(pos.y - cursor.height/2)
|
(MODE === MODE_PLAY && player.id !== CLIENT_ID)
|
||||||
)
|
|| (MODE === MODE_REPLAY)
|
||||||
if (MODE === 'play') {
|
) {
|
||||||
if (player.id !== CLIENT_ID) {
|
// performance:
|
||||||
|
// not drawing text directly here, to have less ctx
|
||||||
|
// switches between drawImage and fillTxt
|
||||||
|
texts.push([
|
||||||
|
`${player.name} (${player.points})`,
|
||||||
|
pos.x,
|
||||||
|
pos.y + CURSOR_H,
|
||||||
|
])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Names
|
||||||
ctx.fillStyle = 'white'
|
ctx.fillStyle = 'white'
|
||||||
ctx.font = '10px sans-serif'
|
ctx.font = '10px sans-serif'
|
||||||
ctx.textAlign = 'center'
|
ctx.textAlign = 'center'
|
||||||
ctx.fillText(player.name + ' (' + player.points + ')',
|
for (let [txt, x, y] of texts) {
|
||||||
Math.round(pos.x),
|
ctx.fillText(txt, x, y)
|
||||||
Math.round(pos.y) + cursor.height
|
|
||||||
)
|
|
||||||
}
|
|
||||||
} else if (MODE === 'replay') {
|
|
||||||
ctx.fillStyle = 'white'
|
|
||||||
ctx.font = '10px sans-serif'
|
|
||||||
ctx.textAlign = 'center'
|
|
||||||
ctx.fillText(player.name + ' (' + player.points + ')',
|
|
||||||
Math.round(pos.x),
|
|
||||||
Math.round(pos.y) + cursor.height
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (DEBUG) Debug.checkpoint('players done')
|
if (DEBUG) Debug.checkpoint('players done')
|
||||||
|
|
||||||
// DRAW PLAYERS
|
// DRAW PLAYERS
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue