Skip to content

Commit

Permalink
Finish collision
Browse files Browse the repository at this point in the history
  • Loading branch information
s5bug committed Nov 15, 2024
1 parent e32149a commit c897ee3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 13 additions & 1 deletion src/multiplayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ export type GameOverPayload = {
losers: Record<string, number>,
}

export type CollisionPayload = {
initiatorVx: number,
initiatorVy: number
}

export interface RawGolfRoom {
nameAndPass: NameAndPass,
rawRoom: Room,
Expand Down Expand Up @@ -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<CollisionPayload>,
recvCollision: ActionReceiver<CollisionPayload>
}

const createActions = (nameAndPass: NameAndPass, rawRoom: Room): RawGolfRoom => {
Expand All @@ -202,6 +211,7 @@ const createActions = (nameAndPass: NameAndPass, rawRoom: Room): RawGolfRoom =>
const [sendHoleLost, recvHoleLost] = rawRoom.makeAction<HoleLostPayload>("holeLose")
const [sendHoleFinished, recvHoleFinished] = rawRoom.makeAction<HoleFinishedPayload>("holeFin")
const [sendGameOver, recvGameOver] = rawRoom.makeAction<GameOverPayload>("gameOver")
const [sendCollision, recvCollision] = rawRoom.makeAction<CollisionPayload>("collision")

return {
nameAndPass,
Expand All @@ -228,7 +238,9 @@ const createActions = (nameAndPass: NameAndPass, rawRoom: Room): RawGolfRoom =>
sendHoleFinished,
recvHoleFinished,
sendGameOver,
recvGameOver
recvGameOver,
sendCollision,
recvCollision
}
}

Expand Down

0 comments on commit c897ee3

Please sign in to comment.