Skip to content
Snippets Groups Projects
spawning.rs 792 B
Newer Older
use crate::entities::motion::Velocity;
use crate::entities::Player;
use bevy::ecs::system::SystemParam;
use bevy::prelude::{AssetServer, Commands, Entity, Res, SpriteBundle, Transform, Vec2};

#[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
			.spawn((
				SpriteBundle {
					texture: self.assets.load("sprites/ship.png"),
					transform: Transform::from_translation(position.extend(Z_PLAYER)),
					..Default::default()
				},
				Velocity::ZERO,
				Player,
			))
			.id()
	}
}