Skip to content

Commit

Permalink
move towards mouse
Browse files Browse the repository at this point in the history
  • Loading branch information
cheryllium committed Jan 5, 2024
1 parent 3d54162 commit 1473994
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/fish.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ export default class Fish {
this.limitSpeed();
}

moveTowardsMouse () {
// Do nothing else if mouse is off screen
if (mouseX < 0 || mouseX > GAME_WIDTH
|| mouseY < 0 || mouseY > GAME_HEIGHT) {
return;
}

let d = distance(this.x, this.y, mouseX, mouseY);
if (d < 200) {
this.dx += (mouseX - this.x) / 80;
this.dy += (mouseY - this.y) / 80;
}
}

moveTowardsFood () {
foodInTank.forEach(food => {
// For each food in tank, calculate distance between this fish
Expand All @@ -145,14 +159,14 @@ 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 () {
// Move towards nearby food if not in the middle of an action
if (!this.action) {
this.moveTowardsFood();
}

// Update velocity based on state
switch (this.state) {
case states.BOIDING:
case states.BOIDING:
this.moveTowardsMouse();
// Move towards nearby food if not in the middle of an action
if (!this.action) {
this.moveTowardsFood();
}
this.boids();
break;
case states.IDLING:
Expand Down

0 comments on commit 1473994

Please sign in to comment.