This commit is contained in:
Theis Pieter Hollebeek 2024-04-10 09:50:29 +02:00
parent cb157cbdfa
commit ccec00fcc0

View File

@ -13,6 +13,24 @@ struct Sprite {
sprite: engine::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)] #[derive(Component)]
struct Position(f64, f64); struct Position(f64, f64);
@ -22,7 +40,7 @@ struct Velocity(f64, f64);
struct VelocitySystem; struct VelocitySystem;
impl System for VelocitySystem { impl System for VelocitySystem {
fn on_update(&self, ctx: &mut engine::Context, delta: f64) -> Result<(), engine::Error> { fn on_update(&self, ctx: &mut engine::Context, delta: f64) -> Result<(), engine::Error> {
for id in run_quwi::<Velocity, Position>(ctx) { for id in run_quwi!(ctx, Velocity, Position) {
let vel = ctx.entity_component::<Velocity>(id).clone(); let vel = ctx.entity_component::<Velocity>(id).clone();
let Position(x, y) = ctx.entity_component::<Position>(id); let Position(x, y) = ctx.entity_component::<Position>(id);
*x += vel.0 * delta; *x += vel.0 * delta;
@ -38,7 +56,7 @@ struct Gravity;
struct GravitySystem; struct GravitySystem;
impl System for GravitySystem { impl System for GravitySystem {
fn on_update(&self, ctx: &mut engine::Context, delta: f64) -> Result<(), engine::Error> { fn on_update(&self, ctx: &mut engine::Context, delta: f64) -> Result<(), engine::Error> {
for id in run_quwi::<Gravity, Velocity>(ctx) { for id in run_quwi!(ctx, Gravity, Velocity) {
let Velocity(_, y) = ctx.entity_component::<Velocity>(id); let Velocity(_, y) = ctx.entity_component::<Velocity>(id);
*y = if *y < 800.0 { *y + 400.0 * delta } else { *y }; *y = if *y < 800.0 { *y + 400.0 * delta } else { *y };
} }
@ -64,12 +82,12 @@ impl System for CloudSystem {
]); ]);
} }
for id in run_quwi::<Cloud, Velocity>(ctx) { for id in run_quwi!(ctx, Cloud, Velocity) {
let Velocity(x, _) = ctx.entity_component::<Velocity>(id); let Velocity(x, _) = ctx.entity_component::<Velocity>(id);
*x = if *x < 200.0 { *x + 200.0 * delta } else { *x }; *x = if *x < 200.0 { *x + 200.0 * delta } else { *x };
} }
for id in run_quwi::<Cloud, Velocity>(ctx) { for id in run_quwi!(ctx, Cloud, Velocity) {
let Position(x, _) = ctx.entity_component::<Position>(id); let Position(x, _) = ctx.entity_component::<Position>(id);
if *x > 1400.0 { if *x > 1400.0 {
ctx.despawn(id); ctx.despawn(id);
@ -79,14 +97,10 @@ impl System for CloudSystem {
} }
} }
fn run_quwi<A: 'static + Component, B: 'static + Component>(ctx: &mut engine::Context) -> Vec<u64> {
engine::Quwi::<(A, B)>::new().run(&ctx)
}
struct SpriteRenderer; struct SpriteRenderer;
impl System for SpriteRenderer { impl System for SpriteRenderer {
fn on_update(&self, ctx: &mut engine::Context, _delta: f64) -> Result<(), engine::Error> { fn on_update(&self, ctx: &mut engine::Context, _delta: f64) -> Result<(), engine::Error> {
for id in run_quwi::<Sprite, Position>(ctx) { for id in run_quwi!(ctx, Sprite, Position) {
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::<Sprite>(id).sprite; let sprite = ctx.entity_component::<Sprite>(id).sprite;
@ -102,7 +116,7 @@ struct PlayerMovement;
struct PlayerMovementSystem; struct PlayerMovementSystem;
impl System for PlayerMovementSystem { impl System for PlayerMovementSystem {
fn on_update(&self, ctx: &mut engine::Context, _delta: f64) -> Result<(), engine::Error> { fn on_update(&self, ctx: &mut engine::Context, _delta: f64) -> Result<(), engine::Error> {
for id in run_quwi::<PlayerMovement, Velocity>(ctx) { for id in run_quwi!(ctx, PlayerMovement, Velocity) {
let d_down = ctx.key_pressed(engine::Keycode::D); let d_down = ctx.key_pressed(engine::Keycode::D);
let a_down = ctx.key_pressed(engine::Keycode::A); let a_down = ctx.key_pressed(engine::Keycode::A);
let Velocity(x, _) = ctx.entity_component::<Velocity>(id); let Velocity(x, _) = ctx.entity_component::<Velocity>(id);