Entity render structure
This commit is contained in:
+1
-9
@@ -1,19 +1,11 @@
|
|||||||
use osmium::entity::Component;
|
use osmium::entity::Component;
|
||||||
|
|
||||||
pub struct Velocity(pub i32);
|
|
||||||
|
|
||||||
impl Component for Velocity {
|
|
||||||
fn tick(&mut self) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut engine = osmium::init();
|
let mut engine = osmium::init();
|
||||||
|
|
||||||
engine.thread("Main.tick", || {});
|
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);
|
// r.draw_rect(0, 0, 10, 10);
|
||||||
});
|
});
|
||||||
|
|
||||||
my_rect.component(Velocity(10));
|
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-5
@@ -1,13 +1,32 @@
|
|||||||
|
use crate::Renderer;
|
||||||
|
|
||||||
pub trait Component {
|
pub trait Component {
|
||||||
fn init_add(&mut self) {}
|
#[allow(unused_variables)]
|
||||||
fn init(&mut self) {}
|
fn init_add(&mut self, entity: &mut Entity) {}
|
||||||
fn tick(&mut self) {}
|
#[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 {
|
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) {
|
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));
|
self.threads.insert(label.to_string(), Box::new(f));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw<F: FnMut(&mut Renderer) + 'static>(&self, mut f: F) -> entity::Entity {
|
pub fn entity<F: FnMut(&mut Renderer) + 'static>(&mut self, f: F) -> &mut entity::Entity {
|
||||||
let mut renderer = Renderer {};
|
let e = entity::Entity::new(f);
|
||||||
(f)(&mut renderer);
|
self.entities.push(e);
|
||||||
entity::Entity(Vec::new())
|
self.entities
|
||||||
|
.last_mut()
|
||||||
|
.expect("Unable to obtain entity (unspawned)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user