turtlegame/src/ui.rs
luckyturtledev dadc5d59db
All checks were successful
Rust / rustfmt (push) Successful in 19s
Rust / clippy (push) Successful in 1m3s
Rust / build (push) Successful in 2m15s
add score
2024-07-07 12:03:00 +02:00

51 lines
1.6 KiB
Rust

use crate::State;
use comfy::{
draw_rect, draw_rect_outline, egui, screen_height, screen_to_world, screen_width,
EngineContext, Vec2, BLUE, 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()));
// 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;
// draw fill level
{
let ghost = &state.ghost;
let percent = ghost.charge / ghost.max_charge;
let mut size = section_size;
size.y = section_size.y * section_count as f32 * percent;
let mut position = start_positon;
position.y += 0.5 * -section_size.y + 0.5 * size.y;
draw_rect(position, size, BLUE, 100);
}
// draw sections
for i in 0 .. section_count {
let mut position = start_positon;
position.y += i as f32 * section_size.y;
draw_rect_outline(position, section_size, 0.1, RED, 100);
}
}
pub fn draw_highscore(state: &State, _engine: &EngineContext<'_>) {
egui::Area::new("score")
.anchor(egui::Align2::RIGHT_TOP, egui::vec2(0.0, 0.0))
.show(egui(), |ui| {
ui.label(
RichText::new(format!("{:.0}", state.score))
.color(WHITE)
.monospace()
.size(16.0)
.strong()
);
});
}
pub fn draw(state: &State, engine: &EngineContext<'_>) {
draw_batterie(state, engine);
draw_highscore(state, engine);
}