2020-11-25 22:03:35 +01:00
|
|
|
"use strict"
|
|
|
|
|
import {run} from './gameloop.js'
|
|
|
|
|
import Camera from './Camera.js'
|
|
|
|
|
import Graphics from './Graphics.js'
|
|
|
|
|
import Debug from './Debug.js'
|
|
|
|
|
import Communication from './Communication.js'
|
|
|
|
|
import Util from './../common/Util.js'
|
|
|
|
|
import PuzzleGraphics from './PuzzleGraphics.js'
|
|
|
|
|
import Game from './Game.js'
|
2020-12-09 01:27:47 +01:00
|
|
|
import fireworksController from './Fireworks.js'
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
|
|
|
if (typeof GAME_ID === 'undefined') throw '[ GAME_ID not set ]'
|
|
|
|
|
if (typeof WS_ADDRESS === 'undefined') throw '[ WS_ADDRESS not set ]'
|
|
|
|
|
|
|
|
|
|
if (typeof DEBUG === 'undefined') window.DEBUG = false
|
|
|
|
|
|
|
|
|
|
let RERENDER = true
|
|
|
|
|
|
|
|
|
|
function addCanvasToDom(canvas) {
|
|
|
|
|
canvas.width = window.innerWidth
|
|
|
|
|
canvas.height = window.innerHeight
|
|
|
|
|
document.body.appendChild(canvas)
|
|
|
|
|
window.addEventListener('resize', () => {
|
|
|
|
|
canvas.width = window.innerWidth
|
|
|
|
|
canvas.height = window.innerHeight
|
|
|
|
|
RERENDER = true
|
|
|
|
|
})
|
|
|
|
|
return canvas
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-07 02:38:07 +01:00
|
|
|
function addMenuToDom(gameId) {
|
|
|
|
|
const previewImageUrl = Game.getImageUrl(gameId)
|
2020-11-25 22:03:35 +01:00
|
|
|
function row (...elements) {
|
|
|
|
|
const row = document.createElement('tr')
|
|
|
|
|
for (let el of elements) {
|
|
|
|
|
const td = document.createElement('td')
|
|
|
|
|
td.appendChild(el)
|
|
|
|
|
row.appendChild(td)
|
|
|
|
|
}
|
|
|
|
|
return row
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function colorinput() {
|
|
|
|
|
const input = document.createElement('input')
|
|
|
|
|
input.type = 'color'
|
|
|
|
|
return input
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function textinput(maxLength) {
|
|
|
|
|
const input = document.createElement('input')
|
|
|
|
|
input.type = 'text'
|
|
|
|
|
input.maxLength = maxLength
|
|
|
|
|
return input
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function label(text) {
|
|
|
|
|
const label = document.createElement('label')
|
|
|
|
|
label.innerText = text
|
|
|
|
|
return label
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bgColorPickerEl = colorinput()
|
|
|
|
|
const bgColorPickerRow = row(
|
|
|
|
|
label('Background: '),
|
|
|
|
|
bgColorPickerEl
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const playerColorPickerEl = colorinput()
|
|
|
|
|
const playerColorPickerRow = row(
|
|
|
|
|
label('Color: '),
|
|
|
|
|
playerColorPickerEl
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const nameChangeEl = textinput(16)
|
|
|
|
|
const nameChangeRow = row(
|
|
|
|
|
label('Name: '),
|
|
|
|
|
nameChangeEl
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const settingsEl = document.createElement('table')
|
2020-12-06 21:55:23 +01:00
|
|
|
settingsEl.classList.add('settings')
|
2020-11-25 22:03:35 +01:00
|
|
|
settingsEl.appendChild(bgColorPickerRow)
|
|
|
|
|
settingsEl.appendChild(playerColorPickerRow)
|
|
|
|
|
settingsEl.appendChild(nameChangeRow)
|
2020-12-06 21:55:23 +01:00
|
|
|
settingsEl.addEventListener('click', (e) => {
|
|
|
|
|
e.stopPropagation()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const settingsOverlay = document.createElement('div')
|
|
|
|
|
settingsOverlay.classList.add('overlay', 'transparent', 'closed')
|
|
|
|
|
settingsOverlay.appendChild(settingsEl)
|
|
|
|
|
settingsOverlay.addEventListener('click', () => {
|
|
|
|
|
settingsOverlay.classList.toggle('closed')
|
|
|
|
|
})
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
|
|
|
const previewEl = document.createElement('div')
|
|
|
|
|
previewEl.classList.add('preview')
|
|
|
|
|
|
|
|
|
|
const imgEl = document.createElement('div')
|
|
|
|
|
imgEl.classList.add('img')
|
|
|
|
|
imgEl.style.backgroundImage = `url(${previewImageUrl})`
|
|
|
|
|
previewEl.appendChild(imgEl)
|
|
|
|
|
|
|
|
|
|
const previewOverlay = document.createElement('div')
|
|
|
|
|
previewOverlay.classList.add('overlay', 'closed')
|
|
|
|
|
previewOverlay.appendChild(previewEl)
|
|
|
|
|
previewOverlay.addEventListener('click', () => {
|
|
|
|
|
previewOverlay.classList.toggle('closed')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const settingsOpenerEl = document.createElement('div')
|
|
|
|
|
settingsOpenerEl.classList.add('opener')
|
|
|
|
|
settingsOpenerEl.appendChild(document.createTextNode('🛠️ Settings'))
|
|
|
|
|
settingsOpenerEl.addEventListener('click', () => {
|
2020-12-06 21:55:23 +01:00
|
|
|
settingsOverlay.classList.toggle('closed')
|
2020-11-25 22:03:35 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const homeEl = document.createElement('a')
|
|
|
|
|
homeEl.classList.add('opener')
|
|
|
|
|
homeEl.appendChild(document.createTextNode('🧩 Puzzles'))
|
|
|
|
|
homeEl.href = "/"
|
|
|
|
|
|
|
|
|
|
const previewOpenerEl = document.createElement('div')
|
|
|
|
|
previewOpenerEl.classList.add('opener')
|
|
|
|
|
previewOpenerEl.appendChild(document.createTextNode('🖼️ Preview'))
|
|
|
|
|
previewOpenerEl.addEventListener('click', () => {
|
|
|
|
|
previewOverlay.classList.toggle('closed')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const tabsEl = document.createElement('div')
|
|
|
|
|
tabsEl.classList.add('tabs')
|
|
|
|
|
tabsEl.appendChild(homeEl)
|
|
|
|
|
tabsEl.appendChild(previewOpenerEl)
|
|
|
|
|
tabsEl.appendChild(settingsOpenerEl)
|
|
|
|
|
|
|
|
|
|
const menuEl = document.createElement('div')
|
|
|
|
|
menuEl.classList.add('menu')
|
|
|
|
|
menuEl.appendChild(tabsEl)
|
|
|
|
|
|
|
|
|
|
const scoresTitleEl = document.createElement('div')
|
|
|
|
|
scoresTitleEl.appendChild(document.createTextNode('Scores'))
|
|
|
|
|
|
|
|
|
|
const scoresListEl = document.createElement('table')
|
2020-12-07 02:38:07 +01:00
|
|
|
const updateScores = () => {
|
2020-12-05 19:45:34 +01:00
|
|
|
const activePlayers = Game.getActivePlayers(gameId)
|
2020-11-25 22:03:35 +01:00
|
|
|
const scores = activePlayers.map(p => ({
|
|
|
|
|
name: p.name,
|
|
|
|
|
points: p.points,
|
|
|
|
|
color: p.color,
|
|
|
|
|
}))
|
|
|
|
|
scores.sort((a, b) => b.points - a.points)
|
|
|
|
|
scoresListEl.innerHTML = ''
|
|
|
|
|
for (let score of scores) {
|
|
|
|
|
const r = row(
|
|
|
|
|
document.createTextNode(score.name),
|
|
|
|
|
document.createTextNode(score.points)
|
|
|
|
|
)
|
|
|
|
|
r.style.color = score.color
|
|
|
|
|
scoresListEl.appendChild(r)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-07 02:38:07 +01:00
|
|
|
const timerStr = () => {
|
|
|
|
|
const started = Game.getStartTs(gameId)
|
|
|
|
|
const ended = Game.getFinishTs(gameId)
|
|
|
|
|
|
|
|
|
|
const icon = ended ? '🏁' : '⏳'
|
|
|
|
|
|
|
|
|
|
const from = started;
|
|
|
|
|
const to = ended || Util.timestamp()
|
|
|
|
|
|
|
|
|
|
const MS = 1
|
|
|
|
|
const SEC = MS * 1000
|
|
|
|
|
const MIN = SEC * 60
|
|
|
|
|
const HOUR = MIN * 60
|
|
|
|
|
const DAY = HOUR * 24
|
|
|
|
|
|
|
|
|
|
let diff = to - from
|
|
|
|
|
const d = Math.floor(diff / DAY)
|
|
|
|
|
diff = diff % DAY
|
|
|
|
|
|
|
|
|
|
const h = Math.floor(diff / HOUR)
|
|
|
|
|
diff = diff % HOUR
|
|
|
|
|
|
|
|
|
|
const m = Math.floor(diff / MIN)
|
|
|
|
|
diff = diff % MIN
|
|
|
|
|
|
|
|
|
|
const s = Math.floor(diff / SEC)
|
|
|
|
|
|
|
|
|
|
return `${icon} ${d}d ${h}h ${m}m ${s}s`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const timerCountdownEl = document.createElement('div')
|
|
|
|
|
timerCountdownEl.innerText = timerStr()
|
|
|
|
|
setInterval(() => {
|
|
|
|
|
timerCountdownEl.innerText = timerStr()
|
|
|
|
|
}, 1000)
|
|
|
|
|
|
|
|
|
|
const timerEl = document.createElement('div')
|
|
|
|
|
timerEl.classList.add('timer')
|
|
|
|
|
timerEl.appendChild(timerCountdownEl)
|
|
|
|
|
|
2020-11-25 22:03:35 +01:00
|
|
|
const scoresEl = document.createElement('div')
|
|
|
|
|
scoresEl.classList.add('scores')
|
|
|
|
|
scoresEl.appendChild(scoresTitleEl)
|
|
|
|
|
scoresEl.appendChild(scoresListEl)
|
|
|
|
|
|
2020-12-06 21:55:23 +01:00
|
|
|
document.body.appendChild(settingsOverlay)
|
2020-11-25 22:03:35 +01:00
|
|
|
document.body.appendChild(previewOverlay)
|
2020-12-07 02:38:07 +01:00
|
|
|
document.body.appendChild(timerEl)
|
2020-11-25 22:03:35 +01:00
|
|
|
document.body.appendChild(menuEl)
|
|
|
|
|
document.body.appendChild(scoresEl)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
bgColorPickerEl,
|
|
|
|
|
playerColorPickerEl,
|
|
|
|
|
nameChangeEl,
|
|
|
|
|
updateScores,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function initme() {
|
|
|
|
|
// return uniqId()
|
|
|
|
|
let ID = localStorage.getItem('ID')
|
|
|
|
|
if (!ID) {
|
|
|
|
|
ID = Util.uniqId()
|
|
|
|
|
localStorage.setItem('ID', ID)
|
|
|
|
|
}
|
|
|
|
|
return ID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default class EventAdapter {
|
|
|
|
|
constructor(canvas, viewport) {
|
|
|
|
|
this.events = []
|
|
|
|
|
this._viewport = viewport
|
|
|
|
|
canvas.addEventListener('mousedown', this._mouseDown.bind(this))
|
|
|
|
|
canvas.addEventListener('mouseup', this._mouseUp.bind(this))
|
|
|
|
|
canvas.addEventListener('mousemove', this._mouseMove.bind(this))
|
|
|
|
|
canvas.addEventListener('wheel', this._wheel.bind(this))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addEvent(event) {
|
|
|
|
|
this.events.push(event)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
consumeAll() {
|
|
|
|
|
if (this.events.length === 0) {
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
const all = this.events.slice()
|
|
|
|
|
this.events = []
|
|
|
|
|
return all
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_mouseDown(e) {
|
|
|
|
|
if (e.button === 0) {
|
|
|
|
|
const pos = this._viewport.viewportToWorld({
|
|
|
|
|
x: e.offsetX,
|
|
|
|
|
y: e.offsetY,
|
|
|
|
|
})
|
|
|
|
|
this.addEvent(['down', pos.x, pos.y])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_mouseUp(e) {
|
|
|
|
|
if (e.button === 0) {
|
|
|
|
|
const pos = this._viewport.viewportToWorld({
|
|
|
|
|
x: e.offsetX,
|
|
|
|
|
y: e.offsetY,
|
|
|
|
|
})
|
|
|
|
|
this.addEvent(['up', pos.x, pos.y])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_mouseMove(e) {
|
|
|
|
|
const pos = this._viewport.viewportToWorld({
|
|
|
|
|
x: e.offsetX,
|
|
|
|
|
y: e.offsetY,
|
|
|
|
|
})
|
|
|
|
|
this.addEvent(['move', pos.x, pos.y])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_wheel(e) {
|
|
|
|
|
const pos = this._viewport.viewportToWorld({
|
|
|
|
|
x: e.offsetX,
|
|
|
|
|
y: e.offsetY,
|
|
|
|
|
})
|
|
|
|
|
const evt = e.deltaY < 0 ? 'zoomin' : 'zoomout'
|
|
|
|
|
this.addEvent([evt, pos.x, pos.y])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
|
let gameId = GAME_ID
|
|
|
|
|
let CLIENT_ID = initme()
|
|
|
|
|
|
|
|
|
|
const cursorGrab = await Graphics.loadImageToBitmap('/grab.png')
|
|
|
|
|
const cursorHand = await Graphics.loadImageToBitmap('/hand.png')
|
|
|
|
|
const cursorGrabMask = await Graphics.loadImageToBitmap('/grab_mask.png')
|
|
|
|
|
const cursorHandMask = await Graphics.loadImageToBitmap('/hand_mask.png')
|
|
|
|
|
|
|
|
|
|
const cursors = {}
|
|
|
|
|
const getPlayerCursor = async (p) => {
|
|
|
|
|
let key = p.color + ' ' + p.d
|
|
|
|
|
if (!cursors[key]) {
|
|
|
|
|
const cursor = p.d ? cursorGrab : cursorHand
|
|
|
|
|
const mask = p.d ? cursorGrabMask : cursorHandMask
|
|
|
|
|
cursors[key] = await Graphics.colorize(cursor, mask, p.color)
|
|
|
|
|
}
|
|
|
|
|
return cursors[key]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const game = await Communication.connect(gameId, CLIENT_ID)
|
2020-12-05 19:45:34 +01:00
|
|
|
Game.newGame(game)
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
|
|
|
const bitmaps = await PuzzleGraphics.loadPuzzleBitmaps(game.puzzle)
|
|
|
|
|
|
2020-12-07 02:38:07 +01:00
|
|
|
const {bgColorPickerEl, playerColorPickerEl, nameChangeEl, updateScores} = addMenuToDom(gameId)
|
|
|
|
|
updateScores()
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
|
|
|
// Create a dom and attach adapters to it so we can work with it
|
|
|
|
|
const canvas = addCanvasToDom(Graphics.createCanvas())
|
2020-12-09 01:27:47 +01:00
|
|
|
|
|
|
|
|
const longFinished = Game.getFinishTs(gameId)
|
|
|
|
|
let finished = longFinished ? true : false
|
|
|
|
|
const justFinished = () => !!(finished && !longFinished)
|
|
|
|
|
|
|
|
|
|
const fireworks = new fireworksController(canvas)
|
|
|
|
|
fireworks.init(canvas)
|
|
|
|
|
|
2020-11-25 22:03:35 +01:00
|
|
|
const ctx = canvas.getContext('2d')
|
|
|
|
|
|
|
|
|
|
// initialize some view data
|
|
|
|
|
// this global data will change according to input events
|
|
|
|
|
const viewport = new Camera(canvas)
|
|
|
|
|
// center viewport
|
|
|
|
|
viewport.move(
|
2020-12-05 19:45:34 +01:00
|
|
|
-(Game.getTableWidth(gameId) - viewport.width) /2,
|
|
|
|
|
-(Game.getTableHeight(gameId) - viewport.height) /2
|
2020-11-25 22:03:35 +01:00
|
|
|
)
|
|
|
|
|
|
2020-12-07 12:20:09 +01:00
|
|
|
const playerBgColor = () => {
|
|
|
|
|
return (Game.getPlayerBgColor(gameId, CLIENT_ID)
|
|
|
|
|
|| localStorage.getItem('bg_color')
|
|
|
|
|
|| '#222222')
|
|
|
|
|
}
|
|
|
|
|
const playerColor = () => {
|
|
|
|
|
return (Game.getPlayerColor(gameId, CLIENT_ID)
|
|
|
|
|
|| localStorage.getItem('player_color')
|
|
|
|
|
|| '#ffffff')
|
|
|
|
|
}
|
|
|
|
|
const playerName = () => {
|
|
|
|
|
return (Game.getPlayerName(gameId, CLIENT_ID)
|
|
|
|
|
|| localStorage.getItem('player_name')
|
|
|
|
|
|| 'anon')
|
|
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
|
2020-12-07 12:20:09 +01:00
|
|
|
const evts = new EventAdapter(canvas, viewport)
|
|
|
|
|
bgColorPickerEl.value = playerBgColor()
|
|
|
|
|
evts.addEvent(['bg_color', bgColorPickerEl.value])
|
2020-11-25 22:03:35 +01:00
|
|
|
bgColorPickerEl.addEventListener('change', () => {
|
2020-12-07 12:20:09 +01:00
|
|
|
localStorage.setItem('bg_color', bgColorPickerEl.value)
|
2020-11-25 22:03:35 +01:00
|
|
|
evts.addEvent(['bg_color', bgColorPickerEl.value])
|
|
|
|
|
})
|
2020-12-07 12:20:09 +01:00
|
|
|
playerColorPickerEl.value = playerColor()
|
|
|
|
|
evts.addEvent(['player_color', playerColorPickerEl.value])
|
2020-11-25 22:03:35 +01:00
|
|
|
playerColorPickerEl.addEventListener('change', () => {
|
2020-12-07 12:20:09 +01:00
|
|
|
localStorage.setItem('player_color', playerColorPickerEl.value)
|
2020-11-25 22:03:35 +01:00
|
|
|
evts.addEvent(['player_color', playerColorPickerEl.value])
|
|
|
|
|
})
|
2020-12-07 12:20:09 +01:00
|
|
|
nameChangeEl.value = playerName()
|
|
|
|
|
evts.addEvent(['player_name', nameChangeEl.value])
|
2020-11-25 22:03:35 +01:00
|
|
|
nameChangeEl.addEventListener('change', () => {
|
2020-12-07 12:20:09 +01:00
|
|
|
localStorage.setItem('player_name', nameChangeEl.value)
|
2020-11-25 22:03:35 +01:00
|
|
|
evts.addEvent(['player_name', nameChangeEl.value])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Communication.onServerChange((msg) => {
|
|
|
|
|
const msgType = msg[0]
|
|
|
|
|
const evClientId = msg[1]
|
|
|
|
|
const evClientSeq = msg[2]
|
|
|
|
|
const evChanges = msg[3]
|
|
|
|
|
for(let [changeType, changeData] of evChanges) {
|
|
|
|
|
switch (changeType) {
|
|
|
|
|
case 'player': {
|
2020-12-05 19:45:34 +01:00
|
|
|
const p = Util.decodePlayer(changeData)
|
|
|
|
|
if (p.id !== CLIENT_ID) {
|
|
|
|
|
Game.setPlayer(gameId, p.id, p)
|
2020-11-25 22:03:35 +01:00
|
|
|
RERENDER = true
|
|
|
|
|
}
|
|
|
|
|
} break;
|
|
|
|
|
case 'tile': {
|
2020-12-05 19:45:34 +01:00
|
|
|
const t = Util.decodeTile(changeData)
|
|
|
|
|
Game.setTile(gameId, t.idx, t)
|
2020-11-25 22:03:35 +01:00
|
|
|
RERENDER = true
|
|
|
|
|
} break;
|
|
|
|
|
case 'data': {
|
2020-12-05 19:45:34 +01:00
|
|
|
Game.setPuzzleData(gameId, changeData)
|
2020-11-25 22:03:35 +01:00
|
|
|
RERENDER = true
|
|
|
|
|
} break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-09 01:27:47 +01:00
|
|
|
finished = Game.getFinishTs(gameId)
|
2020-11-25 22:03:35 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
let _last_mouse_down = null
|
|
|
|
|
const onUpdate = () => {
|
|
|
|
|
for (let evt of evts.consumeAll()) {
|
|
|
|
|
|
|
|
|
|
// LOCAL ONLY CHANGES
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
const type = evt[0]
|
2020-12-03 21:24:59 +01:00
|
|
|
if (type === 'move') {
|
2020-12-05 19:45:34 +01:00
|
|
|
if (_last_mouse_down && !Game.getFirstOwnedTile(gameId, CLIENT_ID)) {
|
2020-11-25 22:03:35 +01:00
|
|
|
// move the cam
|
2020-12-05 19:45:34 +01:00
|
|
|
const pos = { x: evt[1], y: evt[2] }
|
2020-11-25 22:03:35 +01:00
|
|
|
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)
|
|
|
|
|
viewport.move(diffX, diffY)
|
|
|
|
|
|
|
|
|
|
_last_mouse_down = mouse
|
|
|
|
|
}
|
|
|
|
|
} else if (type === 'down') {
|
|
|
|
|
const pos = { x: evt[1], y: evt[2] }
|
|
|
|
|
_last_mouse_down = viewport.worldToViewport(pos)
|
|
|
|
|
} else if (type === 'up') {
|
|
|
|
|
_last_mouse_down = null
|
|
|
|
|
} else if (type === 'zoomin') {
|
|
|
|
|
if (viewport.zoomIn()) {
|
|
|
|
|
const pos = { x: evt[1], y: evt[2] }
|
|
|
|
|
RERENDER = true
|
2020-12-05 19:45:34 +01:00
|
|
|
Game.changePlayer(gameId, CLIENT_ID, pos)
|
2020-11-25 22:03:35 +01:00
|
|
|
}
|
|
|
|
|
} else if (type === 'zoomout') {
|
|
|
|
|
if (viewport.zoomOut()) {
|
|
|
|
|
const pos = { x: evt[1], y: evt[2] }
|
|
|
|
|
RERENDER = true
|
2020-12-05 19:45:34 +01:00
|
|
|
Game.changePlayer(gameId, CLIENT_ID, pos)
|
2020-11-25 22:03:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LOCAL + SERVER CHANGES
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
const changes = Game.handleInput(GAME_ID, CLIENT_ID, evt)
|
|
|
|
|
if (changes.length > 0) {
|
|
|
|
|
RERENDER = true
|
|
|
|
|
}
|
|
|
|
|
Communication.sendClientEvent(evt)
|
|
|
|
|
}
|
2020-12-09 01:27:47 +01:00
|
|
|
|
|
|
|
|
finished = Game.getFinishTs(gameId)
|
|
|
|
|
if (justFinished()) {
|
|
|
|
|
fireworks.update()
|
|
|
|
|
RERENDER = true
|
|
|
|
|
}
|
2020-11-25 22:03:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const onRender = async () => {
|
|
|
|
|
if (!RERENDER) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let pos
|
|
|
|
|
let dim
|
|
|
|
|
|
|
|
|
|
if (DEBUG) Debug.checkpoint_start(0)
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
if (DEBUG) Debug.checkpoint('clear done')
|
|
|
|
|
// ---------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DRAW BOARD
|
|
|
|
|
// ---------------------------------------------------------------
|
|
|
|
|
pos = viewport.worldToViewport({
|
2020-12-05 19:45:34 +01:00
|
|
|
x: (Game.getTableWidth(gameId) - Game.getPuzzleWidth(gameId)) / 2,
|
|
|
|
|
y: (Game.getTableHeight(gameId) - Game.getPuzzleHeight(gameId)) / 2
|
2020-11-25 22:03:35 +01:00
|
|
|
})
|
|
|
|
|
dim = viewport.worldDimToViewport({
|
2020-12-05 19:45:34 +01:00
|
|
|
w: Game.getPuzzleWidth(gameId),
|
|
|
|
|
h: Game.getPuzzleHeight(gameId),
|
2020-11-25 22:03:35 +01:00
|
|
|
})
|
|
|
|
|
ctx.fillStyle = 'rgba(255, 255, 255, .5)'
|
|
|
|
|
ctx.fillRect(pos.x, pos.y, dim.w, dim.h)
|
|
|
|
|
if (DEBUG) Debug.checkpoint('board done')
|
|
|
|
|
// ---------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DRAW TILES
|
|
|
|
|
// ---------------------------------------------------------------
|
2020-12-05 19:45:34 +01:00
|
|
|
for (let tile of Game.getTilesSortedByZIndex(gameId)) {
|
2020-11-25 22:03:35 +01:00
|
|
|
const bmp = bitmaps[tile.idx]
|
|
|
|
|
pos = viewport.worldToViewport({
|
2020-12-05 19:45:34 +01:00
|
|
|
x: Game.getTileDrawOffset(gameId) + tile.pos.x,
|
|
|
|
|
y: Game.getTileDrawOffset(gameId) + tile.pos.y,
|
2020-11-25 22:03:35 +01:00
|
|
|
})
|
|
|
|
|
dim = viewport.worldDimToViewport({
|
2020-12-05 19:45:34 +01:00
|
|
|
w: Game.getTileDrawSize(gameId),
|
|
|
|
|
h: Game.getTileDrawSize(gameId),
|
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
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
if (DEBUG) Debug.checkpoint('tiles done')
|
|
|
|
|
// ---------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DRAW PLAYERS
|
|
|
|
|
// ---------------------------------------------------------------
|
2020-12-05 19:45:34 +01:00
|
|
|
for (let player of Game.getActivePlayers(gameId)) {
|
|
|
|
|
const cursor = await getPlayerCursor(player)
|
|
|
|
|
const pos = viewport.worldToViewport(player)
|
2020-11-25 22:03:35 +01:00
|
|
|
ctx.drawImage(cursor,
|
|
|
|
|
Math.round(pos.x - cursor.width/2),
|
|
|
|
|
Math.round(pos.y - cursor.height/2)
|
|
|
|
|
)
|
2020-12-05 19:45:34 +01:00
|
|
|
if (player.id !== CLIENT_ID) {
|
2020-11-25 22:03:35 +01:00
|
|
|
ctx.fillStyle = 'white'
|
|
|
|
|
ctx.font = '10px sans-serif'
|
|
|
|
|
ctx.textAlign = 'center'
|
2020-12-05 19:45:34 +01:00
|
|
|
ctx.fillText(player.name + ' (' + player.points + ')',
|
2020-11-25 22:03:35 +01:00
|
|
|
Math.round(pos.x),
|
|
|
|
|
Math.round(pos.y) + cursor.height
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (DEBUG) Debug.checkpoint('players done')
|
|
|
|
|
|
|
|
|
|
// DRAW PLAYERS
|
|
|
|
|
// ---------------------------------------------------------------
|
2020-12-07 02:38:07 +01:00
|
|
|
updateScores()
|
2020-11-25 22:03:35 +01:00
|
|
|
if (DEBUG) Debug.checkpoint('scores done')
|
|
|
|
|
// ---------------------------------------------------------------
|
|
|
|
|
|
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,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main()
|