diff --git a/engine/BUILD b/engine/BUILD index c2cadca..dbd3209 100644 --- a/engine/BUILD +++ b/engine/BUILD @@ -14,6 +14,7 @@ rust_binary( "src/main.rs", "src/input.rs", + "src/scripting.rs", "src/physics/mod.rs", "src/physics/color.rs", @@ -46,6 +47,7 @@ rust_binary( "//lib/ecs", "//third_party/cargo:cgmath", "//third_party/cargo:image", + "//third_party/cargo:mlua", "//third_party/cargo:winit", "//third_party/cargo:log", "//third_party/cargo:env_logger", diff --git a/engine/src/main.rs b/engine/src/main.rs index 81396fd..080ca33 100644 --- a/engine/src/main.rs +++ b/engine/src/main.rs @@ -25,6 +25,7 @@ pub mod input; mod render; mod util; mod physics; +mod scripting; use ecs::{Component, World, Processor}; use ecs_macros::Access; @@ -217,6 +218,9 @@ fn main() { let mut renderer = render::Renderer::initialize(&mut world); let main = Main::new(&mut world, &mut renderer); + let context = scripting::WorldContext::new(); + context.eval("print(\"Hello, Lua!\", 1337)").unwrap(); + log::info!("Starting..."); let mut p = Processor::new(&world); diff --git a/engine/src/scripting.rs b/engine/src/scripting.rs new file mode 100644 index 0000000..b47aa45 --- /dev/null +++ b/engine/src/scripting.rs @@ -0,0 +1,31 @@ +pub struct WorldContext { + lua: mlua::Lua, +} + +fn debug_str(v: &mlua::Value) -> String { + match v { + mlua::Value::String(s) => s.to_str().map_or(format!("{:?}", v), |s| s.to_string()), + mlua::Value::Integer(i) => format!("{}", i), + _ => format!("{:?}", v), + } +} + +impl WorldContext { + pub fn new() -> Self { + let lua = mlua::Lua::new(); + log::info!("Lua WorldContext created."); + lua.globals().set("print", lua.create_function(|_, vals: mlua::Variadic| -> mlua::Result<()> { + let msg: Vec = vals.iter().map(|val| debug_str(val)).collect(); + log::info!("[Lua] {}", msg.join("\t")); + Ok(()) + }).unwrap()).unwrap(); + + Self { + lua, + } + } + + pub fn eval(&self, val: &str) -> mlua::Result<()> { + self.lua.load(val).exec() + } +}