Skip to content

Commit

Permalink
colors on the health bar
Browse files Browse the repository at this point in the history
rpruizc committed Sep 14, 2020
1 parent fbff011 commit 49c6403
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use crate::world::HitPoints;

use chargrid::{
render::{ColModify, Frame, Style, View, ViewContext},
decorator::{AlignView, Alignment, BoundView},
render::{ColModify, Frame, Style, View, ViewCell, ViewContext},
text::StringViewSingleLine,
};
use coord_2d::{Coord, Size};
use rgb24::Rgb24;

const HEALTH_WIDTH: u32 = 10;
const HEALTH_FILL_COLOUR: Rgb24 = Rgb24::new(200, 0, 0);
const HEALTH_EMPTY_COLOUR: Rgb24 = Rgb24::new(100, 0, 0);

pub struct UiData {
pub player_hit_points: HitPoints,
}
@@ -29,7 +35,34 @@ impl View<UiData> for UiView {
"{}/{}",
data.player_hit_points.current, data.player_hit_points.max
).unwrap();
StringViewSingleLine::new(Style::new().with_foreground(Rgb24::new_grey(255)))
.view(&self.buf, context, frame);
let mut hit_points_text_view = BoundView {
size: Size::new(HEALTH_WIDTH, 1),
view: AlignView {
alignment: Alignment::centre(),
view: StringViewSingleLine::new(Style::new().with_foreground(Rgb24::new_grey(255))),
},
};
hit_points_text_view.view(&self.buf, context.add_depth(1), frame);
let mut health_fill_width =
(data.player_hit_points.current * HEALTH_WIDTH) / data.player_hit_points.max;
if data.player_hit_points.current > 0 {
health_fill_width = health_fill_width.max(1);
}
for i in 0..health_fill_width {
frame.set_cell_relative(
Coord::new(i as i32, 0),
0,
ViewCell::new().with_background(HEALTH_FILL_COLOUR),
context,
);
}
for i in health_fill_width..HEALTH_WIDTH {
frame.set_cell_relative(
Coord::new(i as i32, 0),
0,
ViewCell::new().with_background(HEALTH_EMPTY_COLOUR),
context,
);
}
}
}

0 comments on commit 49c6403

Please sign in to comment.