Spaces:
Runtime error
Runtime error
Pedro de Carvalho commited on
Commit ·
3752d30
1
Parent(s): bd42552
Update game
Browse files
client/src/game/BoardStore.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import { BitBoard } from 'shared/bot/BitBoard.ts'
|
| 2 |
import { isBoardCoordInBounds } from 'shared/board.ts'
|
| 3 |
import type { Cell, SerializedBitBoard } from 'shared/types.ts'
|
|
|
|
| 4 |
|
| 5 |
export class BoardStore {
|
| 6 |
boardSize: number
|
|
@@ -22,18 +23,21 @@ export class BoardStore {
|
|
| 22 |
}
|
| 23 |
|
| 24 |
setCell(x: number, y: number, cell: Cell): void {
|
| 25 |
-
|
|
|
|
| 26 |
const player = Number(cell.playerId)
|
| 27 |
-
if (player !== 0 && player !== 1) return
|
| 28 |
-
if (this.bitBoard.has(x, y)) this.bitBoard.remove(x, y)
|
| 29 |
this.bitBoard.place(x, y, player)
|
| 30 |
}
|
| 31 |
|
| 32 |
removeCell(x: number, y: number): void {
|
|
|
|
| 33 |
this.bitBoard.remove(x, y)
|
| 34 |
}
|
| 35 |
|
| 36 |
clear(): void {
|
|
|
|
| 37 |
this.bitBoard.clear()
|
| 38 |
}
|
| 39 |
|
|
@@ -51,6 +55,7 @@ export class BoardStore {
|
|
| 51 |
}
|
| 52 |
|
| 53 |
deserialize(data: SerializedBitBoard | Record<string, Cell>, symbols = this.symbols): void {
|
|
|
|
| 54 |
this.symbols = [symbols[0], symbols[1]]
|
| 55 |
if (isSerializedBitBoard(data)) {
|
| 56 |
this.boardSize = data.boardSize
|
|
@@ -62,6 +67,7 @@ export class BoardStore {
|
|
| 62 |
|
| 63 |
setBoardSize(boardSize: number): void {
|
| 64 |
if (this.boardSize === boardSize) return
|
|
|
|
| 65 |
this.boardSize = boardSize
|
| 66 |
this.bitBoard = BitBoard.empty(boardSize)
|
| 67 |
}
|
|
|
|
| 1 |
import { BitBoard } from 'shared/bot/BitBoard.ts'
|
| 2 |
import { isBoardCoordInBounds } from 'shared/board.ts'
|
| 3 |
import type { Cell, SerializedBitBoard } from 'shared/types.ts'
|
| 4 |
+
import { debug } from '../debug.ts'
|
| 5 |
|
| 6 |
export class BoardStore {
|
| 7 |
boardSize: number
|
|
|
|
| 23 |
}
|
| 24 |
|
| 25 |
setCell(x: number, y: number, cell: Cell): void {
|
| 26 |
+
debug('BoardStore', 'setCell', { x, y, playerId: cell.playerId, symbol: cell.symbol })
|
| 27 |
+
if (!isBoardCoordInBounds(x, y, this.boardSize)) { debug('BoardStore', 'setCell: out of bounds'); return }
|
| 28 |
const player = Number(cell.playerId)
|
| 29 |
+
if (player !== 0 && player !== 1) { debug('BoardStore', 'setCell: invalid player', player); return }
|
| 30 |
+
if (this.bitBoard.has(x, y)) { debug('BoardStore', 'setCell: removing existing'); this.bitBoard.remove(x, y) }
|
| 31 |
this.bitBoard.place(x, y, player)
|
| 32 |
}
|
| 33 |
|
| 34 |
removeCell(x: number, y: number): void {
|
| 35 |
+
debug('BoardStore', 'removeCell', { x, y })
|
| 36 |
this.bitBoard.remove(x, y)
|
| 37 |
}
|
| 38 |
|
| 39 |
clear(): void {
|
| 40 |
+
debug('BoardStore', 'clear')
|
| 41 |
this.bitBoard.clear()
|
| 42 |
}
|
| 43 |
|
|
|
|
| 55 |
}
|
| 56 |
|
| 57 |
deserialize(data: SerializedBitBoard | Record<string, Cell>, symbols = this.symbols): void {
|
| 58 |
+
debug('BoardStore', 'deserialize', { type: isSerializedBitBoard(data) ? 'bitboard' : 'record', dataSize: Object.keys(data).length })
|
| 59 |
this.symbols = [symbols[0], symbols[1]]
|
| 60 |
if (isSerializedBitBoard(data)) {
|
| 61 |
this.boardSize = data.boardSize
|
|
|
|
| 67 |
|
| 68 |
setBoardSize(boardSize: number): void {
|
| 69 |
if (this.boardSize === boardSize) return
|
| 70 |
+
debug('BoardStore', 'setBoardSize', { from: this.boardSize, to: boardSize })
|
| 71 |
this.boardSize = boardSize
|
| 72 |
this.bitBoard = BitBoard.empty(boardSize)
|
| 73 |
}
|
client/src/game/CameraController.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import { Container, Ticker } from 'pixi.js'
|
| 2 |
import type { CameraState } from 'shared/board.ts'
|
| 3 |
import { screenToWorld, CELL_SIZE } from 'shared/board.ts'
|
|
|
|
| 4 |
|
| 5 |
const ZOOM_MIN = 0.1
|
| 6 |
const ZOOM_MAX = 5
|
|
@@ -66,6 +67,7 @@ export class CameraController {
|
|
| 66 |
this.state.zoom = Math.max(ZOOM_MIN, Math.min(ZOOM_MAX, zoom))
|
| 67 |
this.state.x = boardSize / 2 * CELL_SIZE
|
| 68 |
this.state.y = boardSize / 2 * CELL_SIZE
|
|
|
|
| 69 |
this.apply()
|
| 70 |
this.triggerChange()
|
| 71 |
}
|
|
@@ -216,6 +218,7 @@ export class CameraController {
|
|
| 216 |
}
|
| 217 |
|
| 218 |
private onPointerDown = (e: PointerEvent): void => {
|
|
|
|
| 219 |
this.stopMomentum()
|
| 220 |
this.pointers.set(e.pointerId, { x: e.clientX, y: e.clientY })
|
| 221 |
this.moved = false
|
|
@@ -228,6 +231,7 @@ export class CameraController {
|
|
| 228 |
this.camStartY = this.state.y
|
| 229 |
this.lastMoveX = e.clientX
|
| 230 |
this.lastMoveY = e.clientY
|
|
|
|
| 231 |
}
|
| 232 |
|
| 233 |
if (this.pointers.size === 2) {
|
|
@@ -235,6 +239,7 @@ export class CameraController {
|
|
| 235 |
const p = Array.from(this.pointers.values())
|
| 236 |
this.pinchDist = Math.hypot(p[1].x - p[0].x, p[1].y - p[0].y)
|
| 237 |
this.pinchZoomStart = this.state.zoom
|
|
|
|
| 238 |
}
|
| 239 |
}
|
| 240 |
|
|
@@ -285,6 +290,7 @@ export class CameraController {
|
|
| 285 |
}
|
| 286 |
|
| 287 |
private onPointerUp = (e: PointerEvent): void => {
|
|
|
|
| 288 |
this.pointers.delete(e.pointerId)
|
| 289 |
|
| 290 |
if (e.button === 1) {
|
|
@@ -294,6 +300,7 @@ export class CameraController {
|
|
| 294 |
if (this.pointers.size === 0) {
|
| 295 |
this.clearHover()
|
| 296 |
if (this.moved) {
|
|
|
|
| 297 |
this.startMomentum(this.velocityX, this.velocityY)
|
| 298 |
} else if (this.onCellClick && this.canvas && e.button !== 1) {
|
| 299 |
const rect = this.canvas.getBoundingClientRect()
|
|
@@ -302,7 +309,10 @@ export class CameraController {
|
|
| 302 |
const world = screenToWorld(sx, sy, this.state, this.vw, this.vh)
|
| 303 |
const gx = Math.floor(world.x / CELL_SIZE)
|
| 304 |
const gy = Math.floor(world.y / CELL_SIZE)
|
|
|
|
| 305 |
this.onCellClick(gx, gy)
|
|
|
|
|
|
|
| 306 |
}
|
| 307 |
}
|
| 308 |
}
|
|
@@ -313,6 +323,7 @@ export class CameraController {
|
|
| 313 |
|
| 314 |
private onWheel = (e: WheelEvent): void => {
|
| 315 |
e.preventDefault()
|
|
|
|
| 316 |
|
| 317 |
const rect = this.canvas!.getBoundingClientRect()
|
| 318 |
const sx = e.clientX - rect.left
|
|
|
|
| 1 |
import { Container, Ticker } from 'pixi.js'
|
| 2 |
import type { CameraState } from 'shared/board.ts'
|
| 3 |
import { screenToWorld, CELL_SIZE } from 'shared/board.ts'
|
| 4 |
+
import { debug } from '../debug.ts'
|
| 5 |
|
| 6 |
const ZOOM_MIN = 0.1
|
| 7 |
const ZOOM_MAX = 5
|
|
|
|
| 67 |
this.state.zoom = Math.max(ZOOM_MIN, Math.min(ZOOM_MAX, zoom))
|
| 68 |
this.state.x = boardSize / 2 * CELL_SIZE
|
| 69 |
this.state.y = boardSize / 2 * CELL_SIZE
|
| 70 |
+
debug('CameraController', 'setInitialZoom', { boardSize, fitRatio, zoom: this.state.zoom, vw: this.vw, vh: this.vh })
|
| 71 |
this.apply()
|
| 72 |
this.triggerChange()
|
| 73 |
}
|
|
|
|
| 218 |
}
|
| 219 |
|
| 220 |
private onPointerDown = (e: PointerEvent): void => {
|
| 221 |
+
debug('CameraController', 'pointerDown', { id: e.pointerId, button: e.button, clientX: e.clientX.toFixed(0), clientY: e.clientY.toFixed(0), pointerCount: this.pointers.size + 1 })
|
| 222 |
this.stopMomentum()
|
| 223 |
this.pointers.set(e.pointerId, { x: e.clientX, y: e.clientY })
|
| 224 |
this.moved = false
|
|
|
|
| 231 |
this.camStartY = this.state.y
|
| 232 |
this.lastMoveX = e.clientX
|
| 233 |
this.lastMoveY = e.clientY
|
| 234 |
+
debug('CameraController', 'drag start')
|
| 235 |
}
|
| 236 |
|
| 237 |
if (this.pointers.size === 2) {
|
|
|
|
| 239 |
const p = Array.from(this.pointers.values())
|
| 240 |
this.pinchDist = Math.hypot(p[1].x - p[0].x, p[1].y - p[0].y)
|
| 241 |
this.pinchZoomStart = this.state.zoom
|
| 242 |
+
debug('CameraController', 'pinch start', { pinchDist: this.pinchDist.toFixed(1), zoomStart: this.pinchZoomStart.toFixed(3) })
|
| 243 |
}
|
| 244 |
}
|
| 245 |
|
|
|
|
| 290 |
}
|
| 291 |
|
| 292 |
private onPointerUp = (e: PointerEvent): void => {
|
| 293 |
+
debug('CameraController', 'pointerUp', { id: e.pointerId, button: e.button, moved: this.moved, pointerCount: this.pointers.size })
|
| 294 |
this.pointers.delete(e.pointerId)
|
| 295 |
|
| 296 |
if (e.button === 1) {
|
|
|
|
| 300 |
if (this.pointers.size === 0) {
|
| 301 |
this.clearHover()
|
| 302 |
if (this.moved) {
|
| 303 |
+
debug('CameraController', 'start momentum')
|
| 304 |
this.startMomentum(this.velocityX, this.velocityY)
|
| 305 |
} else if (this.onCellClick && this.canvas && e.button !== 1) {
|
| 306 |
const rect = this.canvas.getBoundingClientRect()
|
|
|
|
| 309 |
const world = screenToWorld(sx, sy, this.state, this.vw, this.vh)
|
| 310 |
const gx = Math.floor(world.x / CELL_SIZE)
|
| 311 |
const gy = Math.floor(world.y / CELL_SIZE)
|
| 312 |
+
debug('CameraController', 'cell click detected', { gx, gy })
|
| 313 |
this.onCellClick(gx, gy)
|
| 314 |
+
} else {
|
| 315 |
+
debug('CameraController', 'no action on pointerUp')
|
| 316 |
}
|
| 317 |
}
|
| 318 |
}
|
|
|
|
| 323 |
|
| 324 |
private onWheel = (e: WheelEvent): void => {
|
| 325 |
e.preventDefault()
|
| 326 |
+
debug('CameraController', 'wheel', { deltaY: e.deltaY.toFixed(0), zoomBefore: this.state.zoom.toFixed(3) })
|
| 327 |
|
| 328 |
const rect = this.canvas!.getBoundingClientRect()
|
| 329 |
const sx = e.clientX - rect.left
|
client/src/game/GameEngine.ts
CHANGED
|
@@ -3,13 +3,14 @@ import { CameraController } from './CameraController.ts'
|
|
| 3 |
import { GridRenderer } from './GridRenderer.ts'
|
| 4 |
import { BoardStore } from './BoardStore.ts'
|
| 5 |
import { SymbolRenderer } from './SymbolRenderer.ts'
|
| 6 |
-
import {
|
| 7 |
import { ParticleSystem } from './effects/ParticleSystem.ts'
|
| 8 |
import { PlacementEffect } from './effects/PlacementEffect.ts'
|
| 9 |
import { WinEffect } from './effects/WinEffect.ts'
|
| 10 |
import { BackgroundAtmosphere } from './effects/BackgroundAtmosphere.ts'
|
| 11 |
import { CELL_SIZE, isBoardCoordInBounds } from 'shared/board.ts'
|
| 12 |
import { theme, onThemeChange } from '../theme.ts'
|
|
|
|
| 13 |
import type { Cell, RoomState, RoomScores, GameRules, SerializedBitBoard } from 'shared/types.ts'
|
| 14 |
import { GameManager } from 'shared/GameManager.ts'
|
| 15 |
|
|
@@ -23,7 +24,7 @@ export class GameEngine {
|
|
| 23 |
effectLayer!: Container
|
| 24 |
particleLayer!: Container
|
| 25 |
|
| 26 |
-
audio
|
| 27 |
particleSys!: ParticleSystem
|
| 28 |
screenLayer!: Container
|
| 29 |
private placementEffect!: PlacementEffect
|
|
@@ -51,6 +52,7 @@ export class GameEngine {
|
|
| 51 |
playerSymbols: [string, string] = ['X', 'O']
|
| 52 |
|
| 53 |
localMode = false
|
|
|
|
| 54 |
localInputEnabled = true
|
| 55 |
|
| 56 |
boardSize = 15
|
|
@@ -67,12 +69,14 @@ export class GameEngine {
|
|
| 67 |
onError: ((message: string) => void) | null = null
|
| 68 |
|
| 69 |
async init(containerEl: HTMLElement): Promise<void> {
|
|
|
|
| 70 |
this.containerEl = containerEl
|
| 71 |
|
| 72 |
const w = containerEl.clientWidth
|
| 73 |
const h = containerEl.clientHeight
|
| 74 |
|
| 75 |
this.app = new Application()
|
|
|
|
| 76 |
await this.app.init({
|
| 77 |
width: w,
|
| 78 |
height: h,
|
|
@@ -81,8 +85,9 @@ export class GameEngine {
|
|
| 81 |
resolution: window.devicePixelRatio || 1,
|
| 82 |
autoDensity: true,
|
| 83 |
})
|
|
|
|
| 84 |
|
| 85 |
-
if (this.destroyed) return
|
| 86 |
|
| 87 |
containerEl.appendChild(this.app.canvas as HTMLCanvasElement)
|
| 88 |
|
|
@@ -96,7 +101,7 @@ export class GameEngine {
|
|
| 96 |
this.bgAtmo = new BackgroundAtmosphere(this.app)
|
| 97 |
this.app.stage.addChildAt(this.bgAtmo.container, 0)
|
| 98 |
|
| 99 |
-
if (this.destroyed) return
|
| 100 |
|
| 101 |
this.camera = new CameraController(this.worldContainer, w, h)
|
| 102 |
this.camera.setInitialZoom(this.boardSize)
|
|
@@ -104,8 +109,9 @@ export class GameEngine {
|
|
| 104 |
this.camera.onChange = () => this.redraw()
|
| 105 |
this.camera.onCellClick = (gx, gy) => this.handleCellClick(gx, gy)
|
| 106 |
this.camera.onCellHover = (gx, gy) => this.handleCellHover(gx, gy)
|
|
|
|
| 107 |
|
| 108 |
-
if (this.destroyed) return
|
| 109 |
|
| 110 |
this.grid = new GridRenderer()
|
| 111 |
this.worldContainer.addChild(this.grid.container)
|
|
@@ -124,7 +130,6 @@ export class GameEngine {
|
|
| 124 |
this.particleLayer = new Container()
|
| 125 |
this.worldContainer.addChild(this.particleLayer)
|
| 126 |
|
| 127 |
-
this.audio = new AudioManager()
|
| 128 |
this.particleSys = new ParticleSystem(this.particleLayer)
|
| 129 |
this.placementEffect = new PlacementEffect(this.effectLayer, this.particleSys)
|
| 130 |
this.winEffect = new WinEffect(this.effectLayer, this.particleSys, this.camera, w, h, this.screenLayer)
|
|
@@ -148,9 +153,11 @@ export class GameEngine {
|
|
| 148 |
|
| 149 |
this.resizeObserver = new ResizeObserver(() => this.handleResize())
|
| 150 |
this.resizeObserver.observe(containerEl)
|
|
|
|
| 151 |
}
|
| 152 |
|
| 153 |
destroy(): void {
|
|
|
|
| 154 |
this.destroyed = true
|
| 155 |
if (this.camera) {
|
| 156 |
this.camera.onChange = null
|
|
@@ -163,7 +170,6 @@ export class GameEngine {
|
|
| 163 |
this.bgAtmo?.destroy()
|
| 164 |
this.symbols?.clear()
|
| 165 |
this.particleSys?.clear()
|
| 166 |
-
this.audio?.destroy()
|
| 167 |
if (this.particleTickerCb) {
|
| 168 |
Ticker.shared.remove(this.particleTickerCb)
|
| 169 |
this.particleTickerCb = null
|
|
@@ -213,15 +219,17 @@ export class GameEngine {
|
|
| 213 |
}
|
| 214 |
|
| 215 |
private handleCellClick(gx: number, gy: number): void {
|
| 216 |
-
|
| 217 |
-
if (this.
|
| 218 |
-
if (
|
| 219 |
-
if (this.
|
|
|
|
| 220 |
|
| 221 |
if (this.localMode && this.manager) {
|
| 222 |
this.placeLocalMove(gx, gy)
|
| 223 |
} else if (!this.localMode) {
|
| 224 |
if (this.playerIndex !== this.currentPlayer) {
|
|
|
|
| 225 |
this.onError?.('Not your turn')
|
| 226 |
return
|
| 227 |
}
|
|
@@ -244,13 +252,15 @@ export class GameEngine {
|
|
| 244 |
}
|
| 245 |
|
| 246 |
placeLocalMove(gx: number, gy: number): boolean {
|
| 247 |
-
|
| 248 |
-
if (this.
|
| 249 |
-
if (this.
|
| 250 |
-
if (
|
|
|
|
| 251 |
|
| 252 |
const result = this.manager.placeMove(gx, gy)
|
| 253 |
-
|
|
|
|
| 254 |
|
| 255 |
const cell = this.manager.getBoard().get(`${gx}:${gy}`)
|
| 256 |
if (!cell) return false
|
|
@@ -331,6 +341,7 @@ export class GameEngine {
|
|
| 331 |
}
|
| 332 |
|
| 333 |
applyRoomState(s: RoomState): void {
|
|
|
|
| 334 |
this.clearHover()
|
| 335 |
this.winEffect.clear()
|
| 336 |
if (s.boardSize) {
|
|
@@ -359,26 +370,44 @@ export class GameEngine {
|
|
| 359 |
}
|
| 360 |
|
| 361 |
applyMove(currentPlayer: number): void {
|
|
|
|
| 362 |
this.currentPlayer = currentPlayer
|
| 363 |
this.onStateChanged?.()
|
| 364 |
}
|
| 365 |
|
| 366 |
rejectMove(x: number, y: number, _message: string): void {
|
|
|
|
| 367 |
this.removeCell(x, y)
|
| 368 |
this.currentPlayer = this.previousTurnPlayer
|
| 369 |
this.onStateChanged?.()
|
| 370 |
}
|
| 371 |
|
| 372 |
applyWin(playerId: string, cells: { x: number; y: number }[] = []): void {
|
|
|
|
| 373 |
this.status = 'win'
|
| 374 |
this.winnerPlayerId = playerId
|
| 375 |
this.onStateChanged?.()
|
| 376 |
this.audio?.stopMusic()
|
| 377 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 378 |
this.winEffect.play(cells)
|
| 379 |
}
|
| 380 |
|
| 381 |
applyDraw(): void {
|
|
|
|
| 382 |
this.status = 'draw'
|
| 383 |
this.currentPlayer = -1
|
| 384 |
this.onStateChanged?.()
|
|
@@ -386,6 +415,7 @@ export class GameEngine {
|
|
| 386 |
}
|
| 387 |
|
| 388 |
applyRematch(s: RoomState): void {
|
|
|
|
| 389 |
this.applyRoomState(s)
|
| 390 |
}
|
| 391 |
|
|
@@ -430,10 +460,12 @@ export class GameEngine {
|
|
| 430 |
}
|
| 431 |
|
| 432 |
applySwapFromManager(): void {
|
|
|
|
| 433 |
if (!this.manager) return
|
| 434 |
const names = this.manager.getPlayerNames()
|
| 435 |
const symbols = this.manager.getPlayerSymbols()
|
| 436 |
const swapped = this.playerSymbols[0] !== symbols[0] || this.playerSymbols[1] !== symbols[1]
|
|
|
|
| 437 |
this.players = [names[0], names[1]]
|
| 438 |
this.playerSymbols = [symbols[0], symbols[1]]
|
| 439 |
this.board.symbols = [symbols[0], symbols[1]]
|
|
|
|
| 3 |
import { GridRenderer } from './GridRenderer.ts'
|
| 4 |
import { BoardStore } from './BoardStore.ts'
|
| 5 |
import { SymbolRenderer } from './SymbolRenderer.ts'
|
| 6 |
+
import { audioManager } from './effects/AudioManager.ts'
|
| 7 |
import { ParticleSystem } from './effects/ParticleSystem.ts'
|
| 8 |
import { PlacementEffect } from './effects/PlacementEffect.ts'
|
| 9 |
import { WinEffect } from './effects/WinEffect.ts'
|
| 10 |
import { BackgroundAtmosphere } from './effects/BackgroundAtmosphere.ts'
|
| 11 |
import { CELL_SIZE, isBoardCoordInBounds } from 'shared/board.ts'
|
| 12 |
import { theme, onThemeChange } from '../theme.ts'
|
| 13 |
+
import { debug } from '../debug.ts'
|
| 14 |
import type { Cell, RoomState, RoomScores, GameRules, SerializedBitBoard } from 'shared/types.ts'
|
| 15 |
import { GameManager } from 'shared/GameManager.ts'
|
| 16 |
|
|
|
|
| 24 |
effectLayer!: Container
|
| 25 |
particleLayer!: Container
|
| 26 |
|
| 27 |
+
audio = audioManager
|
| 28 |
particleSys!: ParticleSystem
|
| 29 |
screenLayer!: Container
|
| 30 |
private placementEffect!: PlacementEffect
|
|
|
|
| 52 |
playerSymbols: [string, string] = ['X', 'O']
|
| 53 |
|
| 54 |
localMode = false
|
| 55 |
+
localPlayerTypes: ('human' | 'bot')[] = []
|
| 56 |
localInputEnabled = true
|
| 57 |
|
| 58 |
boardSize = 15
|
|
|
|
| 69 |
onError: ((message: string) => void) | null = null
|
| 70 |
|
| 71 |
async init(containerEl: HTMLElement): Promise<void> {
|
| 72 |
+
debug('GameEngine', 'init start', { w: containerEl.clientWidth, h: containerEl.clientHeight })
|
| 73 |
this.containerEl = containerEl
|
| 74 |
|
| 75 |
const w = containerEl.clientWidth
|
| 76 |
const h = containerEl.clientHeight
|
| 77 |
|
| 78 |
this.app = new Application()
|
| 79 |
+
const initStart = performance.now()
|
| 80 |
await this.app.init({
|
| 81 |
width: w,
|
| 82 |
height: h,
|
|
|
|
| 85 |
resolution: window.devicePixelRatio || 1,
|
| 86 |
autoDensity: true,
|
| 87 |
})
|
| 88 |
+
debug('GameEngine', `app.init took ${(performance.now() - initStart).toFixed(1)}ms`)
|
| 89 |
|
| 90 |
+
if (this.destroyed) { debug('GameEngine', 'destroyed after app.init, aborting'); return }
|
| 91 |
|
| 92 |
containerEl.appendChild(this.app.canvas as HTMLCanvasElement)
|
| 93 |
|
|
|
|
| 101 |
this.bgAtmo = new BackgroundAtmosphere(this.app)
|
| 102 |
this.app.stage.addChildAt(this.bgAtmo.container, 0)
|
| 103 |
|
| 104 |
+
if (this.destroyed) { debug('GameEngine', 'destroyed after bgAtmo, aborting'); return }
|
| 105 |
|
| 106 |
this.camera = new CameraController(this.worldContainer, w, h)
|
| 107 |
this.camera.setInitialZoom(this.boardSize)
|
|
|
|
| 109 |
this.camera.onChange = () => this.redraw()
|
| 110 |
this.camera.onCellClick = (gx, gy) => this.handleCellClick(gx, gy)
|
| 111 |
this.camera.onCellHover = (gx, gy) => this.handleCellHover(gx, gy)
|
| 112 |
+
debug('GameEngine', 'camera initialized')
|
| 113 |
|
| 114 |
+
if (this.destroyed) { debug('GameEngine', 'destroyed after camera, aborting'); return }
|
| 115 |
|
| 116 |
this.grid = new GridRenderer()
|
| 117 |
this.worldContainer.addChild(this.grid.container)
|
|
|
|
| 130 |
this.particleLayer = new Container()
|
| 131 |
this.worldContainer.addChild(this.particleLayer)
|
| 132 |
|
|
|
|
| 133 |
this.particleSys = new ParticleSystem(this.particleLayer)
|
| 134 |
this.placementEffect = new PlacementEffect(this.effectLayer, this.particleSys)
|
| 135 |
this.winEffect = new WinEffect(this.effectLayer, this.particleSys, this.camera, w, h, this.screenLayer)
|
|
|
|
| 153 |
|
| 154 |
this.resizeObserver = new ResizeObserver(() => this.handleResize())
|
| 155 |
this.resizeObserver.observe(containerEl)
|
| 156 |
+
debug('GameEngine', 'init complete')
|
| 157 |
}
|
| 158 |
|
| 159 |
destroy(): void {
|
| 160 |
+
debug('GameEngine', 'destroy')
|
| 161 |
this.destroyed = true
|
| 162 |
if (this.camera) {
|
| 163 |
this.camera.onChange = null
|
|
|
|
| 170 |
this.bgAtmo?.destroy()
|
| 171 |
this.symbols?.clear()
|
| 172 |
this.particleSys?.clear()
|
|
|
|
| 173 |
if (this.particleTickerCb) {
|
| 174 |
Ticker.shared.remove(this.particleTickerCb)
|
| 175 |
this.particleTickerCb = null
|
|
|
|
| 219 |
}
|
| 220 |
|
| 221 |
private handleCellClick(gx: number, gy: number): void {
|
| 222 |
+
debug('GameEngine', 'cellClick', { gx, gy, status: this.status, localMode: this.localMode, currentPlayer: this.currentPlayer, playerIndex: this.playerIndex })
|
| 223 |
+
if (this.status !== 'active') { debug('GameEngine', 'cellClick ignored: status not active'); return }
|
| 224 |
+
if (this.board.getCell(gx, gy)) { debug('GameEngine', 'cellClick ignored: cell occupied'); return }
|
| 225 |
+
if (!this.isInBounds(gx, gy)) { debug('GameEngine', 'cellClick ignored: out of bounds'); return }
|
| 226 |
+
if (this.localMode && !this.localInputEnabled) { debug('GameEngine', 'cellClick ignored: local input disabled'); return }
|
| 227 |
|
| 228 |
if (this.localMode && this.manager) {
|
| 229 |
this.placeLocalMove(gx, gy)
|
| 230 |
} else if (!this.localMode) {
|
| 231 |
if (this.playerIndex !== this.currentPlayer) {
|
| 232 |
+
debug('GameEngine', 'cellClick ignored: not your turn')
|
| 233 |
this.onError?.('Not your turn')
|
| 234 |
return
|
| 235 |
}
|
|
|
|
| 252 |
}
|
| 253 |
|
| 254 |
placeLocalMove(gx: number, gy: number): boolean {
|
| 255 |
+
debug('GameEngine', 'placeLocalMove', { gx, gy, currentPlayer: this.currentPlayer })
|
| 256 |
+
if (!this.localMode || !this.manager) { debug('GameEngine', 'placeLocalMove: not local or no manager'); return false }
|
| 257 |
+
if (this.status !== 'active') { debug('GameEngine', 'placeLocalMove: status not active'); return false }
|
| 258 |
+
if (this.board.getCell(gx, gy)) { debug('GameEngine', 'placeLocalMove: cell occupied'); return false }
|
| 259 |
+
if (!this.isInBounds(gx, gy)) { debug('GameEngine', 'placeLocalMove: out of bounds'); return false }
|
| 260 |
|
| 261 |
const result = this.manager.placeMove(gx, gy)
|
| 262 |
+
debug('GameEngine', 'placeLocalMove result:', result)
|
| 263 |
+
if (!result.success) { debug('GameEngine', 'placeLocalMove: manager rejected'); return false }
|
| 264 |
|
| 265 |
const cell = this.manager.getBoard().get(`${gx}:${gy}`)
|
| 266 |
if (!cell) return false
|
|
|
|
| 341 |
}
|
| 342 |
|
| 343 |
applyRoomState(s: RoomState): void {
|
| 344 |
+
debug('GameEngine', 'applyRoomState', { status: s.status, playerIndex: s.playerIndex, currentPlayer: s.currentPlayer, boardSize: s.boardSize, players: s.players })
|
| 345 |
this.clearHover()
|
| 346 |
this.winEffect.clear()
|
| 347 |
if (s.boardSize) {
|
|
|
|
| 370 |
}
|
| 371 |
|
| 372 |
applyMove(currentPlayer: number): void {
|
| 373 |
+
debug('GameEngine', 'applyMove', { currentPlayer })
|
| 374 |
this.currentPlayer = currentPlayer
|
| 375 |
this.onStateChanged?.()
|
| 376 |
}
|
| 377 |
|
| 378 |
rejectMove(x: number, y: number, _message: string): void {
|
| 379 |
+
debug('GameEngine', 'rejectMove', { x, y, message: _message })
|
| 380 |
this.removeCell(x, y)
|
| 381 |
this.currentPlayer = this.previousTurnPlayer
|
| 382 |
this.onStateChanged?.()
|
| 383 |
}
|
| 384 |
|
| 385 |
applyWin(playerId: string, cells: { x: number; y: number }[] = []): void {
|
| 386 |
+
debug('GameEngine', 'applyWin', { playerId, cells, localMode: this.localMode, localPlayerTypes: this.localPlayerTypes })
|
| 387 |
this.status = 'win'
|
| 388 |
this.winnerPlayerId = playerId
|
| 389 |
this.onStateChanged?.()
|
| 390 |
this.audio?.stopMusic()
|
| 391 |
+
|
| 392 |
+
const isPvE = this.localMode && this.localPlayerTypes.some(t => t === 'bot')
|
| 393 |
+
if (isPvE) {
|
| 394 |
+
const humanPlayerId = String(this.localPlayerTypes.indexOf('human'))
|
| 395 |
+
if (playerId === humanPlayerId) {
|
| 396 |
+
this.audio?.playVictoryMusic()
|
| 397 |
+
this.audio?.play('win')
|
| 398 |
+
} else {
|
| 399 |
+
this.audio?.playDefeatMusic()
|
| 400 |
+
}
|
| 401 |
+
} else {
|
| 402 |
+
this.audio?.playVictoryMusic()
|
| 403 |
+
this.audio?.play('win')
|
| 404 |
+
}
|
| 405 |
+
|
| 406 |
this.winEffect.play(cells)
|
| 407 |
}
|
| 408 |
|
| 409 |
applyDraw(): void {
|
| 410 |
+
debug('GameEngine', 'applyDraw')
|
| 411 |
this.status = 'draw'
|
| 412 |
this.currentPlayer = -1
|
| 413 |
this.onStateChanged?.()
|
|
|
|
| 415 |
}
|
| 416 |
|
| 417 |
applyRematch(s: RoomState): void {
|
| 418 |
+
debug('GameEngine', 'applyRematch')
|
| 419 |
this.applyRoomState(s)
|
| 420 |
}
|
| 421 |
|
|
|
|
| 460 |
}
|
| 461 |
|
| 462 |
applySwapFromManager(): void {
|
| 463 |
+
debug('GameEngine', 'applySwapFromManager')
|
| 464 |
if (!this.manager) return
|
| 465 |
const names = this.manager.getPlayerNames()
|
| 466 |
const symbols = this.manager.getPlayerSymbols()
|
| 467 |
const swapped = this.playerSymbols[0] !== symbols[0] || this.playerSymbols[1] !== symbols[1]
|
| 468 |
+
debug('GameEngine', 'swap result', { swapped, symbolsBefore: this.playerSymbols, symbolsAfter: symbols })
|
| 469 |
this.players = [names[0], names[1]]
|
| 470 |
this.playerSymbols = [symbols[0], symbols[1]]
|
| 471 |
this.board.symbols = [symbols[0], symbols[1]]
|
client/src/game/GridRenderer.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import { Container, Graphics, Text, TextStyle } from 'pixi.js'
|
| 2 |
import { CELL_SIZE } from 'shared/board.ts'
|
| 3 |
import { theme, getThemeMode } from '../theme.ts'
|
|
|
|
| 4 |
|
| 5 |
const GRID_WIDTH = 1
|
| 6 |
const GRID_MAJOR_WIDTH = 2
|
|
@@ -49,6 +50,7 @@ export class GridRenderer {
|
|
| 49 |
}
|
| 50 |
|
| 51 |
draw(boardSize: number = 15): void {
|
|
|
|
| 52 |
this.graphics.clear()
|
| 53 |
|
| 54 |
const isGarden = getThemeMode() === 'garden'
|
|
|
|
| 1 |
import { Container, Graphics, Text, TextStyle } from 'pixi.js'
|
| 2 |
import { CELL_SIZE } from 'shared/board.ts'
|
| 3 |
import { theme, getThemeMode } from '../theme.ts'
|
| 4 |
+
import { debug } from '../debug.ts'
|
| 5 |
|
| 6 |
const GRID_WIDTH = 1
|
| 7 |
const GRID_MAJOR_WIDTH = 2
|
|
|
|
| 50 |
}
|
| 51 |
|
| 52 |
draw(boardSize: number = 15): void {
|
| 53 |
+
debug('GridRenderer', 'draw', { boardSize })
|
| 54 |
this.graphics.clear()
|
| 55 |
|
| 56 |
const isGarden = getThemeMode() === 'garden'
|
client/src/game/SymbolRenderer.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import { Container, Graphics, Text, TextStyle } from 'pixi.js'
|
| 2 |
import { CELL_SIZE } from 'shared/board.ts'
|
| 3 |
import { theme } from '../theme.ts'
|
|
|
|
| 4 |
import type { Cell } from 'shared/types.ts'
|
| 5 |
|
| 6 |
export class SymbolRenderer {
|
|
@@ -19,6 +20,7 @@ export class SymbolRenderer {
|
|
| 19 |
_minCellX: number, _minCellY: number,
|
| 20 |
_maxCellX: number, _maxCellY: number,
|
| 21 |
): Container[] {
|
|
|
|
| 22 |
const keepKeys = new Set<string>()
|
| 23 |
const added: Container[] = []
|
| 24 |
|
|
@@ -33,18 +35,22 @@ export class SymbolRenderer {
|
|
| 33 |
added.push(s)
|
| 34 |
}
|
| 35 |
|
|
|
|
| 36 |
for (const [key, symbol] of this.symbols) {
|
| 37 |
if (!keepKeys.has(key)) {
|
| 38 |
this.container.removeChild(symbol)
|
| 39 |
symbol.destroy({ children: true })
|
| 40 |
this.symbols.delete(key)
|
|
|
|
| 41 |
}
|
| 42 |
}
|
|
|
|
| 43 |
|
| 44 |
return added
|
| 45 |
}
|
| 46 |
|
| 47 |
clear(): void {
|
|
|
|
| 48 |
for (const [, symbol] of this.symbols) {
|
| 49 |
this.container.removeChild(symbol)
|
| 50 |
symbol.destroy({ children: true })
|
|
|
|
| 1 |
import { Container, Graphics, Text, TextStyle } from 'pixi.js'
|
| 2 |
import { CELL_SIZE } from 'shared/board.ts'
|
| 3 |
import { theme } from '../theme.ts'
|
| 4 |
+
import { debug } from '../debug.ts'
|
| 5 |
import type { Cell } from 'shared/types.ts'
|
| 6 |
|
| 7 |
export class SymbolRenderer {
|
|
|
|
| 20 |
_minCellX: number, _minCellY: number,
|
| 21 |
_maxCellX: number, _maxCellY: number,
|
| 22 |
): Container[] {
|
| 23 |
+
debug('SymbolRenderer', 'update', { cellCount: cells.length, existingSymbols: this.symbols.size })
|
| 24 |
const keepKeys = new Set<string>()
|
| 25 |
const added: Container[] = []
|
| 26 |
|
|
|
|
| 35 |
added.push(s)
|
| 36 |
}
|
| 37 |
|
| 38 |
+
let removed = 0
|
| 39 |
for (const [key, symbol] of this.symbols) {
|
| 40 |
if (!keepKeys.has(key)) {
|
| 41 |
this.container.removeChild(symbol)
|
| 42 |
symbol.destroy({ children: true })
|
| 43 |
this.symbols.delete(key)
|
| 44 |
+
removed++
|
| 45 |
}
|
| 46 |
}
|
| 47 |
+
if (removed > 0) debug('SymbolRenderer', `removed ${removed} stale symbols`)
|
| 48 |
|
| 49 |
return added
|
| 50 |
}
|
| 51 |
|
| 52 |
clear(): void {
|
| 53 |
+
debug('SymbolRenderer', 'clear', { count: this.symbols.size })
|
| 54 |
for (const [, symbol] of this.symbols) {
|
| 55 |
this.container.removeChild(symbol)
|
| 56 |
symbol.destroy({ children: true })
|