From 5ed483a09a469c19affb617b929d16958aebbcde Mon Sep 17 00:00:00 2001 From: Leo dev Date: Wed, 26 Nov 2025 18:03:13 +0100 Subject: [PATCH] Entity render structure --- examples/cube.rs | 10 +--------- src/entity.rs | 29 ++++++++++++++++++++++++----- src/lib.rs | 10 ++++++---- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/examples/cube.rs b/examples/cube.rs index cbcdcb7..84bc42a 100644 --- a/examples/cube.rs +++ b/examples/cube.rs @@ -1,19 +1,11 @@ use osmium::entity::Component; -pub struct Velocity(pub i32); - -impl Component for Velocity { - fn tick(&mut self) {} -} - fn main() { let mut engine = osmium::init(); engine.thread("Main.tick", || {}); - let mut my_rect = engine.draw(|r| { + let mut my_rect = engine.entity(|r| { // r.draw_rect(0, 0, 10, 10); }); - - my_rect.component(Velocity(10)); } diff --git a/src/entity.rs b/src/entity.rs index 7cf9361..08fa8e6 100644 --- a/src/entity.rs +++ b/src/entity.rs @@ -1,13 +1,32 @@ +use crate::Renderer; + pub trait Component { - fn init_add(&mut self) {} - fn init(&mut self) {} - fn tick(&mut self) {} + #[allow(unused_variables)] + fn init_add(&mut self, entity: &mut Entity) {} + #[allow(unused_variables)] + fn init(&mut self, entity: &mut Entity) {} + #[allow(unused_variables)] + fn tick(&mut self, entity: &mut Entity) {} } -pub struct Entity(pub Vec>); +pub struct Entity { + components: Vec>, + render: Box, +} impl Entity { + pub fn new(render: F) -> Self { + Self { + components: Vec::new(), + render: Box::new(render), + } + } + pub fn component(&mut self, c: C) { - self.0.push(Box::new(c)); + self.components.push(Box::new(c)); + } + + pub fn render(&mut self, renderer: &mut Renderer) { + (self.render)(renderer) } } diff --git a/src/lib.rs b/src/lib.rs index 7bfc07e..1a66c8b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,9 +21,11 @@ impl Engine { self.threads.insert(label.to_string(), Box::new(f)); } - pub fn draw(&self, mut f: F) -> entity::Entity { - let mut renderer = Renderer {}; - (f)(&mut renderer); - entity::Entity(Vec::new()) + pub fn entity(&mut self, f: F) -> &mut entity::Entity { + let e = entity::Entity::new(f); + self.entities.push(e); + self.entities + .last_mut() + .expect("Unable to obtain entity (unspawned)") } }