WIP crisper camera controls (play engine sound)

This commit is contained in:
yuni 2024-11-16 22:29:19 +01:00
parent 06c2d90228
commit 94bf21b340

View file

@ -40,7 +40,7 @@ impl Plugin for ActorPlugin {
update_power.run_if(game_running), update_power.run_if(game_running),
handle_gravity.run_if(game_running), handle_gravity.run_if(game_running),
handle_wants_rotation.run_if(game_running), handle_wants_rotation.run_if(game_running),
handle_wants_acceleration.run_if(game_running), handle_wants_acceleration.run_if(game_running).run_if(alive),
handle_wants_maxrotation.run_if(game_running), handle_wants_maxrotation.run_if(game_running),
handle_wants_maxvelocity handle_wants_maxvelocity
.run_if(game_running) .run_if(game_running)
@ -231,7 +231,7 @@ pub enum EngineType {
Ion, Ion,
} }
#[derive(Component)] #[derive(Component, Copy, Clone)]
pub struct Engine { pub struct Engine {
pub thrust_forward: f32, pub thrust_forward: f32,
pub thrust_back: f32, pub thrust_back: f32,
@ -588,7 +588,9 @@ pub fn handle_vehicle_enter_exit(
commands.entity(driver).remove::<PlayerCamera>(); commands.entity(driver).remove::<PlayerCamera>();
commands.entity(driver).remove::<Collider>(); commands.entity(driver).remove::<Collider>();
commands.entity(driver).insert(JustNowEnteredVehicle); commands.entity(driver).insert(JustNowEnteredVehicle);
commands.entity(vehicle).insert(WantsAcceleration::default()); commands
.entity(vehicle)
.insert(WantsAcceleration::default());
commands.entity(vehicle).remove::<hud::IsTargeted>(); commands.entity(vehicle).remove::<hud::IsTargeted>();
commands.entity(vehicle).insert(PlayerCamera); commands.entity(vehicle).insert(PlayerCamera);
commands.entity(vehicle).insert(PlayerDrivesThis); commands.entity(vehicle).insert(PlayerDrivesThis);
@ -722,23 +724,29 @@ fn handle_wants_rotation(mut q_actor: Query<(&mut Rotation, &WantsRotation)>) {
} }
fn handle_wants_acceleration( fn handle_wants_acceleration(
time: Res<Time>,
settings: Res<var::Settings>,
jupiter_pos: Res<game::JupiterPos>, jupiter_pos: Res<game::JupiterPos>,
q_audiosinks: Query<(&audio::Sfx, &AudioSink)>,
mut q_actor: Query<( mut q_actor: Query<(
Entity, Entity,
&Transform, &Transform,
&Position, &Position,
&mut LinearVelocity, &mut LinearVelocity,
Option<&mut Engine>,
Option<&WantsAcceleration>, Option<&WantsAcceleration>,
Option<&hud::IsTargeted>, Option<&hud::IsTargeted>,
Option<&PlayerCamera>, Option<&PlayerCamera>,
)>, )>,
) { ) {
let dt = time.delta_seconds();
// Vector elements: (Entity, is_player, pos) // Vector elements: (Entity, is_player, pos)
let mut request_closest: Vec<(Entity, bool, DVec3)> = vec![]; let mut request_closest: Vec<(Entity, bool, DVec3)> = vec![];
let mut closest_map: HashMap<Entity, DVec3> = HashMap::new(); let mut closest_map: HashMap<Entity, DVec3> = HashMap::new();
// First, determine whether any actor wants to brake (=match velocity) // First, determine whether any actor wants to brake (=match velocity)
for (entity, _, pos, _, accel, _, is_player) in &mut q_actor { for (entity, _, pos, _, _, accel, _, is_player) in &mut q_actor {
if accel.is_some() && accel.unwrap().brake { if accel.is_some() && accel.unwrap().brake {
request_closest.push((entity, is_player.is_some(), pos.0.clone())); request_closest.push((entity, is_player.is_some(), pos.0.clone()));
} }
@ -751,7 +759,7 @@ fn handle_wants_acceleration(
// First, if this is the player, check whether they targeted anything // First, if this is the player, check whether they targeted anything
// so we can match velocity to the target. // so we can match velocity to the target.
if *is_player { if *is_player {
for (_, _, _, v, _, is_target, _) in &q_actor { for (_, _, _, v, _, _, is_target, _) in &q_actor {
if is_target.is_some() { if is_target.is_some() {
target_v = Some(v.0); target_v = Some(v.0);
break; break;
@ -762,7 +770,7 @@ fn handle_wants_acceleration(
// If not, simply look for the closest object and match velocity to that. // If not, simply look for the closest object and match velocity to that.
if target_v.is_none() { if target_v.is_none() {
let mut closest_distance = camera::MAX_DIST_FOR_MATCH_VELOCITY; let mut closest_distance = camera::MAX_DIST_FOR_MATCH_VELOCITY;
for (testentity, _, testpos, v, _, _, _) in &q_actor { for (testentity, _, testpos, v, _, _, _, _) in &q_actor {
if *entity != testentity { if *entity != testentity {
let distance = (*pos - testpos.0).length(); let distance = (*pos - testpos.0).length();
if distance < closest_distance { if distance < closest_distance {
@ -784,15 +792,83 @@ fn handle_wants_acceleration(
} }
} }
for (entity, trans, _, mut v, accel, _, _) in &mut q_actor { // Finally, apply the requested acceleration to the actor's velocity
if let Some(accel) = accel { let mut play_thruster_sound = false;
let mut players_engine: Option<Engine> = None;
for (entity, trans, _, mut v, engine, accel, _, is_player) in &mut q_actor {
let mut thruster_on = false;
if let (Some(engine), Some(accel)) = (engine, accel) {
if accel.brake { if accel.brake {
if let Some(target_v) = closest_map.get(&entity) { if let Some(target_v) = closest_map.get(&entity) {
**v = *target_v; **v = *target_v;
thruster_on = true;
} }
} else if accel.direction != DVec3::ZERO { } else if accel.direction != DVec3::ZERO {
let delta_v = trans.rotation * accel.direction.as_vec3(); let delta_v = trans.rotation * accel.direction.as_vec3();
**v += delta_v.as_dvec3(); **v += delta_v.as_dvec3();
thruster_on = true;
}
if is_player.is_some() {
play_thruster_sound = thruster_on;
players_engine = Some((*engine).clone());
}
}
}
// Play sound effects for player acceleration
let engine = if let Some(engine) = players_engine {
engine
} else {
warn!("Failed to retrieve player's engine type for playing SFX");
Engine::default()
};
let mut sinks: HashMap<audio::Sfx, &AudioSink> = HashMap::new();
for (sfx, sink) in &q_audiosinks {
sinks.insert(*sfx, sink);
}
let sinks = vec![
(
1.0,
1.0, //boost as f32,
actor::EngineType::Monopropellant,
sinks.get(&audio::Sfx::Thruster),
),
(
1.0,
1.0,
actor::EngineType::Ion,
sinks.get(&audio::Sfx::Ion),
),
];
let seconds_to_max_vol = 0.05;
let seconds_to_min_vol = 0.05;
for sink_data in sinks {
if let (vol_boost, speed, engine_type, Some(sink)) = sink_data {
if settings.mute_sfx {
sink.pause();
} else {
let volume = sink.volume();
let maxvol = settings.volume_sfx * vol_boost;
if engine.engine_type == engine_type {
if play_thruster_sound {
sink.set_speed(speed);
sink.play();
if volume < maxvol {
sink.set_volume(
(volume + dt / seconds_to_max_vol).clamp(0.0, maxvol),
);
}
} else {
sink.set_volume((volume - dt / seconds_to_min_vol).clamp(0.0, maxvol));
}
} else if volume > 0.0 {
sink.set_volume((volume - dt / seconds_to_min_vol).clamp(0.0, maxvol));
}
if volume < 0.0001 {
sink.pause();
}
} }
} }
} }