bevy-playground/src/statue.rs

63 lines
1.5 KiB
Rust

use crate::movement::YSortable;
use crate::prelude::*;
#[derive(Component, Debug, Default)]
pub struct Statue;
#[derive(Bundle, Default)]
pub struct StatueBundle {
gameobject: Gameobject,
statue: Statue,
ysort: YSortable,
sprite: SpriteBundle,
texture: TextureAtlas,
}
pub fn startup(mut events: EventWriter<SpawnStatueEvent>) {
events.send_batch([
SpawnStatueEvent(Vec3::new(50., 50., 0.)),
SpawnStatueEvent(Vec3::new(50., 100., 0.)),
]);
}
#[derive(Event)]
pub struct SpawnStatueEvent(Vec3);
pub fn spawn_statue(
mut events: EventReader<SpawnStatueEvent>,
mut commands: Commands,
assets: Res<AssetServer>,
mut layouts: ResMut<Assets<TextureAtlasLayout>>,
) {
// TODO: rebuilding this layout each spawn is surely not correct?
let layout = layouts.add(TextureAtlasLayout::from_grid(
UVec2::new(40, 74),
1,
1,
None,
Some(UVec2::new(443, 20)),
));
for ev in events.read() {
let bundle = StatueBundle {
sprite: SpriteBundle {
transform: Transform::from_translation(ev.0),
texture: assets.load("img/Props.png"),
..default()
},
texture: TextureAtlas {
layout: layout.clone(),
..default()
},
..default()
};
commands.spawn(bundle);
}
}
pub fn exit(mut commands: Commands, q: Query<Entity, With<Statue>>) {
for id in q.iter() {
commands.entity(id).despawn_recursive();
}
}