add gameover #20
4 changed files with 81 additions and 20 deletions
|
@ -1,7 +1,8 @@
|
||||||
use super::{furniture::Furniture, grid::Grid};
|
use super::{furniture::Furniture, grid::Grid};
|
||||||
use crate::game;
|
use crate::game;
|
||||||
use comfy::{
|
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;
|
use indexmap::IndexSet;
|
||||||
|
|
||||||
|
@ -88,14 +89,17 @@ impl Room {
|
||||||
empty_spots.swap_remove_index(random_idx)
|
empty_spots.swap_remove_index(random_idx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn random_empty_spot_size(empty_spots: &mut IndexSet<u8>, size: u8) -> Option<u8> {
|
fn random_empty_spot_size(
|
||||||
|
empty_spots: &mut IndexSet<u8>,
|
||||||
|
size: u8
|
||||||
|
) -> Option<u8> {
|
||||||
let mut empty_spots_size = IndexSet::<u8>::new();
|
let mut empty_spots_size = IndexSet::<u8>::new();
|
||||||
|
|
||||||
for &index in empty_spots.iter() {
|
for &index in empty_spots.iter() {
|
||||||
let mut is_valid = true;
|
let mut is_valid = true;
|
||||||
|
|
||||||
for offset in 0..size{
|
for offset in 0 .. size {
|
||||||
if !empty_spots.contains(&(index + offset)){
|
if !empty_spots.contains(&(index + offset)) {
|
||||||
is_valid = false;
|
is_valid = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -109,7 +113,7 @@ impl Room {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let random_idx = usize::gen_range(0, empty_spots_size.len());
|
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);
|
empty_spots.swap_remove_index(random_idx + offset as usize);
|
||||||
}
|
}
|
||||||
Some(random_idx as u8)
|
Some(random_idx as u8)
|
||||||
|
@ -293,10 +297,9 @@ impl Room {
|
||||||
pos: vec2(pos as f32, 0.0),
|
pos: vec2(pos as f32, 0.0),
|
||||||
size: vec2(3.0, 1.0),
|
size: vec2(3.0, 1.0),
|
||||||
f: Furniture::new("bedroom", "couch", ctx),
|
f: Furniture::new("bedroom", "couch", ctx),
|
||||||
z:0
|
z: 0
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(pos) = random_empty_spot(&mut empty_spots) {
|
if let Some(pos) = random_empty_spot(&mut empty_spots) {
|
||||||
|
@ -304,7 +307,7 @@ impl Room {
|
||||||
pos: vec2(pos as f32, 0.0),
|
pos: vec2(pos as f32, 0.0),
|
||||||
size: vec2(1.0, 2.0),
|
size: vec2(1.0, 2.0),
|
||||||
f: Furniture::new("bedroom", "bookshelf", ctx),
|
f: Furniture::new("bedroom", "bookshelf", ctx),
|
||||||
z:0
|
z: 0
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -313,10 +316,9 @@ impl Room {
|
||||||
pos: vec2(pos as f32, 0.0),
|
pos: vec2(pos as f32, 0.0),
|
||||||
size: vec2(0.5, 0.9),
|
size: vec2(0.5, 0.9),
|
||||||
f: Furniture::new("bedroom", "mini_ac", ctx),
|
f: Furniture::new("bedroom", "mini_ac", ctx),
|
||||||
z:0
|
z: 0
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|
11
src/game.rs
11
src/game.rs
|
@ -75,7 +75,8 @@ pub enum ZLayer {
|
||||||
MapMax = -1,
|
MapMax = -1,
|
||||||
Human = 0,
|
Human = 0,
|
||||||
Ghost = 1,
|
Ghost = 1,
|
||||||
UI = 100
|
UI = 100,
|
||||||
|
GameOver = 1000
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<ZLayer> for i32 {
|
impl From<ZLayer> for i32 {
|
||||||
|
@ -108,11 +109,16 @@ pub fn setup(_state: &mut State, ctx: &mut EngineContext<'_>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(state: &mut State, engine: &mut EngineContext<'_>) {
|
pub fn update(state: &mut State, engine: &mut EngineContext<'_>) {
|
||||||
|
if state.ghost.charge > 0.0 {
|
||||||
state.score += engine.delta * 10.0;
|
state.score += engine.delta * 10.0;
|
||||||
match state.activity {
|
match state.activity {
|
||||||
Activity::House(_) => house::update(state, engine),
|
Activity::House(_) => house::update(state, engine),
|
||||||
Activity::Overworld => overworld::update(state, engine)
|
Activity::Overworld => overworld::update(state, engine)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
crate::game_over::update(state, engine);
|
||||||
|
}
|
||||||
state.ghost.charge = state.ghost.charge.max(0.0);
|
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)
|
Activity::Overworld => overworld::draw(state, engine)
|
||||||
}
|
}
|
||||||
crate::ui::draw(state, engine);
|
crate::ui::draw(state, engine);
|
||||||
|
if state.ghost.charge <= 0.0 {
|
||||||
|
crate::game_over::draw(state, engine);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
52
src/game_over.rs
Normal file
52
src/game_over.rs
Normal file
|
@ -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)
|
||||||
|
);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
}
|
|
@ -5,9 +5,9 @@
|
||||||
mod assets {
|
mod assets {
|
||||||
include!(env!("ASSETS_RS"));
|
include!(env!("ASSETS_RS"));
|
||||||
}
|
}
|
||||||
|
|
||||||
mod activities;
|
mod activities;
|
||||||
mod game;
|
mod game;
|
||||||
|
mod game_over;
|
||||||
mod ui;
|
mod ui;
|
||||||
|
|
||||||
use std::env::var;
|
use std::env::var;
|
||||||
|
@ -68,14 +68,12 @@ impl State {
|
||||||
|
|
||||||
impl GameLoop for State {
|
impl GameLoop for State {
|
||||||
fn new(_c: &mut EngineState) -> Self {
|
fn new(_c: &mut EngineState) -> Self {
|
||||||
Self {
|
Default::default()
|
||||||
dev: dev_from_env(),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self, ctx: &mut EngineContext<'_>) {
|
fn update(&mut self, ctx: &mut EngineContext<'_>) {
|
||||||
if !self.setup_called {
|
if !self.setup_called {
|
||||||
|
self.dev = dev_from_env();
|
||||||
game::setup(self, ctx);
|
game::setup(self, ctx);
|
||||||
game::setup(self, ctx);
|
game::setup(self, ctx);
|
||||||
self.setup_called = true;
|
self.setup_called = true;
|
||||||
|
|
Loading…
Reference in a new issue