removed boardstate

This commit is contained in:
2026-06-05 00:12:57 -05:00
parent 4f21f7fa8a
commit 6cd34ebb4e
4 changed files with 28 additions and 48 deletions
+14 -21
View File
@@ -11,7 +11,7 @@
//!
//! Scripts **read** the world directly through a read-only [`BoardView`] (getters
//! only) pushed into each scope as `board`, e.g. `board.player_x`. The view holds
//! an `Rc<RefCell<BoardState>>` and borrows it briefly per getter.
//! an `Rc<RefCell<Board>>` and borrows it briefly per getter.
//!
//! Scripts **write** by enqueuing [`Command`]s: host functions (`move`,
//! `set_tile`, `log`) push into a shared queue that [`GameState`] drains and
@@ -23,7 +23,7 @@
//!
//! [`GameState`]: crate::game::GameState
use crate::game::BoardState;
use crate::game::Board;
use crate::log::LogLine;
use rhai::{AST, CallFnOptions, Engine, FuncArgs, ImmutableString, NativeCallContext, Scope};
use std::cell::RefCell;
@@ -81,10 +81,10 @@ impl From<Direction> for (i32, i32) {
}
/// A read-only handle to the world, exposed to scripts as `board`. Holds a shared
/// reference to the [`BoardState`]; only getters are registered, so it is
/// reference to the [`Board`]; only getters are registered, so it is
/// read-only by construction.
#[derive(Clone)]
struct BoardView(Rc<RefCell<BoardState>>);
struct BoardView(Rc<RefCell<Board>>);
/// A compiled script plus which lifecycle hooks it defines.
struct CompiledScript {
@@ -128,15 +128,14 @@ impl ScriptHost {
/// fresh scope per scripted object. Compile errors and references to unknown
/// scripts are queued as [`GameCommand::Error`] (retrievable via
/// [`Self::take_commands`]); no script is run here.
pub fn new(state: &Rc<RefCell<BoardState>>) -> Self {
pub fn new(board_cell: &Rc<RefCell<Board>>) -> Self {
let commands: CommandQueue = Rc::new(RefCell::new(Vec::new()));
let mut engine = Engine::new();
register_read_api(&mut engine);
register_write_api(&mut engine, &commands);
let board_state = state.borrow();
let board = &board_state.board;
let board = board_cell.borrow();
// Compile each referenced script once; attribute failures to the first
// object that references the script.
@@ -200,12 +199,12 @@ impl ScriptHost {
scripts.contains_key(name).then(|| ObjectRuntime {
object_index: i,
script_name: name.clone(),
scope: new_object_scope(state),
scope: new_object_scope(board_cell),
})
})
.collect();
drop(board_state);
drop(board);
Self {
engine,
@@ -280,16 +279,10 @@ impl ScriptHost {
/// borrow the shared state briefly.
fn register_read_api(engine: &mut Engine) {
engine.register_type_with_name::<BoardView>("BoardView");
engine.register_get("player_x", |b: &mut BoardView| {
b.0.borrow().board.player.x as i64
});
engine.register_get("player_y", |b: &mut BoardView| {
b.0.borrow().board.player.y as i64
});
engine.register_get("width", |b: &mut BoardView| b.0.borrow().board.width as i64);
engine.register_get("height", |b: &mut BoardView| {
b.0.borrow().board.height as i64
});
engine.register_get("player_x", |b: &mut BoardView| b.0.borrow().player.x as i64);
engine.register_get("player_y", |b: &mut BoardView| b.0.borrow().player.y as i64);
engine.register_get("width", |b: &mut BoardView| b.0.borrow().width as i64);
engine.register_get("height", |b: &mut BoardView| b.0.borrow().height as i64);
}
/// Registers the write API: the `Direction` type and the `move`/`set_tile`/`log`
@@ -335,9 +328,9 @@ fn source_of(ctx: &NativeCallContext) -> usize {
/// Builds a fresh per-object scope, seeded with the read-only `board` view and
/// the four direction constants (`north`/`south`/`east`/`west`).
fn new_object_scope(state: &Rc<RefCell<BoardState>>) -> Scope<'static> {
fn new_object_scope(board: &Rc<RefCell<Board>>) -> Scope<'static> {
let mut scope = Scope::new();
scope.push_constant("board", BoardView(state.clone()));
scope.push_constant("board", BoardView(board.clone()));
scope.push_constant("north", Direction::North);
scope.push_constant("south", Direction::South);
scope.push_constant("east", Direction::East);