move all music data to struct, save this data now
This commit is contained in:
107
src/music.rs
Normal file
107
src/music.rs
Normal file
@@ -0,0 +1,107 @@
|
||||
use crate::utils::string_to_polygon;
|
||||
use crate::{polygon_draw::*, utils::string_to_color};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use kira::{
|
||||
AudioManager, AudioManagerSettings, DefaultBackend, sound::static_sound::StaticSoundData,
|
||||
};
|
||||
|
||||
use std::f32::consts::PI;
|
||||
use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Music {
|
||||
pub poly_frame: Vec<(f32, PolygonFrame)>,
|
||||
pub nb_sec_for_rev: f32,
|
||||
pub file_name: String,
|
||||
}
|
||||
|
||||
impl Music {
|
||||
fn find_poly_frame(&mut self, delta: f32) -> &mut PolygonFrame {
|
||||
if let Some(i) = self
|
||||
.poly_frame
|
||||
.windows(2)
|
||||
.position(|w| w[0].0 < delta && delta < w[1].0)
|
||||
{
|
||||
&mut self.poly_frame[i].1
|
||||
} else {
|
||||
&mut self.poly_frame[0].1
|
||||
}
|
||||
}
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~PUBLIC~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
pub fn current_frame(&self, delta: f32) -> &PolygonFrame {
|
||||
if let Some(i) = self
|
||||
.poly_frame
|
||||
.windows(2)
|
||||
.position(|w| w[0].0 < delta && delta < w[1].0)
|
||||
{
|
||||
&self.poly_frame[i].1
|
||||
} else {
|
||||
&self.poly_frame[0].1
|
||||
}
|
||||
}
|
||||
|
||||
pub fn default() -> Music {
|
||||
Music {
|
||||
poly_frame: vec![(0.0, PolygonFrame::default())],
|
||||
nb_sec_for_rev: 1.0,
|
||||
file_name: "Polymusic.json".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_frame(&mut self, delta: f32) {
|
||||
self.find_poly_frame(delta).update();
|
||||
}
|
||||
|
||||
pub fn apply_tick(&mut self, delta: f32, time_btw: Duration, audio_manager: &mut AudioManager) {
|
||||
let nb_sec_for_rev = self.nb_sec_for_rev;
|
||||
let current_frame = self.find_poly_frame(delta);
|
||||
let teta_temp = current_frame.teta;
|
||||
current_frame.teta +=
|
||||
2.0 * PI * (1.0 / nb_sec_for_rev) * (time_btw.as_millis() as f32 / 1_000.0);
|
||||
current_frame.teta %= 2.0 * PI;
|
||||
let sound_to_play = current_frame.all_sound_to_play_btw(teta_temp, current_frame.teta);
|
||||
for sound in sound_to_play {
|
||||
audio_manager
|
||||
.play(sound.clone())
|
||||
.expect("Error to play sound");
|
||||
}
|
||||
}
|
||||
|
||||
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~SET~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
pub fn set_sound(
|
||||
&mut self,
|
||||
delta: f32,
|
||||
index: usize,
|
||||
sound: StaticSoundData,
|
||||
sound_name: String,
|
||||
) {
|
||||
let current_frame = self.find_poly_frame(delta);
|
||||
current_frame.polygons[index].sound = sound;
|
||||
current_frame.polygons[index].sound_name = sound_name;
|
||||
}
|
||||
|
||||
pub fn set_teta(&mut self, delta: f32, index: usize, teta: f32) {
|
||||
self.find_poly_frame(delta).polygons[index].global_teta = teta;
|
||||
}
|
||||
|
||||
pub fn set_color(&mut self, delta: f32, index: usize, color_name: String) {
|
||||
let current_frame = self.find_poly_frame(delta);
|
||||
current_frame.polygons[index].color = string_to_color(&color_name);
|
||||
current_frame.polygons[index].color_name = color_name;
|
||||
}
|
||||
|
||||
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ADD/REMOVE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
pub fn add_polygon(&mut self, delta: f32, polygon_name: String) {
|
||||
let current_frame = self.find_poly_frame(delta);
|
||||
let poly = string_to_polygon(polygon_name);
|
||||
current_frame.polygons.push(poly);
|
||||
}
|
||||
|
||||
pub fn remove_polygon(&mut self, delta: f32, i: usize) {
|
||||
self.find_poly_frame(delta).polygons.remove(i);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user