From 7aec7a36a7be41fc7e3c97a5f7c5765c7f91cd4a Mon Sep 17 00:00:00 2001 From: Theis Pieter Hollebeek Date: Wed, 10 Apr 2024 09:25:36 +0200 Subject: [PATCH] background stuff --- src/engine.rs | 8 ++++++++ src/main.rs | 45 +++++++++++++++++++++++++++++++++++++++-- textures/clouds.png | Bin 0 -> 987 bytes textures/mountains.png | Bin 0 -> 10664 bytes 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 textures/clouds.png create mode 100644 textures/mountains.png diff --git a/src/engine.rs b/src/engine.rs index 97b554e..dd6da21 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -195,6 +195,14 @@ impl<'context, 'game> Context<'context, 'game> { self.entities.push(Entity(id, components)); } + pub fn despawn(&mut self, entity_id: u64) { + *self.entities = self + .entities + .drain(..) + .filter(|v| v.0 != entity_id) + .collect(); + } + pub fn add_system(&mut self, system: Rc) { system.on_add(self); self.systems.push(system) diff --git a/src/main.rs b/src/main.rs index 902b05a..23280f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,6 +46,39 @@ impl System for GravitySystem { } } +#[derive(Component)] +struct Cloud; + +struct CloudSystem; + +impl System for CloudSystem { + fn on_update(&self, ctx: &mut engine::Context, delta: f64) -> Result<(), engine::Error> { + let cloud_amount = ctx.entities_with_component::().len(); + if cloud_amount < 1 { + let cloud = ctx.load_sprite("textures/clouds.png").unwrap(); + ctx.spawn(vec![ + Box::new(Cloud), + Box::new(Sprite { sprite: cloud }), + Box::new(Position(-100.0, 150.0)), + Box::new(Velocity(0.0, 0.0)), + ]); + } + + for id in engine::Quwi::<(Cloud, Velocity)>::new().run(&ctx) { + let Velocity(x, _) = ctx.entity_component::(id); + *x = if *x < 200.0 { *x + 200.0 * delta } else { *x }; + } + + for id in engine::Quwi::<(Cloud, Position)>::new().run(&ctx) { + let Position(x, _) = ctx.entity_component::(id); + if *x > 1400.0 { + ctx.despawn(id); + } + } + Ok(()) + } +} + struct SpriteRenderer; impl System for SpriteRenderer { fn on_update(&self, ctx: &mut engine::Context, _delta: f64) -> Result<(), engine::Error> { @@ -89,9 +122,17 @@ fn main() { context.add_system(Rc::new(SpriteRenderer)); context.add_system(Rc::new(GravitySystem)); context.add_system(Rc::new(PlayerMovementSystem)); - let sprite = context.load_sprite("textures/player.png").unwrap(); + context.add_system(Rc::new(CloudSystem)); + let player = context.load_sprite("textures/player.png").unwrap(); + let background = context.load_sprite("textures/mountains.png").unwrap(); + context.spawn(vec![ - Box::new(Sprite { sprite }), + Box::new(Sprite { sprite: background }), + Box::new(Position(0.0, 0.0)), + ]); + + context.spawn(vec![ + Box::new(Sprite { sprite: player }), Box::new(Position(16.0, 500.0)), Box::new(Velocity(0.0, -600.0)), Box::new(Gravity), diff --git a/textures/clouds.png b/textures/clouds.png new file mode 100644 index 0000000000000000000000000000000000000000..696bda7101f03eff766768334963308633f29523 GIT binary patch literal 987 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K58911MRQ8&P5FiyB;1l8sq?Hs*Ts3UcEDig^ ztnU}9-CU6N?(nSNpUh@H4anMOU_K?!J5xW%TgAx6(%!zrAfYKb?_u)n|M6#^oWA{M z_S)?&b?up1S&?BuWlmjdjEruECtt7L)j!>w2ObNw6M-RBTK z5bG+R@TrE$;`E9`>m>vZ#Jg?I+N{v9T!3+&>;v~~-@IIHCZ-!FXMeS;yW7HGwsDUt z(-W2*{|Y(ppPcku_(90q7ye3RTyy*zn0GGT{(HTM)qIA|Eq4EWe>2=SvHo6vi~R>L z<1Y4!%YSN^{w)3ZlLa1pNR0F;`d&I8gzN-d#>tjo^NeaK*;q*Mh~J-GQGOUN*neW7+V7 zL(Ms7S}iaz<$1n%{$IQ4-F4|&mi=MZ|JE_7{J!zbhB?Alto9N+r$G6iN&deQ)&lh% zd@j78n&F@cMlO~O(lcUI7PE9%)}M4U*cr}n`zPza=GH44WtndX-MD_%Z)LJ5 z^AAsf2P+v%;=A^gKj-BTSnAmDoFzcZC0BTXQ-dJq(=tvV-f^g-EpH}+npHYi7U=7d_4MFj2r`d0OmUrO@P;E%7 ze^tZ8q86}Lc!3+k)qh`pvdjqE@w?+i?fqP)Gg@Q z&ELt-w(}V%Jupf(5ICPN|G-b7=HDept9xn(LYq_?g}EP=Y-UL36k@%yh5v)^>&N%E e@WUb_`U88#i+b(wzqhA?ggjmST-G@yGywobGO~yO literal 0 HcmV?d00001 diff --git a/textures/mountains.png b/textures/mountains.png new file mode 100644 index 0000000000000000000000000000000000000000..2cbdc2509263ea655c3e1353801cf43e6cb0ec49 GIT binary patch literal 10664 zcmeHtcUV)|*8WMzNN^AcSU?C>#Su`7hy;Q`s)|yqG(o8vMnH@}Xi-#<5@c+E8bT2h z2}L8)A&yE=fmlK!Dgi|aCP;`7g4FK>@16Oc-*4{RndiCR_t%$yvR2l+-*>IG_t|H| zIp;lgxhz?{axnk^OSZW>dI7)>pb32rmjk~hOH6{nuRssCJx<`azV1e+wYH(wW;y$8 z`l8qLkV8iv7EFKLl>f=&Xp5=UrDHpeZ1CE#%G}m^%a#+Sfv0_sy$`xJAJF;n+1r`< z`T1)VwP%vkjvPJez0d39)(o1tdC&eJZbC!O)$JLPrqe-9EKo!Dgx9X!0Eqsx&o2cP zCqXE%&Cz~O-1RT-cHSSqf6Vz%70$VT*D;?bGhz9czTA~OQT^dG5U}-~``FUeo?Bio zc`KcpumK_jR+2>k<}3hxy&M9oHi5zeePJq$`*2;t&j<|gR-XXecLE?0ng0j)zdrnL ztp~=UxkmeDtJP3=1OYf`KwvC__gntvWY+xZ0$0!-JqoMN2A~_9fwyqo(Zk;zwpz;# z2UpW}bu!H*M$&F_&fOPi2 zGy>DV8XmX>M8Y7$E+Dc!qwj5yj8&Il2eLw-L$dJ;PRQ)li}=15p^rt`daDR)Gl9AD zz!U;=$qATu1ZLs7PxT2Y`h;Yd%FVbEY1`&?zs5SvYB{T5E^P(oi) zToBF6yJI%*W{wc2>f=-}Gugm>D(PW9>O?G?^*9yy5DBZ^Dbea;7d4h7uh)jDq! zzRiy+Q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