feat: Added metronome

This commit is contained in:
Alexey 2026-02-01 15:26:14 +03:00
commit fa2e968b4e
4 changed files with 1964 additions and 684 deletions

2815
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,12 +1,12 @@
cargo-features = ["codegen-backend"]
[package]
name = "bevy_test"
name = "bevy_rhythm_demo"
version = "0.1.0"
edition = "2021"
edition = "2024"
[dependencies]
bevy = { version = "0.14.2", features = ["dynamic_linking"] }
bevy = { version = "0.18.0", features = ["dynamic_linking", "wav"] }
[profile.dev]
opt-level = 1

BIN
assets/sfx/metronome.wav Normal file

Binary file not shown.

View file

@ -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();
}