Skip to content

Commit 57e2d5d

Browse files
UnrudScarabol
authored andcommitted
Stop if obstacle on previously computed path
1 parent 23f685b commit 57e2d5d

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

src/game/model/raider/Raider.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { TooltipSpriteBuilder } from '../../../resource/TooltipSpriteBuilder'
4242
import { SelectionNameComponent } from '../../component/SelectionNameComponent'
4343
import { PRNG } from '../../factory/PRNG'
4444
import { SaveGameRaider } from '../../../resource/SaveGameManager'
45+
import { PathFinder } from '../../terrain/PathFinder'
4546

4647
export class Raider implements Updatable, JobFulfiller {
4748
readonly entityType: EntityType = EntityType.PILOT
@@ -201,6 +202,7 @@ export class Raider implements Updatable, JobFulfiller {
201202

202203
private moveToClosestTargetInternal(target: PathTarget | undefined, elapsedMs: number): MoveState {
203204
if (!target) return MOVE_STATE.targetUnreachable
205+
let pathUpdated = false
204206
if (!this.currentPath || !target.targetLocation.equals(this.currentPath.target.targetLocation)) {
205207
const path = this.findShortestPath(target)
206208
this.currentPath = path && path.locations.length > 0 ? path : undefined
@@ -209,28 +211,39 @@ export class Raider implements Updatable, JobFulfiller {
209211
currentPath.locations.forEach((l, index) => {
210212
if (index < currentPath.locations.length - 1) l.add(new Vector2().random().subScalar(0.5).multiplyScalar(TILESIZE / RAIDER_PATH_PRECISION))
211213
}) // XXX Externalize precision
214+
pathUpdated = true
212215
}
213216
const step = this.determineStep(elapsedMs, this.currentPath)
214217
if (step.targetReached) {
215218
return MOVE_STATE.targetReached
216-
} else {
217-
this.setPosition(step.position)
218-
this.sceneEntity.headTowards(step.focusPoint)
219-
this.sceneEntity.setAnimation(this.getRouteActivity())
220-
if (this.foodLevel > 0) this.foodLevel -= step.stepLength * step.stepLength / TILESIZE / TILESIZE / 5
221-
if (!!this.carries) {
222-
// XXX Adjust balancing for resting
223-
const chanceToRestPerSecond = this.stats.restPercent / 20 * Math.max(0, 1 - this.foodLevel / this.stats.restPercent)
224-
if (PRNG.movement.random() < chanceToRestPerSecond * elapsedMs / 1000) {
225-
this.resting = true
226-
this.sceneEntity.setAnimation('Activity_Rest', () => {
227-
this.resting = false
228-
})
229-
}
219+
}
220+
if (!pathUpdated) {
221+
const pathFinder = this.worldMgr.sceneMgr.terrain.pathFinder
222+
const currentSurface = this.worldMgr.sceneMgr.terrain.getSurfaceFromWorld(this.getPosition())
223+
const nextSurface = this.worldMgr.sceneMgr.terrain.getSurfaceFromWorld(step.position)
224+
if (PathFinder.getWeight(pathFinder.surfaces[currentSurface.x][currentSurface.y], this.stats) !== 0 &&
225+
PathFinder.getWeight(pathFinder.surfaces[nextSurface.x][nextSurface.y], this.stats) === 0) {
226+
// Recalculate path when running into obstacle
227+
this.currentPath = undefined
228+
return this.moveToClosestTarget(target, elapsedMs)
229+
}
230+
}
231+
this.setPosition(step.position)
232+
this.sceneEntity.headTowards(step.focusPoint)
233+
this.sceneEntity.setAnimation(this.getRouteActivity())
234+
if (this.foodLevel > 0) this.foodLevel -= step.stepLength * step.stepLength / TILESIZE / TILESIZE / 5
235+
if (!!this.carries) {
236+
// XXX Adjust balancing for resting
237+
const chanceToRestPerSecond = this.stats.restPercent / 20 * Math.max(0, 1 - this.foodLevel / this.stats.restPercent)
238+
if (PRNG.movement.random() < chanceToRestPerSecond * elapsedMs / 1000) {
239+
this.resting = true
240+
this.sceneEntity.setAnimation('Activity_Rest', () => {
241+
this.resting = false
242+
})
230243
}
231-
this.worldMgr.ecs.getComponents(this.entity).get(RaiderInfoComponent).setHungerIndicator(this.foodLevel)
232-
return MOVE_STATE.moved
233244
}
245+
this.worldMgr.ecs.getComponents(this.entity).get(RaiderInfoComponent).setHungerIndicator(this.foodLevel)
246+
return MOVE_STATE.moved
234247
}
235248

236249
private determineStep(elapsedMs: number, currentPath: TerrainPath): EntityStep {

src/game/model/vehicle/VehicleEntity.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import { MaterialSpawner } from '../../factory/MaterialSpawner'
4848
import { MovableStatsComponent } from '../../component/MovableStatsComponent'
4949
import { PRNG } from '../../factory/PRNG'
5050
import { SaveGameManager } from '../../../resource/SaveGameManager'
51+
import { PathFinder } from '../../terrain/PathFinder'
5152

5253
export class VehicleEntity implements Updatable, JobFulfiller {
5354
readonly entityType: EntityType
@@ -212,23 +213,35 @@ export class VehicleEntity implements Updatable, JobFulfiller {
212213

213214
private moveToClosestTargetInternal(target: PathTarget | undefined, elapsedMs: number): MoveState {
214215
if (!target) return MOVE_STATE.targetUnreachable
216+
let pathUpdated = false
215217
if (!this.currentPath || !target.targetLocation.equals(this.currentPath.target.targetLocation)) {
216218
const path = this.findShortestPath(target)
217219
this.currentPath = path && path.locations.length > 0 ? path : undefined
218220
if (!this.currentPath) return MOVE_STATE.targetUnreachable
221+
pathUpdated = true
219222
}
220223
const step = this.determineStep(elapsedMs, this.currentPath)
221224
if (step.targetReached) {
222225
if (target.building) this.sceneEntity.headTowards(target.building.primarySurface.getCenterWorld2D())
223226
return MOVE_STATE.targetReached
224-
} else {
225-
this.setPosition(step.position)
226-
this.sceneEntity.headTowards(step.focusPoint)
227-
this.sceneEntity.setAnimation(this.getRouteActivity())
228-
const angle = elapsedMs * this.getSpeed() / 1000 * 4 * Math.PI
229-
this.sceneEntity.wheelJoints.forEach((w) => w.radius && w.mesh.rotateX(angle / w.radius))
230-
return MOVE_STATE.moved
231227
}
228+
if (!pathUpdated) {
229+
const pathFinder = this.worldMgr.sceneMgr.terrain.pathFinder
230+
const currentSurface = this.worldMgr.sceneMgr.terrain.getSurfaceFromWorld(this.getPosition())
231+
const nextSurface = this.worldMgr.sceneMgr.terrain.getSurfaceFromWorld(step.position)
232+
if (PathFinder.getWeight(pathFinder.surfaces[currentSurface.x][currentSurface.y], this.stats) !== 0 &&
233+
PathFinder.getWeight(pathFinder.surfaces[nextSurface.x][nextSurface.y], this.stats) === 0) {
234+
// Recalculate path when running into obstacle
235+
this.currentPath = undefined
236+
return this.moveToClosestTarget(target, elapsedMs)
237+
}
238+
}
239+
this.setPosition(step.position)
240+
this.sceneEntity.headTowards(step.focusPoint)
241+
this.sceneEntity.setAnimation(this.getRouteActivity())
242+
const angle = elapsedMs * this.getSpeed() / 1000 * 4 * Math.PI
243+
this.sceneEntity.wheelJoints.forEach((w) => w.radius && w.mesh.rotateX(angle / w.radius))
244+
return MOVE_STATE.moved
232245
}
233246

234247
private determineStep(elapsedMs: number, currentPath: TerrainPath): EntityStep {

0 commit comments

Comments
 (0)