spawning.rs 1.72 KiB
use crate::entities::motion::Velocity;
use crate::entities::{BoxSize, CollisionGroup, Player};
use crate::system::{Background, BACKGROUND_HEIGHT, BACKGROUND_WIDTH};
use bevy::ecs::system::SystemParam;
use bevy::prelude::{AssetServer, Commands, Entity, Res, Sprite, 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>,
}
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()
}
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,
BoxSize::from(Vec2::new(BACKGROUND_WIDTH, BACKGROUND_HEIGHT)),
))
.id()
}
pub fn spawn_alien_at(&mut self, position: Vec2) -> Entity {
self.commands
.spawn((
SpriteBundle {
texture: self.assets.load("sprites/alien_ship.png"),
transform: Transform::from_translation(position.extend(Z_ENEMY)),
sprite: Sprite {
flip_x: true,
..Default::default()
},
..Default::default()
},
BoxSize::from(Vec2::new(50.0, 25.0)),
CollisionGroup::Enemy,
))
.id()
}
}