Imrpoved component structure
This commit is contained in:
@@ -10,6 +10,10 @@ impl Component for Velocity {
|
||||
apply_velocity(tick, self.0.x, &mut position.x);
|
||||
apply_velocity(tick, self.0.y, &mut position.y);
|
||||
}
|
||||
|
||||
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_velocity(tick: u16, velocity: i32, x: &mut u16) -> bool {
|
||||
|
||||
@@ -19,10 +19,11 @@ pub fn tick(engine: &Arc<Engine>) {
|
||||
}
|
||||
|
||||
pub fn key(engine: &Arc<Engine>) {
|
||||
crossterm::terminal::enable_raw_mode().unwrap();
|
||||
|
||||
engine.thread("Main.key", |engine| {
|
||||
loop {
|
||||
// Pass an event to the thread
|
||||
|
||||
engine.emit_event(crossterm::event::read().unwrap());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+37
-2
@@ -3,8 +3,6 @@ pub mod components;
|
||||
use osmium::{renderer::Point, terminal};
|
||||
|
||||
fn main() {
|
||||
terminal::hide_cursor().unwrap();
|
||||
|
||||
let engine = osmium::init();
|
||||
|
||||
let my_rect = engine.entity(|r| {
|
||||
@@ -15,6 +13,43 @@ fn main() {
|
||||
|
||||
my_rect.component(components::Velocity(Point { x: 0, y: 0 }));
|
||||
|
||||
components::threads::tick(&engine);
|
||||
components::threads::key(&engine);
|
||||
|
||||
engine.on_event({
|
||||
let my_rect = my_rect.clone();
|
||||
move |e: &crossterm::event::Event| {
|
||||
if let Some(key_event) = e.as_key_event() {
|
||||
match key_event.code {
|
||||
crossterm::event::KeyCode::Right => {
|
||||
my_rect.update_component(|c: &mut components::Velocity| {
|
||||
c.0.x = 100;
|
||||
});
|
||||
}
|
||||
crossterm::event::KeyCode::Left => {
|
||||
my_rect.update_component(|c: &mut components::Velocity| {
|
||||
c.0.x = -100;
|
||||
});
|
||||
}
|
||||
|
||||
crossterm::event::KeyCode::Down => {
|
||||
my_rect.update_component(|c: &mut components::Velocity| {
|
||||
c.0.y = 100;
|
||||
});
|
||||
}
|
||||
crossterm::event::KeyCode::Up => {
|
||||
my_rect.update_component(|c: &mut components::Velocity| {
|
||||
c.0.y = -100;
|
||||
});
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
terminal::hide_cursor().unwrap();
|
||||
|
||||
engine.start();
|
||||
|
||||
terminal::show_cursor().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user