Skip to content
Snippets Groups Projects
parallax.rs 878 B
Newer Older
use crate::entities::{BoxSize, EntitySpawner};
Louis's avatar
Louis committed
use crate::system::window_bounds;
use bevy::prelude::{Component, Query, Transform, Vec2, With};
Louis's avatar
Louis committed

#[derive(Component)]
pub struct Background;

// TODO: No Magic Numbers!
pub const BACKGROUND_WIDTH: f32 = 2110.0;
pub const BACKGROUND_HEIGHT: f32 = 300.0;
Louis's avatar
Louis committed

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,
	));
}

pub fn cycle_background_positions(
	mut background_query: Query<(&mut Transform, &BoxSize), With<Background>>,
) {
	for (mut transform, size) in &mut background_query {
		if transform.translation.x <= -(size.x / 2.0) {
			transform.translation.x = size.x + (size.x / 2.0);
		}
	}
}