@@ -13,6 +13,8 @@ import { Animation, Graphic, UpdateFlags } from './update-flags';
13
13
import { Skills } from './skills' ;
14
14
import { Pathfinding } from './pathfinding' ;
15
15
import { ActorMetadata } from './metadata' ;
16
+ import { Task , TaskScheduler } from '@engine/task' ;
17
+ import { logger } from '@runejs/common' ;
16
18
17
19
18
20
export type ActorType = 'player' | 'npc' ;
@@ -52,6 +54,13 @@ export abstract class Actor {
52
54
protected randomMovementInterval ;
53
55
protected _instance : WorldInstance = null ;
54
56
57
+ /**
58
+ * Is this actor currently active? If true, the actor will have its task queue processed.
59
+ *
60
+ * This is true for players that are currently logged in, and NPCs that are currently in the world.
61
+ */
62
+ protected active : boolean ;
63
+
55
64
/**
56
65
* @deprecated - use new action system instead
57
66
*/
@@ -64,6 +73,8 @@ export abstract class Actor {
64
73
private _faceDirection : number ;
65
74
private _bonuses : { offensive : OffensiveBonuses , defensive : DefensiveBonuses , skill : SkillBonuses } ;
66
75
76
+ private readonly scheduler = new TaskScheduler ( ) ;
77
+
67
78
protected constructor ( actorType : ActorType ) {
68
79
this . type = actorType ;
69
80
this . _walkDirection = - 1 ;
@@ -72,6 +83,22 @@ export abstract class Actor {
72
83
this . clearBonuses ( ) ;
73
84
}
74
85
86
+ /**
87
+ * Adds a task to the actor's scheduler queue. These tasks will be stopped when the become inactive.
88
+ *
89
+ * If the task has a stack type of `NEVER`, other tasks in the same group will be cancelled.
90
+ *
91
+ * @param task The task to add
92
+ */
93
+ public enqueueTask ( task : Task ) : void {
94
+ if ( ! this . active ) {
95
+ logger . warn ( `Attempted to enqueue task for logged out player` ) ;
96
+ return ;
97
+ }
98
+
99
+ this . scheduler . enqueue ( task ) ;
100
+ }
101
+
75
102
public clearBonuses ( ) : void {
76
103
this . _bonuses = {
77
104
offensive : {
@@ -439,6 +466,28 @@ export abstract class Actor {
439
466
return true ;
440
467
}
441
468
469
+ /**
470
+ * Initialise the actor.
471
+ */
472
+ protected init ( ) {
473
+ this . active = true ;
474
+ }
475
+
476
+ /**
477
+ * Destroy this actor.
478
+ *
479
+ * This will stop the processing of its action queue.
480
+ */
481
+ protected destroy ( ) {
482
+ this . active = false ;
483
+
484
+ this . scheduler . clear ( ) ;
485
+ }
486
+
487
+ protected tick ( ) {
488
+ this . scheduler . tick ( ) ;
489
+ }
490
+
442
491
public abstract equals ( actor : Actor ) : boolean ;
443
492
444
493
public get position ( ) : Position {
@@ -520,5 +569,4 @@ export abstract class Actor {
520
569
public get bonuses ( ) : { offensive : OffensiveBonuses , defensive : DefensiveBonuses , skill : SkillBonuses } {
521
570
return this . _bonuses ;
522
571
}
523
-
524
572
}
0 commit comments