From 7e52872f39b523653a41125829b787cbcb60b491 Mon Sep 17 00:00:00 2001 From: luckyturtledev Date: Sun, 7 Jul 2024 18:17:40 +0200 Subject: [PATCH 1/3] finish gameover --- src/activities/house/room.rs | 22 ++++++++------- src/game.rs | 19 +++++++++---- src/game_over.rs | 52 ++++++++++++++++++++++++++++++++++++ src/main.rs | 8 +++--- 4 files changed, 81 insertions(+), 20 deletions(-) create mode 100644 src/game_over.rs 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/game.rs b/src/game.rs index 4a45a4a..5c481b3 100644 --- a/src/game.rs +++ b/src/game.rs @@ -75,7 +75,8 @@ pub enum ZLayer { MapMax = -1, Human = 0, Ghost = 1, - UI = 100 + UI = 100, + GameOver = 1000 } impl From for i32 { @@ -108,10 +109,15 @@ pub fn setup(_state: &mut State, ctx: &mut EngineContext<'_>) { } pub fn update(state: &mut State, engine: &mut EngineContext<'_>) { - state.score += engine.delta * 10.0; - match state.activity { - Activity::House(_) => house::update(state, engine), - Activity::Overworld => overworld::update(state, engine) + if state.ghost.charge > 0.0 { + state.score += engine.delta * 10.0; + match state.activity { + Activity::House(_) => house::update(state, engine), + Activity::Overworld => overworld::update(state, engine) + } + } + { + crate::game_over::update(state, engine); } state.ghost.charge = state.ghost.charge.max(0.0); } @@ -122,4 +128,7 @@ pub fn draw(state: &State, engine: &EngineContext<'_>) { Activity::Overworld => overworld::draw(state, engine) } crate::ui::draw(state, engine); + if state.ghost.charge <= 0.0 { + crate::game_over::draw(state, engine); + } } diff --git a/src/game_over.rs b/src/game_over.rs new file mode 100644 index 0000000..0e2f434 --- /dev/null +++ b/src/game_over.rs @@ -0,0 +1,52 @@ +use std::mem::take; + +use crate::{game::ZLayer, State}; +use comfy::{ + draw_rect, egui, + egui::{Align, Layout}, + is_key_pressed, main_camera, Color, EngineContext, WHITE +}; +use egui::{Align2, RichText}; +use log::info; + +pub fn update(state: &mut State, _engine: &mut EngineContext<'_>) { + if is_key_pressed(comfy::KeyCode::Return) { + info!("Restart game"); + take(state); //reset to default + } +} + +pub fn draw(state: &State, _engine: &EngineContext<'_>) { + let cam = main_camera(); + draw_rect( + cam.center, + cam.world_viewport() * 1.2, + Color::new(0.0, 0.0, 0.0, 0.5), + ZLayer::GameOver.into() + ); + + egui::Area::new("game_over") + .anchor(Align2::CENTER_CENTER, egui::vec2(0.0, 0.0)) + .show(egui(), |ui| { + ui.with_layout(Layout::top_down_justified(Align::Center), |ui| { + ui.heading( + RichText::new( + "The poor spirit lost all their energy and starve to death" + ) + .color(WHITE) + .strong() + .size(50.0) + ); + ui.label( + RichText::new(format!("\nYour Score:\n{:.0}", state.score)) + .color(WHITE) + .size(30.0) + ); + ui.label( + RichText::new("\n\n\npress enter for restart") + .color(WHITE) + .size(30.0) + ); + }) + }); +} diff --git a/src/main.rs b/src/main.rs index 9573f72..a5b9b6d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,9 +5,9 @@ mod assets { include!(env!("ASSETS_RS")); } - mod activities; mod game; +mod game_over; mod ui; use std::env::var; @@ -68,14 +68,12 @@ impl State { impl GameLoop for State { fn new(_c: &mut EngineState) -> Self { - Self { - dev: dev_from_env(), - ..Default::default() - } + Default::default() } fn update(&mut self, ctx: &mut EngineContext<'_>) { if !self.setup_called { + self.dev = dev_from_env(); game::setup(self, ctx); game::setup(self, ctx); self.setup_called = true; -- 2.45.2 From fb5149945e1fd3b3f62cd8df9badbd06c1c2622b Mon Sep 17 00:00:00 2001 From: luckyturtledev Date: Sun, 7 Jul 2024 18:45:29 +0200 Subject: [PATCH 2/3] fix merge --- src/activities/house/room.rs | 4 ++-- src/game.rs | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/activities/house/room.rs b/src/activities/house/room.rs index 2c7c85c..9618f25 100644 --- a/src/activities/house/room.rs +++ b/src/activities/house/room.rs @@ -2,10 +2,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, Index, RandomRange as _, Vec2, GREEN, PURPLE, RED, WHITE - PURPLE, RED, WHITE + HashSet, RandomRange as _, Vec2, GREEN, PURPLE, RED, WHITE, }; use indexmap::IndexSet; +use comfy::texture_id; pub const SCALE: f32 = 4.0; diff --git a/src/game.rs b/src/game.rs index 6352858..31a4634 100644 --- a/src/game.rs +++ b/src/game.rs @@ -75,7 +75,8 @@ pub enum ZLayer { MapMax = -1, Human = 0, Ghost = 1, - UI = 100 + UI = 100, + GameOver = 1000, } impl From for i32 { @@ -134,6 +135,8 @@ 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; @@ -168,6 +171,9 @@ pub fn update(state: &mut State, ctx: &mut EngineContext<'_>) { }, _ => {} } + }} + else { + crate::game_over::update(state, ctx); } // Make sure the ghost's charge never drops below 0. @@ -180,4 +186,7 @@ pub fn draw(state: &State, engine: &EngineContext<'_>) { Activity::Overworld => overworld::draw(state, engine) } crate::ui::draw(state, engine); + if state.ghost.charge <= 0.0 { + crate::game_over::draw(state, engine); + } } -- 2.45.2 From 612d9e49138ef50df53e097499118497bb221d8d Mon Sep 17 00:00:00 2001 From: luckyturtledev Date: Sun, 7 Jul 2024 18:45:40 +0200 Subject: [PATCH 3/3] fmt --- src/activities/house/room.rs | 5 ++- src/game.rs | 69 ++++++++++++++++++------------------ 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/src/activities/house/room.rs b/src/activities/house/room.rs index 9618f25..7d3a06c 100644 --- a/src/activities/house/room.rs +++ b/src/activities/house/room.rs @@ -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; diff --git a/src/game.rs b/src/game.rs index 31a4634..534288d 100644 --- a/src/game.rs +++ b/src/game.rs @@ -76,7 +76,7 @@ pub enum ZLayer { Human = 0, Ghost = 1, UI = 100, - GameOver = 1000, + GameOver = 1000 } impl From 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); } -- 2.45.2