1
0
Fork 0
roguelike-game/src/gui.rs

150 lines
4.1 KiB
Rust

use crate::{gamelog::GameLog, CombatStats, Map, Name, Player, Position};
use rltk::{Point, Rltk, RGB};
use specs::prelude::*;
pub fn draw_ui(ecs: &World, ctx: &mut Rltk) {
ctx.draw_box(
0,
43,
79,
6,
RGB::named(rltk::WHITE),
RGB::named(rltk::BLACK),
);
let combat_stats = ecs.read_storage::<CombatStats>();
let players = ecs.read_storage::<Player>();
// Display player health
for (_player, stats) in (&players, &combat_stats).join() {
let health = format!(" HP: {} / {} ", stats.hp, stats.max_hp);
ctx.print_color(
12,
43,
RGB::named(rltk::YELLOW),
RGB::named(rltk::BLACK),
&health,
);
ctx.draw_bar_horizontal(
29,
43,
51,
stats.hp,
stats.max_hp,
RGB::named(rltk::RED),
RGB::named(rltk::BLACK),
);
}
// Display logs
let log = ecs.fetch::<GameLog>();
let mut y = 44;
for s in log.entries.iter().rev() {
if y < 49 {
ctx.print(2, y, s);
}
y += 1;
}
// Mouse cursor
let mouse_pos = ctx.mouse_pos();
ctx.set_bg(mouse_pos.0, mouse_pos.1, RGB::named(rltk::MAGENTA));
draw_tooltips(ecs, ctx);
}
fn draw_tooltips(ecs: &World, ctx: &mut Rltk) {
let map = ecs.fetch::<Map>();
let names = ecs.read_storage::<Name>();
let positions = ecs.read_storage::<Position>();
let mouse_pos = ctx.mouse_pos();
if mouse_pos.0 >= map.width || mouse_pos.1 >= map.height {
return;
}
let mut tooltip: Vec<String> = Vec::new();
for (name, position) in (&names, &positions).join() {
let idx = map.xy_idx(position.x, position.y);
if position.x == mouse_pos.0 && position.y == mouse_pos.1 && map.visible_tiles[idx] {
tooltip.push(name.name.to_string());
}
}
if !tooltip.is_empty() {
let mut width: i32 = 0;
for s in tooltip.iter() {
if width < s.len() as i32 {
width = s.len() as i32;
}
}
width += 3;
if mouse_pos.0 > 40 {
let arrow_pos = Point::new(mouse_pos.0 - 2, mouse_pos.1);
let left_x = mouse_pos.0 - width;
let mut y = mouse_pos.1;
for s in tooltip.iter() {
ctx.print_color(left_x, y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), s);
let padding = (width - s.len() as i32) - 1;
for i in 0..padding {
ctx.print_color(
arrow_pos.x - i,
y,
RGB::named(rltk::WHITE),
RGB::named(rltk::GREY),
&" ".to_string(),
);
}
y += 1;
}
ctx.print_color(
arrow_pos.x,
arrow_pos.y,
RGB::named(rltk::WHITE),
RGB::named(rltk::GREY),
&"->".to_string(),
);
} else {
let arrow_pos = Point::new(mouse_pos.0 + 1, mouse_pos.1);
let left_x = mouse_pos.0 + 3;
let mut y = mouse_pos.1;
for s in tooltip.iter() {
ctx.print_color(
left_x + 1,
y,
RGB::named(rltk::WHITE),
RGB::named(rltk::GREY),
s,
);
let padding = (width - s.len() as i32) - 1;
for i in 0..padding {
ctx.print_color(
arrow_pos.x + 1 + i,
y,
RGB::named(rltk::WHITE),
RGB::named(rltk::GREY),
&" ".to_string(),
);
}
y += 1;
}
ctx.print_color(
arrow_pos.x,
arrow_pos.y,
RGB::named(rltk::WHITE),
RGB::named(rltk::GREY),
&"<-".to_string(),
);
}
}
}