From ccec00fcc075bd588f764b3dc7451f1836c3956a Mon Sep 17 00:00:00 2001 From: Theis Pieter Hollebeek Date: Wed, 10 Apr 2024 09:50:29 +0200 Subject: [PATCH] run_quwi --- src/main.rs | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index c058787..71cfdb6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,24 @@ struct Sprite { sprite: engine::Sprite, } +macro_rules! tuplify { + ($t:ty) => { + $t + }; + ($t:ty, $($ts:ty),+) => { + $t, tuplify!($($ts),+) + }; +} + +macro_rules! run_quwi { + ($ctx:expr, $t:ty) => { + engine::Quwi::<$t>::new().run($ctx) + }; + ($ctx:expr, $t:ty, $($ts:ty),+) => { + engine::Quwi::<($t, tuplify!($($ts),+))>::new().run($ctx) + }; +} + #[derive(Component)] struct Position(f64, f64); @@ -22,7 +40,7 @@ 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 run_quwi::(ctx) { + for id in run_quwi!(ctx, Velocity, Position) { let vel = ctx.entity_component::(id).clone(); let Position(x, y) = ctx.entity_component::(id); *x += vel.0 * delta; @@ -38,7 +56,7 @@ struct Gravity; struct GravitySystem; impl System for GravitySystem { fn on_update(&self, ctx: &mut engine::Context, delta: f64) -> Result<(), engine::Error> { - for id in run_quwi::(ctx) { + for id in run_quwi!(ctx, Gravity, Velocity) { let Velocity(_, y) = ctx.entity_component::(id); *y = if *y < 800.0 { *y + 400.0 * delta } else { *y }; } @@ -64,12 +82,12 @@ impl System for CloudSystem { ]); } - for id in run_quwi::(ctx) { + for id in run_quwi!(ctx, Cloud, Velocity) { let Velocity(x, _) = ctx.entity_component::(id); *x = if *x < 200.0 { *x + 200.0 * delta } else { *x }; } - for id in run_quwi::(ctx) { + for id in run_quwi!(ctx, Cloud, Velocity) { let Position(x, _) = ctx.entity_component::(id); if *x > 1400.0 { ctx.despawn(id); @@ -79,14 +97,10 @@ impl System for CloudSystem { } } -fn run_quwi(ctx: &mut engine::Context) -> Vec { - engine::Quwi::<(A, B)>::new().run(&ctx) -} - struct SpriteRenderer; impl System for SpriteRenderer { fn on_update(&self, ctx: &mut engine::Context, _delta: f64) -> Result<(), engine::Error> { - for id in run_quwi::(ctx) { + for id in run_quwi!(ctx, Sprite, Position) { let &mut Position(x, y) = ctx.entity_component::(id); let sprite = ctx.entity_component::(id).sprite; @@ -102,7 +116,7 @@ struct PlayerMovement; struct PlayerMovementSystem; impl System for PlayerMovementSystem { fn on_update(&self, ctx: &mut engine::Context, _delta: f64) -> Result<(), engine::Error> { - for id in run_quwi::(ctx) { + for id in run_quwi!(ctx, PlayerMovement, Velocity) { 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);