generated from 2ndbeam/bevy-template
feat: Added metronome
This commit is contained in:
parent
5170b588b2
commit
fa2e968b4e
4 changed files with 1964 additions and 684 deletions
71
src/main.rs
71
src/main.rs
|
|
@ -1,54 +1,57 @@
|
|||
use bevy::prelude::*;
|
||||
use std::time::Duration;
|
||||
|
||||
use bevy::{audio::PlaybackMode, prelude::*};
|
||||
|
||||
fn duration_from_bpm(bpm: f32) -> Duration {
|
||||
Duration::from_millis((60_000f32 / bpm) as u64)
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct Person;
|
||||
|
||||
#[derive(Component)]
|
||||
struct Name(String);
|
||||
struct Metronome;
|
||||
|
||||
#[derive(Resource)]
|
||||
struct GreetTimer(Timer);
|
||||
struct MetronomeData {
|
||||
pub sound: Handle<AudioSource>,
|
||||
pub timer: Timer,
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
struct UpdateTimer(Timer);
|
||||
pub struct RhythmPlugin;
|
||||
|
||||
pub struct HelloPlugin;
|
||||
|
||||
impl Plugin for HelloPlugin {
|
||||
impl Plugin for RhythmPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.insert_resource(GreetTimer(Timer::from_seconds(2.0, TimerMode::Repeating)));
|
||||
app.insert_resource(UpdateTimer(Timer::from_seconds(10.0, TimerMode::Once)));
|
||||
app.add_systems(Startup, add_people);
|
||||
app.add_systems(Update, (update_people, greet_people).chain());
|
||||
app.add_systems(Startup, setup_metronome)
|
||||
.add_systems(Update, tick_metronome);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn add_people(mut commands: Commands) {
|
||||
commands.spawn((Person, Name("Alkesey Mirnekov".to_string())));
|
||||
commands.spawn((Person, Name("Alkesey Mirnekov 2".to_string())));
|
||||
commands.spawn((Person, Name("Alkesey Mirnekov 3".to_string())));
|
||||
fn setup_metronome(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
commands.insert_resource( MetronomeData {
|
||||
sound: asset_server.load("sfx/metronome.wav"),
|
||||
timer: Timer::new(duration_from_bpm(120f32), TimerMode::Repeating),
|
||||
});
|
||||
commands.spawn(Metronome);
|
||||
}
|
||||
|
||||
fn greet_people(time: Res<Time>, mut timer: ResMut<GreetTimer>, query: Query<&Name, With<Person>>) {
|
||||
if timer.0.tick(time.delta()).just_finished() {
|
||||
for name in &query {
|
||||
println!("hello {}!", name.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn update_people(time: Res<Time>, mut timer: ResMut<UpdateTimer>, mut query: Query<&mut Name, With<Person>>) {
|
||||
if timer.0.tick(time.delta()).just_finished() {
|
||||
for mut name in &mut query {
|
||||
name.0 = format!("{} II", name.0);
|
||||
}
|
||||
fn tick_metronome(
|
||||
mut commands: Commands,
|
||||
mut metronome_data: ResMut<MetronomeData>,
|
||||
time: Res<Time>,
|
||||
metronome: Single<Entity, With<Metronome>>,
|
||||
) {
|
||||
if metronome_data.timer.tick(time.delta()).just_finished() {
|
||||
commands.entity(*metronome).insert((
|
||||
AudioPlayer::new(metronome_data.sound.clone()),
|
||||
PlaybackSettings {
|
||||
mode: PlaybackMode::Remove,
|
||||
..default()
|
||||
}
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_plugins(HelloPlugin)
|
||||
.add_plugins(RhythmPlugin)
|
||||
.run();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue