2025-03-28 21:28:30 -04:00
|
|
|
use mlua::{Function, Lua};
|
|
|
|
|
|
|
|
use crate::config::get_config;
|
|
|
|
|
2025-03-28 22:34:34 -04:00
|
|
|
use super::{LuaPlayer, Zone};
|
2025-03-28 21:28:30 -04:00
|
|
|
|
|
|
|
pub struct Event {
|
|
|
|
lua: Lua,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Event {
|
|
|
|
pub fn new(path: &str) -> Self {
|
|
|
|
let lua = Lua::new();
|
|
|
|
|
|
|
|
let config = get_config();
|
|
|
|
let file_name = format!("{}/{}", &config.world.scripts_location, path);
|
|
|
|
lua.load(std::fs::read(&file_name).expect("Failed to locate scripts directory!"))
|
|
|
|
.set_name("@".to_string() + &file_name)
|
|
|
|
.exec()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
Self { lua }
|
|
|
|
}
|
|
|
|
|
2025-03-28 22:34:34 -04:00
|
|
|
pub fn enter_territory(&mut self, player: &mut LuaPlayer, zone: &Zone) {
|
2025-03-28 21:28:30 -04:00
|
|
|
self.lua
|
|
|
|
.scope(|scope| {
|
|
|
|
let player = scope.create_userdata_ref_mut(player).unwrap();
|
2025-03-28 22:34:34 -04:00
|
|
|
let zone = scope.create_userdata_ref(zone).unwrap();
|
2025-03-28 21:28:30 -04:00
|
|
|
|
|
|
|
let func: Function = self.lua.globals().get("onEnterTerritory").unwrap();
|
|
|
|
|
2025-03-28 22:34:34 -04:00
|
|
|
func.call::<()>((player, zone)).unwrap();
|
2025-03-28 21:28:30 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn scene_finished(&mut self, player: &mut LuaPlayer, scene: u16) {
|
|
|
|
self.lua
|
|
|
|
.scope(|scope| {
|
|
|
|
let player = scope.create_userdata_ref_mut(player).unwrap();
|
|
|
|
|
|
|
|
let func: Function = self.lua.globals().get("onSceneFinished").unwrap();
|
|
|
|
|
|
|
|
func.call::<()>((player, scene)).unwrap();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|