Work on adding sound playback

This commit is contained in:
Daniel Flanagan 2023-12-24 21:30:18 -06:00
parent dbc197f462
commit c54d393d00
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
7 changed files with 73 additions and 43 deletions

BIN
assets/sfx/meow.wav Normal file

Binary file not shown.

View file

@ -17,6 +17,11 @@ the heavy-lifting for me _plus_ it's a fun excuse to write more Rust.
- [X] Movement
- [X] Flipping sprites
- [X] Camera
- [ ] SFX
- Examples:
- https://github.com/bevyengine/bevy/blob/main/examples/audio/audio.rs
- https://github.com/bevyengine/bevy/blob/main/examples/audio/audio_control.rs
- https://github.com/bevyengine/bevy/blob/main/examples/audio/spatial_audio_2d.rs
- [ ] Tile system
- [ ] Basic map
- [ ] Enemies

View file

@ -2,7 +2,8 @@ use bevy::prelude::*;
#[derive(Resource, Debug, Default)]
pub struct AssetLoader {
pub sprites: Sprites,
pub images: Sprites,
pub sounds: Sounds,
}
#[derive(Resource, Debug, Default)]
@ -11,6 +12,11 @@ pub struct Sprites {
pub statue: Handle<TextureAtlas>,
}
#[derive(Resource, Debug, Default)]
pub struct Sounds {
pub meow: Handle<AudioSource>,
}
pub struct AssetPlugin;
impl Plugin for AssetPlugin {
@ -49,9 +55,13 @@ fn load_assets(
let statue_atlas_handle = texture_atlases.add(statue_atlas);
*assets = AssetLoader {
sprites: Sprites {
images: Sprites {
player: player_atlas_handle,
statue: statue_atlas_handle,
},
sounds: Sounds {
meow: asset_server.load("sfx/meow.wav"),
},
};
}

View file

@ -1,3 +1,4 @@
use bevy::audio::{AudioPlugin, SpatialScale};
use bevy::prelude::*;
mod assets;
@ -8,6 +9,7 @@ mod statue;
fn main() {
let mut app = App::new();
app.add_plugins(
DefaultPlugins
.set(WindowPlugin {
@ -18,6 +20,10 @@ fn main() {
}),
..Default::default()
})
.set(AudioPlugin {
spatial_scale: SpatialScale::new_2d(100.),
..default()
})
.set(ImagePlugin::default_nearest()),
)
.add_plugins((
@ -27,7 +33,6 @@ fn main() {
player::Player,
movement::Movement,
))
.add_systems(Update, keyboard_input_system)
.insert_resource(ClearColor(Color::rgb(0.3, 0., 0.5)))
.insert_resource(AmbientLight {
color: Color::rgb(1., 1., 1.),
@ -36,24 +41,3 @@ fn main() {
app.run()
}
fn keyboard_input_system(
mut query: Query<(&Handle<TextureAtlas>, &mut TextureAtlasSprite), With<player::Player>>,
keyboard_input: Res<Input<KeyCode>>,
mut app_exit_events: ResMut<Events<bevy::app::AppExit>>,
texture_atlases: Res<Assets<TextureAtlas>>,
) {
if keyboard_input.pressed(KeyCode::Escape) {
app_exit_events.send(bevy::app::AppExit);
} else if keyboard_input.just_pressed(KeyCode::N) {
let (texture, mut sprite) = query.single_mut();
let t = texture_atlases
.get(texture)
.expect("could not load player texture");
if sprite.index < t.textures.len() - 1 {
sprite.index += 1;
} else {
sprite.index = 0;
}
}
}

View file

@ -32,6 +32,9 @@ pub struct Movement;
impl Plugin for Movement {
fn build(&self, app: &mut App) {
// TODO: these systems probably needs ordering?
app.add_systems(Update, (mover_movement, update_position));
app.add_systems(
Update,
(update_position, mover_movement.after(update_position)),
);
}
}

View file

@ -5,7 +5,7 @@ use crate::{
movement::{Heading, Mover, Speed, Velocity},
};
const PLAYER_SPEED: f32 = 500.;
const PLAYER_SPEED: f32 = 100.;
#[derive(Component, Debug)]
pub struct Player;
@ -13,27 +13,33 @@ pub struct Player;
impl Plugin for Player {
fn build(&self, app: &mut App) {
app.add_systems(PostStartup, spawn_player)
.add_systems(Update, player_movement);
.add_systems(Update, (sprite_select, controls.after(sprite_select)));
}
}
fn spawn_player(mut commands: Commands, assets: Res<AssetLoader>) {
commands.spawn((
Player,
SpriteSheetBundle {
texture_atlas: assets.sprites.player.clone(),
sprite: TextureAtlasSprite::new(0),
..Default::default()
},
Mover {
velocity: Velocity(Vec2::ZERO),
heading: Heading(Vec2::ZERO),
speed: Speed(PLAYER_SPEED),
},
));
let listener = SpatialListener::new(0.);
commands
.spawn((
Player,
SpriteSheetBundle {
texture_atlas: assets.images.player.clone(),
sprite: TextureAtlasSprite::new(0),
..Default::default()
},
Mover {
velocity: Velocity(Vec2::ZERO),
heading: Heading(Vec2::ZERO),
speed: Speed(PLAYER_SPEED),
},
))
.with_children(|parent| {
parent.spawn((SpatialBundle::default(), listener.clone()));
});
}
fn player_movement(
fn controls(
mut query: Query<(&mut TextureAtlasSprite, &mut Heading), With<Player>>,
input: Res<Input<KeyCode>>,
) {
@ -41,6 +47,7 @@ fn player_movement(
**heading = Vec2::ZERO;
for key in input.get_pressed() {
match key {
KeyCode::Space => {}
KeyCode::A | KeyCode::Left => {
**heading -= Vec2::X;
}
@ -65,3 +72,24 @@ fn player_movement(
sprite.flip_x = heading.x > 0.;
}
}
fn sprite_select(
mut query: Query<(&Handle<TextureAtlas>, &mut TextureAtlasSprite), With<Player>>,
keyboard_input: Res<Input<KeyCode>>,
mut app_exit_events: ResMut<Events<bevy::app::AppExit>>,
texture_atlases: Res<Assets<TextureAtlas>>,
) {
if keyboard_input.pressed(KeyCode::Escape) {
app_exit_events.send(bevy::app::AppExit);
} else if keyboard_input.just_pressed(KeyCode::N) {
let (texture, mut sprite) = query.single_mut();
let t = texture_atlases
.get(texture)
.expect("could not load player texture");
if sprite.index < t.textures.len() - 1 {
sprite.index += 1;
} else {
sprite.index = 0;
}
}
}

View file

@ -15,7 +15,7 @@ fn spawn_statue(mut commands: Commands, assets: Res<AssetLoader>) {
commands.spawn((
Statue,
SpriteSheetBundle {
texture_atlas: assets.sprites.statue.clone(),
texture_atlas: assets.images.statue.clone(),
sprite: TextureAtlasSprite::new(0),
transform: Transform::from_translation(Vec3::new(50., 50., 0.)),
..Default::default()
@ -24,7 +24,7 @@ fn spawn_statue(mut commands: Commands, assets: Res<AssetLoader>) {
commands.spawn((
Statue,
SpriteSheetBundle {
texture_atlas: assets.sprites.statue.clone(),
texture_atlas: assets.images.statue.clone(),
sprite: TextureAtlasSprite::new(0),
transform: Transform::from_translation(Vec3::new(50., 100., 0.)),
..Default::default()