prototypes/life: placing cells works again! now with color and sound!

main
Elijah Voigt 1 month ago
parent 314e604067
commit c731eeb49c

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

Loading…
Cancel
Save