Skip to content

Commit 0cf88f9

Browse files
committed
feat(orcust-automaton): nav mesh arrive behavior
1 parent 4be8c00 commit 0cf88f9

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { NavMesh, Vehicle, Vector3 as YukaVector3 } from 'yuka'
2+
3+
import { ArriveBehavior, FollowPathBehavior } from 'yuka'
4+
5+
export class NavMeshArriveBehavior extends ArriveBehavior {
6+
public navMesh?: NavMesh
7+
private followPathBehavior = new FollowPathBehavior()
8+
private navMeshInitialized = false
9+
10+
calculate(vehicle: Vehicle, force: YukaVector3) {
11+
if (this.navMesh) {
12+
if (!this.navMeshInitialized || this.followPathBehavior.path.finished()) {
13+
this.followPathBehavior.path.clear()
14+
15+
const path = this.navMesh.findPath(vehicle.position, this.target)
16+
for (const point of path) {
17+
this.followPathBehavior.path.add(point)
18+
}
19+
}
20+
21+
return this.followPathBehavior.calculate(vehicle, force)
22+
}
23+
else {
24+
return super.calculate(vehicle, force)
25+
}
26+
}
27+
}

packages/orcust-automaton/src/entities/orcust-automaton.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { AnimationAction } from 'three'
22
import type { GameEntity, NavMesh } from 'yuka'
33

4-
import { ArriveBehavior, ObstacleAvoidanceBehavior, Smoother, Think, Vehicle } from 'yuka'
4+
import { ObstacleAvoidanceBehavior, Smoother, Think, Vehicle } from 'yuka'
55

6+
import { NavMeshArriveBehavior } from '../behaviors/nav-mesh-arrive'
67
import { FollowEvaluator } from '../evaluators/follow'
78
import { RestEvaluator } from '../evaluators/rest'
89

@@ -42,7 +43,8 @@ export class OrcustAutomaton extends Vehicle {
4243
// obstacleAvoidanceBehavior.brakingWeight = 1
4344
this.steering.add(obstacleAvoidanceBehavior)
4445

45-
const arriveBehavior = new ArriveBehavior()
46+
// const arriveBehavior = new ArriveBehavior()
47+
const arriveBehavior = new NavMeshArriveBehavior()
4648
arriveBehavior.deceleration = 1.5
4749
this.steering.add(arriveBehavior)
4850
}
@@ -58,7 +60,7 @@ export class OrcustAutomaton extends Vehicle {
5860
}
5961

6062
public setNavMesh(navMesh?: NavMesh) {
61-
this.navMesh = navMesh
63+
(this.steering.behaviors.at(1) as NavMeshArriveBehavior).navMesh = navMesh
6264
}
6365

6466
public setObstacles(obstacles: GameEntity[]) {

0 commit comments

Comments
 (0)