From 3485576d46cefbae8f84060dbdc2a253096e8b70 Mon Sep 17 00:00:00 2001 From: yamyam263 Date: Tue, 8 Feb 2022 02:24:33 +0200 Subject: [PATCH] feat: food hits boundaries --- package.json | 2 +- src/assets/Blob.js | 4 ++-- src/assets/Food.js | 20 ++++++++++++++++---- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 7b787c1..95b674d 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/assets/Blob.js b/src/assets/Blob.js index c72f3d0..17852a2 100644 --- a/src/assets/Blob.js +++ b/src/assets/Blob.js @@ -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) { diff --git a/src/assets/Food.js b/src/assets/Food.js index 6f0f478..bfc8d6c 100644 --- a/src/assets/Food.js +++ b/src/assets/Food.js @@ -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(); } }