improved velocity algorithm
This commit is contained in:
+37
-15
@@ -9,35 +9,57 @@ use crate::{
|
|||||||
|
|
||||||
pub struct VelocityExtension;
|
pub struct VelocityExtension;
|
||||||
|
|
||||||
impl Extension for VelocityExtension {
|
impl VelocityExtension {
|
||||||
fn render(&self, _widget: &Arc<Widget>) {
|
fn apply_velocity(ticks: u16, velocity: i32, x: &mut u16) {
|
||||||
if let Some(velocity) = _widget.get::<Velocity>() {
|
if velocity.abs() != 0 && ticks as i32 % (1000 / velocity.abs()) == 0 {
|
||||||
if let Some(mut t) = _widget.get::<Transform>() {
|
if velocity > 0 {
|
||||||
match &mut t.x {
|
|
||||||
Position::Const(x) => {
|
|
||||||
if velocity.0 > 0 {
|
|
||||||
*x += 1;
|
*x += 1;
|
||||||
} else if velocity.0 < 0 {
|
} else if velocity < 0 {
|
||||||
*x -= 1;
|
*x -= 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VelocityExtension {
|
||||||
|
fn apply_velocity_xy(ticks: u16, widget: &Arc<Widget>) {
|
||||||
|
if let Some(velocity) = widget.get::<Velocity>() {
|
||||||
|
if let Some(mut t) = widget.get::<Transform>() {
|
||||||
|
match &mut t.x {
|
||||||
|
Position::Const(x) => Self::apply_velocity(ticks, velocity.0, x),
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
match &mut t.y {
|
match &mut t.y {
|
||||||
Position::Const(y) => {
|
Position::Const(y) => Self::apply_velocity(ticks, velocity.1, y),
|
||||||
if velocity.1 > 0 {
|
_ => {}
|
||||||
*y += 1;
|
}
|
||||||
} else if velocity.1 < 0 {
|
|
||||||
*y -= 1;
|
widget.set_component(t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_widget.set_component(t);
|
impl Extension for VelocityExtension {
|
||||||
|
fn init(&self, widgets: &Vec<Arc<Widget>>) {
|
||||||
|
std::thread::spawn({
|
||||||
|
let widgets = widgets.clone();
|
||||||
|
move || {
|
||||||
|
let mut tick = 0;
|
||||||
|
loop {
|
||||||
|
for widget in &widgets {
|
||||||
|
Self::apply_velocity_xy(tick, widget);
|
||||||
|
}
|
||||||
|
if tick > 1000 {
|
||||||
|
tick = 0;
|
||||||
|
} else {
|
||||||
|
tick += 1;
|
||||||
|
}
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -15,7 +15,7 @@ fn main() {
|
|||||||
screen
|
screen
|
||||||
.draw(format!("Hello, World!"))
|
.draw(format!("Hello, World!"))
|
||||||
.component(Transform::new())
|
.component(Transform::new())
|
||||||
.component(Velocity(1, 0))
|
.component(Velocity(100, 0))
|
||||||
.component(OnTick(|w| {
|
.component(OnTick(|w| {
|
||||||
if let Some(mut v) = w.get::<Velocity>() {
|
if let Some(mut v) = w.get::<Velocity>() {
|
||||||
if let Some(t) = w.get::<Transform>() {
|
if let Some(t) = w.get::<Transform>() {
|
||||||
|
|||||||
Reference in New Issue
Block a user