diff --git a/apps/client/src/entities/GameMap.ts b/apps/client/src/entities/GameMap.ts index e54dd9e..90c6cbe 100644 --- a/apps/client/src/entities/GameMap.ts +++ b/apps/client/src/entities/GameMap.ts @@ -1,7 +1,7 @@ // apps/client/src/game/map/GameMap.ts (パスは適宜読み替えてください) import { Container, Graphics } from "pixi.js"; import { GAME_CONFIG } from "@repo/shared/src/config/gameConfig"; -import type { MapState } from "@repo/shared/src/types/map"; +import type { MapState, CellUpdate } from "@repo/shared/src/types/map"; // 親クラスを Graphics から Container に変更し、レイヤー管理を可能にする export class GameMap extends Container { @@ -94,4 +94,24 @@ } } } + + /** + * 差分データを受け取って指定のマスだけ色を更新する + */ + public updateCells(updates: CellUpdate[]) { + const { GRID_CELL_SIZE, TEAM_COLORS } = GAME_CONFIG; + + updates.forEach(({ index, teamId }) => { + const cell = this.cells[index]; + if (!cell) return; + + cell.clear(); + + if (teamId !== -1) { + const colorString = TEAM_COLORS[teamId] || '#FFFFFF'; + const hexColor = parseInt(colorString.replace("#", "0x"), 16); + cell.rect(0, 0, GRID_CELL_SIZE, GRID_CELL_SIZE).fill(hexColor); + } + }); + } } \ No newline at end of file diff --git a/apps/client/src/managers/GameManager.ts b/apps/client/src/managers/GameManager.ts index 0611b10..e62c924 100644 --- a/apps/client/src/managers/GameManager.ts +++ b/apps/client/src/managers/GameManager.ts @@ -12,6 +12,7 @@ private players: Record = {}; private myId: string; private container: HTMLDivElement; + private gameMap!: GameMap; // 入力と状態管理 private joystickInput = { x: 0, y: 0 }; @@ -44,6 +45,7 @@ // 背景マップの配置 const gameMap = new GameMap(); + this.gameMap = gameMap; this.worldContainer.addChild(gameMap); this.app.stage.addChild(this.worldContainer); @@ -100,6 +102,10 @@ delete this.players[id]; } }); + + socketClient.onUpdateMapCells((updates) => { + this.gameMap.updateCells(updates); + }); } /** @@ -149,9 +155,6 @@ this.players = {}; // イベント購読の解除 - socketClient.socket.off("current_players"); - socketClient.socket.off("new_player"); - socketClient.socket.off("update_player"); - socketClient.socket.off("remove_player"); + socketClient.removeAllListeners(); } } \ No newline at end of file diff --git a/apps/client/src/network/SocketClient.ts b/apps/client/src/network/SocketClient.ts index bdd00cd..8112676 100644 --- a/apps/client/src/network/SocketClient.ts +++ b/apps/client/src/network/SocketClient.ts @@ -1,6 +1,7 @@ import { io, Socket } from "socket.io-client"; import { SocketEvents } from "@repo/shared/src/protocol/events"; import type { PlayerData } from "@repo/shared/src/types/player"; +import type { CellUpdate } from "@repo/shared/src/types/map"; /** * サーバー WebSocket 通信管理クラス @@ -67,6 +68,13 @@ } /** + * マス目の差分更新イベント購読 + */ + onUpdateMapCells(callback: (updates: CellUpdate[]) => void) { + this.socket.on(SocketEvents.UPDATE_MAP_CELLS, callback); + } + + /** * ルーム入室リクエスト送信 * @param roomId 入室先のID * @param playerName 表示名 @@ -102,6 +110,17 @@ readyForGame() { this.socket.emit(SocketEvents.READY_FOR_GAME); } + + /** + * 全てのゲームプレイ関連イベントを解除(一括解除用) + */ + removeAllListeners() { + this.socket.off(SocketEvents.CURRENT_PLAYERS); + this.socket.off(SocketEvents.NEW_PLAYER); + this.socket.off(SocketEvents.UPDATE_PLAYER); + this.socket.off(SocketEvents.REMOVE_PLAYER); + this.socket.off(SocketEvents.UPDATE_MAP_CELLS); + } } // シングルトン利用向け共有インスタンス diff --git a/packages/shared/src/protocol/events.ts b/packages/shared/src/protocol/events.ts index 17ebae2..a69f60f 100644 --- a/packages/shared/src/protocol/events.ts +++ b/packages/shared/src/protocol/events.ts @@ -15,5 +15,6 @@ NEW_PLAYER: "new_player", UPDATE_PLAYER: "update_player", REMOVE_PLAYER: "remove_player", - MOVE: "move" + MOVE: "move", + UPDATE_MAP_CELLS: "update_map_cells", } as const; diff --git a/packages/shared/src/types/map.ts b/packages/shared/src/types/map.ts index a603272..d85090e 100644 --- a/packages/shared/src/types/map.ts +++ b/packages/shared/src/types/map.ts @@ -1,3 +1,8 @@ export interface MapState { gridColors: number[]; +} + +export interface CellUpdate { + index: number; + teamId: number; } \ No newline at end of file