From d7df3ebaee3593a7dc0dd9141c711d7a4cec6938 Mon Sep 17 00:00:00 2001
From: Louis Capitanchik <contact@louiscap.co>
Date: Sun, 2 Jul 2023 11:46:11 +0100
Subject: [PATCH] Render moving background

---
 src/entities/spawning.rs | 25 ++++++++++++++++++++-----
 src/system/mod.rs        |  5 ++++-
 src/system/parallax.rs   | 19 +++++++++++++++++++
 3 files changed, 43 insertions(+), 6 deletions(-)
 create mode 100644 src/system/parallax.rs

diff --git a/src/entities/spawning.rs b/src/entities/spawning.rs
index 27d3422..0c8c7a5 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 1a93ae6..b405660 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 0000000..b064555
--- /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,
+	));
+}
-- 
GitLab