2024-07-07 14:39:54 +00:00
|
|
|
use crate::{game::ZLayer, State};
|
2024-07-06 20:45:05 +00:00
|
|
|
use comfy::{
|
2024-07-07 10:02:48 +00:00
|
|
|
draw_rect, draw_rect_outline, egui, screen_height, screen_to_world, screen_width,
|
2024-07-07 15:18:32 +00:00
|
|
|
vec2, Color, EngineContext, Vec2, BLACK, BLUE, PURPLE, RED, WHITE
|
2024-07-06 20:45:05 +00:00
|
|
|
};
|
2024-07-07 10:02:48 +00:00
|
|
|
use egui::widget_text::RichText;
|
2024-07-06 20:45:05 +00:00
|
|
|
|
2024-07-07 15:18:32 +00:00
|
|
|
// 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) {
|
2024-07-06 20:45:05 +00:00
|
|
|
// section size in world codinates
|
2024-07-07 15:18:32 +00:00
|
|
|
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;
|
2024-07-06 20:45:05 +00:00
|
|
|
// draw fill level
|
|
|
|
{
|
2024-07-07 15:18:32 +00:00
|
|
|
let percent = charge / max_charge;
|
2024-07-06 20:45:05 +00:00
|
|
|
let mut size = section_size;
|
2024-07-07 15:18:32 +00:00
|
|
|
size.y = section_size.y * BATTERY_SECTION_COUNT as f32;
|
|
|
|
let mut position = start_position;
|
2024-07-06 20:45:05 +00:00
|
|
|
position.y += 0.5 * -section_size.y + 0.5 * size.y;
|
2024-07-07 14:39:54 +00:00
|
|
|
draw_rect(position, size, BLACK, ZLayer::UI.into());
|
|
|
|
size.y *= percent;
|
2024-07-07 15:18:32 +00:00
|
|
|
let mut position = start_position;
|
2024-07-07 14:39:54 +00:00
|
|
|
position.y += 0.5 * -section_size.y + 0.5 * size.y;
|
|
|
|
draw_rect(position, size, BLUE, ZLayer::UI + 1);
|
2024-07-06 20:45:05 +00:00
|
|
|
}
|
|
|
|
// draw sections
|
2024-07-07 15:18:32 +00:00
|
|
|
for i in 0 .. BATTERY_SECTION_COUNT {
|
|
|
|
let mut position = start_position;
|
2024-07-06 20:45:05 +00:00
|
|
|
position.y += i as f32 * section_size.y;
|
2024-07-07 15:18:32 +00:00
|
|
|
draw_rect_outline(position, section_size, 0.1, color, ZLayer::UI + 2);
|
2024-07-06 20:45:05 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-07 10:02:48 +00:00
|
|
|
|
2024-07-07 15:18:32 +00:00
|
|
|
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) {
|
2024-07-07 10:02:48 +00:00
|
|
|
egui::Area::new("score")
|
|
|
|
.anchor(egui::Align2::RIGHT_TOP, egui::vec2(0.0, 0.0))
|
|
|
|
.show(egui(), |ui| {
|
|
|
|
ui.label(
|
2024-07-07 14:39:54 +00:00
|
|
|
RichText::new(format!("{:.0} ", state.score))
|
2024-07-07 10:02:48 +00:00
|
|
|
.color(WHITE)
|
|
|
|
.monospace()
|
|
|
|
.size(16.0)
|
|
|
|
.strong()
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-07-07 15:18:32 +00:00
|
|
|
pub fn draw(state: &State, _ctx: &EngineContext<'_>) {
|
|
|
|
draw_ghost_battery(state);
|
|
|
|
draw_house_battery(state);
|
|
|
|
draw_highscore(state);
|
2024-07-07 10:02:48 +00:00
|
|
|
}
|