|
| 1 | +import { loadImage, random } from './util.js'; |
| 2 | +import { generatePuzzle } from '../../index.js'; |
| 3 | + |
| 4 | +const socket = io('http://localhost:3000'); |
| 5 | + |
| 6 | +socket.on('gameState', handleGameState); |
| 7 | + |
| 8 | +class GameState { |
| 9 | + puzzle; |
| 10 | + |
| 11 | + constructor(puzzle) { |
| 12 | + this.puzzle = puzzle; |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +class ServerPuzzleState { |
| 17 | + row; |
| 18 | + col; |
| 19 | + x; |
| 20 | + y; |
| 21 | + imageSrc; |
| 22 | + isActive; |
| 23 | + |
| 24 | + constructor({ |
| 25 | + row, col, x, y, imageSrc, isActive |
| 26 | + }) { |
| 27 | + this.row = row; |
| 28 | + this.col = col; |
| 29 | + this.x = x; |
| 30 | + this.y = y; |
| 31 | + this.imageSrc = imageSrc; |
| 32 | + this.isActive = isActive; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +const canvas = document.getElementById('original'); |
| 37 | +const container = document.getElementById("result"); |
| 38 | +let clientPuzzle; |
| 39 | +let isDown; |
| 40 | +let metadata; |
| 41 | + |
| 42 | +init().then(puzzle => { |
| 43 | + clientPuzzle = puzzle; |
| 44 | + |
| 45 | + const serverPuzzle = clientPuzzle.map(p => |
| 46 | + new ServerPuzzleState({ |
| 47 | + x: p.style.left.replace('px', ''), |
| 48 | + y: p.style.top.replace('px', ''), |
| 49 | + isActive: false, |
| 50 | + }) |
| 51 | + ); |
| 52 | + const serverGameState = new GameState(serverPuzzle); |
| 53 | + console.log(serverGameState); |
| 54 | + socket.emit('start', serverGameState); |
| 55 | +}); |
| 56 | + |
| 57 | +// Initialize the client puzzle and the game state |
| 58 | +async function init() { |
| 59 | + |
| 60 | + const img = await loadImage("https://images.unsplash.com/photo-1478827217976-7214a0556393?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1500&q=80"); |
| 61 | + |
| 62 | + canvas.width = img.width; |
| 63 | + canvas.height = img.height; |
| 64 | + const context = canvas.getContext('2d'); |
| 65 | + context.drawImage(img, 0, 0, canvas.width, canvas.height); |
| 66 | + |
| 67 | + // Creating the puzzle pieces |
| 68 | + const puzzle = await generatePuzzle(img, 25, 25); |
| 69 | + const puzzlePieces = puzzle.map(p => { |
| 70 | + const img = new Image(); |
| 71 | + img.src = p.imageSrc; |
| 72 | + img.width = p.width; |
| 73 | + img.height = p.height; |
| 74 | + img.className = "unselectable"; |
| 75 | + return img; |
| 76 | + }); |
| 77 | + |
| 78 | + // shuffle(puzzlePieces); |
| 79 | + const fragment = document.createDocumentFragment(); |
| 80 | + puzzlePieces.forEach(piece => { |
| 81 | + piece.classList.add("puzzle-piece") |
| 82 | + fragment.appendChild(piece); |
| 83 | + }); |
| 84 | + |
| 85 | + container.appendChild(fragment); |
| 86 | + |
| 87 | + puzzlePieces.forEach((piece, idx) => { |
| 88 | + piece.dataset.index = idx; |
| 89 | + piece.style.position = "absolute"; |
| 90 | + const { x, y } = random(canvas.offsetLeft, canvas.offsetTop, piece.width, piece.height, canvas.width, canvas.height); |
| 91 | + |
| 92 | + piece.style.left = `${x}px`; |
| 93 | + piece.style.top = `${y}px`; |
| 94 | + |
| 95 | + piece.addEventListener('dragstart', (event) => { |
| 96 | + return false; |
| 97 | + }); |
| 98 | + |
| 99 | + ['mousedown', 'touchstart'].forEach(listener => piece.addEventListener(listener, (event) => { |
| 100 | + const x = event.clientX || event.touches[0].pageX; |
| 101 | + const y = event.clientY || event.touches[0].pageY; |
| 102 | + console.log('touchstart'); |
| 103 | + if (isDown) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + isDown = true; |
| 108 | + metadata = { |
| 109 | + idx, |
| 110 | + offset: [ |
| 111 | + piece.offsetLeft - x, |
| 112 | + piece.offsetTop - y |
| 113 | + ] |
| 114 | + } |
| 115 | + }, { passive: false})); |
| 116 | + }); |
| 117 | + |
| 118 | + ['mouseup', 'touchend'].forEach(listener => container.addEventListener(listener, function() { |
| 119 | + console.log('touchend'); |
| 120 | + isDown = false; |
| 121 | + metadata = undefined; |
| 122 | + }, { passive: false })); |
| 123 | + |
| 124 | + ['mousemove', 'touchmove'].forEach(listener => container.addEventListener(listener, (event) => { |
| 125 | + event.preventDefault(); |
| 126 | + console.log(isDown); |
| 127 | + if (isDown) { |
| 128 | + const piece = puzzlePieces[metadata.idx]; |
| 129 | + const x = event.clientX || event.touches[0].pageX; |
| 130 | + const y = event.clientY || event.touches[0].pageY; |
| 131 | + const newX = x + metadata.offset[0]; |
| 132 | + const newY = y + metadata.offset[1]; |
| 133 | + |
| 134 | + piece.style.left = `${newX}px`; |
| 135 | + piece.style.top = `${newY}px`; |
| 136 | + socket.emit('move', { idx: metadata.idx, x: newX, y: newY }); |
| 137 | + } |
| 138 | + }, { passive: false })); |
| 139 | + |
| 140 | + return puzzlePieces; |
| 141 | +} |
| 142 | + |
| 143 | +// Render the game given the game state, match the updated server game state with the client game state |
| 144 | +function render(gameState) { |
| 145 | + const serverPuzzle = gameState.puzzle; |
| 146 | + |
| 147 | + for (let i = 0; i < clientPuzzle.length; i++) { |
| 148 | + const sp = serverPuzzle[i]; |
| 149 | + const cp = clientPuzzle[i]; |
| 150 | + |
| 151 | + if (metadata?.idx === i) { |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + if (cp.style.left !== `${sp.x}px`) { |
| 156 | + cp.style.left = `${sp.x}px`; |
| 157 | + } |
| 158 | + |
| 159 | + if (cp.style.top !== `${sp.y}px`) { |
| 160 | + cp.style.top = `${sp.y}px`; |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +function handleGameState(gameState) { |
| 166 | + const state = JSON.parse(gameState); |
| 167 | + |
| 168 | + requestAnimationFrame(() => render(state)); |
| 169 | +} |
0 commit comments