|
1 | 1 | <script lang="ts"> |
2 | 2 | import { onMount } from 'svelte'; |
3 | 3 | import { Application, Container, Graphics, Point, Assets, FederatedPointerEvent } from 'pixi.js'; |
4 | | - import { game } from '$lib/stores/game.svelte.ts'; |
5 | | - import { ui } from '$lib/stores/ui.svelte'; |
6 | 4 | import type { BuildingType } from '$lib/stores/ui.svelte'; |
7 | 5 | import { |
8 | 6 | type World, |
|
28 | 26 | import { getClosestPoint, getTwoClosestPoints, matrixCoordToGridWorldCoord } from './mapUtils'; |
29 | 27 | import * as honeycombGrid from 'honeycomb-grid'; |
30 | 28 | import type { Grid as HoneycombGrid } from 'honeycomb-grid'; |
| 29 | + import { getWorld } from '$lib/stores/socket.svelte'; |
| 30 | + import { playerName, sendAction } from '$lib/stores/game.svelte'; |
31 | 31 | const { defineHex, Grid, Orientation } = honeycombGrid; |
| 32 | + import type { Resources } from '../../../../islanders-shared/lib/Shared'; |
32 | 33 |
|
| 34 | + type TradeParameters = { player: string; resources: Resources; wants: Resources }; |
33 | 35 | type HexType = ReturnType<typeof defineHex>; |
34 | 36 | type CustomHex = InstanceType<HexType>; |
35 | 37 |
|
36 | 38 | const currentPlayer: Player | undefined = $derived( |
37 | | - game.world?.players.find((player: Player) => player.name === game.playerName) |
| 39 | + getWorld()?.players.find((player: Player) => player.name === playerName) |
38 | 40 | ); |
| 41 | + let isBuilding = $state<BuildingType>('None'); |
| 42 | + let isMovingThief = $state(false); |
| 43 | + let isPlayingRoadBuilding = $state(false); |
| 44 | + let isPlayingKnight = $state(false); |
| 45 | + let isStealingFromPlayers = $state(false); |
| 46 | + let playerProposesTrade = $state<TradeParameters | undefined>(undefined); |
39 | 47 |
|
40 | 48 | const hexSize = 200; |
41 | 49 | const tileHeight = 348; |
|
106 | 114 | let latestWorld: World | undefined; |
107 | 115 | let loadingPromise: Promise<void> | undefined; |
108 | 116 |
|
109 | | - let isBuilding: BuildingType = $derived(ui.isBuilding); |
110 | | - let isMovingThief = $derived(ui.isMovingThief); |
111 | | - let isPlayingKnight = $derived(ui.isPlayingKnight); |
112 | | - let isPlayingRoadBuilding = $derived(ui.isPlayingRoadBuilding); |
113 | | -
|
114 | 117 | $effect(() => { |
115 | | - updateMap(game.world); |
| 118 | + updateMap(getWorld()); |
116 | 119 | }); |
117 | 120 |
|
118 | 121 | const toWorld = (screenPoint: { x: number; y: number }): { x: number; y: number } => { |
|
154 | 157 | cursorGraphics.clear(); |
155 | 158 | cursorGraphics.removeChildren(); |
156 | 159 | try { |
157 | | - await game.sendAction(action); |
| 160 | + await sendAction(action); |
158 | 161 | } catch (error) { |
159 | 162 | console.warn('Failed to send action', error); |
160 | 163 | } |
|
168 | 171 | const hexCoord = { x: hexToFind.col, y: hexToFind.row }; |
169 | 172 | const moveThiefAction = new MoveThiefAction(currentPlayer.name, hexCoord); |
170 | 173 | dispatchActionClearCursor(moveThiefAction); |
171 | | - ui.setMovingThief(false); |
172 | | - // Don't set isStealingFromPlayers here - let the world update handler do it |
| 174 | + isMovingThief = false; |
173 | 175 | }; |
174 | 176 |
|
175 | 177 | const handleIsPlayingKnightClick = (event: FederatedPointerEvent) => { |
|
180 | 182 | const hexCoord = { x: hexToFind.col, y: hexToFind.row }; |
181 | 183 | const moveThiefAction = new MoveThiefDevCardAction(currentPlayer.name, hexCoord); |
182 | 184 | dispatchActionClearCursor(moveThiefAction); |
183 | | - ui.setPlayingKnight(false); |
184 | | - // Don't set isStealingFromPlayers here - let the world update handler do it |
| 185 | + isStealingFromPlayers = true; |
185 | 186 | }; |
186 | 187 |
|
187 | 188 | const handleBuildClick = (event: FederatedPointerEvent) => { |
188 | | - if (!worldContainer || !currentPlayer || !game.world || !grid) return; |
| 189 | + if (!worldContainer || !currentPlayer || !getWorld() || !grid) return; |
189 | 190 |
|
190 | 191 | const inWorld = toWorld(event.global); |
191 | 192 | const closestPoints = getTwoClosestPoints(grid, inWorld); |
|
198 | 199 |
|
199 | 200 | if (isBuilding === 'House') { |
200 | 201 | const action = |
201 | | - game.world.gameState === 'Started' |
| 202 | + getWorld()?.gameState === 'Started' |
202 | 203 | ? new BuildHouseAction(currentPlayer.name, coord) |
203 | 204 | : new BuildHouseInitialAction(currentPlayer.name, coord); |
204 | 205 |
|
205 | 206 | dispatchActionClearCursor(action); |
206 | | - ui.setBuilding('None'); |
| 207 | + isBuilding = 'None'; |
207 | 208 | } |
208 | 209 | if (isBuilding === 'City') { |
209 | 210 | dispatchActionClearCursor(new BuildCityAction(currentPlayer.name, coord)); |
210 | | - ui.setBuilding('None'); |
| 211 | + isBuilding = 'None'; |
211 | 212 | } |
212 | 213 | if (isBuilding === 'Road' && closestPoints[1].index !== -1) { |
213 | 214 | const coord2 = getMatrixCoordCorner(hexCoord, closestPoints[1].index); |
214 | 215 | const action = |
215 | | - game.world.gameState === 'Started' |
| 216 | + getWorld()?.gameState === 'Started' |
216 | 217 | ? new BuildRoadAction(currentPlayer.name, coord, coord2) |
217 | 218 | : new BuildRoadInitialAction(currentPlayer.name, coord, coord2); |
218 | 219 |
|
219 | 220 | dispatchActionClearCursor(action); |
220 | | - ui.setBuilding('None'); |
| 221 | + isBuilding = 'None'; |
221 | 222 | } |
222 | 223 | }; |
223 | 224 |
|
|
580 | 581 | }; |
581 | 582 |
|
582 | 583 | onMount(() => { |
583 | | - game.bindToWorld(); |
584 | 584 | setupCanvas(); |
585 | 585 | handleResize(); |
586 | 586 |
|
|
0 commit comments