2022-05-18 18:04:58 +00:00
|
|
|
use std::f32;
|
|
|
|
|
|
|
|
use bevy::{asset::LoadState, prelude::*};
|
|
|
|
use bevy_synthizer::*;
|
|
|
|
|
|
|
|
#[derive(Component, Deref, DerefMut)]
|
|
|
|
struct RotationTimer(Timer);
|
|
|
|
|
|
|
|
impl Default for RotationTimer {
|
|
|
|
fn default() -> Self {
|
2022-12-06 22:43:37 +00:00
|
|
|
Self(Timer::from_seconds(30., TimerMode::Repeating))
|
2022-05-18 18:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-06 22:43:37 +00:00
|
|
|
#[derive(Resource, Default)]
|
2022-05-18 18:04:58 +00:00
|
|
|
struct AssetHandles {
|
|
|
|
sounds: Vec<HandleUntyped>,
|
|
|
|
loaded: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup(asset_server: Res<AssetServer>, mut handles: ResMut<AssetHandles>) {
|
|
|
|
handles.sounds = asset_server.load_folder(".").expect("Failed to load sfx");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load_and_create(
|
|
|
|
mut commands: Commands,
|
|
|
|
asset_server: Res<AssetServer>,
|
|
|
|
mut handles: ResMut<AssetHandles>,
|
|
|
|
) {
|
|
|
|
if handles.loaded {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
handles.loaded = asset_server
|
2023-03-06 20:24:23 +00:00
|
|
|
.get_group_load_state(handles.sounds.iter().map(|handle| handle.id()))
|
2022-05-18 18:04:58 +00:00
|
|
|
== LoadState::Loaded;
|
|
|
|
if handles.loaded {
|
2022-12-06 22:43:37 +00:00
|
|
|
commands.spawn((
|
|
|
|
TransformBundle::default(),
|
|
|
|
Listener,
|
|
|
|
RotationTimer::default(),
|
|
|
|
));
|
2022-05-18 18:04:58 +00:00
|
|
|
let handle = handles.sounds[0].clone();
|
|
|
|
let buffer = asset_server.get_handle(handle);
|
2022-12-06 22:43:37 +00:00
|
|
|
commands.spawn((
|
|
|
|
TransformBundle::from(Transform::from_translation(Vec3::new(10., 0., 0.))),
|
|
|
|
Source::default(),
|
|
|
|
Sound {
|
2023-03-06 19:59:53 +00:00
|
|
|
audio: buffer.into(),
|
2022-05-18 18:04:58 +00:00
|
|
|
looping: true,
|
|
|
|
..default()
|
2022-12-06 22:43:37 +00:00
|
|
|
},
|
|
|
|
));
|
2022-05-18 18:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rotate_listener(time: Res<Time>, mut query: Query<(&mut RotationTimer, &mut Transform)>) {
|
|
|
|
for (mut timer, mut transform) in query.iter_mut() {
|
|
|
|
timer.tick(time.delta());
|
2022-05-18 20:39:37 +00:00
|
|
|
let angle = f32::consts::PI * 2. * timer.percent();
|
|
|
|
transform.rotation = Quat::from_rotation_z(angle);
|
2022-05-18 18:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::new()
|
2023-07-16 18:24:12 +00:00
|
|
|
.add_plugins((
|
|
|
|
DefaultPlugins,
|
|
|
|
SynthizerPlugin {
|
|
|
|
default_panner_strategy: Some(bevy_synthizer::syz::PannerStrategy::Hrtf),
|
|
|
|
default_distance_model: Some(bevy_synthizer::syz::DistanceModel::Inverse),
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
))
|
2022-05-18 18:04:58 +00:00
|
|
|
.init_resource::<AssetHandles>()
|
2023-07-16 18:24:12 +00:00
|
|
|
.add_systems(Startup, setup)
|
|
|
|
.add_systems(
|
|
|
|
Update,
|
|
|
|
(bevy::window::close_on_esc, load_and_create, rotate_listener),
|
|
|
|
)
|
2022-05-18 18:04:58 +00:00
|
|
|
.run();
|
|
|
|
}
|