2020-11-25 22:03:35 +01:00
|
|
|
"use strict"
|
2021-05-09 13:49:40 +02:00
|
|
|
|
2021-05-17 00:27:47 +02:00
|
|
|
import {run} from './gameloop'
|
|
|
|
|
import Camera from './Camera'
|
|
|
|
|
import Graphics from './Graphics'
|
|
|
|
|
import Debug from './Debug'
|
|
|
|
|
import Communication from './Communication'
|
|
|
|
|
import Util from './../common/Util'
|
|
|
|
|
import PuzzleGraphics from './PuzzleGraphics'
|
2021-05-29 17:58:05 +02:00
|
|
|
import Game from './../common/GameCommon'
|
2021-05-17 00:27:47 +02:00
|
|
|
import fireworksController from './Fireworks'
|
|
|
|
|
import Protocol from '../common/Protocol'
|
|
|
|
|
import Time from '../common/Time'
|
2021-05-17 02:32:33 +02:00
|
|
|
import { Dim, Point } from '../common/Geometry'
|
2021-05-29 17:58:05 +02:00
|
|
|
import {
|
|
|
|
|
FixedLengthArray,
|
|
|
|
|
Game as GameType,
|
|
|
|
|
Player,
|
|
|
|
|
Piece,
|
|
|
|
|
EncodedGame,
|
|
|
|
|
ReplayData,
|
|
|
|
|
Timestamp,
|
|
|
|
|
GameEvent,
|
|
|
|
|
} from '../common/Types'
|
2021-05-17 00:27:47 +02:00
|
|
|
declare global {
|
|
|
|
|
interface Window {
|
|
|
|
|
DEBUG?: boolean
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
const images = import.meta.globEager('./*.png')
|
2021-05-15 17:40:04 +02:00
|
|
|
|
2021-05-13 22:45:55 +02:00
|
|
|
export const MODE_PLAY = 'play'
|
|
|
|
|
export const MODE_REPLAY = 'replay'
|
2021-04-17 17:23:32 +02:00
|
|
|
|
2021-05-09 22:17:41 +02:00
|
|
|
let PIECE_VIEW_FIXED = true
|
|
|
|
|
let PIECE_VIEW_LOOSE = true
|
2020-11-25 22:03:35 +01:00
|
|
|
|
2021-05-17 00:27:47 +02:00
|
|
|
interface Hud {
|
|
|
|
|
setActivePlayers: (v: Array<any>) => void
|
|
|
|
|
setIdlePlayers: (v: Array<any>) => void
|
|
|
|
|
setFinished: (v: boolean) => void
|
|
|
|
|
setDuration: (v: number) => void
|
|
|
|
|
setPiecesDone: (v: number) => void
|
|
|
|
|
setPiecesTotal: (v: number) => void
|
|
|
|
|
setConnectionState: (v: number) => void
|
|
|
|
|
togglePreview: () => void
|
|
|
|
|
setReplaySpeed?: (v: number) => void
|
|
|
|
|
setReplayPaused?: (v: boolean) => void
|
|
|
|
|
}
|
|
|
|
|
interface Replay {
|
2021-05-29 11:44:55 +02:00
|
|
|
final: boolean
|
|
|
|
|
requesting: boolean
|
2021-05-29 15:36:03 +02:00
|
|
|
log: Array<any> // current log entries
|
|
|
|
|
logPointer: number // pointer to current item in the log array
|
2021-05-17 00:27:47 +02:00
|
|
|
speeds: Array<number>
|
|
|
|
|
speedIdx: number
|
|
|
|
|
paused: boolean
|
|
|
|
|
lastRealTs: number
|
|
|
|
|
lastGameTs: number
|
|
|
|
|
gameStartTs: number
|
2021-05-29 15:36:03 +02:00
|
|
|
//
|
|
|
|
|
dataOffset: number
|
|
|
|
|
dataSize: number
|
2021-05-17 00:27:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const shouldDrawPiece = (piece: Piece) => {
|
2021-05-10 01:13:23 +02:00
|
|
|
if (piece.owner === -1) {
|
|
|
|
|
return PIECE_VIEW_FIXED
|
|
|
|
|
}
|
|
|
|
|
return PIECE_VIEW_LOOSE
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-09 22:17:41 +02:00
|
|
|
let RERENDER = true
|
2020-12-22 22:35:09 +01:00
|
|
|
|
2021-05-17 00:27:47 +02:00
|
|
|
function addCanvasToDom(TARGET_EL: HTMLElement, canvas: HTMLCanvasElement) {
|
2020-11-25 22:03:35 +01:00
|
|
|
canvas.width = window.innerWidth
|
|
|
|
|
canvas.height = window.innerHeight
|
2021-05-01 08:59:39 +02:00
|
|
|
TARGET_EL.appendChild(canvas)
|
2020-11-25 22:03:35 +01:00
|
|
|
window.addEventListener('resize', () => {
|
|
|
|
|
canvas.width = window.innerWidth
|
|
|
|
|
canvas.height = window.innerHeight
|
|
|
|
|
RERENDER = true
|
|
|
|
|
})
|
|
|
|
|
return canvas
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-17 00:27:47 +02:00
|
|
|
function EventAdapter (canvas: HTMLCanvasElement, window: any, viewport: any) {
|
2021-05-29 17:58:05 +02:00
|
|
|
let events: Array<GameEvent> = []
|
2021-05-07 14:31:32 +02:00
|
|
|
|
2021-05-09 22:17:41 +02:00
|
|
|
let KEYS_ON = true
|
|
|
|
|
|
2021-05-07 14:31:32 +02:00
|
|
|
let LEFT = false
|
|
|
|
|
let RIGHT = false
|
|
|
|
|
let UP = false
|
|
|
|
|
let DOWN = false
|
|
|
|
|
let ZOOM_IN = false
|
|
|
|
|
let ZOOM_OUT = false
|
|
|
|
|
let SHIFT = false
|
|
|
|
|
|
2021-05-17 00:27:47 +02:00
|
|
|
const toWorldPoint = (x: number, y: number) => {
|
2021-05-07 14:31:32 +02:00
|
|
|
const pos = viewport.viewportToWorld({x, y})
|
|
|
|
|
return [pos.x, pos.y]
|
2020-11-25 22:03:35 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-17 00:27:47 +02:00
|
|
|
const mousePos = (ev: MouseEvent) => toWorldPoint(ev.offsetX, ev.offsetY)
|
2021-05-07 14:31:32 +02:00
|
|
|
const canvasCenter = () => toWorldPoint(canvas.width / 2, canvas.height / 2)
|
|
|
|
|
|
2021-05-17 00:27:47 +02:00
|
|
|
const key = (state: boolean, ev: KeyboardEvent) => {
|
2021-05-09 22:17:41 +02:00
|
|
|
if (!KEYS_ON) {
|
2021-05-07 14:31:32 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (ev.key === 'Shift') {
|
|
|
|
|
SHIFT = state
|
|
|
|
|
} else if (ev.key === 'ArrowUp' || ev.key === 'w' || ev.key === 'W') {
|
|
|
|
|
UP = state
|
|
|
|
|
} else if (ev.key === 'ArrowDown' || ev.key === 's' || ev.key === 'S') {
|
|
|
|
|
DOWN = state
|
|
|
|
|
} else if (ev.key === 'ArrowLeft' || ev.key === 'a' || ev.key === 'A') {
|
|
|
|
|
LEFT = state
|
|
|
|
|
} else if (ev.key === 'ArrowRight' || ev.key === 'd' || ev.key === 'D') {
|
|
|
|
|
RIGHT = state
|
|
|
|
|
} else if (ev.key === 'q') {
|
|
|
|
|
ZOOM_OUT = state
|
|
|
|
|
} else if (ev.key === 'e') {
|
|
|
|
|
ZOOM_IN = state
|
|
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-07 14:31:32 +02:00
|
|
|
canvas.addEventListener('mousedown', (ev) => {
|
|
|
|
|
if (ev.button === 0) {
|
|
|
|
|
addEvent([Protocol.INPUT_EV_MOUSE_DOWN, ...mousePos(ev)])
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
canvas.addEventListener('mouseup', (ev) => {
|
|
|
|
|
if (ev.button === 0) {
|
|
|
|
|
addEvent([Protocol.INPUT_EV_MOUSE_UP, ...mousePos(ev)])
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
canvas.addEventListener('mousemove', (ev) => {
|
|
|
|
|
addEvent([Protocol.INPUT_EV_MOUSE_MOVE, ...mousePos(ev)])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
canvas.addEventListener('wheel', (ev) => {
|
2021-05-13 23:38:36 +02:00
|
|
|
if (viewport.canZoom(ev.deltaY < 0 ? 'in' : 'out')) {
|
|
|
|
|
const evt = ev.deltaY < 0
|
|
|
|
|
? Protocol.INPUT_EV_ZOOM_IN
|
|
|
|
|
: Protocol.INPUT_EV_ZOOM_OUT
|
|
|
|
|
addEvent([evt, ...mousePos(ev)])
|
|
|
|
|
}
|
2021-05-07 14:31:32 +02:00
|
|
|
})
|
|
|
|
|
|
2021-05-17 00:27:47 +02:00
|
|
|
window.addEventListener('keydown', (ev: KeyboardEvent) => key(true, ev))
|
|
|
|
|
window.addEventListener('keyup', (ev: KeyboardEvent) => key(false, ev))
|
2021-05-07 14:31:32 +02:00
|
|
|
|
2021-05-17 00:27:47 +02:00
|
|
|
window.addEventListener('keypress', (ev: KeyboardEvent) => {
|
2021-05-09 22:17:41 +02:00
|
|
|
if (!KEYS_ON) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (ev.key === ' ') {
|
|
|
|
|
addEvent([Protocol.INPUT_EV_TOGGLE_PREVIEW])
|
|
|
|
|
}
|
|
|
|
|
if (ev.key === 'F' || ev.key === 'f') {
|
|
|
|
|
PIECE_VIEW_FIXED = !PIECE_VIEW_FIXED
|
|
|
|
|
RERENDER = true
|
|
|
|
|
}
|
|
|
|
|
if (ev.key === 'G' || ev.key === 'g') {
|
|
|
|
|
PIECE_VIEW_LOOSE = !PIECE_VIEW_LOOSE
|
|
|
|
|
RERENDER = true
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2021-05-29 17:58:05 +02:00
|
|
|
const addEvent = (event: GameEvent) => {
|
2021-05-07 14:31:32 +02:00
|
|
|
events.push(event)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-29 17:58:05 +02:00
|
|
|
const consumeAll = (): GameEvent[] => {
|
2021-05-07 14:31:32 +02:00
|
|
|
if (events.length === 0) {
|
2020-11-25 22:03:35 +01:00
|
|
|
return []
|
|
|
|
|
}
|
2021-05-07 14:31:32 +02:00
|
|
|
const all = events.slice()
|
|
|
|
|
events = []
|
2020-11-25 22:03:35 +01:00
|
|
|
return all
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-07 14:31:32 +02:00
|
|
|
const createKeyEvents = () => {
|
2021-05-10 01:13:23 +02:00
|
|
|
const amount = SHIFT ? 20 : 10
|
|
|
|
|
const x = (LEFT ? amount : 0) - (RIGHT ? amount : 0)
|
|
|
|
|
const y = (UP ? amount : 0) - (DOWN ? amount : 0)
|
2021-04-19 23:59:14 +02:00
|
|
|
if (x !== 0 || y !== 0) {
|
2021-05-07 14:31:32 +02:00
|
|
|
addEvent([Protocol.INPUT_EV_MOVE, x, y])
|
2021-04-19 23:59:14 +02:00
|
|
|
}
|
2021-04-27 08:53:17 +02:00
|
|
|
|
2021-05-07 14:31:32 +02:00
|
|
|
if (ZOOM_IN && ZOOM_OUT) {
|
2021-04-27 08:53:17 +02:00
|
|
|
// cancel each other out
|
2021-05-07 14:31:32 +02:00
|
|
|
} else if (ZOOM_IN) {
|
2021-05-13 23:38:36 +02:00
|
|
|
if (viewport.canZoom('in')) {
|
|
|
|
|
addEvent([Protocol.INPUT_EV_ZOOM_IN, ...canvasCenter()])
|
|
|
|
|
}
|
2021-05-07 14:31:32 +02:00
|
|
|
} else if (ZOOM_OUT) {
|
2021-05-13 23:38:36 +02:00
|
|
|
if (viewport.canZoom('out')) {
|
|
|
|
|
addEvent([Protocol.INPUT_EV_ZOOM_OUT, ...canvasCenter()])
|
|
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-17 00:27:47 +02:00
|
|
|
const setHotkeys = (state: boolean) => {
|
2021-05-09 22:17:41 +02:00
|
|
|
KEYS_ON = state
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-07 14:31:32 +02:00
|
|
|
return {
|
|
|
|
|
addEvent,
|
|
|
|
|
consumeAll,
|
|
|
|
|
createKeyEvents,
|
2021-05-09 22:17:41 +02:00
|
|
|
setHotkeys,
|
2020-11-25 22:03:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-15 20:04:30 +02:00
|
|
|
export async function main(
|
2021-05-17 00:27:47 +02:00
|
|
|
gameId: string,
|
|
|
|
|
clientId: string,
|
|
|
|
|
wsAddress: string,
|
|
|
|
|
MODE: string,
|
|
|
|
|
TARGET_EL: HTMLElement,
|
|
|
|
|
HUD: Hud
|
2021-05-15 20:04:30 +02:00
|
|
|
) {
|
2021-05-17 00:27:47 +02:00
|
|
|
if (typeof window.DEBUG === 'undefined') window.DEBUG = false
|
2021-05-13 17:20:10 +02:00
|
|
|
|
2021-05-23 17:53:46 +02:00
|
|
|
const shouldDrawPlayer = (player: Player) => {
|
2021-05-13 23:38:36 +02:00
|
|
|
return MODE === MODE_REPLAY || player.id !== clientId
|
2021-05-10 01:13:23 +02:00
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
|
2021-05-17 00:27:47 +02:00
|
|
|
const cursorGrab = await Graphics.loadImageToBitmap(images['./grab.png'].default)
|
|
|
|
|
const cursorHand = await Graphics.loadImageToBitmap(images['./hand.png'].default)
|
|
|
|
|
const cursorGrabMask = await Graphics.loadImageToBitmap(images['./grab_mask.png'].default)
|
|
|
|
|
const cursorHandMask = await Graphics.loadImageToBitmap(images['./hand_mask.png'].default)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
2021-04-17 17:23:32 +02:00
|
|
|
// 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)
|
|
|
|
|
|
2021-05-17 00:27:47 +02:00
|
|
|
const cursors: Record<string, ImageBitmap> = {}
|
|
|
|
|
const getPlayerCursor = async (p: Player) => {
|
2021-04-14 21:04:07 +02:00
|
|
|
const key = p.color + ' ' + p.d
|
2020-11-25 22:03:35 +01:00
|
|
|
if (!cursors[key]) {
|
|
|
|
|
const cursor = p.d ? cursorGrab : cursorHand
|
2021-05-17 00:27:47 +02:00
|
|
|
if (p.color) {
|
|
|
|
|
const mask = p.d ? cursorGrabMask : cursorHandMask
|
2021-05-23 17:53:46 +02:00
|
|
|
cursors[key] = await createImageBitmap(
|
|
|
|
|
Graphics.colorizedCanvas(cursor, mask, p.color)
|
|
|
|
|
)
|
2021-05-17 00:27:47 +02:00
|
|
|
} else {
|
|
|
|
|
cursors[key] = cursor
|
|
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
}
|
|
|
|
|
return cursors[key]
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 22:35:09 +01:00
|
|
|
// Create a canvas and attach adapters to it so we can work with it
|
2021-05-13 17:20:10 +02:00
|
|
|
const canvas = addCanvasToDom(TARGET_EL, Graphics.createCanvas())
|
2020-11-25 22:03:35 +01:00
|
|
|
|
2020-12-22 22:35:09 +01:00
|
|
|
// stuff only available in replay mode...
|
|
|
|
|
// TODO: refactor
|
2021-05-17 00:27:47 +02:00
|
|
|
const REPLAY: Replay = {
|
2021-05-29 11:44:55 +02:00
|
|
|
final: false,
|
|
|
|
|
requesting: true,
|
2021-05-17 00:27:47 +02:00
|
|
|
log: [],
|
2021-05-29 11:44:55 +02:00
|
|
|
logPointer: 0,
|
2021-05-29 13:08:42 +02:00
|
|
|
speeds: [0.5, 1, 2, 5, 10, 20, 50, 100, 250, 500],
|
2021-04-15 12:16:14 +02:00
|
|
|
speedIdx: 1,
|
|
|
|
|
paused: false,
|
2021-05-17 00:27:47 +02:00
|
|
|
lastRealTs: 0,
|
|
|
|
|
lastGameTs: 0,
|
|
|
|
|
gameStartTs: 0,
|
2021-05-29 15:36:03 +02:00
|
|
|
dataOffset: 0,
|
|
|
|
|
dataSize: 10000,
|
2021-04-15 12:16:14 +02:00
|
|
|
}
|
2020-12-22 22:35:09 +01:00
|
|
|
|
2021-05-15 20:04:30 +02:00
|
|
|
Communication.onConnectionStateChange((state) => {
|
|
|
|
|
HUD.setConnectionState(state)
|
2021-05-15 17:40:04 +02:00
|
|
|
})
|
|
|
|
|
|
2021-05-29 15:36:03 +02:00
|
|
|
const queryNextReplayBatch = async (
|
|
|
|
|
gameId: string
|
|
|
|
|
): Promise<ReplayData> => {
|
|
|
|
|
REPLAY.requesting = true
|
|
|
|
|
const replay: ReplayData = await Communication.requestReplayData(
|
|
|
|
|
gameId,
|
|
|
|
|
REPLAY.dataOffset,
|
|
|
|
|
REPLAY.dataSize
|
|
|
|
|
)
|
|
|
|
|
REPLAY.dataOffset += REPLAY.dataSize
|
|
|
|
|
REPLAY.requesting = false
|
|
|
|
|
return replay
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-29 12:40:46 +02:00
|
|
|
const getNextReplayBatch = async (
|
2021-05-29 15:36:03 +02:00
|
|
|
gameId: string
|
2021-05-29 12:40:46 +02:00
|
|
|
) => {
|
2021-05-29 15:36:03 +02:00
|
|
|
const replay: ReplayData = await queryNextReplayBatch(gameId)
|
2021-05-29 12:40:46 +02:00
|
|
|
// cut log that was already handled
|
|
|
|
|
REPLAY.log = REPLAY.log.slice(REPLAY.logPointer)
|
|
|
|
|
REPLAY.logPointer = 0
|
|
|
|
|
|
|
|
|
|
REPLAY.log.push(...replay.log)
|
2021-05-29 15:36:03 +02:00
|
|
|
if (replay.log.length < REPLAY.dataSize) {
|
2021-05-29 12:40:46 +02:00
|
|
|
REPLAY.final = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-29 15:36:03 +02:00
|
|
|
|
2021-05-17 00:27:47 +02:00
|
|
|
let TIME: () => number = () => 0
|
2021-05-15 20:04:30 +02:00
|
|
|
const connect = async () => {
|
|
|
|
|
if (MODE === MODE_PLAY) {
|
2021-05-29 15:36:03 +02:00
|
|
|
const game: EncodedGame = await Communication.connect(wsAddress, gameId, clientId)
|
|
|
|
|
const gameObject: GameType = Util.decodeGame(game)
|
2021-05-15 20:04:30 +02:00
|
|
|
Game.setGame(gameObject.id, gameObject)
|
|
|
|
|
TIME = () => Time.timestamp()
|
|
|
|
|
} else if (MODE === MODE_REPLAY) {
|
2021-05-29 15:36:03 +02:00
|
|
|
const replay: ReplayData = await queryNextReplayBatch(gameId)
|
|
|
|
|
if (!replay.game) {
|
|
|
|
|
throw '[ 2021-05-29 no game received ]'
|
|
|
|
|
}
|
|
|
|
|
const gameObject: GameType = Util.decodeGame(replay.game)
|
2021-05-15 20:04:30 +02:00
|
|
|
Game.setGame(gameObject.id, gameObject)
|
2021-05-29 11:44:55 +02:00
|
|
|
REPLAY.requesting = false
|
2021-05-17 00:27:47 +02:00
|
|
|
REPLAY.log = replay.log
|
2021-05-15 20:04:30 +02:00
|
|
|
REPLAY.lastRealTs = Time.timestamp()
|
2021-05-29 11:44:55 +02:00
|
|
|
REPLAY.gameStartTs = parseInt(REPLAY.log[0][4], 10)
|
2021-05-15 20:04:30 +02:00
|
|
|
REPLAY.lastGameTs = REPLAY.gameStartTs
|
|
|
|
|
TIME = () => REPLAY.lastGameTs
|
|
|
|
|
} else {
|
|
|
|
|
throw '[ 2020-12-22 MODE invalid, must be play|replay ]'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// rerender after (re-)connect
|
|
|
|
|
RERENDER = true
|
2020-12-22 22:35:09 +01:00
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
|
2021-05-15 20:04:30 +02:00
|
|
|
await connect()
|
|
|
|
|
|
2021-05-29 15:36:03 +02:00
|
|
|
const PIECE_DRAW_OFFSET = Game.getPieceDrawOffset(gameId)
|
|
|
|
|
const PIECE_DRAW_SIZE = Game.getPieceDrawSize(gameId)
|
2021-04-14 21:04:07 +02:00
|
|
|
const PUZZLE_WIDTH = Game.getPuzzleWidth(gameId)
|
|
|
|
|
const PUZZLE_HEIGHT = Game.getPuzzleHeight(gameId)
|
|
|
|
|
const TABLE_WIDTH = Game.getTableWidth(gameId)
|
|
|
|
|
const TABLE_HEIGHT = Game.getTableHeight(gameId)
|
|
|
|
|
|
2021-05-10 01:13:23 +02:00
|
|
|
const BOARD_POS = {
|
|
|
|
|
x: (TABLE_WIDTH - PUZZLE_WIDTH) / 2,
|
|
|
|
|
y: (TABLE_HEIGHT - PUZZLE_HEIGHT) / 2
|
|
|
|
|
}
|
|
|
|
|
const BOARD_DIM = {
|
|
|
|
|
w: PUZZLE_WIDTH,
|
|
|
|
|
h: PUZZLE_HEIGHT,
|
|
|
|
|
}
|
|
|
|
|
const PIECE_DIM = {
|
2021-05-29 15:36:03 +02:00
|
|
|
w: PIECE_DRAW_SIZE,
|
|
|
|
|
h: PIECE_DRAW_SIZE,
|
2021-05-10 01:13:23 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-22 22:35:09 +01:00
|
|
|
const bitmaps = await PuzzleGraphics.loadPuzzleBitmaps(Game.getPuzzle(gameId))
|
|
|
|
|
|
2021-05-09 22:17:41 +02:00
|
|
|
const fireworks = new fireworksController(canvas, Game.getRng(gameId))
|
2021-05-17 00:27:47 +02:00
|
|
|
fireworks.init()
|
2021-05-09 22:17:41 +02:00
|
|
|
|
2021-05-17 00:27:47 +02:00
|
|
|
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D
|
2021-05-09 22:17:41 +02:00
|
|
|
canvas.classList.add('loaded')
|
|
|
|
|
|
|
|
|
|
// initialize some view data
|
|
|
|
|
// this global data will change according to input events
|
2021-05-17 00:27:47 +02:00
|
|
|
const viewport = Camera()
|
2021-05-09 22:17:41 +02:00
|
|
|
// center viewport
|
|
|
|
|
viewport.move(
|
|
|
|
|
-(TABLE_WIDTH - canvas.width) /2,
|
|
|
|
|
-(TABLE_HEIGHT - canvas.height) /2
|
|
|
|
|
)
|
|
|
|
|
|
2021-05-17 00:27:47 +02:00
|
|
|
const evts = EventAdapter(canvas, window, viewport)
|
2021-05-09 22:17:41 +02:00
|
|
|
|
2021-05-13 22:45:55 +02:00
|
|
|
const previewImageUrl = Game.getImageUrl(gameId)
|
2021-05-04 10:21:47 +02:00
|
|
|
|
2021-05-10 01:13:23 +02:00
|
|
|
const updateTimerElements = () => {
|
2021-05-13 22:45:55 +02:00
|
|
|
const startTs = Game.getStartTs(gameId)
|
|
|
|
|
const finishTs = Game.getFinishTs(gameId)
|
|
|
|
|
const ts = TIME()
|
|
|
|
|
|
|
|
|
|
HUD.setFinished(!!(finishTs))
|
|
|
|
|
HUD.setDuration((finishTs || ts) - startTs)
|
2021-05-10 01:13:23 +02:00
|
|
|
}
|
2021-05-10 00:44:27 +02:00
|
|
|
|
|
|
|
|
updateTimerElements()
|
2021-05-29 15:36:03 +02:00
|
|
|
HUD.setPiecesDone(Game.getFinishedPiecesCount(gameId))
|
|
|
|
|
HUD.setPiecesTotal(Game.getPieceCount(gameId))
|
2021-05-10 00:44:27 +02:00
|
|
|
const ts = TIME()
|
2021-05-13 23:38:36 +02:00
|
|
|
HUD.setActivePlayers(Game.getActivePlayers(gameId, ts))
|
|
|
|
|
HUD.setIdlePlayers(Game.getIdlePlayers(gameId, ts))
|
2020-12-09 01:27:47 +01:00
|
|
|
|
2021-04-15 12:16:14 +02:00
|
|
|
const longFinished = !! Game.getFinishTs(gameId)
|
|
|
|
|
let finished = longFinished
|
|
|
|
|
const justFinished = () => finished && !longFinished
|
2020-12-09 01:27:47 +01:00
|
|
|
|
2020-12-07 12:20:09 +01:00
|
|
|
const playerBgColor = () => {
|
2021-05-13 23:38:36 +02:00
|
|
|
return (Game.getPlayerBgColor(gameId, clientId)
|
2020-12-07 12:20:09 +01:00
|
|
|
|| localStorage.getItem('bg_color')
|
|
|
|
|
|| '#222222')
|
|
|
|
|
}
|
|
|
|
|
const playerColor = () => {
|
2021-05-13 23:38:36 +02:00
|
|
|
return (Game.getPlayerColor(gameId, clientId)
|
2020-12-07 12:20:09 +01:00
|
|
|
|| localStorage.getItem('player_color')
|
|
|
|
|
|| '#ffffff')
|
|
|
|
|
}
|
|
|
|
|
const playerName = () => {
|
2021-05-13 23:38:36 +02:00
|
|
|
return (Game.getPlayerName(gameId, clientId)
|
2020-12-07 12:20:09 +01:00
|
|
|
|| localStorage.getItem('player_name')
|
|
|
|
|
|| 'anon')
|
|
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
|
2021-05-23 17:53:46 +02:00
|
|
|
let cursorDown: string = ''
|
|
|
|
|
let cursor: string = ''
|
|
|
|
|
let cursorState: boolean = false
|
|
|
|
|
const updatePlayerCursorState = (d: boolean) => {
|
|
|
|
|
cursorState = d
|
|
|
|
|
const [url, fallback] = d ? [cursorDown, 'grab'] : [cursor, 'default']
|
|
|
|
|
canvas.style.cursor = `url('${url}') ${CURSOR_W_2} ${CURSOR_H_2}, ${fallback}`
|
|
|
|
|
}
|
|
|
|
|
const updatePlayerCursorColor = (color: string) => {
|
|
|
|
|
cursorDown = Graphics.colorizedCanvas(cursorGrab, cursorGrabMask, color).toDataURL()
|
|
|
|
|
cursor = Graphics.colorizedCanvas(cursorHand, cursorHandMask, color).toDataURL()
|
|
|
|
|
updatePlayerCursorState(cursorState)
|
|
|
|
|
}
|
|
|
|
|
updatePlayerCursorColor(playerColor())
|
|
|
|
|
|
2021-05-13 22:45:55 +02:00
|
|
|
const doSetSpeedStatus = () => {
|
2021-05-17 00:27:47 +02:00
|
|
|
if (HUD.setReplaySpeed) {
|
|
|
|
|
HUD.setReplaySpeed(REPLAY.speeds[REPLAY.speedIdx])
|
|
|
|
|
}
|
|
|
|
|
if (HUD.setReplayPaused) {
|
|
|
|
|
HUD.setReplayPaused(REPLAY.paused)
|
|
|
|
|
}
|
2021-05-13 22:45:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const replayOnSpeedUp = () => {
|
|
|
|
|
if (REPLAY.speedIdx + 1 < REPLAY.speeds.length) {
|
|
|
|
|
REPLAY.speedIdx++
|
|
|
|
|
doSetSpeedStatus()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const replayOnSpeedDown = () => {
|
|
|
|
|
if (REPLAY.speedIdx >= 1) {
|
|
|
|
|
REPLAY.speedIdx--
|
|
|
|
|
doSetSpeedStatus()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const replayOnPauseToggle = () => {
|
|
|
|
|
REPLAY.paused = !REPLAY.paused
|
|
|
|
|
doSetSpeedStatus()
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-17 17:23:32 +02:00
|
|
|
if (MODE === MODE_PLAY) {
|
2021-05-10 00:44:27 +02:00
|
|
|
setInterval(updateTimerElements, 1000)
|
2021-04-17 17:23:32 +02:00
|
|
|
} else if (MODE === MODE_REPLAY) {
|
2021-05-13 22:45:55 +02:00
|
|
|
doSetSpeedStatus()
|
2020-12-22 22:35:09 +01:00
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
|
2021-04-17 17:23:32 +02:00
|
|
|
if (MODE === MODE_PLAY) {
|
2021-05-29 11:44:55 +02:00
|
|
|
// TODO: register onServerChange function before connecting to server
|
2020-12-22 22:35:09 +01:00
|
|
|
Communication.onServerChange((msg) => {
|
|
|
|
|
const msgType = msg[0]
|
|
|
|
|
const evClientId = msg[1]
|
|
|
|
|
const evClientSeq = msg[2]
|
|
|
|
|
const evChanges = msg[3]
|
2021-05-09 22:17:41 +02:00
|
|
|
for (const [changeType, changeData] of evChanges) {
|
2020-12-22 22:35:09 +01:00
|
|
|
switch (changeType) {
|
2021-04-19 23:59:14 +02:00
|
|
|
case Protocol.CHANGE_PLAYER: {
|
2020-12-22 22:35:09 +01:00
|
|
|
const p = Util.decodePlayer(changeData)
|
2021-05-13 23:38:36 +02:00
|
|
|
if (p.id !== clientId) {
|
2020-12-22 22:35:09 +01:00
|
|
|
Game.setPlayer(gameId, p.id, p)
|
|
|
|
|
RERENDER = true
|
|
|
|
|
}
|
|
|
|
|
} break;
|
2021-04-19 23:59:14 +02:00
|
|
|
case Protocol.CHANGE_TILE: {
|
2021-05-29 15:36:03 +02:00
|
|
|
const t = Util.decodePiece(changeData)
|
|
|
|
|
Game.setPiece(gameId, t.idx, t)
|
2020-11-25 22:03:35 +01:00
|
|
|
RERENDER = true
|
2020-12-22 22:35:09 +01:00
|
|
|
} break;
|
2021-04-19 23:59:14 +02:00
|
|
|
case Protocol.CHANGE_DATA: {
|
2020-12-22 22:35:09 +01:00
|
|
|
Game.setPuzzleData(gameId, changeData)
|
|
|
|
|
RERENDER = true
|
|
|
|
|
} break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-15 12:16:14 +02:00
|
|
|
finished = !! Game.getFinishTs(gameId)
|
2020-12-22 22:35:09 +01:00
|
|
|
})
|
2021-04-17 17:23:32 +02:00
|
|
|
} else if (MODE === MODE_REPLAY) {
|
2020-12-22 22:35:09 +01:00
|
|
|
// no external communication for replay mode,
|
2021-04-15 12:16:14 +02:00
|
|
|
// only the REPLAY.log is relevant
|
2021-05-29 17:58:05 +02:00
|
|
|
const inter = setInterval(() => {
|
2021-04-15 12:16:14 +02:00
|
|
|
const realTs = Time.timestamp()
|
2021-05-29 11:44:55 +02:00
|
|
|
if (REPLAY.requesting) {
|
|
|
|
|
REPLAY.lastRealTs = realTs
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (REPLAY.logPointer + 1 >= REPLAY.log.length) {
|
|
|
|
|
REPLAY.lastRealTs = realTs
|
2021-05-29 15:36:03 +02:00
|
|
|
getNextReplayBatch(gameId)
|
2021-05-29 11:44:55 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-15 12:16:14 +02:00
|
|
|
if (REPLAY.paused) {
|
|
|
|
|
REPLAY.lastRealTs = realTs
|
2020-12-22 22:35:09 +01:00
|
|
|
return
|
|
|
|
|
}
|
2021-04-15 12:16:14 +02:00
|
|
|
const timePassedReal = realTs - REPLAY.lastRealTs
|
|
|
|
|
const timePassedGame = timePassedReal * REPLAY.speeds[REPLAY.speedIdx]
|
|
|
|
|
const maxGameTs = REPLAY.lastGameTs + timePassedGame
|
2020-12-22 22:35:09 +01:00
|
|
|
do {
|
2021-04-15 12:16:14 +02:00
|
|
|
if (REPLAY.paused) {
|
2020-12-22 22:35:09 +01:00
|
|
|
break
|
|
|
|
|
}
|
2021-05-29 11:44:55 +02:00
|
|
|
const nextIdx = REPLAY.logPointer + 1
|
2021-04-15 12:16:14 +02:00
|
|
|
if (nextIdx >= REPLAY.log.length) {
|
2021-05-29 11:44:55 +02:00
|
|
|
if (REPLAY.final) {
|
|
|
|
|
clearInterval(inter)
|
|
|
|
|
}
|
2020-12-22 22:35:09 +01:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-15 12:16:14 +02:00
|
|
|
const logEntry = REPLAY.log[nextIdx]
|
2021-05-29 15:36:03 +02:00
|
|
|
const nextTs: Timestamp = REPLAY.gameStartTs + logEntry[logEntry.length - 1]
|
2020-12-22 22:35:09 +01:00
|
|
|
if (nextTs > maxGameTs) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-15 12:16:14 +02:00
|
|
|
const entryWithTs = logEntry.slice()
|
2020-12-23 01:19:30 +01:00
|
|
|
if (entryWithTs[0] === Protocol.LOG_ADD_PLAYER) {
|
2021-05-17 00:27:47 +02:00
|
|
|
const playerId = entryWithTs[1]
|
|
|
|
|
Game.addPlayer(gameId, playerId, nextTs)
|
2020-11-25 22:03:35 +01:00
|
|
|
RERENDER = true
|
2020-12-23 01:19:30 +01:00
|
|
|
} else if (entryWithTs[0] === Protocol.LOG_UPDATE_PLAYER) {
|
2021-04-15 12:16:14 +02:00
|
|
|
const playerId = Game.getPlayerIdByIndex(gameId, entryWithTs[1])
|
2021-05-17 02:32:33 +02:00
|
|
|
if (!playerId) {
|
|
|
|
|
throw '[ 2021-05-17 player not found (update player) ]'
|
|
|
|
|
}
|
2021-05-17 00:27:47 +02:00
|
|
|
Game.addPlayer(gameId, playerId, nextTs)
|
2020-12-23 01:19:30 +01:00
|
|
|
RERENDER = true
|
|
|
|
|
} else if (entryWithTs[0] === Protocol.LOG_HANDLE_INPUT) {
|
2021-04-15 12:16:14 +02:00
|
|
|
const playerId = Game.getPlayerIdByIndex(gameId, entryWithTs[1])
|
2021-05-17 02:32:33 +02:00
|
|
|
if (!playerId) {
|
|
|
|
|
throw '[ 2021-05-17 player not found (handle input) ]'
|
|
|
|
|
}
|
2021-05-17 00:27:47 +02:00
|
|
|
const input = entryWithTs[2]
|
|
|
|
|
Game.handleInput(gameId, playerId, input, nextTs)
|
2020-11-25 22:03:35 +01:00
|
|
|
RERENDER = true
|
2020-12-22 22:35:09 +01:00
|
|
|
}
|
2021-05-29 11:44:55 +02:00
|
|
|
REPLAY.logPointer = nextIdx
|
2020-12-22 22:35:09 +01:00
|
|
|
} while (true)
|
2021-04-15 12:16:14 +02:00
|
|
|
REPLAY.lastRealTs = realTs
|
|
|
|
|
REPLAY.lastGameTs = maxGameTs
|
2021-05-10 00:44:27 +02:00
|
|
|
updateTimerElements()
|
2020-12-22 22:35:09 +01:00
|
|
|
}, 50)
|
|
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
|
2021-05-17 00:27:47 +02:00
|
|
|
let _last_mouse_down: Point|null = null
|
2021-05-29 17:58:05 +02:00
|
|
|
const onUpdate = (): void => {
|
2021-04-19 23:59:14 +02:00
|
|
|
// handle key downs once per onUpdate
|
|
|
|
|
// this will create Protocol.INPUT_EV_MOVE events if something
|
|
|
|
|
// relevant is pressed
|
2021-05-07 14:31:32 +02:00
|
|
|
evts.createKeyEvents()
|
2021-04-19 23:59:14 +02:00
|
|
|
|
2021-05-09 22:17:41 +02:00
|
|
|
for (const evt of evts.consumeAll()) {
|
2021-04-17 17:23:32 +02:00
|
|
|
if (MODE === MODE_PLAY) {
|
2020-12-22 22:35:09 +01:00
|
|
|
// LOCAL ONLY CHANGES
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
const type = evt[0]
|
2021-04-19 23:59:14 +02:00
|
|
|
if (type === Protocol.INPUT_EV_MOVE) {
|
|
|
|
|
const diffX = evt[1]
|
|
|
|
|
const diffY = evt[2]
|
|
|
|
|
RERENDER = true
|
|
|
|
|
viewport.move(diffX, diffY)
|
|
|
|
|
} else if (type === Protocol.INPUT_EV_MOUSE_MOVE) {
|
2021-05-29 15:36:03 +02:00
|
|
|
if (_last_mouse_down && !Game.getFirstOwnedPiece(gameId, clientId)) {
|
2020-12-22 22:35:09 +01:00
|
|
|
// move the cam
|
|
|
|
|
const pos = { x: evt[1], y: evt[2] }
|
|
|
|
|
const mouse = viewport.worldToViewport(pos)
|
|
|
|
|
const diffX = Math.round(mouse.x - _last_mouse_down.x)
|
|
|
|
|
const diffY = Math.round(mouse.y - _last_mouse_down.y)
|
|
|
|
|
RERENDER = true
|
|
|
|
|
viewport.move(diffX, diffY)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
2020-12-22 22:35:09 +01:00
|
|
|
_last_mouse_down = mouse
|
|
|
|
|
}
|
2021-05-23 17:53:46 +02:00
|
|
|
} else if (type === Protocol.INPUT_EV_PLAYER_COLOR) {
|
|
|
|
|
updatePlayerCursorColor(evt[1])
|
2020-12-23 01:19:30 +01:00
|
|
|
} else if (type === Protocol.INPUT_EV_MOUSE_DOWN) {
|
2020-12-05 19:45:34 +01:00
|
|
|
const pos = { x: evt[1], y: evt[2] }
|
2020-12-22 22:35:09 +01:00
|
|
|
_last_mouse_down = viewport.worldToViewport(pos)
|
2021-05-23 17:53:46 +02:00
|
|
|
updatePlayerCursorState(true)
|
2020-12-23 01:19:30 +01:00
|
|
|
} else if (type === Protocol.INPUT_EV_MOUSE_UP) {
|
2020-12-22 22:35:09 +01:00
|
|
|
_last_mouse_down = null
|
2021-05-23 17:53:46 +02:00
|
|
|
updatePlayerCursorState(false)
|
2020-12-23 01:19:30 +01:00
|
|
|
} else if (type === Protocol.INPUT_EV_ZOOM_IN) {
|
2021-04-15 10:01:27 +02:00
|
|
|
const pos = { x: evt[1], y: evt[2] }
|
2021-05-13 23:38:36 +02:00
|
|
|
RERENDER = true
|
|
|
|
|
viewport.zoom('in', viewport.worldToViewport(pos))
|
2020-12-23 01:19:30 +01:00
|
|
|
} else if (type === Protocol.INPUT_EV_ZOOM_OUT) {
|
2021-04-15 10:01:27 +02:00
|
|
|
const pos = { x: evt[1], y: evt[2] }
|
2021-05-13 23:38:36 +02:00
|
|
|
RERENDER = true
|
|
|
|
|
viewport.zoom('out', viewport.worldToViewport(pos))
|
2021-05-09 22:17:41 +02:00
|
|
|
} else if (type === Protocol.INPUT_EV_TOGGLE_PREVIEW) {
|
2021-05-13 22:45:55 +02:00
|
|
|
HUD.togglePreview()
|
2020-11-25 22:03:35 +01:00
|
|
|
}
|
2020-12-22 22:35:09 +01:00
|
|
|
|
|
|
|
|
// LOCAL + SERVER CHANGES
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
const ts = TIME()
|
2021-05-13 23:38:36 +02:00
|
|
|
const changes = Game.handleInput(gameId, clientId, evt, ts)
|
2020-12-22 22:35:09 +01:00
|
|
|
if (changes.length > 0) {
|
2020-11-25 22:03:35 +01:00
|
|
|
RERENDER = true
|
|
|
|
|
}
|
2020-12-22 22:35:09 +01:00
|
|
|
Communication.sendClientEvent(evt)
|
2021-04-17 17:23:32 +02:00
|
|
|
} else if (MODE === MODE_REPLAY) {
|
2020-12-22 22:35:09 +01:00
|
|
|
// LOCAL ONLY CHANGES
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
const type = evt[0]
|
2021-05-10 01:13:23 +02:00
|
|
|
if (type === Protocol.INPUT_EV_MOVE) {
|
|
|
|
|
const diffX = evt[1]
|
|
|
|
|
const diffY = evt[2]
|
|
|
|
|
RERENDER = true
|
|
|
|
|
viewport.move(diffX, diffY)
|
|
|
|
|
} else if (type === Protocol.INPUT_EV_MOUSE_MOVE) {
|
2020-12-22 22:35:09 +01:00
|
|
|
if (_last_mouse_down) {
|
|
|
|
|
// move the cam
|
|
|
|
|
const pos = { x: evt[1], y: evt[2] }
|
|
|
|
|
const mouse = viewport.worldToViewport(pos)
|
|
|
|
|
const diffX = Math.round(mouse.x - _last_mouse_down.x)
|
|
|
|
|
const diffY = Math.round(mouse.y - _last_mouse_down.y)
|
|
|
|
|
RERENDER = true
|
|
|
|
|
viewport.move(diffX, diffY)
|
|
|
|
|
|
|
|
|
|
_last_mouse_down = mouse
|
|
|
|
|
}
|
2020-12-23 01:19:30 +01:00
|
|
|
} else if (type === Protocol.INPUT_EV_MOUSE_DOWN) {
|
2020-11-25 22:03:35 +01:00
|
|
|
const pos = { x: evt[1], y: evt[2] }
|
2020-12-22 22:35:09 +01:00
|
|
|
_last_mouse_down = viewport.worldToViewport(pos)
|
2020-12-23 01:19:30 +01:00
|
|
|
} else if (type === Protocol.INPUT_EV_MOUSE_UP) {
|
2020-12-22 22:35:09 +01:00
|
|
|
_last_mouse_down = null
|
2020-12-23 01:19:30 +01:00
|
|
|
} else if (type === Protocol.INPUT_EV_ZOOM_IN) {
|
2021-04-15 10:01:27 +02:00
|
|
|
const pos = { x: evt[1], y: evt[2] }
|
2021-05-13 23:38:36 +02:00
|
|
|
RERENDER = true
|
|
|
|
|
viewport.zoom('in', viewport.worldToViewport(pos))
|
2020-12-23 01:19:30 +01:00
|
|
|
} else if (type === Protocol.INPUT_EV_ZOOM_OUT) {
|
2021-04-15 10:01:27 +02:00
|
|
|
const pos = { x: evt[1], y: evt[2] }
|
2021-05-13 23:38:36 +02:00
|
|
|
RERENDER = true
|
|
|
|
|
viewport.zoom('out', viewport.worldToViewport(pos))
|
2021-05-09 22:17:41 +02:00
|
|
|
} else if (type === Protocol.INPUT_EV_TOGGLE_PREVIEW) {
|
2021-05-13 22:45:55 +02:00
|
|
|
HUD.togglePreview()
|
2020-11-25 22:03:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-09 01:27:47 +01:00
|
|
|
|
2021-04-15 12:16:14 +02:00
|
|
|
finished = !! Game.getFinishTs(gameId)
|
2020-12-09 01:27:47 +01:00
|
|
|
if (justFinished()) {
|
|
|
|
|
fireworks.update()
|
|
|
|
|
RERENDER = true
|
|
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-29 17:58:05 +02:00
|
|
|
const onRender = async (): Promise<void> => {
|
2020-11-25 22:03:35 +01:00
|
|
|
if (!RERENDER) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 01:13:23 +02:00
|
|
|
const ts = TIME()
|
|
|
|
|
|
2021-05-17 02:32:33 +02:00
|
|
|
let pos: Point
|
|
|
|
|
let dim: Dim
|
|
|
|
|
let bmp: ImageBitmap
|
2020-11-25 22:03:35 +01:00
|
|
|
|
2021-05-17 00:27:47 +02:00
|
|
|
if (window.DEBUG) Debug.checkpoint_start(0)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
|
|
|
// CLEAR CTX
|
|
|
|
|
// ---------------------------------------------------------------
|
2020-12-07 12:20:09 +01:00
|
|
|
ctx.fillStyle = playerBgColor()
|
2020-11-25 22:03:35 +01:00
|
|
|
ctx.fillRect(0, 0, canvas.width, canvas.height)
|
2021-05-17 00:27:47 +02:00
|
|
|
if (window.DEBUG) Debug.checkpoint('clear done')
|
2020-11-25 22:03:35 +01:00
|
|
|
// ---------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DRAW BOARD
|
|
|
|
|
// ---------------------------------------------------------------
|
2021-05-10 01:13:23 +02:00
|
|
|
pos = viewport.worldToViewportRaw(BOARD_POS)
|
|
|
|
|
dim = viewport.worldDimToViewportRaw(BOARD_DIM)
|
2020-12-24 20:17:01 +01:00
|
|
|
ctx.fillStyle = 'rgba(255, 255, 255, .3)'
|
2020-11-25 22:03:35 +01:00
|
|
|
ctx.fillRect(pos.x, pos.y, dim.w, dim.h)
|
2021-05-17 00:27:47 +02:00
|
|
|
if (window.DEBUG) Debug.checkpoint('board done')
|
2020-11-25 22:03:35 +01:00
|
|
|
// ---------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DRAW TILES
|
|
|
|
|
// ---------------------------------------------------------------
|
2021-05-29 15:36:03 +02:00
|
|
|
const tiles = Game.getPiecesSortedByZIndex(gameId)
|
2021-05-17 00:27:47 +02:00
|
|
|
if (window.DEBUG) Debug.checkpoint('get tiles done')
|
2021-04-15 12:42:47 +02:00
|
|
|
|
2021-05-10 01:13:23 +02:00
|
|
|
dim = viewport.worldDimToViewportRaw(PIECE_DIM)
|
2021-05-09 22:17:41 +02:00
|
|
|
for (const tile of tiles) {
|
2021-05-10 01:13:23 +02:00
|
|
|
if (!shouldDrawPiece(tile)) {
|
|
|
|
|
continue
|
2021-04-27 22:56:50 +02:00
|
|
|
}
|
2021-05-10 01:13:23 +02:00
|
|
|
bmp = bitmaps[tile.idx]
|
2020-12-24 20:17:01 +01:00
|
|
|
pos = viewport.worldToViewportRaw({
|
2021-05-29 15:36:03 +02:00
|
|
|
x: PIECE_DRAW_OFFSET + tile.pos.x,
|
|
|
|
|
y: PIECE_DRAW_OFFSET + tile.pos.y,
|
2020-11-25 22:03:35 +01:00
|
|
|
})
|
|
|
|
|
ctx.drawImage(bmp,
|
|
|
|
|
0, 0, bmp.width, bmp.height,
|
|
|
|
|
pos.x, pos.y, dim.w, dim.h
|
|
|
|
|
)
|
|
|
|
|
}
|
2021-05-17 00:27:47 +02:00
|
|
|
if (window.DEBUG) Debug.checkpoint('tiles done')
|
2020-11-25 22:03:35 +01:00
|
|
|
// ---------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DRAW PLAYERS
|
|
|
|
|
// ---------------------------------------------------------------
|
2021-05-17 00:27:47 +02:00
|
|
|
const texts: Array<FixedLengthArray<[string, number, number]>> = []
|
2021-04-17 17:23:32 +02:00
|
|
|
// Cursors
|
2021-05-10 01:13:23 +02:00
|
|
|
for (const p of Game.getActivePlayers(gameId, ts)) {
|
2021-05-23 17:53:46 +02:00
|
|
|
if (shouldDrawPlayer(p)) {
|
|
|
|
|
bmp = await getPlayerCursor(p)
|
|
|
|
|
pos = viewport.worldToViewport(p)
|
|
|
|
|
ctx.drawImage(bmp, pos.x - CURSOR_W_2, pos.y - CURSOR_H_2)
|
2021-04-17 17:23:32 +02:00
|
|
|
// performance:
|
|
|
|
|
// not drawing text directly here, to have less ctx
|
|
|
|
|
// switches between drawImage and fillTxt
|
2021-05-10 01:13:23 +02:00
|
|
|
texts.push([`${p.name} (${p.points})`, pos.x, pos.y + CURSOR_H])
|
2020-11-25 22:03:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
2021-04-17 17:23:32 +02:00
|
|
|
|
|
|
|
|
// Names
|
|
|
|
|
ctx.fillStyle = 'white'
|
|
|
|
|
ctx.textAlign = 'center'
|
2021-05-09 22:17:41 +02:00
|
|
|
for (const [txt, x, y] of texts) {
|
2021-04-17 17:23:32 +02:00
|
|
|
ctx.fillText(txt, x, y)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-17 00:27:47 +02:00
|
|
|
if (window.DEBUG) Debug.checkpoint('players done')
|
2020-11-25 22:03:35 +01:00
|
|
|
|
2021-05-13 22:45:55 +02:00
|
|
|
// propagate HUD changes
|
2020-11-25 22:03:35 +01:00
|
|
|
// ---------------------------------------------------------------
|
2021-05-13 23:38:36 +02:00
|
|
|
HUD.setActivePlayers(Game.getActivePlayers(gameId, ts))
|
|
|
|
|
HUD.setIdlePlayers(Game.getIdlePlayers(gameId, ts))
|
2021-05-29 15:36:03 +02:00
|
|
|
HUD.setPiecesDone(Game.getFinishedPiecesCount(gameId))
|
2021-05-17 00:27:47 +02:00
|
|
|
if (window.DEBUG) Debug.checkpoint('HUD done')
|
2020-11-25 22:03:35 +01:00
|
|
|
// ---------------------------------------------------------------
|
|
|
|
|
|
2020-12-09 01:27:47 +01:00
|
|
|
if (justFinished()) {
|
|
|
|
|
fireworks.render()
|
|
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
|
|
|
RERENDER = false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
run({
|
|
|
|
|
update: onUpdate,
|
|
|
|
|
render: onRender,
|
|
|
|
|
})
|
|
|
|
|
|
2021-05-13 22:45:55 +02:00
|
|
|
return {
|
2021-05-17 00:27:47 +02:00
|
|
|
setHotkeys: (state: boolean) => {
|
2021-05-13 22:45:55 +02:00
|
|
|
evts.setHotkeys(state)
|
|
|
|
|
},
|
2021-05-17 00:27:47 +02:00
|
|
|
onBgChange: (value: string) => {
|
2021-05-13 23:38:36 +02:00
|
|
|
localStorage.setItem('bg_color', value)
|
|
|
|
|
evts.addEvent([Protocol.INPUT_EV_BG_COLOR, value])
|
|
|
|
|
},
|
2021-05-17 00:27:47 +02:00
|
|
|
onColorChange: (value: string) => {
|
2021-05-13 23:38:36 +02:00
|
|
|
localStorage.setItem('player_color', value)
|
|
|
|
|
evts.addEvent([Protocol.INPUT_EV_PLAYER_COLOR, value])
|
|
|
|
|
},
|
2021-05-17 00:27:47 +02:00
|
|
|
onNameChange: (value: string) => {
|
2021-05-13 23:38:36 +02:00
|
|
|
localStorage.setItem('player_name', value)
|
|
|
|
|
evts.addEvent([Protocol.INPUT_EV_PLAYER_NAME, value])
|
|
|
|
|
},
|
2021-05-13 22:45:55 +02:00
|
|
|
replayOnSpeedUp,
|
|
|
|
|
replayOnSpeedDown,
|
|
|
|
|
replayOnPauseToggle,
|
|
|
|
|
previewImageUrl,
|
|
|
|
|
player: {
|
|
|
|
|
background: playerBgColor(),
|
|
|
|
|
color: playerColor(),
|
|
|
|
|
name: playerName(),
|
|
|
|
|
},
|
|
|
|
|
disconnect: Communication.disconnect,
|
2021-05-15 20:04:30 +02:00
|
|
|
connect: connect,
|
2021-05-13 22:45:55 +02:00
|
|
|
}
|
|
|
|
|
}
|