sorta fixed loading cells, but now limited cellpitch is not respected...

main
Elijah Voigt 2 weeks ago
parent ee8a70cc16
commit bd1cb17c36

@ -1,3 +1,7 @@
#![allow(clippy::complexity)]
// Only because of FromTemplat macros unfortunately...
#![allow(dead_code)]
use bevy::{ use bevy::{
feathers::{containers::*, theme::ThemeProps}, feathers::{containers::*, theme::ThemeProps},
ui::{Checked, InteractionDisabled}, ui::{Checked, InteractionDisabled},
@ -590,6 +594,7 @@ fn new_cell(
sinks: &Query<Entity, With<AudioSink>>, sinks: &Query<Entity, With<AudioSink>>,
sim_state: &Res<State<SimulationPlayback>>, sim_state: &Res<State<SimulationPlayback>>,
audio_state: &Res<State<AudioPlayback>>, audio_state: &Res<State<AudioPlayback>>,
pitch_map: &Res<PitchMap>,
) -> impl Scene { ) -> impl Scene {
let c = Coordinates { x, y }; let c = Coordinates { x, y };
@ -605,14 +610,19 @@ fn new_cell(
let playback_settings = PlaybackSettings::ONCE.with_volume(volume); let playback_settings = PlaybackSettings::ONCE.with_volume(volume);
info!("using coordinates {:?}", c);
info!("cell_pitch: {:?}", pitch_map.coordinates_cell_pitch(&c));
info!("pitch map {:?}", pitch_map);
info!("cell assets materials: {:#?}", cell_assets.materials);
bsn! { bsn! {
Cell Cell
Coordinates { x, y } Coordinates { x, y }
template_value(playback_settings) template_value(playback_settings)
template_value(Transform::from_translation(c.as_translation() + Vec3::new(0.0, 0.0, 1.0))) template_value(Transform::from_translation(c.as_translation() + Vec3::new(0.0, 0.0, 1.0)))
template_value(Mesh2dTemplate(cell_assets.mesh.clone().into())) template_value(Mesh2dTemplate(cell_assets.mesh.clone().into()))
// TODO: template_value(AudioPlayerTemplate(cell_assets.pitches.get(&pitch_map.coordinates_cell_pitch(c)).unwrap().clone().into())) template_value(AudioPlayerTemplate(cell_assets.pitches.get(&pitch_map.coordinates_cell_pitch(&c)).unwrap().clone().into()))
// TODO: template_value(MeshMaterial2dTemplate(cell_assets.materials.get(&pitch_map.coordinates_cell_pitch(c)).unwrap().clone().into())) template_value(MeshMaterial2dTemplate(cell_assets.materials.get(&pitch_map.coordinates_cell_pitch(&c)).unwrap().clone().into()))
} }
} }
@ -623,6 +633,7 @@ fn cell_lifecycle(
query: Query<Entity, With<AudioSink>>, query: Query<Entity, With<AudioSink>>,
sim_state: Res<State<SimulationPlayback>>, sim_state: Res<State<SimulationPlayback>>,
audio_state: Res<State<AudioPlayback>>, audio_state: Res<State<AudioPlayback>>,
pitch_map: Res<PitchMap>,
) { ) {
reader.read().for_each(|msg| match msg { reader.read().for_each(|msg| match msg {
Lifecycle::Alive(c) => { Lifecycle::Alive(c) => {
@ -634,6 +645,7 @@ fn cell_lifecycle(
&query, &query,
&sim_state, &sim_state,
&audio_state, &audio_state,
&pitch_map,
)); ));
} }
Lifecycle::Dead(e) => { Lifecycle::Dead(e) => {
@ -657,7 +669,7 @@ fn cooldown_secs(input: f32) -> impl FnMut(Local<Timer>, Res<Time>) -> bool + Cl
// Scale grid with background? // Scale grid with background?
// Pan camera: click and drag // Pan camera: click and drag
#[derive(Resource, Default)] #[derive(Resource, Default, Debug)]
struct CellAssets { struct CellAssets {
pitches: HashMap<CellPitch, Handle<Pitch>>, pitches: HashMap<CellPitch, Handle<Pitch>>,
materials: HashMap<CellPitch, Handle<ColorMaterial>>, materials: HashMap<CellPitch, Handle<ColorMaterial>>,
@ -759,43 +771,45 @@ impl CellPitch {
..default() ..default()
}) })
} }
fn iter_all() -> impl Iterator<Item = Self> {
[
Note::A,
Note::ASharp,
Note::B,
Note::C,
Note::CSharp,
Note::D,
Note::DSharp,
Note::E,
Note::F,
Note::FSharp,
Note::G,
Note::GSharp,
].into_iter().flat_map(|note| {
(-5..6).map(move |octave| {
CellPitch {
note: note.clone(),
octave,
}
})
})
}
} }
fn load_audio_tones(mut pitches: ResMut<Assets<Pitch>>, mut cell_assets: ResMut<CellAssets>) { fn load_audio_tones(mut pitches: ResMut<Assets<Pitch>>, mut cell_assets: ResMut<CellAssets>) {
let notes = [ CellPitch::iter_all().for_each(|cell_pitch| {
Note::A, let frequency = cell_pitch.to_frequency();
Note::ASharp, let duration = Duration::from_secs(100);
Note::B, debug!(
Note::C, "Adding Pitch frequency: {:?} duration: {:?}",
Note::CSharp, frequency, duration
Note::D, );
Note::DSharp, let handle = pitches.add(Pitch {
Note::E, frequency,
Note::F, duration,
Note::FSharp,
Note::G,
Note::GSharp,
];
let octaves = -5..6;
octaves.for_each(|octave| {
notes.iter().for_each(|note| {
let cell_pitch = CellPitch {
note: note.clone(),
octave,
};
let frequency = cell_pitch.to_frequency();
let duration = Duration::from_secs(100);
debug!(
"Adding Pitch frequency: {:?} duration: {:?}",
frequency, duration
);
let handle = pitches.add(Pitch {
frequency,
duration,
});
cell_assets.pitches.insert(cell_pitch, handle);
}); });
cell_assets.pitches.insert(cell_pitch, handle);
}); });
debug_assert_eq!(cell_assets.pitches.iter().len(), 132); debug_assert_eq!(cell_assets.pitches.iter().len(), 132);
} }
@ -831,32 +845,20 @@ fn load_materials(
mut cell_assets: ResMut<CellAssets>, mut cell_assets: ResMut<CellAssets>,
mut materials: ResMut<Assets<ColorMaterial>>, mut materials: ResMut<Assets<ColorMaterial>>,
) { ) {
// Up to 11 octaves total
let vertical = -5..6;
debug_assert_eq!(vertical.len(), 11);
// 12 total colors supported
let horizontal = -6..6;
debug_assert_eq!(horizontal.len(), 12);
let tile_handle = server.load(TILE); let tile_handle = server.load(TILE);
vertical.for_each(|x| { CellPitch::iter_all().for_each(|cell_pitch| {
horizontal.clone().for_each(|y| { let handle = {
let c = Coordinates { x, y }; let color = cell_pitch.to_color();
let pitch = c.to_cell_pitch(); let texture = Some(tile_handle.clone());
let handle = { let color_material = ColorMaterial {
let color = pitch.to_color(); color,
let texture = Some(tile_handle.clone()); texture,
let color_material = ColorMaterial { ..default()
color,
texture,
..default()
};
materials.add(color_material)
}; };
cell_assets.materials.insert(pitch, handle); materials.add(color_material)
}); };
cell_assets.materials.insert(cell_pitch, handle);
}); });
debug_assert_eq!(cell_assets.materials.iter().len(), 132); debug_assert_eq!(cell_assets.materials.iter().len(), 132);
} }
@ -945,7 +947,7 @@ fn toggle_interactive<T: Component + States + PartialEq>(
}) })
} }
#[derive(Resource)] #[derive(Resource, Debug)]
struct PitchMap { struct PitchMap {
notes: Vec<Note>, notes: Vec<Note>,
octaves_up: usize, octaves_up: usize,

Loading…
Cancel
Save