From cb1e5dd581c4154e9d782135124e400808909732 Mon Sep 17 00:00:00 2001 From: Lee Hoon Lim Date: Wed, 31 Jul 2024 16:45:21 +0800 Subject: [PATCH] prevent idle lumberjack --- sketch.js | 22 ++++++---------------- sprites/baseSprite.js | 44 +++++++++++++++++++++++-------------------- sprites/lumberjack.js | 11 ++++++++--- sprites/sprout.js | 2 +- 4 files changed, 39 insertions(+), 40 deletions(-) diff --git a/sketch.js b/sketch.js index cb67f24..87b0240 100644 --- a/sketch.js +++ b/sketch.js @@ -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); diff --git a/sprites/baseSprite.js b/sprites/baseSprite.js index 18bd3bb..ed6112a 100644 --- a/sprites/baseSprite.js +++ b/sprites/baseSprite.js @@ -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; } /** @@ -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(); } } @@ -54,14 +60,12 @@ 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) { @@ -69,7 +73,6 @@ class BaseSprite { if (this.distanceHyp(x, y) < 3) { return true; } - this.checkMapChange(); return false; } @@ -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; } } @@ -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 @@ -179,6 +181,8 @@ class BaseSprite { this.y = newY; } } + + this.checkMapChange(); } /** diff --git a/sprites/lumberjack.js b/sprites/lumberjack.js index e80b05a..821b685 100644 --- a/sprites/lumberjack.js +++ b/sprites/lumberjack.js @@ -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); @@ -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; diff --git a/sprites/sprout.js b/sprites/sprout.js index 26766e5..1e14106 100644 --- a/sprites/sprout.js +++ b/sprites/sprout.js @@ -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; } }