From b98bc61a027ca69a6b90d6f08821f637a165f0ef Mon Sep 17 00:00:00 2001 From: luckyturtledev Date: Sat, 6 Jul 2024 20:45:05 +0000 Subject: [PATCH] add battery-ui (#6) Reviewed-on: https://msrd0.dev/spielemarmelade/turtlegame/pulls/6 Co-authored-by: luckyturtledev Co-committed-by: luckyturtledev --- src/game.rs | 17 +++++++++++++++++ src/main.rs | 5 ++++- src/ui.rs | 31 +++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/ui.rs diff --git a/src/game.rs b/src/game.rs index fbe5ab7..2a2abf2 100644 --- a/src/game.rs +++ b/src/game.rs @@ -5,6 +5,22 @@ use crate::{ use comfy::EngineContext; use std::ops::Sub; +#[derive(Debug)] +pub struct Ghost { + /// current electric charge of the Ghost + pub charge: f32, + /// max electric charge of the Ghost + pub max_charge: f32 +} + +impl Default for Ghost { + fn default() -> Self { + Self { + charge: 1000.0, + max_charge: 1000.0 + } + } +} #[repr(i32)] pub enum ZLayer { MapMax = -1, @@ -39,4 +55,5 @@ pub fn draw(state: &State, engine: &EngineContext<'_>) { Activity::House => house::draw(state, engine), Activity::Overworld => overworld::draw(state, engine) } + crate::ui::draw(state, engine); } diff --git a/src/main.rs b/src/main.rs index 21b58d5..0f0fe28 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,10 +8,12 @@ mod assets { mod activities; mod game; +mod ui; use self::{ activities::{house::HouseState, overworld::worldgen::Overworld, Activity}, - assets::Assets + assets::Assets, + game::Ghost }; use comfy::{ init_game_config, pollster, run_comfy_main_async, EngineContext, EngineState, @@ -24,6 +26,7 @@ const GAME_NAME: &str = "Powercreep"; struct State { setup_called: bool, activity: Activity, + ghost: Ghost, overworld: Overworld, house: HouseState } diff --git a/src/ui.rs b/src/ui.rs new file mode 100644 index 0000000..7de61ee --- /dev/null +++ b/src/ui.rs @@ -0,0 +1,31 @@ +use crate::State; +use comfy::{ + draw_rect, draw_rect_outline, screen_height, screen_to_world, screen_width, + EngineContext, Vec2, BLUE, RED +}; + +pub fn draw(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); + } +}