diff options
Diffstat (limited to 'src/game/index.ts')
-rw-r--r-- | src/game/index.ts | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/src/game/index.ts b/src/game/index.ts index 3ab20ca..62debf0 100644 --- a/src/game/index.ts +++ b/src/game/index.ts @@ -1,15 +1,22 @@ -import { GAME_SIZE, PADDLE_HEIGHT, PADDLE_WIDTH, SessionState } from "../state"; -import { VELOCITY_SCALING_FACTOR } from "./const"; +import { + createRandomBall, + Event, + GAME_SIZE, + PADDLE_HEIGHT, + PADDLE_WIDTH, + SessionState, +} from "../state"; import { Action, Collider } from "./types"; import { applyBallBounce, applyBallVelocity } from "./utils"; -export const advanceState = async ( +export const advanceStateLocal = async ( curState: SessionState, action: Action | undefined ): Promise<SessionState> => { - //simulate network + //minimum delay await new Promise((res) => setTimeout(res, 15)); + let candidateOutboundEventQueue: Event[] = []; let candidatePaddle = curState.localPlayerGameState.paddle; if (action === Action.MOVE_LEFT) { @@ -48,7 +55,10 @@ export const advanceState = async ( max: pos + 0.5, })) as Collider["boundingBox"], normal: [0, 1], - onHit: () => candidateBricks.splice(i, 1), + onHit: () => { + candidateOutboundEventQueue.push({ name: "NEW_BALL" }); + candidateBricks.splice(i, 1); + }, }); }); @@ -101,13 +111,24 @@ export const advanceState = async ( }) .filter((ball) => !!ball); + //apply incoming events + for (const event of curState.inboundEventQueue) { + switch (event.name) { + case "NEW_BALL": + candidateBalls.push(createRandomBall()); + } + } + return { ...curState, + inboundEventQueue: [], + outboundEventQueue: candidateOutboundEventQueue, localPlayerGameState: { ...curState.localPlayerGameState, bricks: candidateBricks, paddle: candidatePaddle, balls: candidateBalls, }, + seqno: curState.seqno + 1, }; }; |