diff --git a/src/activities/house/mod.rs b/src/activities/house/mod.rs index 72fe2cc..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; @@ -18,7 +18,12 @@ pub struct HouseState { rooms: Vec, //grid: Grid, player: Player, - human_layer: bool //Human, magnetic, electric + human_layer: bool, //Human, magnetic, electric + + /// The energy level remaining in the house. Should decrease by itself, and much + /// faster when inhabited by the ghost. + pub charge: f32, + pub max_charge: f32 } impl HouseState { @@ -31,12 +36,16 @@ 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, rooms, player, - human_layer: false + human_layer: false, + // TODO this should be lower depending on the time elapsed + charge: max_charge, + max_charge } } } diff --git a/src/activities/house/room.rs b/src/activities/house/room.rs index 16d19f7..f25ae5d 100644 --- a/src/activities/house/room.rs +++ b/src/activities/house/room.rs @@ -1,7 +1,8 @@ use super::{furniture::Furniture, grid::Grid}; use crate::game; use comfy::{ - draw_rect, draw_rect_outline, draw_sprite, error, random_i32, vec2, EngineContext, HashSet, Index, RandomRange as _, Vec2, GREEN, PURPLE, RED, WHITE + draw_rect, draw_rect_outline, draw_sprite, error, random_i32, vec2, EngineContext, + HashSet, Index, RandomRange as _, Vec2, GREEN, PURPLE, RED, WHITE }; use indexmap::IndexSet; @@ -88,14 +89,17 @@ impl Room { empty_spots.swap_remove_index(random_idx) } - fn random_empty_spot_size(empty_spots: &mut IndexSet, size: u8) -> Option { + fn random_empty_spot_size( + empty_spots: &mut IndexSet, + size: u8 + ) -> Option { let mut empty_spots_size = IndexSet::::new(); for &index in empty_spots.iter() { let mut is_valid = true; - for offset in 0..size{ - if !empty_spots.contains(&(index + offset)){ + for offset in 0 .. size { + if !empty_spots.contains(&(index + offset)) { is_valid = false; break; } @@ -109,7 +113,7 @@ impl Room { return None; } let random_idx = usize::gen_range(0, empty_spots_size.len()); - for offset in (0..size).rev() { + for offset in (0 .. size).rev() { empty_spots.swap_remove_index(random_idx + offset as usize); } Some(random_idx as u8) @@ -293,10 +297,9 @@ impl Room { pos: vec2(pos as f32, 0.0), size: vec2(3.0, 1.0), f: Furniture::new("bedroom", "couch", ctx), - z:0 + z: 0 }); } - } if let Some(pos) = random_empty_spot(&mut empty_spots) { @@ -304,7 +307,7 @@ impl Room { pos: vec2(pos as f32, 0.0), size: vec2(1.0, 2.0), f: Furniture::new("bedroom", "bookshelf", ctx), - z:0 + z: 0 }); } @@ -313,10 +316,9 @@ impl Room { pos: vec2(pos as f32, 0.0), size: vec2(0.5, 0.9), f: Furniture::new("bedroom", "mini_ac", ctx), - z:0 + z: 0 }); } - }, _ => {} 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); } diff --git a/src/ui.rs b/src/ui.rs index 1f31ada..4a22853 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,41 +1,61 @@ use crate::{game::ZLayer, State}; use comfy::{ draw_rect, draw_rect_outline, egui, screen_height, screen_to_world, screen_width, - EngineContext, Vec2, BLACK, BLUE, RED, WHITE + vec2, Color, EngineContext, Vec2, BLACK, BLUE, PURPLE, RED, WHITE }; use egui::widget_text::RichText; -pub fn draw_batterie(state: &State, _engine: &EngineContext<'_>) { - // seperate fill state into smaller section for better readability - let section_count = 5; - let mut start_positon = screen_to_world(Vec2::new(screen_width(), screen_height())); +// seperate fill state into smaller section for better readability +const BATTERY_SECTION_COUNT: u8 = 5; +const BATTERY_SECTION_WIDTH: f32 = 1.0; +const BATTERY_SECTION_HEIGHT: f32 = 0.5; + +fn draw_battery(mut start_position: Vec2, charge: f32, max_charge: f32, color: Color) { // section size in world codinates - let section_size = Vec2::new(0.5, 0.25); - start_positon.x -= 0.5 * section_size.x + 0.5 * section_size.y; - start_positon.y += 0.5 * section_size.y + 0.5 * section_size.y; + let section_size = vec2(BATTERY_SECTION_WIDTH, BATTERY_SECTION_HEIGHT); + start_position.x -= 0.5 * section_size.x + 0.5 * section_size.y; + start_position.y += 0.5 * section_size.y + 0.5 * section_size.y; // draw fill level { - let ghost = &state.ghost; - let percent = ghost.charge / ghost.max_charge; + let percent = charge / max_charge; let mut size = section_size; - size.y = section_size.y * section_count as f32; - let mut position = start_positon; + size.y = section_size.y * BATTERY_SECTION_COUNT as f32; + let mut position = start_position; position.y += 0.5 * -section_size.y + 0.5 * size.y; draw_rect(position, size, BLACK, ZLayer::UI.into()); size.y *= percent; - let mut position = start_positon; + let mut position = start_position; position.y += 0.5 * -section_size.y + 0.5 * size.y; draw_rect(position, size, BLUE, ZLayer::UI + 1); } // draw sections - for i in 0 .. section_count { - let mut position = start_positon; + for i in 0 .. BATTERY_SECTION_COUNT { + let mut position = start_position; position.y += i as f32 * section_size.y; - draw_rect_outline(position, section_size, 0.1, RED, ZLayer::UI + 2); + draw_rect_outline(position, section_size, 0.1, color, ZLayer::UI + 2); } } -pub fn draw_highscore(state: &State, _engine: &EngineContext<'_>) { +pub fn draw_ghost_battery(state: &State) { + let start_position = screen_to_world(Vec2::new(screen_width(), screen_height())); + draw_battery( + start_position, + state.ghost.charge, + state.ghost.max_charge, + RED + ); +} + +pub fn draw_house_battery(state: &State) { + let Some(house) = state.house() else { + return; + }; + let mut start_position = screen_to_world(Vec2::new(screen_width(), screen_height())); + start_position.x -= 2.0 * BATTERY_SECTION_WIDTH; + draw_battery(start_position, house.charge, house.max_charge, PURPLE) +} + +pub fn draw_highscore(state: &State) { egui::Area::new("score") .anchor(egui::Align2::RIGHT_TOP, egui::vec2(0.0, 0.0)) .show(egui(), |ui| { @@ -49,7 +69,8 @@ pub fn draw_highscore(state: &State, _engine: &EngineContext<'_>) { }); } -pub fn draw(state: &State, engine: &EngineContext<'_>) { - draw_batterie(state, engine); - draw_highscore(state, engine); +pub fn draw(state: &State, _ctx: &EngineContext<'_>) { + draw_ghost_battery(state); + draw_house_battery(state); + draw_highscore(state); }