From d3d0122065cd9fc68b0a7f7e3537617af560b3e7 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Fri, 29 Oct 2021 16:00:18 +0200 Subject: [PATCH] Placing items --- Cargo.lock | 10 ++++------ Cargo.toml | 2 +- src/main.rs | 39 +++++++++++++++++++++++++++++++++++---- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dc00aa9..152ebd0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -22,9 +22,9 @@ checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" [[package]] name = "lazy_static" -version = "0.1.16" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf186d1a8aa5f5bee5fd662bc9c1b949e0259e1bcc379d1f006847b0080c7417" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" @@ -96,8 +96,7 @@ dependencies = [ [[package]] name = "tcod" version = "0.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44e518b0661949712e8dbc6d6d9d7c00a405dd88bc539102c1edfc2d22e5e144" +source = "git+https://github.com/tomassedovic/tcod-rs#d4ad0749867acad6c1e510bf5eda802b074e153c" dependencies = [ "bitflags", "lazy_static", @@ -107,8 +106,7 @@ dependencies = [ [[package]] name = "tcod-sys" version = "5.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "730fb27d2261f7c860ae7a61c3ae70277f0836173ceeccb594193abb4fa5f4aa" +source = "git+https://github.com/tomassedovic/tcod-rs#d4ad0749867acad6c1e510bf5eda802b074e153c" dependencies = [ "cc", "pkg-config", diff --git a/Cargo.toml b/Cargo.toml index 450bdaa..9d9f51e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,5 +6,5 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -tcod = "*" +tcod = { git = "https://github.com/tomassedovic/tcod-rs" } rand = "0.3.9" diff --git a/src/main.rs b/src/main.rs index e4e349e..d966208 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) -> 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::>(); + + 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();