Entity render structure
This commit is contained in:
+1
-9
@@ -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));
|
||||
}
|
||||
|
||||
+24
-5
@@ -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<Box<dyn Component>>);
|
||||
pub struct Entity {
|
||||
components: Vec<Box<dyn Component>>,
|
||||
render: Box<dyn FnMut(&mut Renderer)>,
|
||||
}
|
||||
|
||||
impl Entity {
|
||||
pub fn new<F: FnMut(&mut Renderer) + 'static>(render: F) -> Self {
|
||||
Self {
|
||||
components: Vec::new(),
|
||||
render: Box::new(render),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn component<C: Component + 'static>(&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)
|
||||
}
|
||||
}
|
||||
|
||||
+6
-4
@@ -21,9 +21,11 @@ impl Engine {
|
||||
self.threads.insert(label.to_string(), Box::new(f));
|
||||
}
|
||||
|
||||
pub fn draw<F: FnMut(&mut Renderer) + 'static>(&self, mut f: F) -> entity::Entity {
|
||||
let mut renderer = Renderer {};
|
||||
(f)(&mut renderer);
|
||||
entity::Entity(Vec::new())
|
||||
pub fn entity<F: FnMut(&mut Renderer) + 'static>(&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)")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user