Skip to main content

TriggerManager

Struct TriggerManager 

Source
pub struct TriggerManager { /* private fields */ }
Expand description

Manages a collection of Triggers and determines which ones should fire given the player’s current (and previous) position.

§Usage

let mut mgr = TriggerManager::new();
mgr.add_trigger(Trigger::single_tile(
    "heal_npc", "StartTown", TriggerType::OnStep,
    3, 5, "healParty", true,
));

// Every frame
let triggered: Vec<String> = mgr.check_triggers("StartTown", 3, 5);
for name in &triggered {
    script_engine.call_function(name);
}

// On A button press
if let Some(name) = mgr.check_interact_mut("StartTown", facing_x, facing_y) {
    script_engine.call_function(&name);
}

Implementations§

Source§

impl TriggerManager

Source

pub fn new() -> Self

Creates an empty trigger manager.

Source

pub fn add_trigger(&mut self, trigger: Trigger)

Registers a single trigger.

Source

pub fn add_triggers(&mut self, triggers: impl IntoIterator<Item = Trigger>)

Registers multiple triggers in bulk.

Source

pub fn remove_triggers_for_map(&mut self, map_id: &str)

Removes all triggers associated with map_id.

Source

pub fn reset_fired_for_map(&mut self, map_id: &str)

Resets the fired flag for every trigger on the given map, allowing one-shot triggers to fire again (e.g. after the player re-enters the map).

Source

pub fn all_triggers(&self) -> impl Iterator<Item = &Trigger>

Returns an iterator over all registered triggers (regardless of map).

Source

pub fn len(&self) -> usize

Number of registered triggers.

Source

pub fn is_empty(&self) -> bool

Returns true when there are no registered triggers.

Source

pub fn check_triggers( &mut self, map_id: &str, player_x: u32, player_y: u32, ) -> Vec<String>

Checks all triggers for the given map against the player’s current position. Handles OnStep and OnEnter triggers.

Returns the script_name of every trigger that should fire this frame. Callers should iterate the returned list and invoke each function on the script engine.

One-shot triggers are automatically marked as fired so they won’t activate again until reset_fired_for_map is called.

§OnEnter vs OnStep
  • OnEnter — fires when the player was not in the area last frame but is this frame (entry detection).
  • OnStep — fires every frame the player stands inside the area.
Source

pub fn check_interact( &self, map_id: &str, facing_x: u32, facing_y: u32, ) -> Option<&str>

Checks for OnInteract triggers at the tile the player is facing. Returns the script name if one is found.

This is a read-only query — it does not mark one-shot triggers as fired. Use check_interact_mut when you also want to disable the trigger after activation.

Source

pub fn check_interact_mut( &mut self, map_id: &str, facing_x: u32, facing_y: u32, ) -> Option<String>

Checks for OnInteract triggers at the tile the player is facing. Returns the script name and marks the trigger as fired (so one-shot triggers won’t activate again).

Trait Implementations§

Source§

impl Clone for TriggerManager

Source§

fn clone(&self) -> TriggerManager

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TriggerManager

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TriggerManager

Source§

fn default() -> TriggerManager

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.