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
2815
Cargo.lock
generated
2815
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -1,12 +1,12 @@
|
||||||
cargo-features = ["codegen-backend"]
|
cargo-features = ["codegen-backend"]
|
||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "bevy_test"
|
name = "bevy_rhythm_demo"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bevy = { version = "0.14.2", features = ["dynamic_linking"] }
|
bevy = { version = "0.18.0", features = ["dynamic_linking", "wav"] }
|
||||||
|
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
opt-level = 1
|
opt-level = 1
|
||||||
|
|
|
||||||
BIN
assets/sfx/metronome.wav
Normal file
BIN
assets/sfx/metronome.wav
Normal file
Binary file not shown.
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)]
|
#[derive(Component)]
|
||||||
struct Person;
|
struct Metronome;
|
||||||
|
|
||||||
#[derive(Component)]
|
|
||||||
struct Name(String);
|
|
||||||
|
|
||||||
#[derive(Resource)]
|
#[derive(Resource)]
|
||||||
struct GreetTimer(Timer);
|
struct MetronomeData {
|
||||||
|
pub sound: Handle<AudioSource>,
|
||||||
|
pub timer: Timer,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Resource)]
|
pub struct RhythmPlugin;
|
||||||
struct UpdateTimer(Timer);
|
|
||||||
|
|
||||||
pub struct HelloPlugin;
|
impl Plugin for RhythmPlugin {
|
||||||
|
|
||||||
impl Plugin for HelloPlugin {
|
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.insert_resource(GreetTimer(Timer::from_seconds(2.0, TimerMode::Repeating)));
|
app.add_systems(Startup, setup_metronome)
|
||||||
app.insert_resource(UpdateTimer(Timer::from_seconds(10.0, TimerMode::Once)));
|
.add_systems(Update, tick_metronome);
|
||||||
app.add_systems(Startup, add_people);
|
|
||||||
app.add_systems(Update, (update_people, greet_people).chain());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn setup_metronome(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
fn add_people(mut commands: Commands) {
|
commands.insert_resource( MetronomeData {
|
||||||
commands.spawn((Person, Name("Alkesey Mirnekov".to_string())));
|
sound: asset_server.load("sfx/metronome.wav"),
|
||||||
commands.spawn((Person, Name("Alkesey Mirnekov 2".to_string())));
|
timer: Timer::new(duration_from_bpm(120f32), TimerMode::Repeating),
|
||||||
commands.spawn((Person, Name("Alkesey Mirnekov 3".to_string())));
|
});
|
||||||
|
commands.spawn(Metronome);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn greet_people(time: Res<Time>, mut timer: ResMut<GreetTimer>, query: Query<&Name, With<Person>>) {
|
fn tick_metronome(
|
||||||
if timer.0.tick(time.delta()).just_finished() {
|
mut commands: Commands,
|
||||||
for name in &query {
|
mut metronome_data: ResMut<MetronomeData>,
|
||||||
println!("hello {}!", name.0);
|
time: Res<Time>,
|
||||||
}
|
metronome: Single<Entity, With<Metronome>>,
|
||||||
}
|
) {
|
||||||
}
|
if metronome_data.timer.tick(time.delta()).just_finished() {
|
||||||
|
commands.entity(*metronome).insert((
|
||||||
fn update_people(time: Res<Time>, mut timer: ResMut<UpdateTimer>, mut query: Query<&mut Name, With<Person>>) {
|
AudioPlayer::new(metronome_data.sound.clone()),
|
||||||
if timer.0.tick(time.delta()).just_finished() {
|
PlaybackSettings {
|
||||||
for mut name in &mut query {
|
mode: PlaybackMode::Remove,
|
||||||
name.0 = format!("{} II", name.0);
|
..default()
|
||||||
}
|
}
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.add_plugins(DefaultPlugins)
|
.add_plugins(DefaultPlugins)
|
||||||
.add_plugins(HelloPlugin)
|
.add_plugins(RhythmPlugin)
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue