From 631a9fa407dc0a68592b552fedcc44b42d602317 Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Tue, 9 Apr 2024 14:52:04 +0200 Subject: [PATCH] vel and pos --- component-macro/src/lib.rs | 6 +-- src/engine.rs | 10 +++-- src/main.rs | 82 ++++++++++++++++++++++++++++---------- 3 files changed, 70 insertions(+), 28 deletions(-) diff --git a/component-macro/src/lib.rs b/component-macro/src/lib.rs index 116c490..e600e1b 100644 --- a/component-macro/src/lib.rs +++ b/component-macro/src/lib.rs @@ -12,10 +12,10 @@ fn impl_derive_macro(ast: &syn::DeriveInput) -> TokenStream { let name = &ast.ident; let gen = quote! { impl Component for #name { - fn inner_type_id(&self) -> TypeId { - TypeId::of::() + fn inner_type_id(&self) -> std::any::TypeId { + std::any::TypeId::of::() } - fn as_any(&mut self) -> &mut dyn Any { + fn as_any(&mut self) -> &mut dyn std::any::Any { self } } diff --git a/src/engine.rs b/src/engine.rs index 41f50ac..358b490 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1,7 +1,7 @@ use std::any::{Any, TypeId}; use std::collections::HashSet; use std::rc::Rc; -use std::time::Duration; +use std::time::{Duration, Instant}; pub use sdl2::keyboard::Keycode; @@ -212,7 +212,7 @@ impl<'context, 'game> Context<'context, 'game> { pub trait System { fn on_add(&self, _ctx: &mut Context) {} - fn on_update(&self, _ctx: &mut Context) -> Result<(), Error> { + fn on_update(&self, _ctx: &mut Context, _delta: f64) -> Result<(), Error> { Ok(()) } } @@ -266,6 +266,7 @@ impl<'game> Game<'game> { } pub fn run(&mut self) { + let mut time_before = Instant::now(); 'running: loop { for event in self.event_pump.poll_iter() { match event { @@ -285,8 +286,11 @@ impl<'game> Game<'game> { } self.canvas.set_draw_color(Color::RGB(60, 180, 180)); self.canvas.clear(); + let now = Instant::now(); + let delta = (now - time_before).as_nanos() as f64 / 1_000_000_000.0; + time_before = now; for system in self.systems.clone() { - let Err(err) = system.on_update(&mut self.context()) else { + let Err(err) = system.on_update(&mut self.context(), delta) else { continue; }; println!("error occcurred updating system: {err}"); diff --git a/src/main.rs b/src/main.rs index 9e661db..ffd24a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,52 +1,82 @@ #![allow(dead_code)] -use component_macro::Component; -use engine::QuwiQuwi; -use engine::{Sprite, System}; -use std::any::{Any, TypeId}; -use std::rc::Rc; mod engine; +use component_macro::Component; use engine::Component; +use engine::QuwiQuwi; +use engine::System; +use std::rc::Rc; #[derive(Component)] -struct Player { - sprite: Sprite, +struct Sprite { + sprite: engine::Sprite, } #[derive(Component)] -struct Position(i32, i32); +struct Position(f64, f64); +#[derive(Clone, Component)] +struct Velocity(f64, f64); + +struct VelocitySystem; +impl System for VelocitySystem { + fn on_update(&self, ctx: &mut engine::Context, delta: f64) -> Result<(), engine::Error> { + for id in engine::Quwi::<(Velocity, Position)>::new().run(&ctx) { + let vel = ctx.entity_component::(id).clone(); + let Position(x, y) = ctx.entity_component::(id); + *x += vel.0 * delta; + *y += vel.1 * delta; + } + Ok(()) + } +} + +#[derive(Component)] struct Gravity; -impl System for Gravity { - fn on_update(&self, ctx: &mut engine::Context) -> Result<(), engine::Error> { - for id in ctx.entities_with_component::() { - let Position(_, y) = ctx.entity_component::(id); - *y += 1; +struct GravitySystem; +impl System for GravitySystem { + fn on_update(&self, ctx: &mut engine::Context, delta: f64) -> Result<(), engine::Error> { + for id in engine::Quwi::<(Gravity, Velocity)>::new().run(&ctx) { + let Velocity(_, y) = ctx.entity_component::(id); + *y = if *y < 200.0 { *y + 100.0 * delta } else { *y }; } Ok(()) } } struct SpriteRenderer; - impl System for SpriteRenderer { - fn on_update(&self, ctx: &mut engine::Context) -> Result<(), engine::Error> { - for id in engine::Quwi::<(Player, Position)>::new().run(&ctx) { + fn on_update(&self, ctx: &mut engine::Context, _delta: f64) -> Result<(), engine::Error> { + for id in engine::Quwi::<(Sprite, Position)>::new().run(&ctx) { let &mut Position(x, y) = ctx.entity_component::(id); - let sprite = ctx.entity_component::(id).sprite; + let sprite = ctx.entity_component::(id).sprite; - ctx.draw_sprite(sprite, x, y)?; + ctx.draw_sprite(sprite, x as i32, y as i32)?; } Ok(()) } } +#[derive(Component)] struct PlayerMovement; -impl System for PlayerMovement { - fn on_update(&self, _ctx: &mut engine::Context) -> Result<(), engine::Error> { +struct PlayerMovementSystem; +impl System for PlayerMovementSystem { + fn on_update(&self, ctx: &mut engine::Context, _delta: f64) -> Result<(), engine::Error> { + for id in engine::Quwi::<(PlayerMovement, Velocity)>::new().run(&ctx) { + let d_down = ctx.key_pressed(engine::Keycode::D); + let a_down = ctx.key_pressed(engine::Keycode::A); + let Velocity(x, _) = ctx.entity_component::(id); + *x = if d_down && !a_down { + 100.0 + } else if !d_down && a_down { + -100.0 + } else { + 0.0 + }; + } Ok(()) } } @@ -55,9 +85,17 @@ fn main() { let mut game = engine::Game::new().unwrap(); let mut context = game.context(); + context.add_system(Rc::new(VelocitySystem)); context.add_system(Rc::new(SpriteRenderer)); - context.add_system(Rc::new(Gravity)); + context.add_system(Rc::new(GravitySystem)); + context.add_system(Rc::new(PlayerMovementSystem)); let sprite = context.load_sprite("textures/player.png").unwrap(); - context.spawn(vec![Box::new(Player { sprite }), Box::new(Position(16, 0))]); + context.spawn(vec![ + Box::new(Sprite { sprite }), + Box::new(Position(16.0, 140.0)), + Box::new(Velocity(0.0, -150.0)), + Box::new(Gravity), + Box::new(PlayerMovement), + ]); game.run(); }