-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implements random npc movement via a loopable queueable task #431
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1a0c164
implements random npc movement via a loopable queuable task
charles-m-knox add5555
make object action nullable for queueable-task
Promises 4ff95ec
use null for data param in queueabletask
charles-m-knox ba92aba
Merge branch 'develop' into npc-random-movements
Promises File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,109 @@ | ||
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'>; | ||
|
||
/** | ||
* Processes the provided callback function on every single tick, and also 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 on each tick. These will not get called if the | ||
* `callback` indicates a halting condition. | ||
*/ | ||
private plugins: ObjectActionHook<TAction>[]; | ||
|
||
private data: ObjectActionData<TAction> | null; | ||
|
||
/** | ||
* Optionally provided base task to enqueue on each tick. Can be `null`. | ||
*/ | ||
private task: Task | null; | ||
|
||
/** | ||
* This callback function will be executed on every tick. It must return a | ||
* `QueueableTaskEval` so that this loop can determine if it should | ||
* terminate or continue looping. | ||
*/ | ||
private callback: () => QueueableTaskEval; | ||
|
||
constructor(plugins: ObjectActionHook<TAction>[], actor: Player | Actor, callback: () => QueueableTaskEval, task: Task | null, data: ObjectActionData<TAction> | null) { | ||
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) { // only gets executed if the callback returns true for its result | ||
this.actor.enqueueBaseTask(this.task) | ||
} | ||
|
||
// call the relevant plugins on each tick, if provided | ||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needed to be changed in order to allow NPC's to move. I evaluated their state during regular idle gameplay and these values were
null
. I am not familiar with the purpose of these so I may be breaking something here - please provide thoughts.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nulling instead of setting undefined here seems better, since initial is null