Skip to content

Commit

Permalink
fix dupe walking sound
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeder committed Jun 29, 2023
1 parent 30fe979 commit d28295c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "turtle_time"
version = "0.10.2"
version = "0.10.3"
publish = false
authors = ["Mike Eder <[email protected]>"]
edition = "2021"
Expand Down
1 change: 1 addition & 0 deletions src/player/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl Plugin for PlayerPlugin {
.add_systems(
(
apply_inputs,
set_walking_sound,
apply_player_sprint,
move_players,
checksum_players,
Expand Down
90 changes: 55 additions & 35 deletions src/player/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ pub fn camera_follow(
player_query: Query<(&Transform, &Player), Without<Fireball>>,
mut camera_query: Query<&mut Transform, (Without<Player>, With<Camera>)>,
) {
// todo: follow another player when local player dies
let player_handle = match player_handle {
Some(handle) => handle.0,
None => return, // Session hasn't started yet
Expand All @@ -234,9 +235,15 @@ pub fn spawn_players(
player_count: Res<PlayerCount>,
mut rip: ResMut<RollbackIdProvider>,
spawn_query: Query<&mut PlayerSpawn>,
local_handle: Option<Res<LocalHandle>>,
) {
trace!("spawn_players");

let local_handle = match local_handle {
Some(handle) => handle.0,
None => return, // Session hasn't started yet
};

// find all the spawn points on the map
let spawns: Vec<&PlayerSpawn> = spawn_query.iter().collect();

Expand All @@ -245,52 +252,68 @@ pub fn spawn_players(

for handle in 0..player_count.0 {
let name = format!("Player {}", handle);
commands.spawn((
Name::new(name),
SpriteSheetBundle {
sprite: sprite.clone(),
texture_atlas: characters.turtle_handle.clone(),
transform: Transform {
translation: Vec3::new(spawns[handle].pos.x, spawns[handle].pos.y, 1.),
let player_id = commands
.spawn((
Name::new(name),
SpriteSheetBundle {
sprite: sprite.clone(),
texture_atlas: characters.turtle_handle.clone(),
transform: Transform {
translation: Vec3::new(spawns[handle].pos.x, spawns[handle].pos.y, 1.),
..Default::default()
},
..Default::default()
},
..Default::default()
},
FrameAnimation {
timer: Timer::from_seconds(0.2, TimerMode::Repeating),
frames: characters.turtle_frames.to_vec(),
current_frame: 0,
},
Player {
handle,
..Default::default()
},
FireballAmmo::default(),
FireballReady::default(),
PlayerControls::default(),
PlayerHealth::default(),
PlayerSpeed::default(),
PlayerSpeedBoost::default(),
Checksum::default(),
RoundComponent,
FadedLoopSound {
FrameAnimation {
timer: Timer::from_seconds(0.2, TimerMode::Repeating),
frames: characters.turtle_frames.to_vec(),
current_frame: 0,
},
Player {
handle,
..Default::default()
},
FireballAmmo::default(),
FireballReady::default(),
PlayerControls::default(),
PlayerHealth::default(),
PlayerSpeed::default(),
PlayerSpeedBoost::default(),
Checksum::default(),
RoundComponent,
rip.next(),
))
.id();

if handle == local_handle {
// add walking sound component to local player only
commands.entity(player_id).insert(FadedLoopSound {
audio_instance: None,
clip: sounds.walking.clone(),
fade_in: 0.1,
fade_out: 0.1,
should_play: false,
},
rip.next(),
));
});
}
}
commands.insert_resource(PlayersReady);
}

pub fn set_walking_sound(mut query: Query<(&mut FadedLoopSound, &PlayerControls)>) {
for (mut sound, controls) in query.iter_mut() {
if controls.dir == Vec2::ZERO {
sound.should_play = false
} else {
sound.should_play = true
}
}
}

pub fn apply_inputs(
mut query: Query<(&mut PlayerControls, &Player, &mut FadedLoopSound)>,
mut query: Query<(&mut PlayerControls, &Player)>,
inputs: Res<PlayerInputs<GGRSConfig>>,
) {
for (mut pc, p, mut sound) in query.iter_mut() {
for (mut pc, p) in query.iter_mut() {
let input = match inputs[p.handle].1 {
InputStatus::Confirmed => inputs[p.handle].0.input,
InputStatus::Predicted => inputs[p.handle].0.input,
Expand All @@ -314,9 +337,6 @@ pub fn apply_inputs(

if direction != Vec2::ZERO {
pc.last_dir = pc.dir;
sound.should_play = true
} else {
sound.should_play = false
}

if input & INPUT_FIRE != 0 {
Expand Down

0 comments on commit d28295c

Please sign in to comment.