From ffcdb8b126c5267040ddc8f0304b153fa88755e5 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Sat, 28 Jun 2025 18:41:15 -0700 Subject: local play, some physics --- src/state/const.ts | 11 +++++++++++ src/state/index.ts | 31 +++++++++++++++++++++++++++++++ src/state/types.ts | 25 +++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 src/state/const.ts create mode 100644 src/state/index.ts create mode 100644 src/state/types.ts (limited to 'src/state') diff --git a/src/state/const.ts b/src/state/const.ts new file mode 100644 index 0000000..caac8ce --- /dev/null +++ b/src/state/const.ts @@ -0,0 +1,11 @@ +export const GAME_SIZE = { + rows: 20, + cols: 20, +}; + +export const INITIAL_PADDLE_POSITION = 10; + +export const PADDLE_WIDTH = 3; +export const PADDLE_HEIGHT = 1; + +export const NUM_STARTING_BALLS = 2; diff --git a/src/state/index.ts b/src/state/index.ts new file mode 100644 index 0000000..03d218f --- /dev/null +++ b/src/state/index.ts @@ -0,0 +1,31 @@ +import { GAME_SIZE, NUM_STARTING_BALLS } from "./const"; +import { Ball, GameState, SessionState } from "./types"; + +export * from "./const"; +export * from "./types"; + +const createRandomBall = (): Ball => ({ + position: [ + GAME_SIZE.cols / 4 + (Math.random() * GAME_SIZE.cols) / 2, + GAME_SIZE.rows / 4 + (Math.random() * GAME_SIZE.rows) / 2, + ], + velocity: [-1 + Math.random() * 2, 0.5 + Math.random()], +}); + +const createGameState = () => + ({ + paddle: { position: [0, GAME_SIZE.rows - 1] }, + 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)], + })), + } satisfies GameState); + +export const createSessionState = (sessionId: string) => + ({ + sessionId, + localPlayerGameState: createGameState(), + remotePlayerGameState: createGameState(), + } satisfies SessionState); diff --git a/src/state/types.ts b/src/state/types.ts new file mode 100644 index 0000000..202e65e --- /dev/null +++ b/src/state/types.ts @@ -0,0 +1,25 @@ +export type Paddle = { + position: [number, number]; +}; + +export type Ball = { + //float positions + position: [number, number]; + velocity: [number, number]; +}; + +export type Brick = { + position: [number, number]; +}; + +export type GameState = { + paddle: Paddle; + balls: Ball[]; + bricks: Brick[]; +}; + +export type SessionState = { + sessionId: string; + localPlayerGameState: GameState; + remotePlayerGameState: GameState; +}; -- cgit v1.2.3-70-g09d2