Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
feat: scoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixselve committed Jan 24, 2024
1 parent 22b57e3 commit 3e2b49c
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/client/src/app/canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export function Canvas({ centered }: { centered?: boolean }) {
api.scene.food.forEach((food) => {
const screenFood = worldToScreen(food.position, camera);
c.beginPath();
c.arc(screenFood.x, screenFood.y, 10, 0, 2 * Math.PI); // Draw a circle for each food
c.arc(screenFood.x, screenFood.y, 7, 0, 2 * Math.PI); // Draw a circle for each food
// use green color
c.fillStyle = "green";
c.fill();
Expand All @@ -120,7 +120,7 @@ export function Canvas({ centered }: { centered?: boolean }) {
api.scene.orbs.forEach((orb) => {
const screenOrb = worldToScreen(orb.position, camera);
c.beginPath();
c.arc(screenOrb.x, screenOrb.y, 5 * orb.size, 0, 2 * Math.PI); // Draw a circle for each orb
c.arc(screenOrb.x, screenOrb.y, 3 * orb.size, 0, 2 * Math.PI); // Draw a circle for each orb
// use blue color
c.fillStyle = "blue";
c.fill();
Expand Down
50 changes: 45 additions & 5 deletions packages/server/src/handleFrame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ import {
SPRINT_SPEED,
MAX_ANGLE,
ORB_SPRINTING_DROP_RATE,
SCORE_PER_ORB,
MIN_SCORE_TO_SPRINT,
SCORE_PER_FOOD,
SCORE_PER_BODY_PART,
} from "@viper-vortex/shared";

function movePlayer(player: Player, gameMap: GameMap) {
// move the player
// start with the head

const head = getPlayerHead(player);

// can the player sprint?
if (player.score < MIN_SCORE_TO_SPRINT) {
player.isSprinting = false;
}

const speed = player.isSprinting ? SPRINT_SPEED : BASE_SPEED;

if (player.isSprinting) {
Expand All @@ -27,6 +37,7 @@ function movePlayer(player: Player, gameMap: GameMap) {
position: { x: lastBodyPart.x, y: lastBodyPart.y },
size: 1,
});
player.score -= SCORE_PER_ORB;
}
}

Expand Down Expand Up @@ -83,11 +94,8 @@ export default function handleFrame(
// collision
// remove the food
gameMap.food.splice(i, 1);
// add a new body part
player.body.push({
x: player.body[player.body.length - 1].x,
y: player.body[player.body.length - 1].y,
});
// add score
player.score += SCORE_PER_FOOD;
}
}

Expand All @@ -111,5 +119,37 @@ export default function handleFrame(
}
}
}

// Check for collisions with orbs
for (let i = 0; i < gameMap.orbs.length; i++) {
const orb = gameMap.orbs[i];
if (
Math.abs(orb.position.x - head.x) < FOOD_PICKUP_RADIUS &&
Math.abs(orb.position.y - head.y) < FOOD_PICKUP_RADIUS
) {
// collision
// remove the orb
gameMap.orbs.splice(i, 1);
// add score
player.score += SCORE_PER_ORB;
}
}

// Update the player's body based on the score
// Minimum length of 1 (the head)
const bodyLength = Math.max(
1,
Math.floor(player.score / SCORE_PER_BODY_PART),
);

while (player.body.length < bodyLength) {
player.body.push({
x: player.body[player.body.length - 1].x,
y: player.body[player.body.length - 1].y,
});
}
while (player.body.length > bodyLength) {
player.body.pop();
}
}
}
1 change: 1 addition & 0 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ io.on(SOCKET_EVENTS.CONNECT, (socket) => {
angle: 0,
desiredAngle: 0,
orbToDrop: 0,
score: 0,
};

gameMap.players.push(player);
Expand Down
21 changes: 21 additions & 0 deletions packages/shared/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ export const MAX_ANGLE = Math.PI / 20;
*/
export const ORB_SPRINTING_DROP_RATE = 0.1;

/**
* The amount of score gained/lost per orb.
* You lose orbs when sprinting.
*/
export const SCORE_PER_ORB = 5;

/**
* The amount of score required for each body part.
*/
export const SCORE_PER_BODY_PART = 10;

/**
* The amount of score gained per food.
*/
export const SCORE_PER_FOOD = SCORE_PER_BODY_PART;

/**
* The minimum score required to sprint.
*/
export const MIN_SCORE_TO_SPRINT = SCORE_PER_BODY_PART * 4;

/**
* Enum for socket events.
* @enum {string}
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type Player = {
desiredAngle: number;
// 0 - 1: will vary based on `ORB_SPRINTING_DROP_RATE`
orbToDrop: number;
score: number;
};

/**
Expand Down

0 comments on commit 3e2b49c

Please sign in to comment.