|
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 { gameState, bindToWorld, sendAction } from '$lib/stores/game.svelte.ts'; |
5 | | - import { |
6 | | - uiState, |
7 | | - setIsBuilding, |
8 | | - setIsMovingThief, |
9 | | - setIsPlayingKnight, |
10 | | - setIsStealingFromPlayers |
11 | | - } from '$lib/stores/ui.svelte.ts'; |
12 | | - import type { BuildingType } from '$lib/stores/ui.svelte.ts'; |
| 4 | + import { gameStore } from '$lib/stores/game.svelte.ts'; |
| 5 | + import { uiStore } from '$lib/stores/ui.svelte'; |
| 6 | + import type { BuildingType } from '$lib/stores/ui.svelte'; |
13 | 7 | import { |
14 | 8 | type World, |
15 | 9 | type Player, |
|
41 | 35 | let parentElement: HTMLElement | null = null; |
42 | 36 |
|
43 | 37 | const currentPlayer: Player | undefined = $derived( |
44 | | - gameState.world?.players.find((player: Player) => player.name === gameState.playerName) |
| 38 | + gameStore.world?.players.find((player: Player) => player.name === gameStore.playerName) |
45 | 39 | ); |
46 | 40 |
|
47 | 41 | const hexSize = 200; |
|
120 | 114 | let loadingPromise: Promise<void> | undefined; |
121 | 115 |
|
122 | 116 | $effect(() => { |
123 | | - updateMap(gameState.world); |
| 117 | + updateMap(gameStore.world); |
124 | 118 | }); |
125 | 119 |
|
126 | 120 | $effect(() => { |
127 | | - isBuilding = uiState.isBuilding; |
128 | | - isMovingThief = uiState.isMovingThief; |
129 | | - isPlayingKnight = uiState.isPlayingKnight; |
130 | | - isPlayingRoadBuilding = uiState.isPlayingRoadBuilding; |
| 121 | + isBuilding = uiStore.isBuilding; |
| 122 | + isMovingThief = uiStore.isMovingThief; |
| 123 | + isPlayingKnight = uiStore.isPlayingKnight; |
| 124 | + isPlayingRoadBuilding = uiStore.isPlayingRoadBuilding; |
131 | 125 | }); |
132 | 126 |
|
133 | 127 | const toWorld = (screenPoint: { x: number; y: number }): { x: number; y: number } => { |
|
169 | 163 | cursorGraphics.clear(); |
170 | 164 | cursorGraphics.removeChildren(); |
171 | 165 | try { |
172 | | - await sendAction(action); |
| 166 | + await gameStore.sendAction(action); |
173 | 167 | } catch (error) { |
174 | 168 | console.warn('Failed to send action', error); |
175 | 169 | } |
|
182 | 176 | const hexToFind = grid.pointToHex(inWorld); |
183 | 177 | const moveThiefAction = new MoveThiefAction(currentPlayer.name, hexToFind); |
184 | 178 | dispatchActionClearCursor(moveThiefAction); |
185 | | - setIsMovingThief(false); |
186 | | - setIsStealingFromPlayers(true); |
| 179 | + uiStore.setMovingThief(false); |
| 180 | + uiStore.setStealingFromPlayers(true); |
187 | 181 | }; |
188 | 182 |
|
189 | 183 | const handleIsPlayingKnightClick = (event: FederatedPointerEvent) => { |
|
193 | 187 | const hexToFind = grid.pointToHex(inWorld); |
194 | 188 | const moveThiefAction = new MoveThiefDevCardAction(currentPlayer.name, hexToFind); |
195 | 189 | dispatchActionClearCursor(moveThiefAction); |
196 | | - setIsPlayingKnight(false); |
197 | | - setIsStealingFromPlayers(true); |
| 190 | + uiStore.setPlayingKnight(false); |
| 191 | + uiStore.setStealingFromPlayers(true); |
198 | 192 | }; |
199 | 193 |
|
200 | 194 | const handleBuildClick = (event: FederatedPointerEvent) => { |
201 | | - if (!worldContainer || !currentPlayer || !gameState.world || !grid) return; |
| 195 | + if (!worldContainer || !currentPlayer || !gameStore.world || !grid) return; |
202 | 196 |
|
203 | 197 | const inWorld = toWorld(event.global); |
204 | 198 | const closestPoints = getTwoClosestPoints(grid, inWorld); |
|
210 | 204 |
|
211 | 205 | if (isBuilding === 'House') { |
212 | 206 | const action = |
213 | | - gameState.world.gameState === 'Started' |
| 207 | + gameStore.world.gameState === 'Started' |
214 | 208 | ? new BuildHouseAction(currentPlayer.name, coord) |
215 | 209 | : new BuildHouseInitialAction(currentPlayer.name, coord); |
216 | 210 |
|
217 | 211 | dispatchActionClearCursor(action); |
218 | | - setIsBuilding('None'); |
| 212 | + uiStore.setBuilding('None'); |
219 | 213 | } |
220 | 214 | if (isBuilding === 'City') { |
221 | 215 | dispatchActionClearCursor(new BuildCityAction(currentPlayer.name, coord)); |
222 | | - setIsBuilding('None'); |
| 216 | + uiStore.setBuilding('None'); |
223 | 217 | } |
224 | 218 | if (isBuilding === 'Road' && closestPoints[1].index !== -1) { |
225 | 219 | const coord2 = getMatrixCoordCorner(hexToFind, closestPoints[1].index); |
226 | 220 | const action = |
227 | | - gameState.world.gameState === 'Started' |
| 221 | + gameStore.world.gameState === 'Started' |
228 | 222 | ? new BuildRoadAction(currentPlayer.name, coord, coord2) |
229 | 223 | : new BuildRoadInitialAction(currentPlayer.name, coord, coord2); |
230 | 224 |
|
231 | 225 | dispatchActionClearCursor(action); |
232 | | - setIsBuilding('None'); |
| 226 | + uiStore.setBuilding('None'); |
233 | 227 | } |
234 | 228 | }; |
235 | 229 |
|
|
599 | 593 |
|
600 | 594 | onMount(() => { |
601 | 595 | try { |
602 | | - void bindToWorld(); |
| 596 | + void gameStore.bindToWorld(); |
603 | 597 | } catch (error) { |
604 | 598 | console.warn('Unable to bind to world socket', error); |
605 | 599 | } |
|
0 commit comments