puzzle/game/index.js

336 lines
11 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'
import EventAdapter from './EventAdapter.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:19:02 +01:00
import Geometry from './../common/Geometry.js'
2020-11-12 19:25:42 +01:00
import Util from './../common/Util.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
}
2020-11-10 18:48:16 +01:00
async function createPuzzleTileBitmaps(img, tiles, info) {
2020-11-08 12:58:21 +01:00
var tileSize = info.tileSize
var tileMarginWidth = info.tileMarginWidth
var tileDrawSize = info.tileDrawSize
var tileRatio = tileSize / 100.0
var curvyCoords = [
0, 0, 40, 15, 37, 5,
37, 5, 40, 0, 38, -5,
38, -5, 20, -20, 50, -20,
50, -20, 80, -20, 62, -5,
62, -5, 60, 0, 63, 5,
63, 5, 65, 15, 100, 0
];
const bitmaps = new Array(tiles.length)
const paths = {}
function pathForShape(shape) {
const key = `${shape.top}${shape.right}${shape.left}${shape.bottom}`
if (paths[key]) {
return paths[key]
2020-11-08 12:56:54 +01:00
}
2020-11-07 11:35:29 +01:00
2020-11-08 12:58:21 +01:00
const path = new Path2D()
const topLeftEdge = { x: tileMarginWidth, y: tileMarginWidth }
path.moveTo(topLeftEdge.x, topLeftEdge.y)
for (let i = 0; i < curvyCoords.length / 6; i++) {
2020-11-12 19:19:02 +01:00
const p1 = Geometry.pointAdd(topLeftEdge, { x: curvyCoords[i * 6 + 0] * tileRatio, y: shape.top * curvyCoords[i * 6 + 1] * tileRatio })
const p2 = Geometry.pointAdd(topLeftEdge, { x: curvyCoords[i * 6 + 2] * tileRatio, y: shape.top * curvyCoords[i * 6 + 3] * tileRatio })
const p3 = Geometry.pointAdd(topLeftEdge, { x: curvyCoords[i * 6 + 4] * tileRatio, y: shape.top * curvyCoords[i * 6 + 5] * tileRatio })
2020-11-08 12:58:21 +01:00
path.bezierCurveTo(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
}
2020-11-12 19:19:02 +01:00
const topRightEdge = Geometry.pointAdd(topLeftEdge, { x: tileSize, y: 0 })
2020-11-08 12:58:21 +01:00
for (let i = 0; i < curvyCoords.length / 6; i++) {
2020-11-12 19:19:02 +01:00
const p1 = Geometry.pointAdd(topRightEdge, { x: -shape.right * curvyCoords[i * 6 + 1] * tileRatio, y: curvyCoords[i * 6 + 0] * tileRatio })
const p2 = Geometry.pointAdd(topRightEdge, { x: -shape.right * curvyCoords[i * 6 + 3] * tileRatio, y: curvyCoords[i * 6 + 2] * tileRatio })
const p3 = Geometry.pointAdd(topRightEdge, { x: -shape.right * curvyCoords[i * 6 + 5] * tileRatio, y: curvyCoords[i * 6 + 4] * tileRatio })
2020-11-08 12:58:21 +01:00
path.bezierCurveTo(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
}
2020-11-12 19:19:02 +01:00
const bottomRightEdge = Geometry.pointAdd(topRightEdge, { x: 0, y: tileSize })
2020-11-08 12:58:21 +01:00
for (let i = 0; i < curvyCoords.length / 6; i++) {
2020-11-12 19:19:02 +01:00
let p1 = Geometry.pointSub(bottomRightEdge, { x: curvyCoords[i * 6 + 0] * tileRatio, y: shape.bottom * curvyCoords[i * 6 + 1] * tileRatio })
let p2 = Geometry.pointSub(bottomRightEdge, { x: curvyCoords[i * 6 + 2] * tileRatio, y: shape.bottom * curvyCoords[i * 6 + 3] * tileRatio })
let p3 = Geometry.pointSub(bottomRightEdge, { x: curvyCoords[i * 6 + 4] * tileRatio, y: shape.bottom * curvyCoords[i * 6 + 5] * tileRatio })
2020-11-08 12:58:21 +01:00
path.bezierCurveTo(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
}
2020-11-12 19:19:02 +01:00
const bottomLeftEdge = Geometry.pointSub(bottomRightEdge, { x: tileSize, y: 0 })
2020-11-08 12:58:21 +01:00
for (let i = 0; i < curvyCoords.length / 6; i++) {
2020-11-12 19:19:02 +01:00
let p1 = Geometry.pointSub(bottomLeftEdge, { x: -shape.left * curvyCoords[i * 6 + 1] * tileRatio, y: curvyCoords[i * 6 + 0] * tileRatio })
let p2 = Geometry.pointSub(bottomLeftEdge, { x: -shape.left * curvyCoords[i * 6 + 3] * tileRatio, y: curvyCoords[i * 6 + 2] * tileRatio })
let p3 = Geometry.pointSub(bottomLeftEdge, { x: -shape.left * curvyCoords[i * 6 + 5] * tileRatio, y: curvyCoords[i * 6 + 4] * tileRatio })
2020-11-08 12:58:21 +01:00
path.bezierCurveTo(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
2020-11-07 11:35:29 +01:00
}
2020-11-08 12:58:21 +01:00
paths[key] = path
return path
}
2020-11-07 11:35:29 +01:00
2020-11-08 12:58:21 +01:00
for (let tile of tiles) {
const srcRect = srcRectByIdx(info, tile.idx)
const path = pathForShape(info.shapes[tile.idx])
2020-11-09 00:29:07 +01:00
const c = Graphics.createCanvas(tileDrawSize, tileDrawSize)
2020-11-08 12:58:21 +01:00
const ctx = c.getContext('2d')
// -----------------------------------------------------------
// -----------------------------------------------------------
ctx.lineWidth = 2
ctx.stroke(path)
// -----------------------------------------------------------
// -----------------------------------------------------------
ctx.save();
ctx.clip(path)
ctx.drawImage(
img,
2020-11-10 18:48:16 +01:00
srcRect.x - tileMarginWidth,
srcRect.y - tileMarginWidth,
2020-11-08 12:58:21 +01:00
tileDrawSize,
tileDrawSize,
0,
0,
tileDrawSize,
tileDrawSize,
)
ctx.stroke(path)
ctx.restore();
2020-11-10 18:48:16 +01:00
bitmaps[tile.idx] = await createImageBitmap(c)
2020-11-08 12:58:21 +01:00
}
return bitmaps
2020-11-07 11:35:29 +01:00
}
2020-11-08 12:58:21 +01:00
function srcRectByIdx(puzzleInfo, idx) {
let c = puzzleInfo.coords[idx]
let cx = c.x * puzzleInfo.tileSize
let cy = c.y * puzzleInfo.tileSize
2020-11-10 18:48:16 +01:00
return {
x: cx,
y: cy,
w: puzzleInfo.tileSize,
h: puzzleInfo.tileSize,
}
2020-11-07 11:35:29 +01:00
}
async function loadPuzzleBitmaps(puzzle) {
// load bitmap, to determine the original size of the image
2020-11-09 00:29:07 +01:00
const bmp = await Graphics.loadImageToBitmap(puzzle.info.imageUrl)
2020-11-07 11:35:29 +01:00
// creation of tile bitmaps
// then create the final puzzle bitmap
// NOTE: this can decrease OR increase in size!
2020-11-10 18:48:16 +01:00
const bmpResized = await Graphics.resizeBitmap(bmp, puzzle.info.width, puzzle.info.height)
2020-11-08 12:56:54 +01:00
return await createPuzzleTileBitmaps(bmpResized, puzzle.tiles, puzzle.info)
2020-11-07 11:35:29 +01:00
}
function initme() {
2020-11-08 01:56:36 +01:00
// return uniqId()
2020-11-07 11:35:29 +01:00
let ID = localStorage.getItem("ID")
if (!ID) {
2020-11-12 19:25:42 +01:00
ID = Util.uniqId()
2020-11-07 11:35:29 +01:00
localStorage.setItem("ID", ID)
}
return ID
}
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-07 11:35:29 +01:00
async function main () {
2020-11-09 00:29:07 +01:00
let gameId = GAME_ID
2020-11-07 11:35:29 +01:00
let me = initme()
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-09 01:54:05 +01:00
const game = await Communication.connect(gameId, me)
2020-11-07 11:35:29 +01:00
2020-11-09 01:54:05 +01:00
const bitmaps = await loadPuzzleBitmaps(game.puzzle)
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)) {
players[me][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
const evts = new EventAdapter(canvas)
// 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
Communication.onChanges((changes) => {
2020-11-12 19:19:02 +01:00
for (let [type, typeData] of changes) {
switch (type) {
case 'player': {
if (typeData.id !== me) {
players[typeData.id] = typeData
rerender = true
2020-11-09 01:54:05 +01:00
}
} break;
2020-11-12 19:19:02 +01:00
case 'tile': {
puzzle.tiles[typeData.idx] = typeData
rerender = true
2020-11-09 01:54:05 +01:00
} break;
2020-11-12 19:19:02 +01:00
case 'data': {
puzzle.data = typeData
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 = () => {
for (let mouse of evts.consumeAll()) {
const tp = viewport.viewportToWorld(mouse)
2020-11-12 19:19:02 +01:00
2020-11-09 01:54:05 +01:00
if (mouse.type === 'move') {
2020-11-12 19:19:02 +01:00
Communication.addMouse(['move', tp.x, tp.y])
rerender = true
2020-11-09 01:54:05 +01:00
changePlayer({ x: tp.x, y: tp.y })
2020-11-12 19:19:02 +01:00
if (_last_mouse_down && !getFirstOwnedTile(puzzle, me)) {
2020-11-09 01:54:05 +01:00
// move the cam
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
}
} else if (mouse.type === 'down') {
2020-11-12 19:19:02 +01:00
Communication.addMouse(['down', tp.x, tp.y])
2020-11-09 01:54:05 +01:00
_last_mouse_down = mouse
} else if (mouse.type === 'up') {
2020-11-12 19:19:02 +01:00
Communication.addMouse(['up', tp.x, tp.y])
2020-11-09 01:54:05 +01:00
_last_mouse_down = null
} else if (mouse.type === 'wheel') {
if (
mouse.deltaY < 0 && viewport.zoomIn()
|| mouse.deltaY > 0 && viewport.zoomOut()
) {
rerender = true
changePlayer({ x: tp.x, y: tp.y })
2020-11-07 11:35:29 +01:00
}
2020-11-08 01:56:36 +01:00
}
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-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()