@@ -10,30 +10,31 @@ import { CombatAction } from '@server/world/actor/player/action/combat-action';
10
10
import { Pathfinding } from '@server/world/actor/pathfinding' ;
11
11
import { Subject } from 'rxjs' ;
12
12
import { ActionCancelType } from '@server/world/actor/player/action/action' ;
13
+ import { filter , take } from 'rxjs/operators' ;
13
14
14
15
/**
15
16
* Handles an actor within the game world.
16
17
*/
17
18
export abstract class Actor {
18
19
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 ;
19
30
private _position : Position ;
20
31
private _lastMapRegionUpdatePosition : Position ;
21
32
private _worldIndex : number ;
22
- public readonly updateFlags : UpdateFlags ;
23
- private readonly _walkingQueue : WalkingQueue ;
24
33
private _walkDirection : number ;
25
34
private _runDirection : number ;
26
35
private _faceDirection : number ;
27
- private readonly _inventory : ItemContainer ;
28
- private readonly _bank : ItemContainer ;
29
- public readonly skills : Skills ;
30
36
private _busy : boolean ;
31
- public readonly metadata : { [ key : string ] : any } = { } ;
32
37
private _combatActions : CombatAction [ ] ;
33
- public pathfinding : Pathfinding ;
34
- public lastMovementPosition : Position ;
35
- public readonly actionsCancelled : Subject < ActionCancelType > ;
36
- public readonly movementEvent : Subject < Position > ;
37
38
38
39
protected constructor ( ) {
39
40
this . updateFlags = new UpdateFlags ( ) ;
@@ -52,7 +53,80 @@ export abstract class Actor {
52
53
}
53
54
54
55
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 ) ) ;
55
122
123
+ this . actionsCancelled . pipe (
124
+ filter ( type => type !== 'pathing-movement' ) ,
125
+ take ( 1 )
126
+ ) . subscribe ( ( ) => {
127
+ subscription . unsubscribe ( ) ;
128
+ this . face ( null ) ;
129
+ } ) ;
56
130
}
57
131
58
132
public face ( face : Position | Actor | null , clearWalkingQueue : boolean = true , autoClear : boolean = true , clearedByWalking : boolean = true ) : void {
0 commit comments