initial commit
using rltk
This commit is contained in:
parent
40a2ac07be
commit
d3a09df7a8
29 changed files with 3323 additions and 0 deletions
112
src/gui.rs
Normal file
112
src/gui.rs
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
use super::{CombatStats, GameLog, Map, Name, Player, Point, Position};
|
||||
use rltk::{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));
|
||||
|
||||
// Render stats
|
||||
let combat_stats = ecs.read_storage::<CombatStats>();
|
||||
let players = ecs.read_storage::<Player>();
|
||||
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(28, 43, 51, stats.hp, stats.max_hp, RGB::named(rltk::RED), RGB::named(rltk::BLACK));
|
||||
}
|
||||
|
||||
// Render message log
|
||||
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;
|
||||
}
|
||||
|
||||
// Render 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(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue