From fd793f4b45684cdcbb2ad86e7c911e1ab62e29de Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Tue, 9 Apr 2024 13:28:31 +0200 Subject: [PATCH] =?UTF-8?q?rust=20=F0=9F=98=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/engine.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++-- src/main.rs | 17 ++++++++------- 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index ba03a1b..41f50ac 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -69,6 +69,60 @@ where currently_pressed_keys: &'context HashSet, } +pub struct Quwi(std::marker::PhantomData); + +impl Quwi { + pub fn new() -> Self { + Self(std::marker::PhantomData) + } +} + +pub trait QuwiQuwi { + fn run(&self, context: &Context) -> Vec; +} + +impl QuwiQuwi for Quwi +where + T0: 'static + Component, +{ + fn run(&self, context: &Context) -> Vec { + context.entities_with_component::() + } +} + +impl QuwiQuwi for Quwi<(T0, T1)> +where + T0: 'static + Component, + T1: 'static + Component, +{ + fn run(&self, context: &Context) -> Vec { + let vs0 = context.entities_with_component::(); + let vs1 = context.entities_with_component::(); + vs0.into_iter() + .filter(|v0| vs1.iter().find(|v1| *v0 == **v1).is_some()) + .collect() + } +} + +impl QuwiQuwi for Quwi<(T0, T1, T2)> +where + T0: 'static + Component, + T1: 'static + Component, + T2: 'static + Component, +{ + fn run(&self, context: &Context) -> Vec { + let vs0 = context.entities_with_component::(); + let vs1 = context.entities_with_component::(); + let vs2 = context.entities_with_component::(); + vs0.into_iter() + .filter(|v0| { + vs1.iter().find(|v1| *v0 == **v1).is_some() + && vs2.iter().find(|v2| *v0 == **v2).is_some() + }) + .collect() + } +} + impl<'context, 'game> Context<'context, 'game> { pub fn entities_with_component(&self) -> Vec { let entity_type_id = TypeId::of::(); @@ -117,12 +171,11 @@ impl<'context, 'game> Context<'context, 'game> { let texture: Texture<'game> = unsafe { (*self.texture_creator).load_texture(&path)? }; let id = *self.id_counter; *self.id_counter += 1; - self.textures.push((id, texture)); Ok(Sprite(id)) } - pub fn draw_sprite(&mut self, sprite: &Sprite, x: i32, y: i32) -> Result<(), Error> { + pub fn draw_sprite(&mut self, sprite: Sprite, x: i32, y: i32) -> Result<(), Error> { let texture = self .textures .iter() @@ -254,6 +307,7 @@ impl<'game> Game<'game> { entities: &mut self.entities, systems: &mut self.systems, textures: &mut self.textures, + currently_pressed_keys: &mut self.currently_pressed_keys, } } } diff --git a/src/main.rs b/src/main.rs index fa1a7e7..9e661db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ #![allow(dead_code)] use component_macro::Component; +use engine::QuwiQuwi; use engine::{Sprite, System}; use std::any::{Any, TypeId}; use std::rc::Rc; @@ -28,15 +29,15 @@ impl System for Gravity { } } -struct PlayerRenderer; +struct SpriteRenderer; -impl System for PlayerRenderer { +impl System for SpriteRenderer { fn on_update(&self, ctx: &mut engine::Context) -> Result<(), engine::Error> { - for id in ctx.entities_with_component::() { + for id in engine::Quwi::<(Player, Position)>::new().run(&ctx) { let &mut Position(x, y) = ctx.entity_component::(id); - let player = ctx.entity_component::(id); + let sprite = ctx.entity_component::(id).sprite; - ctx.draw_sprite(&player.sprite, x, y)?; + ctx.draw_sprite(sprite, x, y)?; } Ok(()) } @@ -45,14 +46,16 @@ impl System for PlayerRenderer { struct PlayerMovement; impl System for PlayerMovement { - fn on_update(&self, _ctx: &mut engine::Context) -> Result<(), engine::Error> {} + fn on_update(&self, _ctx: &mut engine::Context) -> Result<(), engine::Error> { + Ok(()) + } } fn main() { let mut game = engine::Game::new().unwrap(); let mut context = game.context(); - context.add_system(Rc::new(PlayerRenderer)); + context.add_system(Rc::new(SpriteRenderer)); context.add_system(Rc::new(Gravity)); let sprite = context.load_sprite("textures/player.png").unwrap(); context.spawn(vec![Box::new(Player { sprite }), Box::new(Position(16, 0))]);