diff --git a/prototypes/src/bin/life.rs b/prototypes/src/bin/life.rs index e7bf273..f23e7fc 100644 --- a/prototypes/src/bin/life.rs +++ b/prototypes/src/bin/life.rs @@ -275,15 +275,21 @@ impl Coordinates { fn as_cell_pitch(&self) -> CellPitch { - let note = match (self.x % 5) - 2 { - -2 => Note::A, - -1 => Note::B, - 0 => Note::CSharp, - 1 => Note::E, - 2 => Note::FSharp, + debug!("Generating cell pitch for {:?}", self); + + let note = match (self.x + 2).abs() % 5 { + 0 => Note::A, + 1 => Note::B, + 2 => Note::CSharp, + 3 => Note::E, + 4 => Note::FSharp, _ => panic!("This shouldn't happen!"), }; - let octave = (self.y % 5) - 2; + + let octave = ((self.y + 2).abs() % 5) - 2; + debug_assert!((-2..=2).contains(&octave)); + + debug!("\t{:?}", CellPitch { note: note.clone(), octave }); CellPitch { note, octave @@ -302,7 +308,8 @@ fn on_add_coordinates( // based on coordinates, get material, mesh, and tone from cell_assets if cells.contains(trigger.entity) { - let mat_h = cell_assets.materials.get(c).unwrap(); + 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); @@ -317,7 +324,6 @@ fn on_add_coordinates( }; commands.entity(trigger.entity).insert(t); - // FIXME!!! let p = cell_assets.pitches.get(&c.as_cell_pitch()).unwrap(); commands.entity(trigger.entity).insert((AudioPlayer(p.clone()), PlaybackSettings::LOOP)); } @@ -532,7 +538,7 @@ fn visualize_ui_state( #[derive(Resource, Default)] struct CellAssets { pitches: HashMap>, - materials: HashMap>, + materials: HashMap>, mesh: Handle, } @@ -601,6 +607,7 @@ fn load_audio_tones(mut pitches: ResMut>, mut cell_assets: ResMut< cell_assets.pitches.insert(cell_pitch, handle); }); }); + debug_assert!(cell_assets.pitches.iter().len() == 25); } fn load_materials( @@ -610,22 +617,22 @@ fn load_materials( ) { let vertical = [-2, -1, 0, 1, 2]; let horizontal = [-2, -1, 0, 1, 2]; - let texture = server.load(TILE); + let tile_handle = server.load(TILE); vertical.iter().for_each(|&x| { horizontal.iter().for_each(|&y| { let c = Coordinates { x, y }; - let color = c.as_color(); - cell_assets.materials.insert( - c, - materials.add(ColorMaterial { - texture: Some(texture.clone()), - color, - ..default() - }), - ); + let pitch = c.as_cell_pitch(); + let handle = { + let color = c.as_color(); + let texture = Some(tile_handle.clone()); + let color_material = ColorMaterial { color, texture, ..default() }; + materials.add(color_material) + }; + cell_assets.materials.insert(pitch, handle); }); }); + debug_assert!(cell_assets.materials.iter().len() == 25); } fn load_meshes(mut cell_assets: ResMut, mut meshes: ResMut>) {