From c4309c7a69cb1c03b2c4577afaa5ef352d87469a Mon Sep 17 00:00:00 2001 From: Dominic Date: Sun, 7 Jul 2024 17:18:00 +0200 Subject: [PATCH] ghost interacts with house battery --- src/activities/house/mod.rs | 7 +++-- src/activities/overworld/mod.rs | 10 +++++-- src/game.rs | 49 ++++++++++++++++++++++++++++++--- 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/src/activities/house/mod.rs b/src/activities/house/mod.rs index 1634b97..5a25152 100644 --- a/src/activities/house/mod.rs +++ b/src/activities/house/mod.rs @@ -3,7 +3,7 @@ mod grid; mod player; mod room; -use comfy::{main_camera_mut, random_i32, vec2, EngineContext}; +use comfy::{main_camera_mut, random_i32, vec2, EngineContext, RandomRange as _}; use grid::Grid; use log::error; use player::Player; @@ -36,6 +36,7 @@ impl HouseState { } let player = Player::new(rooms.first().unwrap()); + let max_charge = f32::gen_range(2_000.0, 5_000.0); HouseState { current_room_id: 0, room_count, @@ -43,8 +44,8 @@ impl HouseState { player, human_layer: false, // TODO this should be lower depending on the time elapsed - charge: 1000.0, - max_charge: 1000.0 + charge: max_charge, + max_charge } } } diff --git a/src/activities/overworld/mod.rs b/src/activities/overworld/mod.rs index 3ad9c12..47baf43 100644 --- a/src/activities/overworld/mod.rs +++ b/src/activities/overworld/mod.rs @@ -1,4 +1,8 @@ -use crate::{activities::Activity, game::ZLayer, State}; +use crate::{ + activities::Activity, + game::{ZLayer, GHOST_DISCHARGE_RATE, GHOST_DISCHARGE_RATE_MOVEMENT}, + State +}; use comfy::{ delta, draw_circle, draw_rect_outline, draw_sprite, error, info, is_key_down, main_camera_mut, EngineContext, IVec2, KeyCode, Vec2, RED, WHITE @@ -177,9 +181,9 @@ pub fn update(state: &mut State, ctx: &mut EngineContext<'_>) { // energie lost { let ghost = &mut state.ghost; - ghost.charge -= 70.0 * ctx.delta; + ghost.charge -= GHOST_DISCHARGE_RATE * ctx.delta; if ghost.overworld_movement_pending != Vec2::ZERO { - ghost.charge -= 70.0 * ctx.delta; + ghost.charge -= GHOST_DISCHARGE_RATE_MOVEMENT * ctx.delta; } } diff --git a/src/game.rs b/src/game.rs index 4a45a4a..d7aaad3 100644 --- a/src/game.rs +++ b/src/game.rs @@ -107,12 +107,53 @@ pub fn setup(_state: &mut State, ctx: &mut EngineContext<'_>) { //house::setup(state, ctx); } -pub fn update(state: &mut State, engine: &mut EngineContext<'_>) { - state.score += engine.delta * 10.0; +/// The amount of energy a ghost consumes idle. +pub const GHOST_DISCHARGE_RATE: f32 = 70.0; +/// The amount of energy additionally consumed by a moving ghost. +pub const GHOST_DISCHARGE_RATE_MOVEMENT: f32 = 70.0; +/// The amount of energy a house consumes idle. +pub const HOUSE_DISCHARGE_RATE: f32 = 30.0; +/// The amount of energy a ghost can charge when inside a house. +pub const GHOST_CHARGE_RATE: f32 = 200.0; + +pub fn update(state: &mut State, ctx: &mut EngineContext<'_>) { + // Update the score. It's based on time. + state.score += ctx.delta * 10.0; + + // Update the currently active activity. match state.activity { - Activity::House(_) => house::update(state, engine), - Activity::Overworld => overworld::update(state, engine) + Activity::House(_) => house::update(state, ctx), + Activity::Overworld => overworld::update(state, ctx) } + + // We update the charge of houses here - the charge will always decrease, even if the + // house is not the current activity. + for (house_pos, house) in &mut state.houses { + house.charge -= ctx.delta * HOUSE_DISCHARGE_RATE; + + match state.activity { + Activity::House(pos) if *house_pos == pos => { + // The ghost is currently inside the house. Increase its discarge rate. + house.charge -= ctx.delta * GHOST_DISCHARGE_RATE; + if house.charge < 0.0 { + state.ghost.charge += house.charge; + house.charge = 0.0; + } + + // And possibly also charge the ghost when inside a house. + if state.ghost.charge < state.ghost.max_charge { + let charge_transfer = (ctx.delta * GHOST_CHARGE_RATE) + .min(state.ghost.max_charge - state.ghost.charge) + .min(house.charge); + state.ghost.charge += charge_transfer; + house.charge -= charge_transfer; + } + }, + _ => {} + } + } + + // Make sure the ghost's charge never drops below 0. state.ghost.charge = state.ghost.charge.max(0.0); }