use concinnity_core::components::CharacterCapsule;
use concinnity_core::ecs::MaterialHandle;
use concinnity_core::ecs::de_opt_material_handle;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(default)]
pub struct CharacterModel {
pub schema: String,
pub source: String,
pub skin_index: u32,
#[serde(deserialize_with = "de_opt_material_handle")]
pub material: Option<MaterialHandle>,
pub position: [f32; 3],
pub rotation_deg: [f32; 3],
pub scale: [f32; 3],
pub lod_levels: u32,
pub lod_distances: Vec<f32>,
pub max_instances: u32,
pub capsule: Option<CharacterCapsule>,
}
impl Default for CharacterModel {
fn default() -> Self {
Self {
schema: String::from("builtin:humanoid"),
source: String::new(),
skin_index: 0,
material: None,
position: [0.0, 0.0, 0.0],
rotation_deg: [0.0, 0.0, 0.0],
scale: [1.0, 1.0, 1.0],
lod_levels: 1,
lod_distances: Vec::new(),
max_instances: 0,
capsule: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_blank_model_names_the_builtin_schema_and_no_source() {
let m = CharacterModel::default();
assert_eq!(m.schema, "builtin:humanoid");
assert!(m.source.is_empty());
assert_eq!(m.lod_levels, 1);
assert_eq!(m.scale, [1.0, 1.0, 1.0]);
assert!(m.capsule.is_none());
}
fn install_len_resolvers() {
static ONCE: std::sync::Once = std::sync::Once::new();
ONCE.call_once(|| {
concinnity_core::ecs::resolver::set_material_handle_resolver(|n| Some(n.len() as u32));
});
}
#[test]
fn a_model_round_trips_through_postcard() {
install_len_resolvers();
let m: CharacterModel = serde_json::from_str(
r#"{"schema":"humanoid","material":"skin","source":"hero.glb","skin_index":1,
"position":[0,0.1,0],"lod_levels":3,"lod_distances":[6,12],"max_instances":2,
"capsule":{"half_height":0.9,"radius":0.3}}"#,
)
.unwrap();
assert_eq!(m.material, Some(MaterialHandle(4)));
let bytes = postcard::to_allocvec(&m).unwrap();
let back: CharacterModel = postcard::from_bytes(&bytes).unwrap();
assert_eq!(back.schema, "humanoid");
assert_eq!(back.source, "hero.glb");
assert_eq!(back.skin_index, 1);
assert_eq!(back.lod_levels, 3);
assert_eq!(back.lod_distances, [6.0, 12.0]);
assert_eq!(back.max_instances, 2);
assert_eq!(back.capsule.unwrap().half_height, 0.9);
}
}