Work on adding sound playback
This commit is contained in:
parent
dbc197f462
commit
c54d393d00
BIN
assets/sfx/meow.wav
Normal file
BIN
assets/sfx/meow.wav
Normal file
Binary file not shown.
|
@ -17,6 +17,11 @@ the heavy-lifting for me _plus_ it's a fun excuse to write more Rust.
|
||||||
- [X] Movement
|
- [X] Movement
|
||||||
- [X] Flipping sprites
|
- [X] Flipping sprites
|
||||||
- [X] Camera
|
- [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
|
- [ ] Tile system
|
||||||
- [ ] Basic map
|
- [ ] Basic map
|
||||||
- [ ] Enemies
|
- [ ] Enemies
|
||||||
|
|
|
@ -2,7 +2,8 @@ use bevy::prelude::*;
|
||||||
|
|
||||||
#[derive(Resource, Debug, Default)]
|
#[derive(Resource, Debug, Default)]
|
||||||
pub struct AssetLoader {
|
pub struct AssetLoader {
|
||||||
pub sprites: Sprites,
|
pub images: Sprites,
|
||||||
|
pub sounds: Sounds,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Resource, Debug, Default)]
|
#[derive(Resource, Debug, Default)]
|
||||||
|
@ -11,6 +12,11 @@ pub struct Sprites {
|
||||||
pub statue: Handle<TextureAtlas>,
|
pub statue: Handle<TextureAtlas>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Resource, Debug, Default)]
|
||||||
|
pub struct Sounds {
|
||||||
|
pub meow: Handle<AudioSource>,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct AssetPlugin;
|
pub struct AssetPlugin;
|
||||||
|
|
||||||
impl Plugin for AssetPlugin {
|
impl Plugin for AssetPlugin {
|
||||||
|
@ -49,9 +55,13 @@ fn load_assets(
|
||||||
let statue_atlas_handle = texture_atlases.add(statue_atlas);
|
let statue_atlas_handle = texture_atlases.add(statue_atlas);
|
||||||
|
|
||||||
*assets = AssetLoader {
|
*assets = AssetLoader {
|
||||||
sprites: Sprites {
|
images: Sprites {
|
||||||
player: player_atlas_handle,
|
player: player_atlas_handle,
|
||||||
statue: statue_atlas_handle,
|
statue: statue_atlas_handle,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
sounds: Sounds {
|
||||||
|
meow: asset_server.load("sfx/meow.wav"),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
28
src/main.rs
28
src/main.rs
|
@ -1,3 +1,4 @@
|
||||||
|
use bevy::audio::{AudioPlugin, SpatialScale};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
mod assets;
|
mod assets;
|
||||||
|
@ -8,6 +9,7 @@ mod statue;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut app = App::new();
|
let mut app = App::new();
|
||||||
|
|
||||||
app.add_plugins(
|
app.add_plugins(
|
||||||
DefaultPlugins
|
DefaultPlugins
|
||||||
.set(WindowPlugin {
|
.set(WindowPlugin {
|
||||||
|
@ -18,6 +20,10 @@ fn main() {
|
||||||
}),
|
}),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
|
.set(AudioPlugin {
|
||||||
|
spatial_scale: SpatialScale::new_2d(100.),
|
||||||
|
..default()
|
||||||
|
})
|
||||||
.set(ImagePlugin::default_nearest()),
|
.set(ImagePlugin::default_nearest()),
|
||||||
)
|
)
|
||||||
.add_plugins((
|
.add_plugins((
|
||||||
|
@ -27,7 +33,6 @@ fn main() {
|
||||||
player::Player,
|
player::Player,
|
||||||
movement::Movement,
|
movement::Movement,
|
||||||
))
|
))
|
||||||
.add_systems(Update, keyboard_input_system)
|
|
||||||
.insert_resource(ClearColor(Color::rgb(0.3, 0., 0.5)))
|
.insert_resource(ClearColor(Color::rgb(0.3, 0., 0.5)))
|
||||||
.insert_resource(AmbientLight {
|
.insert_resource(AmbientLight {
|
||||||
color: Color::rgb(1., 1., 1.),
|
color: Color::rgb(1., 1., 1.),
|
||||||
|
@ -36,24 +41,3 @@ fn main() {
|
||||||
|
|
||||||
app.run()
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -32,6 +32,9 @@ pub struct Movement;
|
||||||
impl Plugin for Movement {
|
impl Plugin for Movement {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
// TODO: these systems probably needs ordering?
|
// 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)),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ use crate::{
|
||||||
movement::{Heading, Mover, Speed, Velocity},
|
movement::{Heading, Mover, Speed, Velocity},
|
||||||
};
|
};
|
||||||
|
|
||||||
const PLAYER_SPEED: f32 = 500.;
|
const PLAYER_SPEED: f32 = 100.;
|
||||||
|
|
||||||
#[derive(Component, Debug)]
|
#[derive(Component, Debug)]
|
||||||
pub struct Player;
|
pub struct Player;
|
||||||
|
@ -13,27 +13,33 @@ pub struct Player;
|
||||||
impl Plugin for Player {
|
impl Plugin for Player {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_systems(PostStartup, spawn_player)
|
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>) {
|
fn spawn_player(mut commands: Commands, assets: Res<AssetLoader>) {
|
||||||
commands.spawn((
|
let listener = SpatialListener::new(0.);
|
||||||
Player,
|
|
||||||
SpriteSheetBundle {
|
commands
|
||||||
texture_atlas: assets.sprites.player.clone(),
|
.spawn((
|
||||||
sprite: TextureAtlasSprite::new(0),
|
Player,
|
||||||
..Default::default()
|
SpriteSheetBundle {
|
||||||
},
|
texture_atlas: assets.images.player.clone(),
|
||||||
Mover {
|
sprite: TextureAtlasSprite::new(0),
|
||||||
velocity: Velocity(Vec2::ZERO),
|
..Default::default()
|
||||||
heading: Heading(Vec2::ZERO),
|
},
|
||||||
speed: Speed(PLAYER_SPEED),
|
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>>,
|
mut query: Query<(&mut TextureAtlasSprite, &mut Heading), With<Player>>,
|
||||||
input: Res<Input<KeyCode>>,
|
input: Res<Input<KeyCode>>,
|
||||||
) {
|
) {
|
||||||
|
@ -41,6 +47,7 @@ fn player_movement(
|
||||||
**heading = Vec2::ZERO;
|
**heading = Vec2::ZERO;
|
||||||
for key in input.get_pressed() {
|
for key in input.get_pressed() {
|
||||||
match key {
|
match key {
|
||||||
|
KeyCode::Space => {}
|
||||||
KeyCode::A | KeyCode::Left => {
|
KeyCode::A | KeyCode::Left => {
|
||||||
**heading -= Vec2::X;
|
**heading -= Vec2::X;
|
||||||
}
|
}
|
||||||
|
@ -65,3 +72,24 @@ fn player_movement(
|
||||||
sprite.flip_x = heading.x > 0.;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ fn spawn_statue(mut commands: Commands, assets: Res<AssetLoader>) {
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
Statue,
|
Statue,
|
||||||
SpriteSheetBundle {
|
SpriteSheetBundle {
|
||||||
texture_atlas: assets.sprites.statue.clone(),
|
texture_atlas: assets.images.statue.clone(),
|
||||||
sprite: TextureAtlasSprite::new(0),
|
sprite: TextureAtlasSprite::new(0),
|
||||||
transform: Transform::from_translation(Vec3::new(50., 50., 0.)),
|
transform: Transform::from_translation(Vec3::new(50., 50., 0.)),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
|
@ -24,7 +24,7 @@ fn spawn_statue(mut commands: Commands, assets: Res<AssetLoader>) {
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
Statue,
|
Statue,
|
||||||
SpriteSheetBundle {
|
SpriteSheetBundle {
|
||||||
texture_atlas: assets.sprites.statue.clone(),
|
texture_atlas: assets.images.statue.clone(),
|
||||||
sprite: TextureAtlasSprite::new(0),
|
sprite: TextureAtlasSprite::new(0),
|
||||||
transform: Transform::from_translation(Vec3::new(50., 100., 0.)),
|
transform: Transform::from_translation(Vec3::new(50., 100., 0.)),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
|
|
Loading…
Reference in a new issue