summaryrefslogtreecommitdiff
path: root/src/state
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2025-06-29 15:56:06 -0700
committerKai Stevenson <kai@kaistevenson.com>2025-06-29 16:36:20 -0700
commit3df0abc4b0a374c57845e96339bf5a46d8189364 (patch)
treee11dcea578948fbd134de93149f5576ab7ff2c8c /src/state
parentffcdb8b126c5267040ddc8f0304b153fa88755e5 (diff)
networking worksHEADmaster
Diffstat (limited to 'src/state')
-rw-r--r--src/state/const.ts5
-rw-r--r--src/state/index.ts39
-rw-r--r--src/state/types.ts9
3 files changed, 44 insertions, 9 deletions
diff --git a/src/state/const.ts b/src/state/const.ts
index caac8ce..380eb99 100644
--- a/src/state/const.ts
+++ b/src/state/const.ts
@@ -8,4 +8,7 @@ export const INITIAL_PADDLE_POSITION = 10;
export const PADDLE_WIDTH = 3;
export const PADDLE_HEIGHT = 1;
-export const NUM_STARTING_BALLS = 2;
+export const NUM_STARTING_BALLS = 1;
+
+export const NUM_BRICKS = GAME_SIZE.cols * 5;
+export const BRICK_DISTRIBUTION = 0.9;
diff --git a/src/state/index.ts b/src/state/index.ts
index 03d218f..6999188 100644
--- a/src/state/index.ts
+++ b/src/state/index.ts
@@ -1,10 +1,15 @@
-import { GAME_SIZE, NUM_STARTING_BALLS } from "./const";
-import { Ball, GameState, SessionState } from "./types";
+import {
+ BRICK_DISTRIBUTION,
+ GAME_SIZE,
+ NUM_BRICKS,
+ NUM_STARTING_BALLS,
+} from "./const";
+import { Ball, Brick, GameState, SessionState } from "./types";
export * from "./const";
export * from "./types";
-const createRandomBall = (): Ball => ({
+export const createRandomBall = (): Ball => ({
position: [
GAME_SIZE.cols / 4 + (Math.random() * GAME_SIZE.cols) / 2,
GAME_SIZE.rows / 4 + (Math.random() * GAME_SIZE.rows) / 2,
@@ -18,14 +23,34 @@ const createGameState = () =>
balls: new Array(NUM_STARTING_BALLS)
.fill(undefined)
.map(() => createRandomBall()),
- bricks: new Array(GAME_SIZE.cols * 5).fill(undefined).map((_, i) => ({
- position: [i % GAME_SIZE.cols, Math.floor(i / GAME_SIZE.cols)],
- })),
+ bricks: new Array(NUM_BRICKS)
+ .fill(undefined)
+ .map((_, i) =>
+ Math.random() < BRICK_DISTRIBUTION
+ ? ({
+ position: [i % GAME_SIZE.cols, Math.floor(i / GAME_SIZE.cols)],
+ } as Brick)
+ : undefined
+ )
+ .filter((b) => !!b),
} satisfies GameState);
-export const createSessionState = (sessionId: string) =>
+export const createLocalSessionState = (sessionId: string) =>
+ ({
+ sessionId,
+ seqno: 0,
+ localPlayerGameState: createGameState(),
+ remotePlayerGameState: undefined,
+ inboundEventQueue: [],
+ outboundEventQueue: [],
+ } satisfies SessionState);
+
+export const createNetworkedSessionState = (sessionId: string) =>
({
sessionId,
+ seqno: 0,
localPlayerGameState: createGameState(),
remotePlayerGameState: createGameState(),
+ inboundEventQueue: [],
+ outboundEventQueue: [],
} satisfies SessionState);
diff --git a/src/state/types.ts b/src/state/types.ts
index 202e65e..517be17 100644
--- a/src/state/types.ts
+++ b/src/state/types.ts
@@ -18,8 +18,15 @@ export type GameState = {
bricks: Brick[];
};
+export type NewBallEvent = { name: "NEW_BALL" };
+
+export type Event = NewBallEvent;
+
export type SessionState = {
sessionId: string;
+ seqno: number;
localPlayerGameState: GameState;
- remotePlayerGameState: GameState;
+ remotePlayerGameState: GameState | undefined;
+ inboundEventQueue: Event[];
+ outboundEventQueue: Event[];
};