ecs: add id() -> str method

master
q3k 2021-04-04 20:35:03 +00:00
parent 8d4bac00cf
commit be1ff9ad9c
4 changed files with 22 additions and 3 deletions

View File

@ -25,7 +25,11 @@ use crate::render::resource::{ResourceID};
pub struct Transform(pub cgm::Matrix4<f32>);
impl Component for Transform {}
impl Component for Transform {
fn id(&self) -> &'static str {
"Transform"
}
}
impl Transform {
pub fn at(x: f32, y: f32, z: f32) -> Self {
@ -48,4 +52,8 @@ pub enum Renderable {
Mesh(ResourceID<Mesh>, ResourceID<Material>),
}
impl Component for Renderable {}
impl Component for Renderable {
fn id(&self) -> &'static str {
"Renderable"
}
}

View File

@ -14,6 +14,9 @@ rust_library(
"src/system.rs",
"src/world.rs",
],
deps = [
"//third_party/cargo:log",
],
visibility = ["//engine:__subpackages__"],
)

View File

@ -2,6 +2,9 @@ pub type ID = std::any::TypeId;
pub trait Component: 'static {
fn id(&self) -> &'static str {
""
}
}
pub fn component_id<T: Component>() -> ID {

View File

@ -159,7 +159,12 @@ impl World {
c: Box<dyn component::Component>,
e: entity::Entity
) {
let map = self.components.entry(cid).or_insert(ComponentMap::new());
let map = self.components.entry(cid).or_insert_with(|| {
if c.id() == "" {
log::warn!("Component type {:?} has no .id() defined, will not be accessible from scripting.", cid);
}
ComponentMap::new()
});
map.insert(e.id(), c).unwrap();
}