redoing bump
This commit is contained in:
+25
-6
@@ -6,8 +6,8 @@
|
||||
//!
|
||||
//! - `init(me, state)` — run once after the whole map is loaded (see [`ScriptHost::run_init`]).
|
||||
//! - `tick(me, state, dt)` — run every frame with the elapsed seconds (see [`ScriptHost::run_tick`]).
|
||||
//! - `bump(me, state, id)` — run when another mover steps into this object's cell, with the
|
||||
//! bumper's object index (`-1` for the player); see [`ScriptHost::run_bump`].
|
||||
//! - `bump(me, dir)` — run when a solid (the player, an object, or a pushed crate) presses into this
|
||||
//! object's cell, with the [`Direction`] the bump came *from*; see [`ScriptHost::run_bump`].
|
||||
//! - `grab(me, state)` — run when the player walks onto a `grab` object; see [`ScriptHost::run_grab`].
|
||||
//! Typically adds a stat + `die()`s.
|
||||
//!
|
||||
@@ -257,10 +257,12 @@ impl ScriptHost {
|
||||
}
|
||||
}
|
||||
|
||||
/// Calls `bump(id)` on the object with [`ObjectId`] `object_id`, if it defines
|
||||
/// the hook. After the hook, drains the object's queue with `dt = 0`.
|
||||
pub fn run_bump(&mut self, id: ObjectId, bumper: i64) {
|
||||
self.run_hook_on_one(Hook::Bump, id, Some(Dynamic::from(bumper)), 0.0);
|
||||
/// Calls `bump(dir)` on the object with [`ObjectId`] `id`, if it defines the
|
||||
/// hook. `dir` is the [`Direction`] the bump came *from* (it points from the
|
||||
/// bumped object toward the bumper). After the hook, drains the object's queue
|
||||
/// with `dt = 0`.
|
||||
pub fn run_bump(&mut self, id: ObjectId, dir: Direction) {
|
||||
self.run_hook_on_one(Hook::Bump, id, Some(Dynamic::from(dir)), 0.0);
|
||||
}
|
||||
|
||||
/// Calls `grab()` on the object with [`ObjectId`] `object_id`, if it defines the
|
||||
@@ -344,6 +346,23 @@ impl ScriptHost {
|
||||
|
||||
fn register_write_api(engine: &mut Engine, board: BoardRef) {
|
||||
engine.register_type_with_name::<Direction>("Direction");
|
||||
// Rhai does not auto-derive comparison for custom types, so register `==`/`!=`
|
||||
// to let scripts test the `bump` direction (e.g. `if dir == West { … }`).
|
||||
engine.register_fn("==", |a: Direction, b: Direction| a == b);
|
||||
engine.register_fn("!=", |a: Direction, b: Direction| a != b);
|
||||
// Let a Direction interpolate/print as its name (e.g. in `log(`from ${dir}`)`).
|
||||
let dir_name = |d: Direction| match d {
|
||||
Direction::North => "North",
|
||||
Direction::South => "South",
|
||||
Direction::East => "East",
|
||||
Direction::West => "West",
|
||||
};
|
||||
engine.register_fn("to_string", dir_name);
|
||||
engine.register_fn("to_debug", dir_name);
|
||||
// Expose the unit-step components so scripts can turn a direction into a cell
|
||||
// offset (e.g. the bumper's cell is `(me.x + dir.dx, me.y + dir.dy)`).
|
||||
engine.register_get("dx", |d: &mut Direction| d.dx());
|
||||
engine.register_get("dy", |d: &mut Direction| d.dy());
|
||||
|
||||
// move(dir): enqueue a Move followed by a rate-limiting Delay.
|
||||
let b = board.clone();
|
||||
|
||||
Reference in New Issue
Block a user