use galeon_engine_macros::Component;
use crate::entity::Entity;
#[derive(Component, Debug, Clone, Copy, PartialEq)]
pub struct Transform {
pub position: [f32; 3],
pub rotation: [f32; 4],
pub scale: [f32; 3],
}
impl Transform {
pub fn identity() -> Self {
Self {
position: [0.0, 0.0, 0.0],
rotation: [0.0, 0.0, 0.0, 1.0],
scale: [1.0, 1.0, 1.0],
}
}
pub fn from_position(x: f32, y: f32, z: f32) -> Self {
Self {
position: [x, y, z],
..Self::identity()
}
}
}
impl Default for Transform {
fn default() -> Self {
Self::identity()
}
}
#[derive(Component, Debug, Clone, Copy, PartialEq, Eq)]
pub struct Visibility {
pub visible: bool,
}
impl Default for Visibility {
fn default() -> Self {
Self { visible: true }
}
}
#[derive(Component, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MeshHandle {
pub id: u32,
}
#[derive(Component, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MaterialHandle {
pub id: u32,
}
#[derive(Component, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct InstanceOf(pub MeshHandle);
#[derive(Component, Debug, Clone, Copy, PartialEq)]
pub struct Tint(pub [f32; 3]);
#[derive(Component, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ParentEntity(pub Entity);
#[derive(Component, Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum ObjectType {
#[default]
Mesh = 0,
PointLight = 1,
DirectionalLight = 2,
LineSegments = 3,
Group = 4,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn transform_identity() {
let t = Transform::identity();
assert_eq!(t.position, [0.0, 0.0, 0.0]);
assert_eq!(t.rotation, [0.0, 0.0, 0.0, 1.0]);
assert_eq!(t.scale, [1.0, 1.0, 1.0]);
}
#[test]
fn transform_from_position() {
let t = Transform::from_position(1.0, 2.0, 3.0);
assert_eq!(t.position, [1.0, 2.0, 3.0]);
assert_eq!(t.scale, [1.0, 1.0, 1.0]);
}
#[test]
fn visibility_default_is_visible() {
assert!(Visibility::default().visible);
}
#[test]
fn parent_entity_stores_entity() {
let entity = crate::entity::Entity::from_raw(42, 0);
let parent = ParentEntity(entity);
assert_eq!(parent.0, entity);
}
#[test]
fn object_type_default_is_mesh() {
assert_eq!(ObjectType::default(), ObjectType::Mesh);
}
#[test]
fn instance_of_wraps_mesh_handle() {
let handle = MeshHandle { id: 42 };
let tag = InstanceOf(handle);
assert_eq!(tag.0, handle);
assert_eq!(tag.0.id, 42);
}
#[test]
fn instance_of_equality_is_by_mesh_handle() {
assert_eq!(
InstanceOf(MeshHandle { id: 7 }),
InstanceOf(MeshHandle { id: 7 })
);
assert_ne!(
InstanceOf(MeshHandle { id: 7 }),
InstanceOf(MeshHandle { id: 8 })
);
}
#[test]
fn tint_stores_rgb_triple() {
let t = Tint([0.25, 0.5, 1.0]);
assert_eq!(t.0[0], 0.25);
assert_eq!(t.0[1], 0.5);
assert_eq!(t.0[2], 1.0);
}
#[test]
fn tint_equality() {
assert_eq!(Tint([1.0, 0.0, 0.0]), Tint([1.0, 0.0, 0.0]));
assert_ne!(Tint([1.0, 0.0, 0.0]), Tint([0.0, 1.0, 0.0]));
}
#[test]
fn object_type_as_u8() {
assert_eq!(ObjectType::Mesh as u8, 0);
assert_eq!(ObjectType::PointLight as u8, 1);
assert_eq!(ObjectType::DirectionalLight as u8, 2);
assert_eq!(ObjectType::LineSegments as u8, 3);
assert_eq!(ObjectType::Group as u8, 4);
}
}