Improved a few things (i don't remember them)
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
pub mod threads;
|
||||
|
||||
use osmium::{entity::Component, renderer::Point};
|
||||
|
||||
pub struct Velocity(pub Point<i32>);
|
||||
|
||||
impl Component for Velocity {
|
||||
fn tick(&mut self, entity: &std::sync::Arc<osmium::entity::Entity>, tick: u16) {
|
||||
let mut position = entity.position();
|
||||
apply_velocity(tick, self.0.x, &mut position.x);
|
||||
apply_velocity(tick, self.0.y, &mut position.y);
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_velocity(tick: u16, velocity: i32, x: &mut u16) -> bool {
|
||||
let abs_v = velocity.abs();
|
||||
if abs_v == 0 {
|
||||
return false;
|
||||
}
|
||||
|
||||
let interval = (1000 / abs_v).max(1);
|
||||
if tick as i32 % interval == 0 {
|
||||
if velocity > 0 {
|
||||
*x = x.saturating_add(1);
|
||||
} else {
|
||||
*x = x.saturating_sub(1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use osmium::{Engine, sleep};
|
||||
|
||||
pub fn tick(engine: &Arc<Engine>) {
|
||||
engine.thread("Main.tick", |engine| {
|
||||
let mut tick = 0;
|
||||
loop {
|
||||
if tick == 60000 {
|
||||
tick = 0;
|
||||
}
|
||||
|
||||
engine.tick(tick);
|
||||
|
||||
tick += 1;
|
||||
sleep(4);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn key(engine: &Arc<Engine>) {
|
||||
engine.thread("Main.key", |engine| {
|
||||
loop {
|
||||
// Pass an event to the thread
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
+4
-43
@@ -1,60 +1,21 @@
|
||||
use osmium::{entity::Component, sleep, terminal};
|
||||
pub mod components;
|
||||
|
||||
use osmium::{renderer::Point, terminal};
|
||||
|
||||
fn main() {
|
||||
terminal::hide_cursor().unwrap();
|
||||
|
||||
let engine = osmium::init();
|
||||
|
||||
engine.thread("Main.tick", |engine| {
|
||||
let mut tick = 0;
|
||||
loop {
|
||||
if tick == 60000 {
|
||||
tick = 0;
|
||||
}
|
||||
|
||||
engine.tick(tick);
|
||||
|
||||
tick += 1;
|
||||
sleep(4);
|
||||
}
|
||||
});
|
||||
|
||||
let my_rect = engine.entity(|r| {
|
||||
r.draw_rect(0, 0, 1, 2, 0xff0000);
|
||||
r.draw_rect(1, 0, 1, 2, 0x00ff00);
|
||||
r.draw_rect(2, 0, 1, 2, 0x0000ff);
|
||||
});
|
||||
|
||||
my_rect.component(Velocity(300));
|
||||
my_rect.component(components::Velocity(Point { x: 0, y: 0 }));
|
||||
|
||||
engine.start();
|
||||
|
||||
terminal::show_cursor().unwrap();
|
||||
}
|
||||
|
||||
pub struct Velocity(pub i32);
|
||||
|
||||
impl Component for Velocity {
|
||||
fn tick(&mut self, entity: &std::sync::Arc<osmium::entity::Entity>, tick: u16) {
|
||||
let mut position = entity.position();
|
||||
apply_velocity(tick, self.0, &mut position.x);
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_velocity(tick: u16, velocity: i32, x: &mut u16) -> bool {
|
||||
let abs_v = velocity.abs();
|
||||
if abs_v == 0 {
|
||||
return false;
|
||||
}
|
||||
|
||||
let interval = (1000 / abs_v).max(1);
|
||||
if tick as i32 % interval == 0 {
|
||||
if velocity > 0 {
|
||||
*x = x.saturating_add(1);
|
||||
} else {
|
||||
*x = x.saturating_sub(1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,9 +1,9 @@
|
||||
use crate::terminal::{hex_bg, print_liner};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Point {
|
||||
pub x: u16,
|
||||
pub y: u16,
|
||||
pub struct Point<I = u16> {
|
||||
pub x: I,
|
||||
pub y: I,
|
||||
}
|
||||
|
||||
pub struct Renderer {
|
||||
|
||||
Reference in New Issue
Block a user