puzzle/game/index.js

308 lines
8.2 KiB
JavaScript
Raw Normal View History

2020-11-07 11:35:29 +01:00
"use strict"
import {run} from './gameloop.js'
import Camera from './Camera.js'
2020-11-09 00:29:07 +01:00
import Graphics from './Graphics.js'
2020-11-09 01:54:05 +01:00
import Debug from './Debug.js'
import Communication from './Communication.js'
2020-11-12 19:25:42 +01:00
import Util from './../common/Util.js'
2020-11-17 21:46:56 +01:00
import PuzzleGraphics from './PuzzleGraphics.js'
2020-11-17 22:34:15 +01:00
import Game from './Game.js'
2020-11-07 11:35:29 +01:00
2020-11-09 01:54:05 +01:00
if (typeof GAME_ID === 'undefined') throw '[ GAME_ID not set ]'
if (typeof WS_ADDRESS === 'undefined') throw '[ WS_ADDRESS not set ]'
2020-11-10 19:09:48 +01:00
if (typeof DEBUG === 'undefined') window.DEBUG = false
2020-11-07 12:21:38 +01:00
2020-11-07 11:35:29 +01:00
function addCanvasToDom(canvas) {
document.body.append(canvas)
return canvas
}
function initme() {
2020-11-08 01:56:36 +01:00
// return uniqId()
2020-11-19 12:38:50 +01:00
let ID = localStorage.getItem('ID')
2020-11-07 11:35:29 +01:00
if (!ID) {
2020-11-12 19:25:42 +01:00
ID = Util.uniqId()
2020-11-19 12:38:50 +01:00
localStorage.setItem('ID', ID)
2020-11-07 11:35:29 +01:00
}
2020-11-19 12:38:50 +01:00
let name = localStorage.getItem('NAME')
if (!name) {
name = prompt('What\'s your name?');
localStorage.setItem('NAME', name)
}
return [ID, name]
2020-11-07 11:35:29 +01:00
}
2020-11-12 19:19:02 +01:00
const getFirstOwnedTile = (puzzle, userId) => {
for (let t of puzzle.tiles) {
if (t.owner === userId) {
return t
}
}
return null
}
2020-11-17 21:46:56 +01:00
export default class EventAdapter {
constructor(canvas, viewport) {
this._mouseEvts = []
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))
}
consumeAll() {
if (this._mouseEvts.length === 0) {
return []
}
const all = this._mouseEvts.slice()
this._mouseEvts = []
return all
}
_mouseDown(e) {
if (e.button === 0) {
const pos = this._viewport.viewportToWorld({
x: e.offsetX,
y: e.offsetY,
})
this._mouseEvts.push(['down', pos.x, pos.y])
}
}
_mouseUp(e) {
if (e.button === 0) {
const pos = this._viewport.viewportToWorld({
x: e.offsetX,
y: e.offsetY,
})
this._mouseEvts.push(['up', pos.x, pos.y])
}
}
_mouseMove(e) {
const pos = this._viewport.viewportToWorld({
x: e.offsetX,
y: e.offsetY,
})
this._mouseEvts.push(['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._mouseEvts.push([evt, pos.x, pos.y])
}
}
async function main() {
2020-11-09 00:29:07 +01:00
let gameId = GAME_ID
2020-11-19 12:38:50 +01:00
let [CLIENT_ID, name] = initme()
2020-11-07 11:35:29 +01:00
2020-11-09 00:29:07 +01:00
let cursorGrab = await Graphics.loadImageToBitmap('/grab.png')
let cursorHand = await Graphics.loadImageToBitmap('/hand.png')
2020-11-07 17:49:42 +01:00
2020-11-19 12:38:50 +01:00
const game = await Communication.connect(gameId, CLIENT_ID, name)
2020-11-17 22:34:15 +01:00
Game.createGame(GAME_ID, game);
2020-11-07 11:35:29 +01:00
2020-11-17 21:46:56 +01:00
const bitmaps = await PuzzleGraphics.loadPuzzleBitmaps(game.puzzle)
2020-11-09 01:54:05 +01:00
const puzzle = game.puzzle
const players = game.players
2020-11-07 11:35:29 +01:00
2020-11-09 01:54:05 +01:00
let rerender = true
2020-11-08 01:56:36 +01:00
2020-11-09 01:54:05 +01:00
const changePlayer = (change) => {
for (let k of Object.keys(change)) {
2020-11-17 22:34:15 +01:00
players[CLIENT_ID][k] = change[k]
2020-11-07 16:33:28 +01:00
}
2020-11-09 01:54:05 +01:00
}
2020-11-07 11:35:29 +01:00
2020-11-09 01:54:05 +01:00
// Create a dom and attach adapters to it so we can work with it
const canvas = addCanvasToDom(Graphics.createCanvas())
2020-11-10 18:48:16 +01:00
const ctx = canvas.getContext('2d')
2020-11-09 01:54:05 +01:00
// initialize some view data
// this global data will change according to input events
const viewport = new Camera(canvas)
2020-11-12 19:19:02 +01:00
// center viewport
viewport.move(
-(puzzle.info.table.width - viewport.width) /2,
-(puzzle.info.table.height - viewport.height) /2
)
2020-11-09 01:54:05 +01:00
2020-11-17 21:46:56 +01:00
const evts = new EventAdapter(canvas, viewport)
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) {
2020-11-12 19:19:02 +01:00
case 'player': {
2020-11-17 22:34:15 +01:00
if (changeData.id !== CLIENT_ID) {
2020-11-17 21:46:56 +01:00
players[changeData.id] = changeData
2020-11-12 19:19:02 +01:00
rerender = true
2020-11-09 01:54:05 +01:00
}
} break;
2020-11-12 19:19:02 +01:00
case 'tile': {
2020-11-17 21:46:56 +01:00
puzzle.tiles[changeData.idx] = changeData
2020-11-12 19:19:02 +01:00
rerender = true
2020-11-09 01:54:05 +01:00
} break;
2020-11-12 19:19:02 +01:00
case 'data': {
2020-11-17 21:46:56 +01:00
puzzle.data = changeData
2020-11-12 19:19:02 +01:00
rerender = true
2020-11-09 01:54:05 +01:00
} break;
2020-11-08 01:56:36 +01:00
}
2020-11-09 01:54:05 +01:00
}
})
2020-11-07 11:35:29 +01:00
2020-11-09 01:54:05 +01:00
// In the middle of the table, there is a board. this is to
// tell the player where to place the final puzzle
2020-11-10 18:48:16 +01:00
const boardColor = '#505050'
2020-11-07 11:35:29 +01:00
2020-11-09 01:54:05 +01:00
const tilesSortedByZIndex = () => {
const sorted = puzzle.tiles.slice()
return sorted.sort((t1, t2) => t1.z - t2.z)
}
2020-11-07 11:35:29 +01:00
2020-11-09 01:54:05 +01:00
let _last_mouse_down = null
const onUpdate = () => {
2020-11-17 21:46:56 +01:00
for (let evt of evts.consumeAll()) {
2020-11-17 22:34:15 +01:00
// LOCAL ONLY CHANGES
// -------------------------------------------------------------
2020-11-17 21:46:56 +01:00
const type = evt[0]
const pos = {x: evt[1], y: evt[2]}
if (type === 'move') {
2020-11-12 19:19:02 +01:00
rerender = true
2020-11-17 21:46:56 +01:00
changePlayer(pos)
2020-11-09 01:54:05 +01:00
2020-11-17 22:34:15 +01:00
if (_last_mouse_down && !getFirstOwnedTile(puzzle, CLIENT_ID)) {
2020-11-09 01:54:05 +01:00
// move the cam
2020-11-17 21:46:56 +01:00
const mouse = viewport.worldToViewport(pos)
2020-11-12 19:19:02 +01:00
const diffX = Math.round(mouse.x - _last_mouse_down.x)
const diffY = Math.round(mouse.y - _last_mouse_down.y)
2020-11-09 01:54:05 +01:00
viewport.move(diffX, diffY)
2020-11-12 19:19:02 +01:00
_last_mouse_down = mouse
2020-11-09 01:54:05 +01:00
}
2020-11-17 21:46:56 +01:00
} else if (type === 'down') {
_last_mouse_down = viewport.worldToViewport(pos)
} else if (type === 'up') {
2020-11-09 01:54:05 +01:00
_last_mouse_down = null
2020-11-17 21:46:56 +01:00
} else if (type === 'zoomin') {
if (viewport.zoomIn()) {
rerender = true
changePlayer(pos)
}
} else if (type === 'zoomout') {
if (viewport.zoomOut()) {
2020-11-09 01:54:05 +01:00
rerender = true
2020-11-17 21:46:56 +01:00
changePlayer(pos)
2020-11-07 11:35:29 +01:00
}
2020-11-08 01:56:36 +01:00
}
2020-11-17 22:34:15 +01:00
// LOCAL + SERVER CHANGES
// -------------------------------------------------------------
2020-11-19 12:38:50 +01:00
const changes = Game.handleInput(GAME_ID, CLIENT_ID, evt)
if (changes.length > 0) {
rerender = true
}
2020-11-17 21:46:56 +01:00
Communication.sendClientEvent(evt)
2020-11-07 11:35:29 +01:00
}
2020-11-09 01:54:05 +01:00
}
2020-11-08 12:56:54 +01:00
2020-11-09 01:54:05 +01:00
const onRender = () => {
2020-11-12 19:19:02 +01:00
if (!rerender) {
2020-11-09 01:54:05 +01:00
return
2020-11-08 12:56:54 +01:00
}
2020-11-09 01:54:05 +01:00
2020-11-10 18:48:16 +01:00
let pos
let dim
2020-11-08 12:56:54 +01:00
2020-11-10 18:48:16 +01:00
if (DEBUG) Debug.checkpoint_start(0)
2020-11-07 11:35:29 +01:00
2020-11-12 19:19:02 +01:00
// CLEAR CTX
// ---------------------------------------------------------------
ctx.clearRect(0, 0, canvas.width, canvas.height)
2020-11-10 18:48:16 +01:00
if (DEBUG) Debug.checkpoint('clear done')
2020-11-12 19:19:02 +01:00
// ---------------------------------------------------------------
2020-11-07 11:35:29 +01:00
2020-11-10 18:48:16 +01:00
// DRAW BOARD
// ---------------------------------------------------------------
2020-11-12 19:19:02 +01:00
pos = viewport.worldToViewport({
x: (puzzle.info.table.width - puzzle.info.width) / 2,
y: (puzzle.info.table.height - puzzle.info.height) / 2
})
dim = viewport.worldDimToViewport({
w: puzzle.info.width,
h: puzzle.info.height,
})
ctx.fillStyle = boardColor
ctx.fillRect(pos.x, pos.y, dim.w, dim.h)
2020-11-10 18:48:16 +01:00
if (DEBUG) Debug.checkpoint('board done')
2020-11-12 19:19:02 +01:00
// ---------------------------------------------------------------
2020-11-10 18:48:16 +01:00
// DRAW TILES
// ---------------------------------------------------------------
for (let tile of tilesSortedByZIndex()) {
2020-11-12 19:19:02 +01:00
const bmp = bitmaps[tile.idx]
2020-11-10 18:48:16 +01:00
pos = viewport.worldToViewport({
x: puzzle.info.tileDrawOffset + tile.pos.x,
y: puzzle.info.tileDrawOffset + tile.pos.y,
})
dim = viewport.worldDimToViewport({
w: puzzle.info.tileDrawSize,
h: puzzle.info.tileDrawSize,
})
ctx.drawImage(bmp,
0, 0, bmp.width, bmp.height,
pos.x, pos.y, dim.w, dim.h
2020-11-09 01:54:05 +01:00
)
}
2020-11-10 18:48:16 +01:00
if (DEBUG) Debug.checkpoint('tiles done')
2020-11-12 19:19:02 +01:00
// ---------------------------------------------------------------
2020-11-10 18:48:16 +01:00
// DRAW PLAYERS
// ---------------------------------------------------------------
for (let id of Object.keys(players)) {
const p = players[id]
const cursor = p.down ? cursorGrab : cursorHand
const pos = viewport.worldToViewport(p)
2020-11-12 19:19:02 +01:00
ctx.drawImage(cursor,
Math.round(pos.x - cursor.width/2),
Math.round(pos.y - cursor.height/2)
)
2020-11-19 12:38:50 +01:00
if (id !== CLIENT_ID) {
ctx.fillStyle = 'white'
ctx.font = '10px sans-serif'
ctx.textAlign = 'center'
ctx.fillText(p.name,
Math.round(pos.x),
Math.round(pos.y) + cursor.height
)
}
2020-11-09 01:54:05 +01:00
}
2020-11-10 18:48:16 +01:00
if (DEBUG) Debug.checkpoint('players done')
2020-11-12 19:19:02 +01:00
// ---------------------------------------------------------------
2020-11-07 17:49:42 +01:00
2020-11-08 12:56:54 +01:00
2020-11-09 01:54:05 +01:00
rerender = false
2020-11-07 11:35:29 +01:00
}
2020-11-09 01:54:05 +01:00
run({
update: onUpdate,
render: onRender,
})
2020-11-07 11:35:29 +01:00
}
main()