Skip to content

Commit

Permalink
Feeding collision detection
Browse files Browse the repository at this point in the history
  • Loading branch information
cheryllium committed Jan 3, 2024
1 parent ce81ec8 commit ab86f95
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 13 additions & 3 deletions src/fish.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,27 @@ 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;
}
});
}

/* 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();
Expand Down
3 changes: 1 addition & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
47 changes: 45 additions & 2 deletions src/routines.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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
Expand Down

0 comments on commit ab86f95

Please sign in to comment.