diff --git a/src/entities/spawning.rs b/src/entities/spawning.rs index 27d3422be0e57cb5a49ce8b267982b1b47b3930e..0c8c7a5e3dab7617464d07992a118c31e84a092d 100644 --- a/src/entities/spawning.rs +++ b/src/entities/spawning.rs @@ -1,19 +1,20 @@ use crate::entities::motion::Velocity; use crate::entities::Player; +use crate::system::Background; use bevy::ecs::system::SystemParam; use bevy::prelude::{AssetServer, Commands, Entity, Res, SpriteBundle, Transform, Vec2}; +static Z_PLAYER: f32 = 300.0; +static Z_ITEMS: f32 = 200.0; +static Z_ENEMY: f32 = 250.0; +static Z_BACKGROUND: f32 = 50.0; + #[derive(SystemParam)] pub struct EntitySpawner<'w, 's> { commands: Commands<'w, 's>, assets: Res<'w, AssetServer>, } -static Z_PLAYER: f32 = 300.0; -static Z_ITEMS: f32 = 200.0; -static Z_ENEMY: f32 = 250.0; -static Z_BACKGROUND: f32 = 50.0; - impl<'w, 's> EntitySpawner<'w, 's> { pub fn spawn_player_at(&mut self, position: Vec2) -> Entity { self.commands @@ -28,4 +29,18 @@ impl<'w, 's> EntitySpawner<'w, 's> { )) .id() } + + pub fn spawn_background_at(&mut self, position: Vec2) -> Entity { + self.commands + .spawn(( + SpriteBundle { + texture: self.assets.load("sprites/background.png"), + transform: Transform::from_translation(position.extend(Z_BACKGROUND)), + ..Default::default() + }, + Velocity::from(Vec2::new(-75.0, 0.0)), + Background, + )) + .id() + } } diff --git a/src/system/mod.rs b/src/system/mod.rs index 1a93ae621aeefd6fa2de44d8beb98ab5578e2211..b4056606d6b269fb30a634d26cf52d2647322bfb 100644 --- a/src/system/mod.rs +++ b/src/system/mod.rs @@ -1,4 +1,5 @@ mod camera; +mod parallax; mod window; mod _plugin { @@ -8,12 +9,14 @@ mod _plugin { pub struct SystemPlugin; impl Plugin for SystemPlugin { fn build(&self, app: &mut App) { - app.add_startup_system(super::camera::spawn_2d_camera); + app.add_startup_system(super::camera::spawn_2d_camera) + .add_startup_system(super::parallax::spawn_backgrounds); } } } pub use camera::default_projection; +pub use parallax::Background; pub use window::{window_bounds, VIEWPORT_HEIGHT, VIEWPORT_WIDTH}; pub use _plugin::SystemPlugin; diff --git a/src/system/parallax.rs b/src/system/parallax.rs new file mode 100644 index 0000000000000000000000000000000000000000..b06455519dc57a13584a4f2cdd55ac2f95c6a016 --- /dev/null +++ b/src/system/parallax.rs @@ -0,0 +1,19 @@ +use crate::entities::EntitySpawner; +use crate::system::window_bounds; +use bevy::prelude::{Component, Vec2}; + +#[derive(Component)] +pub struct Background; + +// TODO: No Magic Numbers! +const BACKGROUND_WIDTH: f32 = 2110.0; + +pub fn spawn_backgrounds(mut spawner: EntitySpawner) { + let bounds = window_bounds(); + spawner.spawn_background_at(Vec2::new(BACKGROUND_WIDTH / 2.0, bounds.height() / 2.0)); + + spawner.spawn_background_at(Vec2::new( + (BACKGROUND_WIDTH / 2.0) + BACKGROUND_WIDTH, + bounds.height() / 2.0, + )); +}