|
| 1 | +import { Actor } from '@server/world/actor/actor'; |
| 2 | + |
| 3 | + |
| 4 | +export const walkToActor = async (actor: Actor, target: Actor): Promise<boolean> => { |
| 5 | + const distance = Math.floor(target.position.distanceBetween(actor.position)); |
| 6 | + if(distance > 16) { |
| 7 | + actor.clearFaceActor(); |
| 8 | + actor.metadata.faceActorClearedByWalking = true; |
| 9 | + throw new Error(`Distance too great!`); |
| 10 | + } |
| 11 | + |
| 12 | + let ignoreDestination = true; |
| 13 | + let desiredPosition = target.position; |
| 14 | + if(target.lastMovementPosition) { |
| 15 | + desiredPosition = target.lastMovementPosition; |
| 16 | + ignoreDestination = false; |
| 17 | + } |
| 18 | + |
| 19 | + await actor.pathfinding.walkTo(desiredPosition, { |
| 20 | + pathingSearchRadius: distance + 2, |
| 21 | + ignoreDestination |
| 22 | + }); |
| 23 | + |
| 24 | + return Promise.resolve(true); |
| 25 | +}; |
| 26 | + |
| 27 | +export const followActor = (follower: Actor, following: Actor): void => { |
| 28 | + follower.face(following, false, false, false); |
| 29 | + |
| 30 | + walkToActor(follower, following); |
| 31 | + |
| 32 | + const subscription = following.movementEvent.subscribe(() => { |
| 33 | + walkToActor(follower, following); |
| 34 | + }); |
| 35 | + const actionCancelled = follower.actionsCancelled.subscribe(type => { |
| 36 | + if(type !== 'pathing-movement') { |
| 37 | + subscription.unsubscribe(); |
| 38 | + actionCancelled.unsubscribe(); |
| 39 | + follower.face(null); |
| 40 | + } |
| 41 | + }); |
| 42 | +}; |
0 commit comments