@@ -42,6 +42,7 @@ import { TooltipSpriteBuilder } from '../../../resource/TooltipSpriteBuilder'
4242import { SelectionNameComponent } from '../../component/SelectionNameComponent'
4343import { PRNG } from '../../factory/PRNG'
4444import { SaveGameRaider } from '../../../resource/SaveGameManager'
45+ import { PathFinder } from '../../terrain/PathFinder'
4546
4647export 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 {
0 commit comments