Skip to content
Snippets Groups Projects
spawning.rs 1.15 KiB
Newer Older
use crate::entities::motion::Velocity;
use crate::entities::Player;
Louis's avatar
Louis committed
use crate::system::Background;
use bevy::ecs::system::SystemParam;
use bevy::prelude::{AssetServer, Commands, Entity, Res, SpriteBundle, Transform, Vec2};

Louis's avatar
Louis committed
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>,
}

impl<'w, 's> EntitySpawner<'w, 's> {
	pub fn spawn_player_at(&mut self, position: Vec2) -> Entity {
		self.commands
			.spawn((
				SpriteBundle {
					texture: self.assets.load("sprites/ship.png"),
					transform: Transform::from_translation(position.extend(Z_PLAYER)),
					..Default::default()
				},
				Velocity::ZERO,
				Player,
			))
			.id()
	}
Louis's avatar
Louis committed

	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()
	}