add gameover #20

Merged
LuckyTurtleDev merged 4 commits from gameover into main 2024-07-07 16:46:29 +00:00
2 changed files with 36 additions and 38 deletions
Showing only changes of commit 612d9e4913 - Show all commits

View file

@ -1,11 +1,10 @@
use super::{furniture::Furniture, grid::Grid};
use crate::game::{self, ZLayer};
use comfy::{
draw_rect, draw_rect_outline, draw_sprite, error, random_i32, vec2, EngineContext,
HashSet, RandomRange as _, Vec2, GREEN, PURPLE, RED, WHITE,
draw_rect, draw_rect_outline, draw_sprite, error, random_i32, texture_id, vec2,
EngineContext, HashSet, RandomRange as _, Vec2, GREEN, PURPLE, RED, WHITE
};
use indexmap::IndexSet;
use comfy::texture_id;
pub const SCALE: f32 = 4.0;

View file

@ -76,7 +76,7 @@ pub enum ZLayer {
Human = 0,
Ghost = 1,
UI = 100,
GameOver = 1000,
GameOver = 1000
}
impl From<ZLayer> for i32 {
@ -135,44 +135,43 @@ pub const HOUSE_DISCHARGE_RATE: f32 = 30.0;
pub const GHOST_CHARGE_RATE: f32 = 200.0;
pub fn update(state: &mut State, ctx: &mut EngineContext<'_>) {
if state.ghost.charge > 0.0 {
// 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, 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;
// Update the score. It's based on time.
state.score += ctx.delta * 10.0;
// Update the currently active activity.
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;
}
},
_ => {}
Activity::House(_) => house::update(state, ctx),
Activity::Overworld => overworld::update(state, ctx)
}
}}
else {
// 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;
}
},
_ => {}
}
}
} else {
crate::game_over::update(state, ctx);
}