Vendor dependencies for 0.3.0 release

This commit is contained in:
2025-09-27 10:29:08 -05:00
parent 0c8d39d483
commit 82ab7f317b
26803 changed files with 16134934 additions and 0 deletions

17
vendor/bevy/examples/audio/audio.rs vendored Normal file
View File

@@ -0,0 +1,17 @@
//! This example illustrates how to load and play an audio file.
//! For loading additional audio formats, you can enable the corresponding feature for that audio format.
use bevy::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.run();
}
fn setup(asset_server: Res<AssetServer>, mut commands: Commands) {
commands.spawn(AudioPlayer::new(
asset_server.load("sounds/Windless Slopes.ogg"),
));
}

View File

@@ -0,0 +1,86 @@
//! This example illustrates how to load and play an audio file, and control how it's played.
use bevy::{audio::Volume, math::ops, prelude::*};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, (update_speed, pause, mute, volume))
.run();
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn((
AudioPlayer::new(asset_server.load("sounds/Windless Slopes.ogg")),
MyMusic,
));
// example instructions
commands.spawn((
Text::new("-/=: Volume Down/Up\nSpace: Toggle Playback\nM: Toggle Mute"),
Node {
position_type: PositionType::Absolute,
bottom: Val::Px(12.0),
left: Val::Px(12.0),
..default()
},
));
// camera
commands.spawn(Camera3d::default());
}
#[derive(Component)]
struct MyMusic;
fn update_speed(music_controller: Query<&AudioSink, With<MyMusic>>, time: Res<Time>) {
let Ok(sink) = music_controller.single() else {
return;
};
sink.set_speed((ops::sin(time.elapsed_secs() / 5.0) + 1.0).max(0.1));
}
fn pause(
keyboard_input: Res<ButtonInput<KeyCode>>,
music_controller: Query<&AudioSink, With<MyMusic>>,
) {
let Ok(sink) = music_controller.single() else {
return;
};
if keyboard_input.just_pressed(KeyCode::Space) {
sink.toggle_playback();
}
}
fn mute(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut music_controller: Query<&mut AudioSink, With<MyMusic>>,
) {
let Ok(mut sink) = music_controller.single_mut() else {
return;
};
if keyboard_input.just_pressed(KeyCode::KeyM) {
sink.toggle_mute();
}
}
fn volume(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut music_controller: Query<&mut AudioSink, With<MyMusic>>,
) {
let Ok(mut sink) = music_controller.single_mut() else {
return;
};
if keyboard_input.just_pressed(KeyCode::Equal) {
let current_volume = sink.volume();
sink.set_volume(current_volume + Volume::Linear(0.1));
} else if keyboard_input.just_pressed(KeyCode::Minus) {
let current_volume = sink.volume();
sink.set_volume(current_volume - Volume::Linear(0.1));
}
}

103
vendor/bevy/examples/audio/decodable.rs vendored Normal file
View File

@@ -0,0 +1,103 @@
//! Shows how to create a custom [`Decodable`] type by implementing a Sine wave.
use bevy::{
audio::{AddAudioSource, AudioPlugin, Source, Volume},
math::ops,
prelude::*,
reflect::TypePath,
};
use core::time::Duration;
// This struct usually contains the data for the audio being played.
// This is where data read from an audio file would be stored, for example.
// This allows the type to be registered as an asset.
#[derive(Asset, TypePath)]
struct SineAudio {
frequency: f32,
}
// This decoder is responsible for playing the audio,
// and so stores data about the audio being played.
struct SineDecoder {
// how far along one period the wave is (between 0 and 1)
current_progress: f32,
// how much we move along the period every frame
progress_per_frame: f32,
// how long a period is
period: f32,
sample_rate: u32,
}
impl SineDecoder {
fn new(frequency: f32) -> Self {
// standard sample rate for most recordings
let sample_rate = 44_100;
SineDecoder {
current_progress: 0.,
progress_per_frame: frequency / sample_rate as f32,
period: std::f32::consts::PI * 2.,
sample_rate,
}
}
}
// The decoder must implement iterator so that it can implement `Decodable`.
impl Iterator for SineDecoder {
type Item = f32;
fn next(&mut self) -> Option<Self::Item> {
self.current_progress += self.progress_per_frame;
// we loop back round to 0 to avoid floating point inaccuracies
self.current_progress %= 1.;
Some(ops::sin(self.period * self.current_progress))
}
}
// `Source` is what allows the audio source to be played by bevy.
// This trait provides information on the audio.
impl Source for SineDecoder {
fn current_frame_len(&self) -> Option<usize> {
None
}
fn channels(&self) -> u16 {
1
}
fn sample_rate(&self) -> u32 {
self.sample_rate
}
fn total_duration(&self) -> Option<Duration> {
None
}
}
// Finally `Decodable` can be implemented for our `SineAudio`.
impl Decodable for SineAudio {
type DecoderItem = <SineDecoder as Iterator>::Item;
type Decoder = SineDecoder;
fn decoder(&self) -> Self::Decoder {
SineDecoder::new(self.frequency)
}
}
fn main() {
let mut app = App::new();
// register the audio source so that it can be used
app.add_plugins(DefaultPlugins.set(AudioPlugin {
global_volume: Volume::Linear(0.2).into(),
..default()
}))
.add_audio_source::<SineAudio>()
.add_systems(Startup, setup)
.run();
}
fn setup(mut assets: ResMut<Assets<SineAudio>>, mut commands: Commands) {
// add a `SineAudio` to the asset server so that it can be played
let audio_handle = assets.add(SineAudio {
frequency: 440., // this is the frequency of A4
});
commands.spawn(AudioPlayer(audio_handle));
}

55
vendor/bevy/examples/audio/pitch.rs vendored Normal file
View File

@@ -0,0 +1,55 @@
//! This example illustrates how to play a single-frequency sound (aka a pitch)
use bevy::prelude::*;
use std::time::Duration;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_event::<PlayPitch>()
.add_systems(Startup, setup)
.add_systems(Update, (play_pitch, keyboard_input_system))
.run();
}
#[derive(Event, Default)]
struct PlayPitch;
#[derive(Resource)]
struct PitchFrequency(f32);
fn setup(mut commands: Commands) {
commands.insert_resource(PitchFrequency(220.0));
}
fn play_pitch(
mut pitch_assets: ResMut<Assets<Pitch>>,
frequency: Res<PitchFrequency>,
mut events: EventReader<PlayPitch>,
mut commands: Commands,
) {
for _ in events.read() {
info!("playing pitch with frequency: {}", frequency.0);
commands.spawn((
AudioPlayer(pitch_assets.add(Pitch::new(frequency.0, Duration::new(1, 0)))),
PlaybackSettings::DESPAWN,
));
info!("number of pitch assets: {}", pitch_assets.len());
}
}
fn keyboard_input_system(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut frequency: ResMut<PitchFrequency>,
mut events: EventWriter<PlayPitch>,
) {
if keyboard_input.just_pressed(KeyCode::ArrowUp) {
frequency.0 *= ops::powf(2.0f32, 1.0 / 12.0);
}
if keyboard_input.just_pressed(KeyCode::ArrowDown) {
frequency.0 /= ops::powf(2.0f32, 1.0 / 12.0);
}
if keyboard_input.just_pressed(KeyCode::Space) {
events.write(PlayPitch);
}
}

155
vendor/bevy/examples/audio/soundtrack.rs vendored Normal file
View File

@@ -0,0 +1,155 @@
//! This example illustrates how to load and play different soundtracks,
//! transitioning between them as the game state changes.
use bevy::{audio::Volume, prelude::*};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, (cycle_game_state, fade_in, fade_out))
.add_systems(Update, change_track)
.run();
}
// This resource simulates game states
#[derive(Resource, Default)]
enum GameState {
#[default]
Peaceful,
Battle,
}
// This timer simulates game state changes
#[derive(Resource)]
struct GameStateTimer(Timer);
// This resource will hold the track list for your soundtrack
#[derive(Resource)]
struct SoundtrackPlayer {
track_list: Vec<Handle<AudioSource>>,
}
impl SoundtrackPlayer {
fn new(track_list: Vec<Handle<AudioSource>>) -> Self {
Self { track_list }
}
}
// This component will be attached to an entity to fade the audio in
#[derive(Component)]
struct FadeIn;
// This component will be attached to an entity to fade the audio out
#[derive(Component)]
struct FadeOut;
fn setup(asset_server: Res<AssetServer>, mut commands: Commands) {
// Instantiate the game state resources
commands.insert_resource(GameState::default());
commands.insert_resource(GameStateTimer(Timer::from_seconds(
10.0,
TimerMode::Repeating,
)));
// Create the track list
let track_1 = asset_server.load::<AudioSource>("sounds/Mysterious acoustic guitar.ogg");
let track_2 = asset_server.load::<AudioSource>("sounds/Epic orchestra music.ogg");
let track_list = vec![track_1, track_2];
commands.insert_resource(SoundtrackPlayer::new(track_list));
}
// Every time the GameState resource changes, this system is run to trigger the song change.
fn change_track(
mut commands: Commands,
soundtrack_player: Res<SoundtrackPlayer>,
soundtrack: Query<Entity, With<AudioSink>>,
game_state: Res<GameState>,
) {
if game_state.is_changed() {
// Fade out all currently running tracks
for track in soundtrack.iter() {
commands.entity(track).insert(FadeOut);
}
// Spawn a new `AudioPlayer` with the appropriate soundtrack based on
// the game state.
//
// Volume is set to start at zero and is then increased by the fade_in system.
match game_state.as_ref() {
GameState::Peaceful => {
commands.spawn((
AudioPlayer(soundtrack_player.track_list.first().unwrap().clone()),
PlaybackSettings {
mode: bevy::audio::PlaybackMode::Loop,
volume: Volume::SILENT,
..default()
},
FadeIn,
));
}
GameState::Battle => {
commands.spawn((
AudioPlayer(soundtrack_player.track_list.get(1).unwrap().clone()),
PlaybackSettings {
mode: bevy::audio::PlaybackMode::Loop,
volume: Volume::SILENT,
..default()
},
FadeIn,
));
}
}
}
}
// Fade effect duration
const FADE_TIME: f32 = 2.0;
// Fades in the audio of entities that has the FadeIn component. Removes the FadeIn component once
// full volume is reached.
fn fade_in(
mut commands: Commands,
mut audio_sink: Query<(&mut AudioSink, Entity), With<FadeIn>>,
time: Res<Time>,
) {
for (mut audio, entity) in audio_sink.iter_mut() {
let current_volume = audio.volume();
audio.set_volume(current_volume + Volume::Linear(time.delta_secs() / FADE_TIME));
if audio.volume().to_linear() >= 1.0 {
audio.set_volume(Volume::Linear(1.0));
commands.entity(entity).remove::<FadeIn>();
}
}
}
// Fades out the audio of entities that has the FadeOut component. Despawns the entities once audio
// volume reaches zero.
fn fade_out(
mut commands: Commands,
mut audio_sink: Query<(&mut AudioSink, Entity), With<FadeOut>>,
time: Res<Time>,
) {
for (mut audio, entity) in audio_sink.iter_mut() {
let current_volume = audio.volume();
audio.set_volume(current_volume - Volume::Linear(time.delta_secs() / FADE_TIME));
if audio.volume().to_linear() <= 0.0 {
commands.entity(entity).despawn();
}
}
}
// Every time the timer ends, switches between the "Peaceful" and "Battle" state.
fn cycle_game_state(
mut timer: ResMut<GameStateTimer>,
mut game_state: ResMut<GameState>,
time: Res<Time>,
) {
timer.0.tick(time.delta());
if timer.0.just_finished() {
match game_state.as_ref() {
GameState::Battle => *game_state = GameState::Peaceful,
GameState::Peaceful => *game_state = GameState::Battle,
}
}
}

View File

@@ -0,0 +1,125 @@
//! This example illustrates how to load and play an audio file, and control where the sounds seems to come from.
use bevy::{
audio::{AudioPlugin, SpatialScale},
color::palettes::css::*,
prelude::*,
time::Stopwatch,
};
/// Spatial audio uses the distance to attenuate the sound volume. In 2D with the default camera,
/// 1 pixel is 1 unit of distance, so we use a scale so that 100 pixels is 1 unit of distance for
/// audio.
const AUDIO_SCALE: f32 = 1. / 100.0;
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(AudioPlugin {
default_spatial_scale: SpatialScale::new_2d(AUDIO_SCALE),
..default()
}))
.add_systems(Startup, setup)
.add_systems(Update, update_emitters)
.add_systems(Update, update_listener)
.run();
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
asset_server: Res<AssetServer>,
) {
// Space between the two ears
let gap = 400.0;
// sound emitter
commands.spawn((
Mesh2d(meshes.add(Circle::new(15.0))),
MeshMaterial2d(materials.add(Color::from(BLUE))),
Transform::from_translation(Vec3::new(0.0, 50.0, 0.0)),
Emitter::default(),
AudioPlayer::new(asset_server.load("sounds/Windless Slopes.ogg")),
PlaybackSettings::LOOP.with_spatial(true),
));
let listener = SpatialListener::new(gap);
commands.spawn((
Transform::default(),
Visibility::default(),
listener.clone(),
children![
// left ear
(
Sprite::from_color(RED, Vec2::splat(20.0)),
Transform::from_xyz(-gap / 2.0, 0.0, 0.0),
),
// right ear
(
Sprite::from_color(LIME, Vec2::splat(20.0)),
Transform::from_xyz(gap / 2.0, 0.0, 0.0),
)
],
));
// example instructions
commands.spawn((
Text::new("Up/Down/Left/Right: Move Listener\nSpace: Toggle Emitter Movement"),
Node {
position_type: PositionType::Absolute,
bottom: Val::Px(12.0),
left: Val::Px(12.0),
..default()
},
));
// camera
commands.spawn(Camera2d);
}
#[derive(Component, Default)]
struct Emitter {
stopwatch: Stopwatch,
}
fn update_emitters(
time: Res<Time>,
mut emitters: Query<(&mut Transform, &mut Emitter), With<Emitter>>,
keyboard: Res<ButtonInput<KeyCode>>,
) {
for (mut emitter_transform, mut emitter) in emitters.iter_mut() {
if keyboard.just_pressed(KeyCode::Space) {
if emitter.stopwatch.is_paused() {
emitter.stopwatch.unpause();
} else {
emitter.stopwatch.pause();
}
}
emitter.stopwatch.tick(time.delta());
if !emitter.stopwatch.is_paused() {
emitter_transform.translation.x = ops::sin(emitter.stopwatch.elapsed_secs()) * 500.0;
}
}
}
fn update_listener(
keyboard: Res<ButtonInput<KeyCode>>,
time: Res<Time>,
mut listener: Single<&mut Transform, With<SpatialListener>>,
) {
let speed = 200.;
if keyboard.pressed(KeyCode::ArrowRight) {
listener.translation.x += speed * time.delta_secs();
}
if keyboard.pressed(KeyCode::ArrowLeft) {
listener.translation.x -= speed * time.delta_secs();
}
if keyboard.pressed(KeyCode::ArrowUp) {
listener.translation.y += speed * time.delta_secs();
}
if keyboard.pressed(KeyCode::ArrowDown) {
listener.translation.y -= speed * time.delta_secs();
}
}

View File

@@ -0,0 +1,139 @@
//! This example illustrates how to load and play an audio file, and control where the sounds seems to come from.
use bevy::{
color::palettes::basic::{BLUE, LIME, RED},
prelude::*,
time::Stopwatch,
};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, update_positions)
.add_systems(Update, update_listener)
.add_systems(Update, mute)
.run();
}
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// Space between the two ears
let gap = 4.0;
// sound emitter
commands.spawn((
Mesh3d(meshes.add(Sphere::new(0.2).mesh().uv(32, 18))),
MeshMaterial3d(materials.add(Color::from(BLUE))),
Transform::from_xyz(0.0, 0.0, 0.0),
Emitter::default(),
AudioPlayer::new(asset_server.load("sounds/Windless Slopes.ogg")),
PlaybackSettings::LOOP.with_spatial(true),
));
let listener = SpatialListener::new(gap);
commands.spawn((
Transform::default(),
Visibility::default(),
listener.clone(),
children![
// left ear indicator
(
Mesh3d(meshes.add(Cuboid::new(0.2, 0.2, 0.2))),
MeshMaterial3d(materials.add(Color::from(RED))),
Transform::from_translation(listener.left_ear_offset),
),
// right ear indicator
(
Mesh3d(meshes.add(Cuboid::new(0.2, 0.2, 0.2))),
MeshMaterial3d(materials.add(Color::from(LIME))),
Transform::from_translation(listener.right_ear_offset),
)
],
));
// light
commands.spawn((
DirectionalLight::default(),
Transform::from_xyz(4.0, 8.0, 4.0).looking_at(Vec3::ZERO, Vec3::Y),
));
// example instructions
commands.spawn((
Text::new(
"Up/Down/Left/Right: Move Listener\nSpace: Toggle Emitter Movement\nM: Toggle Mute",
),
Node {
position_type: PositionType::Absolute,
bottom: Val::Px(12.0),
left: Val::Px(12.0),
..default()
},
));
// camera
commands.spawn((
Camera3d::default(),
Transform::from_xyz(0.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
));
}
#[derive(Component, Default)]
struct Emitter {
stopwatch: Stopwatch,
}
fn update_positions(
time: Res<Time>,
mut emitters: Query<(&mut Transform, &mut Emitter), With<Emitter>>,
keyboard: Res<ButtonInput<KeyCode>>,
) {
for (mut emitter_transform, mut emitter) in emitters.iter_mut() {
if keyboard.just_pressed(KeyCode::Space) {
if emitter.stopwatch.is_paused() {
emitter.stopwatch.unpause();
} else {
emitter.stopwatch.pause();
}
}
emitter.stopwatch.tick(time.delta());
if !emitter.stopwatch.is_paused() {
emitter_transform.translation.x = ops::sin(emitter.stopwatch.elapsed_secs()) * 3.0;
emitter_transform.translation.z = ops::cos(emitter.stopwatch.elapsed_secs()) * 3.0;
}
}
}
fn update_listener(
keyboard: Res<ButtonInput<KeyCode>>,
time: Res<Time>,
mut listeners: Single<&mut Transform, With<SpatialListener>>,
) {
let speed = 2.;
if keyboard.pressed(KeyCode::ArrowRight) {
listeners.translation.x += speed * time.delta_secs();
}
if keyboard.pressed(KeyCode::ArrowLeft) {
listeners.translation.x -= speed * time.delta_secs();
}
if keyboard.pressed(KeyCode::ArrowDown) {
listeners.translation.z += speed * time.delta_secs();
}
if keyboard.pressed(KeyCode::ArrowUp) {
listeners.translation.z -= speed * time.delta_secs();
}
}
fn mute(keyboard_input: Res<ButtonInput<KeyCode>>, mut sinks: Query<&mut SpatialAudioSink>) {
if keyboard_input.just_pressed(KeyCode::KeyM) {
for mut sink in sinks.iter_mut() {
sink.toggle_mute();
}
}
}