display a second battery when inside a house

This commit is contained in:
Dominic 2024-07-07 16:58:56 +02:00
parent 2354e34142
commit 5fdbb0426f
Signed by: msrd0
GPG key ID: AAF7C8430CA3345D
3 changed files with 63 additions and 32 deletions

View file

@ -18,7 +18,12 @@ pub struct HouseState {
rooms: Vec<Room>,
//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 {
@ -36,7 +41,10 @@ impl HouseState {
room_count,
rooms,
player,
human_layer: false
human_layer: false,
// TODO this should be lower depending on the time elapsed
charge: 1000.0,
max_charge: 1000.0
}
}
}

View file

@ -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,7 +89,10 @@ impl Room {
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();
for &index in empty_spots.iter() {
@ -296,7 +300,6 @@ impl Room {
z: 0
});
}
}
if let Some(pos) = random_empty_spot(&mut empty_spots) {
@ -316,7 +319,6 @@ impl Room {
z: 0
});
}
},
_ => {}

View file

@ -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()));
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);
}