From f74e9261d949b775349a48559090a9ce15700b39 Mon Sep 17 00:00:00 2001 From: extrawurst Date: Wed, 2 Apr 2025 11:48:59 +0800 Subject: [PATCH] heal for cash --- src/abilities/mod.rs | 3 +++ src/cash_heal.rs | 24 ++++++++++++++++++++++++ src/control/controls.rs | 11 ++++++++++- src/hitpoints.rs | 4 ++++ src/main.rs | 2 ++ 5 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/cash_heal.rs diff --git a/src/abilities/mod.rs b/src/abilities/mod.rs index 82afbfc..2ed8b0e 100644 --- a/src/abilities/mod.rs +++ b/src/abilities/mod.rs @@ -23,6 +23,9 @@ pub enum TriggerState { Inactive, } +#[derive(Event, Reflect)] +pub struct TriggerCashHeal; + #[derive(Debug, Copy, Clone, PartialEq, Reflect)] pub enum HeadAbility { None, diff --git a/src/cash_heal.rs b/src/cash_heal.rs new file mode 100644 index 0000000..317ea88 --- /dev/null +++ b/src/cash_heal.rs @@ -0,0 +1,24 @@ +use crate::{abilities::TriggerCashHeal, cash::CashResource, hitpoints::Hitpoints, player::Player}; +use bevy::prelude::*; + +pub fn plugin(app: &mut App) { + app.add_observer(on_heal_trigger); +} + +fn on_heal_trigger( + _trigger: Trigger, + mut cash: ResMut, + mut query: Query<&mut Hitpoints, With>, +) { + let Ok(mut hp) = query.get_single_mut() else { + return; + }; + + if hp.max() { + return; + } + + hp.set_health(100); + + cash.cash = cash.cash.saturating_sub(10); +} diff --git a/src/control/controls.rs b/src/control/controls.rs index 7358702..0502dda 100644 --- a/src/control/controls.rs +++ b/src/control/controls.rs @@ -1,5 +1,8 @@ use crate::{ - GameState, abilities::TriggerState, backpack::BackpackAction, heads::SelectActiveHead, + GameState, + abilities::{TriggerCashHeal, TriggerState}, + backpack::BackpackAction, + heads::SelectActiveHead, }; use bevy::{ input::{ @@ -106,6 +109,9 @@ fn gamepad_controls( if gamepad.just_pressed(GamepadButton::DPadUp) { commands.trigger(BackpackAction::OpenClose); } + if gamepad.just_pressed(GamepadButton::East) { + commands.trigger(TriggerCashHeal); + } if controls .gamepad_state @@ -162,6 +168,9 @@ fn keyboard_controls( if keyboard.just_pressed(KeyCode::KeyE) { commands.trigger(SelectActiveHead::Right); } + if keyboard.just_pressed(KeyCode::Enter) { + commands.trigger(TriggerCashHeal); + } controls.keyboard_state.move_dir = direction; controls.keyboard_state.jump = keyboard.pressed(KeyCode::Space); diff --git a/src/hitpoints.rs b/src/hitpoints.rs index 4ac760a..569791a 100644 --- a/src/hitpoints.rs +++ b/src/hitpoints.rs @@ -32,6 +32,10 @@ impl Hitpoints { pub fn get(&self) -> (u32, u32) { (self.current, self.max) } + + pub fn max(&self) -> bool { + self.current == self.max + } } pub fn plugin(app: &mut App) { diff --git a/src/main.rs b/src/main.rs index 7e35a56..db80d10 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ mod alien; mod backpack; mod camera; mod cash; +mod cash_heal; mod control; mod cutscene; mod gates; @@ -118,6 +119,7 @@ fn main() { app.add_plugins(abilities::plugin); app.add_plugins(heads::plugin); app.add_plugins(hitpoints::plugin); + app.add_plugins(cash_heal::plugin); app.init_state::();