From 7aa6566796e1632ae36277648c162fc77af7052b Mon Sep 17 00:00:00 2001 From: Mica White Date: Fri, 10 Jul 2026 19:42:23 -0400 Subject: State utilities --- src/script.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/script.rs') diff --git a/src/script.rs b/src/script.rs index 93f6f99..8539c92 100644 --- a/src/script.rs +++ b/src/script.rs @@ -7,6 +7,7 @@ use crate::{ file::{FileManager, GameManifest}, input::InputManager, scene::{preload_scene, switch_scenes}, + storage::StorageManager, time::TimeContext, }; @@ -15,6 +16,7 @@ mod wasm; pub struct ScriptManager<'a> { scripts: &'a [ScriptDeclaration], pub active_scripts: Vec, + entities: FxHashMap, Box<[u8]>>, script_states: FxHashMap>, } @@ -28,6 +30,7 @@ pub struct ScriptContext<'a> { pub buffer: &'a mut Buffer, pub input: &'a InputManager, pub time: &'a TimeContext, + pub storage: &'a mut StorageManager, pub operation_queue: Vec, } @@ -39,6 +42,7 @@ pub struct FullScriptContext<'a> { pub time: &'a TimeContext, pub scripts: &'a ScriptManager<'a>, pub files: &'a FileManager, + pub storage: &'a StorageManager, pub wasm_runner: &'a WasmRunner, pub operation_queue: &'a mut Vec, } @@ -55,6 +59,13 @@ pub enum Operation { script_id: usize, new_state: Box<[u8]>, }, + UpdateEntity { + name: Box, + new_state: Box<[u8]>, + }, + UpdateStorage { + new_save: Box<[u8]>, + }, Exit, } @@ -130,6 +141,7 @@ impl WasmRunner { buffer: context.buffer, input: context.input, time: context.time, + storage: context.storage, scripts, files, wasm_runner, @@ -160,6 +172,7 @@ impl<'a> ScriptManager<'a> { Self { scripts, active_scripts: Vec::new(), + entities: FxHashMap::default(), script_states: FxHashMap::default(), } } @@ -233,6 +246,7 @@ impl<'a> ScriptManager<'a> { buffer: context.buffer, input: context.input, time: context.time, + storage: context.storage, operation_queue: Vec::new(), }, files, @@ -248,6 +262,7 @@ impl<'a> ScriptManager<'a> { buffer: context.buffer, input: context.input, time: context.time, + storage: context.storage, operation_queue: Vec::new(), }, wasm_runner, @@ -260,6 +275,7 @@ impl<'a> ScriptManager<'a> { buffer: context.buffer, input: context.input, time: context.time, + storage: context.storage, operation_queue: Vec::new(), }, wasm_runner, @@ -269,6 +285,12 @@ impl<'a> ScriptManager<'a> { script_id, new_state, } => *get_script_state(&mut self.script_states, *script_id) = new_state.clone(), + Operation::UpdateEntity { name, new_state } => { + self.entities.insert(name.clone(), new_state.clone()); + } + Operation::UpdateStorage { new_save } => { + context.storage.background_save(new_save.clone()); + } Operation::Exit => context.exit(), } } @@ -328,6 +350,7 @@ impl<'a> ScriptManager<'a> { let Some(script) = self.scripts.get(script_id) else { unreachable!("The active script does not exist"); }; + self.script_states.remove(&script_id); match script { ScriptDeclaration::Static { end, .. } => { end.map(|s| { -- cgit v1.2.3