Skip to content

Commit 7023143

Browse files
Simplify
1 parent b6d521b commit 7023143

17 files changed

Lines changed: 278 additions & 989 deletions

File tree

islanders-client/src/lib/components/Map.svelte

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<script lang="ts">
22
import { onMount } from 'svelte';
33
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';
64
import type { BuildingType } from '$lib/stores/ui.svelte';
75
import {
86
type World,
@@ -28,14 +26,24 @@
2826
import { getClosestPoint, getTwoClosestPoints, matrixCoordToGridWorldCoord } from './mapUtils';
2927
import * as honeycombGrid from 'honeycomb-grid';
3028
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';
3131
const { defineHex, Grid, Orientation } = honeycombGrid;
32+
import type { Resources } from '../../../../islanders-shared/lib/Shared';
3233
34+
type TradeParameters = { player: string; resources: Resources; wants: Resources };
3335
type HexType = ReturnType<typeof defineHex>;
3436
type CustomHex = InstanceType<HexType>;
3537
3638
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)
3840
);
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);
3947
4048
const hexSize = 200;
4149
const tileHeight = 348;
@@ -106,13 +114,8 @@
106114
let latestWorld: World | undefined;
107115
let loadingPromise: Promise<void> | undefined;
108116
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-
114117
$effect(() => {
115-
updateMap(game.world);
118+
updateMap(getWorld());
116119
});
117120
118121
const toWorld = (screenPoint: { x: number; y: number }): { x: number; y: number } => {
@@ -154,7 +157,7 @@
154157
cursorGraphics.clear();
155158
cursorGraphics.removeChildren();
156159
try {
157-
await game.sendAction(action);
160+
await sendAction(action);
158161
} catch (error) {
159162
console.warn('Failed to send action', error);
160163
}
@@ -168,8 +171,7 @@
168171
const hexCoord = { x: hexToFind.col, y: hexToFind.row };
169172
const moveThiefAction = new MoveThiefAction(currentPlayer.name, hexCoord);
170173
dispatchActionClearCursor(moveThiefAction);
171-
ui.setMovingThief(false);
172-
// Don't set isStealingFromPlayers here - let the world update handler do it
174+
isMovingThief = false;
173175
};
174176
175177
const handleIsPlayingKnightClick = (event: FederatedPointerEvent) => {
@@ -180,12 +182,11 @@
180182
const hexCoord = { x: hexToFind.col, y: hexToFind.row };
181183
const moveThiefAction = new MoveThiefDevCardAction(currentPlayer.name, hexCoord);
182184
dispatchActionClearCursor(moveThiefAction);
183-
ui.setPlayingKnight(false);
184-
// Don't set isStealingFromPlayers here - let the world update handler do it
185+
isStealingFromPlayers = true;
185186
};
186187
187188
const handleBuildClick = (event: FederatedPointerEvent) => {
188-
if (!worldContainer || !currentPlayer || !game.world || !grid) return;
189+
if (!worldContainer || !currentPlayer || !getWorld() || !grid) return;
189190
190191
const inWorld = toWorld(event.global);
191192
const closestPoints = getTwoClosestPoints(grid, inWorld);
@@ -198,26 +199,26 @@
198199
199200
if (isBuilding === 'House') {
200201
const action =
201-
game.world.gameState === 'Started'
202+
getWorld()?.gameState === 'Started'
202203
? new BuildHouseAction(currentPlayer.name, coord)
203204
: new BuildHouseInitialAction(currentPlayer.name, coord);
204205
205206
dispatchActionClearCursor(action);
206-
ui.setBuilding('None');
207+
isBuilding = 'None';
207208
}
208209
if (isBuilding === 'City') {
209210
dispatchActionClearCursor(new BuildCityAction(currentPlayer.name, coord));
210-
ui.setBuilding('None');
211+
isBuilding = 'None';
211212
}
212213
if (isBuilding === 'Road' && closestPoints[1].index !== -1) {
213214
const coord2 = getMatrixCoordCorner(hexCoord, closestPoints[1].index);
214215
const action =
215-
game.world.gameState === 'Started'
216+
getWorld()?.gameState === 'Started'
216217
? new BuildRoadAction(currentPlayer.name, coord, coord2)
217218
: new BuildRoadInitialAction(currentPlayer.name, coord, coord2);
218219
219220
dispatchActionClearCursor(action);
220-
ui.setBuilding('None');
221+
isBuilding = 'None';
221222
}
222223
};
223224
@@ -580,7 +581,6 @@
580581
};
581582
582583
onMount(() => {
583-
game.bindToWorld();
584584
setupCanvas();
585585
handleResize();
586586

islanders-client/src/lib/components/PlayerSummary.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
2+
import { getPlayerColorAsHex } from '$lib/helpers';
23
import type { Player } from '../../../../islanders-shared/lib/Player';
3-
import { game } from '$lib/stores/game.svelte';
44
55
interface PlayerInformation {
66
player?: Player;
@@ -15,7 +15,7 @@
1515
const playerName = $derived(player?.name ?? '');
1616
const points = $derived(player?.points ?? 0);
1717
const color = $derived(
18-
player && playerName ? (game.getPlayerColorAsHex(playerName) ?? defaultColor) : defaultColor
18+
player && playerName ? (getPlayerColorAsHex(playerName) ?? defaultColor) : defaultColor
1919
);
2020
</script>
2121

islanders-client/src/lib/components/mapUtils.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,7 @@ export const matrixCoordToGridWorldCoord = (
154154
* @param currentPlayerName - The name of the current player (who cannot be stolen from themselves)
155155
* @returns Array of players who can be stolen from
156156
*/
157-
export const getStealablePlayers = (
158-
world: World,
159-
currentPlayerName: string | undefined
160-
): Player[] => {
157+
export const getStealablePlayers = (world: World): Player[] => {
161158
if (!world.thief || !world.players) return [];
162159

163160
const thiefHex = world.thief.hexCoordinate;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { getSocket, getWorld } from './stores/socket.svelte';
2+
import { Player, SocketActions } from '../../../islanders-shared/lib/Shared';
3+
import { playerName } from './stores/game.svelte';
4+
5+
export const sendMessage = (text: string) => {
6+
const trimmed = text.trim();
7+
if (!trimmed) return;
8+
9+
const outgoing = { text: trimmed, user: playerName };
10+
getSocket()?.emit(SocketActions.chat, outgoing);
11+
};
12+
13+
export const getPlayerColorAsHex = (name: string): string | undefined => {
14+
const color = getWorld()?.players.find((player: Player) => player.name === name)?.color;
15+
if (color === undefined) {
16+
return undefined;
17+
}
18+
return `#${(color >>> 0).toString(16).padStart(6, '0')}`;
19+
};

islanders-client/src/lib/stores/chat.svelte.ts

Lines changed: 0 additions & 137 deletions
This file was deleted.

0 commit comments

Comments
 (0)