battery drain on flashlight

This commit is contained in:
yuni 2024-05-08 02:35:36 +02:00
parent 853b976a43
commit 35a0d51dfb
3 changed files with 48 additions and 7 deletions

View file

@ -33,6 +33,7 @@ impl Plugin for ActorPlugin {
));
app.add_systems(FixedUpdate, (
update_physics_lifeforms,
update_power,
handle_wants_maxrotation,
handle_wants_maxvelocity,
));
@ -198,21 +199,58 @@ impl Default for Engine {
#[derive(Component)]
pub struct Suit {
pub oxygen: f32,
pub power: f32,
pub oxygen_max: f32,
pub power_max: f32,
pub integrity: f32, // [0.0 - 1.0]
}
impl Default for Suit { fn default() -> Self { SUIT_SIMPLE } }
const SUIT_SIMPLE: Suit = Suit {
power: 1e5,
power_max: 1e5,
oxygen: nature::OXY_D,
oxygen_max: nature::OXY_D,
integrity: 1.0,
};
#[derive(Component)]
pub struct Battery {
pub power: f32, // Watt-seconds
pub capacity: f32, // Watt-seconds
pub reactor: f32, // Watt (production)
}
impl Default for Battery {
fn default() -> Self {
Self {
power: 10e3 * 3600.0,
capacity: 10e3 * 3600.0, // 10kWh
reactor: 2000e3, // 2MW
}
}
}
pub fn update_power(
time: Res<Time>,
mut settings: ResMut<var::Settings>,
mut q_battery: Query<(&mut Battery, Option<&Player>)>,
mut q_flashlight: Query<&mut Visibility, With<PlayersFlashLight>>,
mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
) {
let d = time.delta_seconds();
for (mut battery, player) in &mut q_battery {
if player.is_some() && settings.flashlight_active {
battery.power -= 4000000.0 * d;
if battery.power <= 0.0 {
settings.flashlight_active = false;
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Click));
for mut flashlight_vis in &mut q_flashlight {
*flashlight_vis = Visibility::Hidden;
}
}
}
battery.power = (battery.power + battery.reactor * d)
.clamp(0.0, battery.capacity);
}
}
pub fn update_physics_lifeforms(
time: Res<Time>,
mut query: Query<(&mut LifeForm, &mut HitPoints, &mut Suit, &LinearVelocity)>,
@ -344,6 +382,7 @@ pub fn handle_input(
pub fn handle_vehicle_enter_exit(
mut commands: Commands,
mut settings: ResMut<var::Settings>,
mut er_vehicle: EventReader<VehicleEnterExitEvent>,
mut q_playerflashlight: Query<&mut Visibility, (With<PlayersFlashLight>, Without<ActorVehicleBeingEntered>, Without<ActorEnteringVehicle>)>,
mut q_drivers: Query<(Entity, &mut Visibility, Option<&Collider>), (Without<ActorVehicleBeingEntered>, With<ActorEnteringVehicle>)>,
@ -373,6 +412,7 @@ pub fn handle_vehicle_enter_exit(
commands.entity(vehicle).insert(PlayerDrivesThis);
if let Ok(mut flashlight_vis) = q_playerflashlight.get_single_mut() {
*flashlight_vis = Visibility::Hidden;
settings.flashlight_active = false;
}
}
else {

View file

@ -683,6 +683,7 @@ fn spawn_entities(
integrity: state.suit_integrity,
..default()
});
actor.insert(actor::Battery::default());
}
if state.is_clickable {
actor.insert(hud::IsClickable {

View file

@ -685,7 +685,7 @@ fn update_speedometer(
fn update_gauges(
timer: ResMut<FPSUpdateTimer>,
q_player: Query<(&actor::HitPoints, &actor::Suit), With<actor::Player>>,
q_player: Query<(&actor::HitPoints, &actor::Suit, &actor::Battery), With<actor::Player>>,
mut q_gauges: Query<(&mut Style, &mut BackgroundColor, &Gauge, &GaugeLength)>,
settings: Res<var::Settings>,
) {
@ -694,14 +694,14 @@ fn update_gauges(
}
let player = q_player.get_single();
if player.is_err() { return; }
let (hp, suit) = player.unwrap();
let (hp, suit, battery) = player.unwrap();
for (mut style, mut bg, gauge, len) in &mut q_gauges {
let value: f32 = match gauge {
Gauge::Health => hp.current / hp.max,
Gauge::Oxygen => (suit.oxygen / suit.oxygen_max).powf(0.5),
Gauge::Integrity => suit.integrity,
Gauge::Power => suit.power / suit.power_max,
Gauge::Power => battery.power / battery.capacity,
};
if value < 0.5 {
*bg = settings.hud_color_alert.into();