-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implements random npc movement via a loopable queuable task
- Loading branch information
1 parent
94ecc7e
commit 8971715
Showing
3 changed files
with
113 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { ActorTask } from '@engine/task/impl'; | ||
import { Actor, Player } from '@engine/world/actor'; | ||
import { ObjectInteractionAction } from '../object-interaction.action'; | ||
import { ItemOnObjectAction } from '../item-on-object.action'; | ||
import { ActionHook } from '@engine/action/hook'; | ||
import { Task } from '@engine/task'; | ||
|
||
/** | ||
* The result of running a callback function will be recorded here so that the | ||
* `QueueableTask` can make a choice to continue looping and/or to enqueue | ||
* the desired task. | ||
*/ | ||
export interface QueueableTaskEval { | ||
callbackResult: boolean; | ||
shouldContinueLooping: boolean; | ||
} | ||
|
||
/** | ||
* All actions supported by this plugin task. | ||
*/ | ||
type ObjectAction = ObjectInteractionAction | ItemOnObjectAction; | ||
|
||
/** | ||
* An ActionHook for a supported ObjectAction. | ||
*/ | ||
type ObjectActionHook<TAction extends ObjectAction> = ActionHook<TAction, (data: TAction) => void>; | ||
|
||
/** | ||
* The data unique to the action being executed (i.e. excluding shared data) | ||
*/ | ||
type ObjectActionData<TAction extends ObjectAction> = Omit<TAction, 'player' | 'actor' | 'object' | 'position'>; | ||
|
||
/** | ||
* Allows any arbitrary base task to be queued at the next tick. Useful for | ||
* queuing random movements, as well as other things. | ||
* | ||
* Can loop infinitely based on the result of the passed in `callback` function. | ||
*/ | ||
export class QueueableTask<TAction extends ObjectAction> extends ActorTask <Player | Actor> { | ||
/** | ||
* The plugins to execute when the actor arrives at the object. | ||
*/ | ||
private plugins: ObjectActionHook<TAction>[]; | ||
|
||
private data: ObjectActionData<TAction>; | ||
|
||
private task: Task | null; | ||
|
||
private callback: () => QueueableTaskEval; | ||
|
||
constructor(plugins: ObjectActionHook<TAction>[], actor: Player | Actor, callback: () => QueueableTaskEval, task: Task | null, data: ObjectActionData<TAction>) { | ||
super( | ||
actor, | ||
); | ||
|
||
this.plugins = plugins; | ||
this.data = data; | ||
this.task = task; | ||
this.callback = callback; | ||
} | ||
|
||
/** | ||
* Executed every tick. Depending on the callback value, this task can stop | ||
* future executions. | ||
*/ | ||
public execute(): void { | ||
const ev = this.callback(); | ||
if (!ev.callbackResult) { | ||
if (!ev.shouldContinueLooping) { | ||
this.stop() | ||
} | ||
return; | ||
} | ||
|
||
if (this.task) { | ||
this.actor.enqueueBaseTask(this.task) | ||
} | ||
|
||
// call the relevant plugins - setting this.shouldStop allows control | ||
// of further ticks and handlers to be processed | ||
this.plugins.forEach(plugin => { | ||
if (!plugin || !plugin.handler) { | ||
return; | ||
} | ||
|
||
const action = { | ||
player: this.actor, | ||
...this.data | ||
} as TAction; | ||
|
||
plugin.handler(action); | ||
}); | ||
|
||
|
||
if (!ev.shouldContinueLooping) { | ||
this.stop(); | ||
return | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters