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
+7 -19
View File
@@ -393,22 +393,10 @@ impl Board {
}
}
/// The scriptable world: the current [`Board`] plus (in the future) runtime-only
/// state such as a shared variable blackboard.
///
/// `BoardState` is a sibling of the [`ScriptHost`] inside [`GameState`] and is
/// held behind an `Rc<RefCell<…>>`. Keeping it separate from the script engine is
/// what lets host functions hold a shared handle to the world without aliasing the
/// borrow that is currently running the engine.
pub struct BoardState {
/// The active game board.
pub board: Board,
}
/// Holds the active game world and provides game-logic operations.
///
/// `GameState` is the boundary between the engine (rendering, input) and the
/// game data. It owns the world ([`BoardState`], behind a shared `Rc<RefCell>`),
/// game data. It owns the board (behind a shared `Rc<RefCell<Board>>`),
/// the message log, and the [`ScriptHost`] driving object scripts. Front-ends and
/// internal logic reach the board through [`board`](GameState::board) /
/// [`board_mut`](GameState::board_mut). Scripts read the board directly and
@@ -416,7 +404,7 @@ pub struct BoardState {
/// after each script batch.
pub struct GameState {
/// The scriptable world, shared with the script host's read getters.
board_state: Rc<RefCell<BoardState>>,
board: Rc<RefCell<Board>>,
/// The in-game message log, oldest first (newest pushed at the end).
pub log: Vec<LogLine>,
/// The Rhai scripting runtime driving this board's scripted objects.
@@ -430,10 +418,10 @@ impl GameState {
/// call [`GameState::run_init`] once the game is ready to start. Any script
/// compile errors are surfaced into the log here.
pub fn new(board: Board) -> Self {
let board_state = Rc::new(RefCell::new(BoardState { board }));
let scripts = ScriptHost::new(&board_state);
let board = Rc::new(RefCell::new(board));
let scripts = ScriptHost::new(&board);
let mut state = Self {
board_state,
board,
log: Vec::new(),
scripts,
};
@@ -444,12 +432,12 @@ impl GameState {
/// Borrows the active board for reading (e.g. by a front-end renderer).
pub fn board(&self) -> Ref<'_, Board> {
Ref::map(self.board_state.borrow(), |bs| &bs.board)
self.board.borrow()
}
/// Borrows the active board for mutation.
pub fn board_mut(&self) -> RefMut<'_, Board> {
RefMut::map(self.board_state.borrow_mut(), |bs| &mut bs.board)
self.board.borrow_mut()
}
/// Appends a styled message to the log.
+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);