vel and pos

This commit is contained in:
SimonFJ20 2024-04-09 14:52:04 +02:00
parent fd793f4b45
commit 631a9fa407
3 changed files with 70 additions and 28 deletions

View File

@ -12,10 +12,10 @@ fn impl_derive_macro(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident; let name = &ast.ident;
let gen = quote! { let gen = quote! {
impl Component for #name { impl Component for #name {
fn inner_type_id(&self) -> TypeId { fn inner_type_id(&self) -> std::any::TypeId {
TypeId::of::<Self>() std::any::TypeId::of::<Self>()
} }
fn as_any(&mut self) -> &mut dyn Any { fn as_any(&mut self) -> &mut dyn std::any::Any {
self self
} }
} }

View File

@ -1,7 +1,7 @@
use std::any::{Any, TypeId}; use std::any::{Any, TypeId};
use std::collections::HashSet; use std::collections::HashSet;
use std::rc::Rc; use std::rc::Rc;
use std::time::Duration; use std::time::{Duration, Instant};
pub use sdl2::keyboard::Keycode; pub use sdl2::keyboard::Keycode;
@ -212,7 +212,7 @@ impl<'context, 'game> Context<'context, 'game> {
pub trait System { pub trait System {
fn on_add(&self, _ctx: &mut Context) {} 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(()) Ok(())
} }
} }
@ -266,6 +266,7 @@ impl<'game> Game<'game> {
} }
pub fn run(&mut self) { pub fn run(&mut self) {
let mut time_before = Instant::now();
'running: loop { 'running: loop {
for event in self.event_pump.poll_iter() { for event in self.event_pump.poll_iter() {
match event { match event {
@ -285,8 +286,11 @@ impl<'game> Game<'game> {
} }
self.canvas.set_draw_color(Color::RGB(60, 180, 180)); self.canvas.set_draw_color(Color::RGB(60, 180, 180));
self.canvas.clear(); 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() { 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; continue;
}; };
println!("error occcurred updating system: {err}"); println!("error occcurred updating system: {err}");

View File

@ -1,52 +1,82 @@
#![allow(dead_code)] #![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; mod engine;
use component_macro::Component;
use engine::Component; use engine::Component;
use engine::QuwiQuwi;
use engine::System;
use std::rc::Rc;
#[derive(Component)] #[derive(Component)]
struct Player { struct Sprite {
sprite: Sprite, sprite: engine::Sprite,
} }
#[derive(Component)] #[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::<Velocity>(id).clone();
let Position(x, y) = ctx.entity_component::<Position>(id);
*x += vel.0 * delta;
*y += vel.1 * delta;
}
Ok(())
}
}
#[derive(Component)]
struct Gravity; struct Gravity;
impl System for Gravity { struct GravitySystem;
fn on_update(&self, ctx: &mut engine::Context) -> Result<(), engine::Error> { impl System for GravitySystem {
for id in ctx.entities_with_component::<Position>() { fn on_update(&self, ctx: &mut engine::Context, delta: f64) -> Result<(), engine::Error> {
let Position(_, y) = ctx.entity_component::<Position>(id); for id in engine::Quwi::<(Gravity, Velocity)>::new().run(&ctx) {
*y += 1; let Velocity(_, y) = ctx.entity_component::<Velocity>(id);
*y = if *y < 200.0 { *y + 100.0 * delta } else { *y };
} }
Ok(()) Ok(())
} }
} }
struct SpriteRenderer; struct SpriteRenderer;
impl System for SpriteRenderer { impl System for SpriteRenderer {
fn on_update(&self, ctx: &mut engine::Context) -> Result<(), engine::Error> { fn on_update(&self, ctx: &mut engine::Context, _delta: f64) -> Result<(), engine::Error> {
for id in engine::Quwi::<(Player, Position)>::new().run(&ctx) { for id in engine::Quwi::<(Sprite, Position)>::new().run(&ctx) {
let &mut Position(x, y) = ctx.entity_component::<Position>(id); let &mut Position(x, y) = ctx.entity_component::<Position>(id);
let sprite = ctx.entity_component::<Player>(id).sprite; let sprite = ctx.entity_component::<Sprite>(id).sprite;
ctx.draw_sprite(sprite, x, y)?; ctx.draw_sprite(sprite, x as i32, y as i32)?;
} }
Ok(()) Ok(())
} }
} }
#[derive(Component)]
struct PlayerMovement; struct PlayerMovement;
impl System for PlayerMovement { struct PlayerMovementSystem;
fn on_update(&self, _ctx: &mut engine::Context) -> Result<(), engine::Error> { 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::<Velocity>(id);
*x = if d_down && !a_down {
100.0
} else if !d_down && a_down {
-100.0
} else {
0.0
};
}
Ok(()) Ok(())
} }
} }
@ -55,9 +85,17 @@ fn main() {
let mut game = engine::Game::new().unwrap(); let mut game = engine::Game::new().unwrap();
let mut context = game.context(); let mut context = game.context();
context.add_system(Rc::new(VelocitySystem));
context.add_system(Rc::new(SpriteRenderer)); 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(); 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(); game.run();
} }