From ab86f9570c70c839e34c18f57221e6471d52496e Mon Sep 17 00:00:00 2001 From: "C. Yang" Date: Wed, 3 Jan 2024 12:26:42 -0500 Subject: [PATCH] Feeding collision detection --- src/actions.js | 1 - src/fish.js | 16 +++++++++++++--- src/main.js | 3 +-- src/routines.js | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 59 insertions(+), 8 deletions(-) diff --git a/src/actions.js b/src/actions.js index 92ac8a8..e2d0d5a 100644 --- a/src/actions.js +++ b/src/actions.js @@ -18,7 +18,6 @@ export default class ActionManager { } doAction (fish, action) { - console.log('doing action', action); switch (action.type) { case 'emote': fish.emote(action.value); diff --git a/src/fish.js b/src/fish.js index d766849..ac84028 100644 --- a/src/fish.js +++ b/src/fish.js @@ -121,10 +121,10 @@ export default class Fish { // For each food in tank, calculate distance between this fish let d = distance(this.x, this.y, food.x, food.y); // If distance is within a certain amount, move towards food - if (d < 150) { + if (d < 250) { console.log('adjusting for food'); - this.dx += (food.x - this.x) / 100; - this.dy += (food.y - this.y) / 100; + this.dx += (food.x - this.x) / 70; + this.dy += (food.y - this.y) / 70; } }); } @@ -132,6 +132,16 @@ export default class Fish { /* Moves the fish according to its velocity, making sure it's * facing the right direction, and updating any anchored images. */ update () { + /* If off-screen, teleport to random location and reset velocity */ + let offscreenMargin = 50; + if (this.x < -offscreenMargin || this.x > GAME_WIDTH + offscreenMargin + || this.y < -offscreenMargin || this.y > GAME_HEIGHT + offscreenMargin + ) { + this.x = randomIntFromInterval(100, 600); + this.y = randomIntFromInterval(100, 600); + this.setRandomVelocity(); + } + // Move towards nearby food if not in the middle of an action if (!this.action) { this.moveTowardsFood(); diff --git a/src/main.js b/src/main.js index cac9904..aa57f24 100644 --- a/src/main.js +++ b/src/main.js @@ -75,11 +75,10 @@ function draw() { foodInTank = foodInTank.filter(food => !food.remove); actionManager.update(); - routineManager.update(); + routineManager.update(); } function mouseClicked() { - console.log('mouse clicked'); let foodKeys = Object.keys(foodImages); let randomFood = foodKeys[randomIntFromInterval(0, foodKeys.length-1)]; foodInTank.push( diff --git a/src/routines.js b/src/routines.js index 4081d6f..5981bab 100644 --- a/src/routines.js +++ b/src/routines.js @@ -46,6 +46,51 @@ export default class RoutineManager { } update () { + // Check if any fish has collided with a food item + fishInTank.forEach(fish => { + foodInTank.forEach(food => { + // Food center is just the center of the food (x and y) + let center1 = {x: food.x, y: food.y}; + // Food radius is the width of the food + let radius1 = foodImages[food.type].width / 2; + + // Fish center is going to be a little left or right of the mouth depending + // on which way the fish is facing + let flipped = false; + if (fish.flipOverride === 'right') { + flipped = true; + } else if (!fish.flipOverride) { + flipped = fish.dx > 0; + } + let center2 = {x: fish.x, y: fish.y + fishImages[fish.type-1].height/2}; + if (flipped) { + center2.x += fishImages[fish.type-1].width - 10; + } else { + center2.x += 10; + } + // Fish radius is always hard-coded to the same value + let radius2 = 10; + + // Draw the circles for debugging purposes + /*/ + circle(center1.x, center1.y, radius1 * 2); + circle(center2.x, center2.y, radius2 * 2); + //*/ + + let d = distance(center1.x, center1.y, center2.x, center2.y); + let collision = d <= radius1 + radius2; + + if (collision) { + food.remove = true; + actionManager.fishRoutines.push( + { + fish, script: SCRIPTS.happy, + } + ); + } + }); + }); + // Routine random events this.events.forEach(event => event.update()); } @@ -65,8 +110,6 @@ export default class RoutineManager { // Random fish conversation this.addEvent(10000, 0.4, function () { - console.log('Starting conversation'); - let filteredFish = fishInTank.filter(fish => !fish.action); // Find two random fish