diff --git a/assets/sfx/meow.wav b/assets/sfx/meow.wav new file mode 100644 index 0000000..de4a639 Binary files /dev/null and b/assets/sfx/meow.wav differ diff --git a/readme.md b/readme.md index 3a22108..de5e3bd 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/src/assets.rs b/src/assets.rs index 1cb15f5..76251ea 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -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, } +#[derive(Resource, Debug, Default)] +pub struct Sounds { + pub meow: Handle, +} + 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"), + }, }; } diff --git a/src/main.rs b/src/main.rs index a00e750..4b8a04d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, &mut TextureAtlasSprite), With>, - keyboard_input: Res>, - mut app_exit_events: ResMut>, - texture_atlases: Res>, -) { - 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; - } - } -} diff --git a/src/movement.rs b/src/movement.rs index a40e4ed..11ca09c 100644 --- a/src/movement.rs +++ b/src/movement.rs @@ -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)), + ); } } diff --git a/src/player.rs b/src/player.rs index 87cec4a..c834615 100644 --- a/src/player.rs +++ b/src/player.rs @@ -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) { - 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>, input: Res>, ) { @@ -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, &mut TextureAtlasSprite), With>, + keyboard_input: Res>, + mut app_exit_events: ResMut>, + texture_atlases: Res>, +) { + 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; + } + } +} diff --git a/src/statue.rs b/src/statue.rs index a4c6993..99ab0d3 100644 --- a/src/statue.rs +++ b/src/statue.rs @@ -15,7 +15,7 @@ fn spawn_statue(mut commands: Commands, assets: Res) { 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) { 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()