Skip to content

Commit

Permalink
prevent idle lumberjack
Browse files Browse the repository at this point in the history
  • Loading branch information
LegendaryLHL committed Jul 31, 2024
1 parent 4145302 commit cb1e5dd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 40 deletions.
22 changes: 6 additions & 16 deletions sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,23 +407,13 @@ function astar(maxIterations, start, end, sprite, ...targetClasses) {
const tentativeG = current.g + 1; // Assuming each step costs 1

if (!arrayExistVector(heap.heap, neighbor)) {
if (current == start) {
if (checkCollisionAlongPath(sprite,
{ x: sprite.x, y: sprite.y },
{ x: (neighbor.x + 0.5) * tileSize, y: (neighbor.y + 0.5) * tileSize },
collisionCheckedMap,
...targetClasses) && !sprite.isCollidingInRange(sprite.x, sprite.y, sprite.collide_range + tileSize)) {
continue;
}
}
else {
if (checkCollisionAlongPath(sprite,
if (checkCollisionAlongPath(sprite,
current == start ? { x: sprite.x, y: sprite.y } :
{ x: (current.x + 0.5) * tileSize, y: (current.y + 0.5) * tileSize },
{ x: (neighbor.x + 0.5) * tileSize, y: (neighbor.y + 0.5) * tileSize },
collisionCheckedMap,
...targetClasses)) {
continue;
}
{ x: (neighbor.x + 0.5) * tileSize, y: (neighbor.y + 0.5) * tileSize },
collisionCheckedMap,
...targetClasses)) {
continue;
}
neighbor.g = tentativeG;
neighbor.h = heuristic(neighbor, end);
Expand Down
44 changes: 24 additions & 20 deletions sprites/baseSprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,9 @@ class BaseSprite {
this.tickPerUpdate = 1;
this.tickPassed = 0;
this.name = config.name;
this.animation = null;
// [vector: Vector2D, boolean: checkCollision]
// This setup allows other sprites to move it without refrencing deltaTime
}

/**
* Update the sprite
* @returns {Number} Time in milliseconds since last update
*/
deltaTime() {
const deltaTime = Date.now() - this.lastUpdate;
return (deltaTime > maxDeltaTime) ? 0 : deltaTime;
this.animation = { x: 0, y: 0, speed: 0, time: 0 };
this.deltaTime = 0;
this.idleTime = 0;
}

/**
Expand All @@ -43,7 +34,22 @@ class BaseSprite {
this.tickPassed++;
if (this.tickPassed >= this.tickPerUpdate) {
this.tickPassed = 0;
const deltaTime = Date.now() - this.lastUpdate;
if (deltaTime < maxDeltaTime) {
this.deltaTime = deltaTime;
}
else {
this.deltaTime = 0;
}
const lastX = this.x;
const lastY = this.y;
this._tick();
if (lastX == this.x && lastY == this.y) {
this.idleTime += deltaTime;
}
else {
this.idleTime = 0;
}
this.lastUpdate = Date.now();
}
}
Expand All @@ -54,22 +60,19 @@ class BaseSprite {
*/
move(x, y, speed = this.speed) {
this._move(x, y, speed);
this.checkMapChange();
}

moveBy(x, y, t) {
let distance = Math.hypot(x, y);
let speed = distance / t;
this.animation = { x: x, y: y, speed: speed, time: t };
this.checkMapChange();
}

moveTo(x, y) {
this._move(x - this.x, y - this.y, this.speed);
if (this.distanceHyp(x, y) < 3) {
return true;
}
this.checkMapChange();
return false;
}

Expand Down Expand Up @@ -143,9 +146,9 @@ class BaseSprite {
*/
_tick() {
// handle animation
if (this.animation && this.animation.time > 0) {
if (this.animation.time > 0) {
this.move(this.animation.x, this.animation.y, this.animation.speed, this.animation);
this.animation.time -= this.deltaTime();
this.animation.time -= this.deltaTime;
}
}

Expand All @@ -155,16 +158,15 @@ class BaseSprite {
* @param {Boolean} checkCollision - Check for collision when moving if then stop
*/
_move(x, y, speed) {
const deltaTime = this.deltaTime();
// Handle queued up movement
let vectDist = Math.hypot(x, y);
const newX = this.x +
speed *
deltaTime *
this.deltaTime *
(vectDist == 0 ? x : x / vectDist);
const newY = this.y +
speed *
deltaTime *
this.deltaTime *
(vectDist == 0 ? y : y / vectDist);
if (inBoundOfMap(newX, newY)) {
// cliping in map
Expand All @@ -179,6 +181,8 @@ class BaseSprite {
this.y = newY;
}
}

this.checkMapChange();
}

/**
Expand Down
11 changes: 8 additions & 3 deletions sprites/lumberjack.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ class Lumberjack extends BaseSprite {
this.tickPerUpdate = 2;
this.onPath = false;
this.mapChanged = true;
this.stallingScore = 0;
this.maxIdleTime = 5000;
}


_tick() {
this.pathfindCooldown -= this.deltaTime();
this.actionCooldown -= this.deltaTime();
this.pathfindCooldown -= this.deltaTime;
this.actionCooldown -= this.deltaTime;

if (this.pathfindCooldown < 0 && !this.onPath) {
this.path = pathFind(tileSize * 20, this, Tree, Sprout, Police);
Expand Down Expand Up @@ -55,6 +55,11 @@ class Lumberjack extends BaseSprite {
this.path = pathFind(tileSize * 20, this, Tree, Sprout, Police);
}
}

if (this.idleTime > this.maxIdleTime) {
this.onPath = false;
this.path = [];
}
}
else {
this.onPath = false;
Expand Down
2 changes: 1 addition & 1 deletion sprites/sprout.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Sprout extends BaseSprite {
// handle animation
if (this.animation && this.animation.time > 0) {
this.move(this.animation.x, this.animation.y, this.animation.speed, this.animation);
this.animation.time -= this.deltaTime();
this.animation.time -= this.deltaTime;
}
}

Expand Down

0 comments on commit cb1e5dd

Please sign in to comment.