player object
This commit is contained in:
+23
-8
@@ -67,7 +67,7 @@
|
||||
|
||||
use crate::action::{Action, MOVE_COST, ScrollLine, action_to_map};
|
||||
use crate::board::Board;
|
||||
use crate::game::SAY_DURATION;
|
||||
use crate::game::{SAY_DURATION};
|
||||
use crate::log::LogLine;
|
||||
use crate::map_file::{color_to_hex, parse_color};
|
||||
use crate::object_def::ObjectDef;
|
||||
@@ -79,6 +79,7 @@ use rhai::{
|
||||
use std::cell::RefCell;
|
||||
use std::collections::{HashMap, HashSet, VecDeque};
|
||||
use std::rc::Rc;
|
||||
use crate::player::Player;
|
||||
|
||||
/// An action promoted from an object's output queue onto the board queue, tagged
|
||||
/// with the object that issued it.
|
||||
@@ -229,6 +230,7 @@ impl ScriptHost {
|
||||
register_glyph_type(&mut engine);
|
||||
register_registry_type(&mut engine);
|
||||
register_global_constants(&mut engine);
|
||||
register_player_type(&mut engine);
|
||||
|
||||
let board = board_cell.borrow();
|
||||
|
||||
@@ -316,18 +318,18 @@ impl ScriptHost {
|
||||
}
|
||||
|
||||
/// Calls `init()` on every scripted object that defines it and drains each queue.
|
||||
pub fn run_init(&mut self) {
|
||||
self.run("init", |c| c.has_init, (), 0.0);
|
||||
pub fn run_init(&mut self, player: Player) {
|
||||
self.run("init", |c| c.has_init, (), 0.0, player);
|
||||
}
|
||||
|
||||
/// Calls `tick(dt)` on every scripted object that defines it, then drains queues.
|
||||
pub fn run_tick(&mut self, dt: f64) {
|
||||
self.run("tick", |c| c.has_tick, (dt,), dt);
|
||||
pub fn run_tick(&mut self, player: Player, dt: f64) {
|
||||
self.run("tick", |c| c.has_tick, (dt,), dt, player);
|
||||
}
|
||||
|
||||
/// 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, object_id: ObjectId, bumper: i64) {
|
||||
pub fn run_bump(&mut self, player: Player, object_id: ObjectId, bumper: i64) {
|
||||
let Some(i) = self.objects.iter().position(|o| o.object_id == object_id) else {
|
||||
return;
|
||||
};
|
||||
@@ -347,6 +349,7 @@ impl ScriptHost {
|
||||
return;
|
||||
}
|
||||
let options = CallFnOptions::default().with_tag(object_id as i64);
|
||||
obj.scope.set_or_push("Player", player);
|
||||
if let Err(err) = engine.call_fn_with_options::<()>(
|
||||
options,
|
||||
&mut obj.scope,
|
||||
@@ -369,7 +372,7 @@ impl ScriptHost {
|
||||
/// Fired when the player walks onto a grab object or a grab object is pushed
|
||||
/// into the player (see [`GameState`](crate::game::GameState)). The hook
|
||||
/// typically increments a player stat and removes the object via `die()`.
|
||||
pub fn run_grab(&mut self, object_id: ObjectId) {
|
||||
pub fn run_grab(&mut self, player: Player, object_id: ObjectId) {
|
||||
let Some(i) = self.objects.iter().position(|o| o.object_id == object_id) else {
|
||||
return;
|
||||
};
|
||||
@@ -389,6 +392,7 @@ impl ScriptHost {
|
||||
return;
|
||||
}
|
||||
let options = CallFnOptions::default().with_tag(object_id as i64);
|
||||
obj.scope.set_or_push("Player", player);
|
||||
if let Err(err) = engine.call_fn_with_options::<()>(
|
||||
options,
|
||||
&mut obj.scope,
|
||||
@@ -411,7 +415,7 @@ impl ScriptHost {
|
||||
/// If the function accepts zero parameters (or arg is `None`), it is called with
|
||||
/// no args. If neither arity exists, the call is silently skipped.
|
||||
/// After the call, drains the object's queue with `dt = 0`.
|
||||
pub(crate) fn run_send(&mut self, target_id: ObjectId, fn_name: &str, arg: Option<ScriptArg>) {
|
||||
pub(crate) fn run_send(&mut self, player: Player, target_id: ObjectId, fn_name: &str, arg: Option<ScriptArg>) {
|
||||
let Some(i) = self.objects.iter().position(|o| o.object_id == target_id) else {
|
||||
return;
|
||||
};
|
||||
@@ -438,6 +442,7 @@ impl ScriptHost {
|
||||
.any(|f| f.name == fn_name && f.params.is_empty());
|
||||
|
||||
let options = CallFnOptions::default().with_tag(obj.object_id as i64);
|
||||
obj.scope.set_or_push("Player", player);
|
||||
let result = if has_1 {
|
||||
// Call with arg (or unit if arg is absent).
|
||||
let dyn_arg: Dynamic = match &arg {
|
||||
@@ -525,6 +530,7 @@ impl ScriptHost {
|
||||
defined: fn(&CompiledScript) -> bool,
|
||||
args: A,
|
||||
drain_dt: f64,
|
||||
player: Player
|
||||
) {
|
||||
for i in 0..self.objects.len() {
|
||||
{
|
||||
@@ -540,6 +546,7 @@ impl ScriptHost {
|
||||
&& defined(compiled)
|
||||
{
|
||||
let options = CallFnOptions::default().with_tag(obj.object_id as i64);
|
||||
obj.scope.set_or_push("Player", player);
|
||||
if let Err(err) = engine.call_fn_with_options::<()>(
|
||||
options,
|
||||
&mut obj.scope,
|
||||
@@ -559,6 +566,14 @@ impl ScriptHost {
|
||||
}
|
||||
}
|
||||
|
||||
fn register_player_type(engine: &mut Engine) {
|
||||
// TODO also expose keys somehow
|
||||
engine.register_type_with_name::<Player>("Player")
|
||||
.register_get("gems", |player: &mut Player| player.gems)
|
||||
.register_get("health", |player: &mut Player| player.health)
|
||||
.register_get("max_health", |player: &mut Player| player.max_health);
|
||||
}
|
||||
|
||||
// ── Register Me type ──────────────────────────────────────────────────────────
|
||||
|
||||
fn register_me_type(engine: &mut Engine) {
|
||||
|
||||
Reference in New Issue
Block a user