Skip to content

Commit 4c478e1

Browse files
Merge pull request #182 from rune-js/feature/following-refactor
Refactoring the following API a little
2 parents 6ffb16e + 4ee6217 commit 4c478e1

File tree

4 files changed

+86
-56
lines changed

4 files changed

+86
-56
lines changed
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { ActionType } from '../plugin';
22
import { world } from '../../game-server';
3-
import { followActor } from '../../world/actor/player/action/follow-action';
43

54
module.exports = {
65
type: ActionType.COMMAND,
76
commands: 'follow',
8-
action: details => followActor(details.player, world.npcList[0])
7+
action: details => details.player.follow(world.npcList[0])
98
};
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { ActionType } from '../plugin';
2-
import { followActor } from '../../world/actor/player/action/follow-action';
32

43
module.exports = {
54
type: ActionType.PLAYER_ACTION,
65
options: 'follow',
7-
action: details => followActor(details.player, details.otherPlayer)
6+
action: details => details.player.follow(details.otherPlayer)
87
};

src/world/actor/actor.ts

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ import { CombatAction } from '@server/world/actor/player/action/combat-action';
1010
import { Pathfinding } from '@server/world/actor/pathfinding';
1111
import { Subject } from 'rxjs';
1212
import { ActionCancelType } from '@server/world/actor/player/action/action';
13+
import { filter, take } from 'rxjs/operators';
1314

1415
/**
1516
* Handles an actor within the game world.
1617
*/
1718
export abstract class Actor {
1819

20+
public readonly updateFlags: UpdateFlags;
21+
public readonly skills: Skills;
22+
public readonly metadata: { [key: string]: any } = {};
23+
public readonly actionsCancelled: Subject<ActionCancelType>;
24+
public readonly movementEvent: Subject<Position>;
25+
public pathfinding: Pathfinding;
26+
public lastMovementPosition: Position;
27+
private readonly _walkingQueue: WalkingQueue;
28+
private readonly _inventory: ItemContainer;
29+
private readonly _bank: ItemContainer;
1930
private _position: Position;
2031
private _lastMapRegionUpdatePosition: Position;
2132
private _worldIndex: number;
22-
public readonly updateFlags: UpdateFlags;
23-
private readonly _walkingQueue: WalkingQueue;
2433
private _walkDirection: number;
2534
private _runDirection: number;
2635
private _faceDirection: number;
27-
private readonly _inventory: ItemContainer;
28-
private readonly _bank: ItemContainer;
29-
public readonly skills: Skills;
3036
private _busy: boolean;
31-
public readonly metadata: { [key: string]: any } = {};
3237
private _combatActions: CombatAction[];
33-
public pathfinding: Pathfinding;
34-
public lastMovementPosition: Position;
35-
public readonly actionsCancelled: Subject<ActionCancelType>;
36-
public readonly movementEvent: Subject<Position>;
3738

3839
protected constructor() {
3940
this.updateFlags = new UpdateFlags();
@@ -52,7 +53,80 @@ export abstract class Actor {
5253
}
5354

5455
public damage(amount: number, damageType: DamageType = DamageType.DAMAGE): void {
56+
}
57+
58+
public async moveBehind(target: Actor): Promise<boolean> {
59+
const distance = Math.floor(this.position.distanceBetween(target.position));
60+
if(distance > 16) {
61+
this.clearFaceActor();
62+
return false;
63+
}
64+
65+
let ignoreDestination = true;
66+
let desiredPosition = target.position;
67+
if(target.lastMovementPosition) {
68+
desiredPosition = target.lastMovementPosition;
69+
ignoreDestination = false;
70+
}
71+
72+
await this.pathfinding.walkTo(desiredPosition, {
73+
pathingSearchRadius: distance + 2,
74+
ignoreDestination
75+
});
76+
77+
return true;
78+
}
79+
80+
public follow(target: Actor): void {
81+
this.face(target, false, false, false);
82+
83+
this.moveBehind(target);
84+
const subscription = target.movementEvent.subscribe(() => this.moveBehind(target));
85+
86+
this.actionsCancelled.pipe(
87+
filter(type => type !== 'pathing-movement'),
88+
take(1)
89+
).subscribe(() => {
90+
subscription.unsubscribe();
91+
this.face(null);
92+
});
93+
}
94+
95+
public async walkTo(target: Actor): Promise<boolean> {
96+
const distance = Math.floor(this.position.distanceBetween(target.position));
97+
if(distance > 16) {
98+
this.clearFaceActor();
99+
this.metadata.faceActorClearedByWalking = true;
100+
return false;
101+
}
102+
103+
if(distance <= 1) {
104+
return false;
105+
}
106+
107+
const desiredPosition = target.position;
108+
109+
await this.pathfinding.walkTo(desiredPosition, {
110+
pathingSearchRadius: distance + 2,
111+
ignoreDestination: true
112+
});
113+
114+
return true;
115+
}
116+
117+
public tail(target: Actor): void {
118+
this.face(target, false, false, false);
119+
120+
this.walkTo(target);
121+
const subscription = target.movementEvent.subscribe(() => this.walkTo(target));
55122

123+
this.actionsCancelled.pipe(
124+
filter(type => type !== 'pathing-movement'),
125+
take(1)
126+
).subscribe(() => {
127+
subscription.unsubscribe();
128+
this.face(null);
129+
});
56130
}
57131

58132
public face(face: Position | Actor | null, clearWalkingQueue: boolean = true, autoClear: boolean = true, clearedByWalking: boolean = true): void {

src/world/actor/player/action/follow-action.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)