Add tooltips system, names

This commit is contained in:
Daniel Lynn 2021-07-07 21:11:26 -05:00
parent cf599dd2be
commit 4aa39bfd6a
5 changed files with 56 additions and 6 deletions

View File

@ -26,3 +26,6 @@ pub struct Health {
pub current: i32,
pub max: i32,
}
#[derive(Clone, PartialEq)]
pub struct Name(pub String);

View File

@ -74,6 +74,8 @@ impl GameState for State {
ctx.cls();
self.resources.insert(ctx.key);
ctx.set_active_console(0);
self.resources.insert(Point::from_tuple(ctx.mouse_pos()));
let current_state = *self.resources.get::<TurnState>().unwrap();

View File

@ -12,22 +12,36 @@ pub fn spawn_player(ecs: &mut World, pos: Point) {
current: 20,
max: 20,
},
Name("Player 1".to_string()),
));
}
pub fn spawn_monster(ecs: &mut World, rng: &mut RandomNumberGenerator, pos: Point) {
let (hp, name, glyph) = match rng.roll_dice(1, 10) {
1..=8 => goblin(),
_ => orc(),
};
ecs.push((
Enemy,
pos,
Render {
color: ColorPair::new(WHITE, BLACK),
glyph: match rng.range(0, 4) {
0 => to_cp437('E'),
1 => to_cp437('O'),
2 => to_cp437('o'),
_ => to_cp437('g'),
},
glyph,
},
MovingRandomly,
Health {
current: hp,
max: hp,
},
Name(name),
));
}
fn goblin() -> (i32, String, FontCharType) {
(1, "Goblin".to_string(), to_cp437('g'))
}
fn orc() -> (i32, String, FontCharType) {
(2, "Orc".to_string(), to_cp437('o'))
}

View File

@ -6,6 +6,7 @@ mod map_render;
mod movement;
mod player_input;
mod random_move;
mod tooltips;
use crate::prelude::*;
@ -16,6 +17,7 @@ pub fn build_input_scheduler() -> Schedule {
.add_system(map_render::map_render_system())
.add_system(entity_render::entity_render_system())
.add_system(hud::hud_system())
.add_system(tooltips::tooltips_system())
.build()
}
@ -28,6 +30,7 @@ pub fn build_player_scheduler() -> Schedule {
.add_system(map_render::map_render_system())
.add_system(entity_render::entity_render_system())
.add_system(hud::hud_system())
.add_system(tooltips::tooltips_system())
.add_system(end_turn::end_turn_system())
.build()
}
@ -41,6 +44,7 @@ pub fn build_monster_scheduler() -> Schedule {
.add_system(map_render::map_render_system())
.add_system(entity_render::entity_render_system())
.add_system(hud::hud_system())
.add_system(tooltips::tooltips_system())
.add_system(end_turn::end_turn_system())
.build()
}

27
src/systems/tooltips.rs Normal file
View File

@ -0,0 +1,27 @@
use crate::prelude::*;
#[system]
#[read_component(Point)]
#[read_component(Name)]
#[read_component(Health)]
pub fn tooltips(ecs: &SubWorld, #[resource] mouse_pos: &Point, #[resource] camera: &Camera) {
let mut positions = <(Entity, &Point, &Name)>::query();
let offset = Point::new(camera.left_x, camera.top_y);
let map_pos = *mouse_pos + offset;
let mut draw_batch = DrawBatch::new();
draw_batch.target(2);
positions
.iter(ecs)
.filter(|(_, pos, _)| **pos == map_pos)
.for_each(|(entity, _, name)| {
let screen_pos = *mouse_pos * 4;
let display =
if let Ok(health) = ecs.entry_ref(*entity).unwrap().get_component::<Health>() {
format!("{} : {} hp", &name.0, health.current)
} else {
name.0.clone()
};
draw_batch.print(screen_pos, &display);
});
draw_batch.submit(10100).expect("Batch error");
}