diff options
Diffstat (limited to 'src/state/index.ts')
-rw-r--r-- | src/state/index.ts | 39 |
1 files changed, 32 insertions, 7 deletions
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); |