Skip to content

Commit d08f78e

Browse files
committed
refactor: create enqueueTask factory method for Actor
fix: fix enqueueTask for only 1 arg fix: support enqueueTask for a task with 0 params (besides Actor)
1 parent 4ac22af commit d08f78e

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

src/engine/world/actor/actor.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,47 @@ export abstract class Actor {
8484
}
8585

8686
/**
87-
* Adds a task to the actor's scheduler queue. These tasks will be stopped when the become inactive.
87+
* Instantiate a task with the Actor instance and a set of arguments.
88+
*
89+
* @param taskClass The task class to instantiate. Must be a subclass of {@link Task}
90+
* @param args The arguments to pass to the task constructor
91+
*
92+
* If the task has a stack type of `NEVER`, other tasks in the same {@link TaskStackGroup} will be cancelled.
93+
*/
94+
public enqueueTask(taskClass: new (actor: Actor) => Task, ...args: never[]): void;
95+
public enqueueTask<T1, T2, T3, T4, T5, T6>(taskClass: new (actor: Actor, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6) => Task, args: [ T1, T2, T3, T4, T5, T6 ]): void;
96+
public enqueueTask<T1, T2, T3, T4, T5>(taskClass: new (actor: Actor, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Task, args: [ T1, T2, T3, T4, T5 ]): void;
97+
public enqueueTask<T1, T2, T3, T4>(taskClass: new (actor: Actor, arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Task, args: [ T1, T2, T3, T4 ]): void;
98+
public enqueueTask<T1, T2, T3>(taskClass: new (actor: Actor, arg1: T1, arg2: T2, arg3: T3) => Task, args: [ T1, T2, T3 ]): void;
99+
public enqueueTask<T1, T2>(taskClass: new (actor: Actor, arg1: T1, arg2: T2) => Task, args: [ T1, T2 ]): void;
100+
public enqueueTask<T1>(taskClass: new (actor: Actor, arg1: T1) => Task, args: [ T1 ]): void;
101+
public enqueueTask<T>(taskClass: new (actor: Actor, ...args: T[]) => Task, args: T[]): void {
102+
if (!this.active) {
103+
logger.warn(`Attempted to instantiate task for inactive actor`);
104+
return;
105+
}
106+
107+
if (args) {
108+
this.enqueueBaseTask(
109+
new taskClass(this, ...args)
110+
);
111+
} else {
112+
this.enqueueBaseTask(
113+
new taskClass(this)
114+
);
115+
}
116+
}
117+
118+
/**
119+
* Adds a task to the actor's scheduler queue. These tasks will be stopped when they become inactive.
88120
*
89121
* If the task has a stack type of `NEVER`, other tasks in the same group will be cancelled.
90122
*
91123
* @param task The task to add
92124
*/
93-
public enqueueTask(task: Task): void {
125+
public enqueueBaseTask(task: Task): void {
94126
if (!this.active) {
95-
logger.warn(`Attempted to enqueue task for logged out player`);
127+
logger.warn(`Attempted to enqueue task for inactive actor`);
96128
return;
97129
}
98130

0 commit comments

Comments
 (0)