Refactor cells to full bsn! macro

main
Elijah Voigt 1 month ago
parent 774e8f11f9
commit f53de2307c

@ -1,4 +1,4 @@
use bevy::asset::{AssetPath, HandleTemplate};
use bevy::{asset::{AssetPath, HandleTemplate}, audio::AudioPlayerTemplate, mesh::Mesh2dTemplate, sprite_render::MeshMaterial2dTemplate};
use engine::*;
const TILE: &str = "tileGrey_01.png";
@ -73,7 +73,6 @@ fn main() {
Update,
toggle_state_entities::<AudioPlayback>.run_if(state_changed::<AudioPlayback>),
)
.add_observer(on_add_coordinates)
.run();
}
@ -117,9 +116,7 @@ impl UiButton {
}
}
fn ui_button(
button: UiButton,
) -> impl Scene {
fn ui_button(button: UiButton) -> impl Scene {
let color: Color = WHITE.with_alpha(0.5).into();
bsn! {
Button
@ -316,39 +313,6 @@ impl Coordinates {
}
}
fn on_add_coordinates(
trigger: On<Add, Coordinates>,
coordinates: Query<&Coordinates>,
cells: Query<&Cell>,
mut commands: Commands,
cell_assets: Res<CellAssets>,
) {
let c = coordinates.get(trigger.entity).unwrap();
// based on coordinates, get material, mesh, and tone from cell_assets
if cells.contains(trigger.entity) {
let pitch = c.as_cell_pitch();
let mat_h = cell_assets.materials.get(&pitch).unwrap();
let mesh_mat_2d = MeshMaterial2d(mat_h.clone());
commands.entity(trigger.entity).insert(mesh_mat_2d);
let mesh_2d = Mesh2d(cell_assets.mesh.clone());
commands.entity(trigger.entity).insert(mesh_2d);
};
let t = if cells.contains(trigger.entity) {
Transform::from_translation(c.as_translation() + Vec3::new(0.0, 0.0, 1.0))
} else {
Transform::from_translation(c.as_translation())
};
commands.entity(trigger.entity).insert(t);
let p = cell_assets.pitches.get(&c.as_cell_pitch()).unwrap();
commands
.entity(trigger.entity)
.insert((AudioPlayer(p.clone()), PlaybackSettings::ONCE));
}
/// When the cursor moves, update the Coordiantes
fn update_grid_position(
mut coordinates: ResMut<NextCoordinates>,
@ -512,19 +476,26 @@ enum Lifecycle {
Dead(Entity),
}
fn new_cell(Coordinates { x, y }: Coordinates) -> impl Scene {
fn new_cell(Coordinates { x, y }: Coordinates, cell_assets: &Res<CellAssets>) -> impl Scene {
let c = Coordinates { x, y };
bsn! {
Coordinates { x, y }
Cell
Coordinates { x, y }
PlaybackSettings::ONCE
template_value(Transform::from_translation(c.as_translation() + Vec3::new(0.0, 0.0, 1.0)))
template_value(AudioPlayerTemplate(cell_assets.pitches.get(&c.as_cell_pitch()).unwrap().clone().into()))
template_value(Mesh2dTemplate(cell_assets.mesh.clone().into()))
template_value(MeshMaterial2dTemplate(cell_assets.materials.get(&c.as_cell_pitch()).unwrap().clone().into()))
}
}
fn cell_lifecycle(mut reader: MessageReader<Lifecycle>, mut commands: Commands) {
fn cell_lifecycle(mut reader: MessageReader<Lifecycle>, mut commands: Commands, cell_assets: Res<CellAssets>) {
reader.read().for_each(|msg| match msg {
Lifecycle::Alive(c) => {
debug!("creating {:?}", c);
// TODO: Safeguard: ensure no other cell there?
commands.spawn_scene(new_cell(c.clone()));
commands.spawn_scene(new_cell(c.clone(), &cell_assets));
}
Lifecycle::Dead(e) => {
commands.entity(*e).despawn();

Loading…
Cancel
Save