Skip to content
Snippets Groups Projects
Verified Commit d7df3eba authored by Louis's avatar Louis :fire:
Browse files

Render moving background

parent f66ac5d6
No related branches found
No related tags found
No related merge requests found
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()
}
}
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;
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,
));
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment