Placing items

This commit is contained in:
Michael Smith
2021-10-29 16:00:18 +02:00
parent b3431cbdf9
commit d3d0122065
3 changed files with 40 additions and 11 deletions

View File

@@ -7,6 +7,7 @@ use rand::Rng;
use std::cmp;
use tcod::colors::*;
use tcod::console::*;
use tcod::input::{self, Event, Key, Mouse};
use tcod::map::{FovAlgorithm, Map as FovMap};
const SCREEN_WIDTH: i32 = 80;
@@ -51,6 +52,8 @@ struct Tcod {
con: Offscreen,
panel: Offscreen,
fov: FovMap,
key: Key,
mouse: Mouse,
}
#[derive(Debug)]
@@ -271,10 +274,8 @@ fn handle_keys(tcod: &mut Tcod, game: &mut Game, objects: &mut Vec<Object>) -> P
use tcod::input::KeyCode::*;
use PlayerAction::*;
let key = tcod.root.wait_for_keypress(true);
let player_alive = objects[PLAYER].alive;
match (key, key.text(), player_alive) {
match (tcod.key, tcod.key.text(), player_alive) {
(
Key {
code: Enter,
@@ -556,6 +557,19 @@ fn render_bar(
);
}
fn get_names_under_mouse(mouse: Mouse, objects: &[Object], fov_map: &FovMap) -> String {
let (x, y) = (mouse.cx as i32, mouse.cy as i32);
// Create a list with the names of all objects at the mouse's coordinates and in FOV
let names = objects
.iter()
.filter(|obj| obj.pos() == (x, y) && fov_map.is_in_fov(obj.x, obj.y))
.map(|obj| obj.name.clone())
.collect::<Vec<_>>();
names.join(", ")
}
fn render_all(tcod: &mut Tcod, game: &mut Game, objects: &[Object], fov_recompute: bool) {
if fov_recompute {
let player = &objects[PLAYER];
@@ -633,6 +647,16 @@ fn render_all(tcod: &mut Tcod, game: &mut Game, objects: &[Object], fov_recomput
DARKER_RED,
);
// Display names of objects under the mouse
tcod.panel.set_default_foreground(LIGHT_GREY);
tcod.panel.print_ex(
1,
0,
BackgroundFlag::None,
TextAlignment::Left,
get_names_under_mouse(tcod.mouse, objects, &tcod.fov),
);
// Print the game messages, one line at a time
let mut y = MSG_HEIGHT as i32;
for &(ref msg, color) in game.messages.iter().rev() {
@@ -672,6 +696,8 @@ fn main() {
con: Offscreen::new(MAP_WIDTH, MAP_HEIGHT),
panel: Offscreen::new(SCREEN_WIDTH, PANEL_HEIGHT),
fov: FovMap::new(MAP_WIDTH, MAP_HEIGHT),
key: Default::default(),
mouse: Default::default(),
};
// Create player object
@@ -716,8 +742,13 @@ fn main() {
// Main game loop
while !tcod.root.window_closed() {
tcod.con.clear();
match input::check_for_event(input::MOUSE | input::KEY_PRESS) {
Some((_, Event::Mouse(m))) => tcod.mouse = m,
Some((_, Event::Key(k))) => tcod.key = k,
_ => tcod.key = Default::default(),
}
tcod.con.clear();
let fov_recompute = previous_player_position != (objects[PLAYER].pos());
render_all(&mut tcod, &mut game, &objects, fov_recompute);
tcod.root.flush();