use bevy::prelude::*;
use mlua::{Lua, Result as LuaResult, Table};
use crate::plugins::combat::components::Health;
pub fn health_to_lua_table<'lua>(lua: &'lua Lua, health: &Health) -> LuaResult<Table<'lua>> {
let table = lua.create_table()?;
table.set("current", health.current)?;
table.set("max", health.max)?;
Ok(table)
}
pub fn get_component_as_lua_table<'lua>(
lua: &'lua Lua,
world: &World,
entity: Entity,
type_name: &str,
) -> LuaResult<Option<Table<'lua>>> {
match type_name {
"Health" => {
if let Some(health) = world.get::<Health>(entity) {
Ok(Some(health_to_lua_table(lua, health)?))
} else {
Ok(None)
}
}
_ => {
Ok(None)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_health_to_lua_table() {
let lua = Lua::new();
let health = Health::new(100);
let table = health_to_lua_table(&lua, &health).unwrap();
let current: i32 = table.get("current").unwrap();
let max: i32 = table.get("max").unwrap();
assert_eq!(current, 100);
assert_eq!(max, 100);
}
#[test]
fn test_get_component_health() {
let lua = Lua::new();
let mut world = World::new();
let entity = world.spawn(Health::new(75)).id();
let table = get_component_as_lua_table(&lua, &world, entity, "Health")
.unwrap()
.unwrap();
let current: i32 = table.get("current").unwrap();
let max: i32 = table.get("max").unwrap();
assert_eq!(current, 75);
assert_eq!(max, 75);
}
#[test]
fn test_get_component_not_found() {
let lua = Lua::new();
let mut world = World::new();
let entity = world.spawn_empty().id();
let result = get_component_as_lua_table(&lua, &world, entity, "Health").unwrap();
assert!(result.is_none());
}
#[test]
fn test_get_component_unknown_type() {
let lua = Lua::new();
let mut world = World::new();
let entity = world.spawn_empty().id();
let result = get_component_as_lua_table(&lua, &world, entity, "UnknownComponent").unwrap();
assert!(result.is_none());
}
}