diff --git a/src/main.ts b/src/main.ts index e4fcde7..49cdef0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -463,6 +463,12 @@ function nextHole(seed: number) { newState.room.recvGameOver(go => { initLeaderboard(state as InGameState, go) }) + newState.room.recvCollision(coll => { + if(newState.ourBall.pos.y > 0) { + newState.ourBall.state = "fly" + newState.ourBall.vel.add((coll.initiatorVx + Math.random()) * 0.8, (coll.initiatorVy + Math.random()) * 0.8) + } + }) state = newState; } @@ -501,6 +507,12 @@ function nextHole(seed: number) { newState.room.recvHoleFinished((hl, pid) => { newState.finishedBalls[pid] = hl }) + newState.room.recvCollision(coll => { + if(newState.ourBall.pos.y > 0) { + newState.ourBall.state = "fly" + newState.ourBall.vel.add((coll.initiatorVx + Math.random()) * 0.8, (coll.initiatorVy + Math.random()) * 0.8) + } + }) if(didFail) { initGiveUp(newState) @@ -769,6 +781,7 @@ function updateShotState(igs: InGameState) { ball.angleVel = -ball.angleVel; ball.angle += ball.angleVel * 0.05 * 2; } + // @ts-ignore color("light_" + igs.ballColor); line(ball.pos, vec(ball.pos).addWithAngle(ball.angle, 9), 2); @@ -851,6 +864,20 @@ function updateFlyState(igs: InGameState) { return; } } + + // this _has_ to be like this because crisp-lib doesn't have non-mask cd + for(let otherPid in igs.otherBalls) { + if(igs.otherBalls[otherPid].spectating) continue; + + const dist = ball.pos.distanceTo(igs.otherBalls[otherPid].x, igs.otherBalls[otherPid].y); + if(dist <= 3) { + igs.room.sendCollision({ + initiatorVx: ball.vel.x, + initiatorVy: ball.vel.y, + }, otherPid) + } + } + ball.pos.add(ball.vel); ball.vel.mul(0.98); ball.vel.y += 0.1; diff --git a/src/multiplayer.ts b/src/multiplayer.ts index 25cab59..a494e02 100644 --- a/src/multiplayer.ts +++ b/src/multiplayer.ts @@ -123,6 +123,11 @@ export type GameOverPayload = { losers: Record, } +export type CollisionPayload = { + initiatorVx: number, + initiatorVy: number +} + export interface RawGolfRoom { nameAndPass: NameAndPass, rawRoom: Room, @@ -178,6 +183,10 @@ export interface RawGolfRoom { // all peers assume that all peers are not ready, and will send LobbyInfoRequests // these may go unanswered when other peers are still on the game over screen // clients will broadcast their LobbyInfo as soon as they return to the lobby screen + + // the fun and jank + sendCollision: ActionSender, + recvCollision: ActionReceiver } const createActions = (nameAndPass: NameAndPass, rawRoom: Room): RawGolfRoom => { @@ -202,6 +211,7 @@ const createActions = (nameAndPass: NameAndPass, rawRoom: Room): RawGolfRoom => const [sendHoleLost, recvHoleLost] = rawRoom.makeAction("holeLose") const [sendHoleFinished, recvHoleFinished] = rawRoom.makeAction("holeFin") const [sendGameOver, recvGameOver] = rawRoom.makeAction("gameOver") + const [sendCollision, recvCollision] = rawRoom.makeAction("collision") return { nameAndPass, @@ -228,7 +238,9 @@ const createActions = (nameAndPass: NameAndPass, rawRoom: Room): RawGolfRoom => sendHoleFinished, recvHoleFinished, sendGameOver, - recvGameOver + recvGameOver, + sendCollision, + recvCollision } }