From f53de2307c00d792af4c90f57ca15551b421bdf0 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Sat, 20 Jun 2026 22:28:33 -0700 Subject: [PATCH] Refactor cells to full bsn! macro --- life/src/main.rs | 55 ++++++++++++------------------------------------ 1 file changed, 13 insertions(+), 42 deletions(-) diff --git a/life/src/main.rs b/life/src/main.rs index 723a6ea..9f50416 100644 --- a/life/src/main.rs +++ b/life/src/main.rs @@ -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::.run_if(state_changed::), ) - .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, - coordinates: Query<&Coordinates>, - cells: Query<&Cell>, - mut commands: Commands, - cell_assets: Res, -) { - 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, @@ -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) -> 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, mut commands: Commands) { +fn cell_lifecycle(mut reader: MessageReader, mut commands: Commands, cell_assets: Res) { 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();