Skip to content

Commit

Permalink
feat: food hits boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
yamcodes committed Feb 8, 2022
1 parent 92ce941 commit 3485576
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "agario-clone",
"version": "0.9.0",
"version": "0.10.0",
"description": "The easily tweakable clone for modders, developers and .io games enthusiasts of any level!",
"license": "UNLICENSED",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/assets/Blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ export default class Blob {
this.edible = edible;
}

draw(x = this.x, y = this.y) {
draw() {
const { strokeOpacity, strokeSize } = settings.blob;
p5.fill(this.color.hex());
p5.strokeWeight(strokeSize);
p5.stroke(this.color.darken(strokeOpacity).hex());
p5.fill(this.color.hex());
p5.circle(x, y, this.r * 2);
p5.circle(this.x, this.y, this.r * 2);
}

static getMassByRadius(radius) {
Expand Down
20 changes: 16 additions & 4 deletions src/assets/Food.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,27 @@ export default class Food {
&& food.y === p5.constrain(food.y, bottom, top)
&& food.alive
) {
const foodVect = p5.createVector(food.x, food.y);
const trajectory = food.mouse;
if (!this.player.eats(food)) {
food.speed = p5.lerp(0, food.speed, settings.food.deceleration);
}
const trajectory = food.mouse;
const foodVect = p5.createVector(food.x, food.y);
trajectory.setMag(food.speed);
foodVect.add(trajectory);
food.x = foodVect.x;
food.y = foodVect.y;
const a = -food.r / p5.sqrt(2);
const w = settings.game.width / 2 + a;
const h = settings.game.height / 2 + a;
if (foodVect.x !== p5.constrain(foodVect.x, -w, w)) {
food.mouse.x *= -1;
food.edible = true;
}
if (foodVect.y !== p5.constrain(foodVect.y, -h, h)) {
food.mouse.y *= -1;
food.edible = true;
}

food.x = p5.constrain(foodVect.x, -w, w);
food.y = p5.constrain(foodVect.y, -h, h);
food.draw();
}
}
Expand Down

0 comments on commit 3485576

Please sign in to comment.