Placing items

This commit is contained in:
Michael Smith 2021-10-29 16:23:44 +02:00
parent d3d0122065
commit 838dd7749e

View File

@ -46,6 +46,7 @@ const PANEL_Y: i32 = SCREEN_HEIGHT - PANEL_HEIGHT;
const MSG_X: i32 = BAR_WIDTH + 2;
const MSG_WIDTH: i32 = SCREEN_WIDTH - BAR_WIDTH - 2;
const MSG_HEIGHT: usize = PANEL_HEIGHT as usize - 1;
const MAX_ROOM_ITEMS: i32 = 2;
struct Tcod {
root: Root,
@ -178,6 +179,7 @@ type Map = Vec<Vec<Tile>>;
struct Game {
map: Map,
messages: Messages,
inventory: Vec<Object>,
}
// A rectangle on the map, used to characterise a room.
@ -269,6 +271,11 @@ impl DeathCallback {
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
enum Item {
Heal,
}
fn handle_keys(tcod: &mut Tcod, game: &mut Game, objects: &mut Vec<Object>) -> PlayerAction {
use tcod::input::Key;
use tcod::input::KeyCode::*;
@ -380,6 +387,7 @@ fn create_v_tunnel(y1: i32, y2: i32, x: i32, map: &mut Map) {
}
fn place_objects(room: Rect, map: &Map, objects: &mut Vec<Object>) {
// Generate some monsters
let num_monsters = rand::thread_rng().gen_range(0, MAX_ROOM_MONSTERS + 1);
for _ in 0..num_monsters {
@ -417,6 +425,22 @@ fn place_objects(room: Rect, map: &Map, objects: &mut Vec<Object>) {
objects.push(monster);
}
}
// Generate some items
let num_items = rand::thread_rng().gen_range(0, MAX_ROOM_ITEMS + 1);
for _ in 0..num_items {
// Choose random spot for this item
let x = rand::thread_rng().gen_range(room.x1 + 1, room.x2);
let y = rand::thread_rng().gen_range(room.y1 + 1, room.y2);
// Only place it if the tile is note blocked
if !is_blocked(x, y, map, objects) {
// Create a healing potion
let mut object = Object::new(x, y, '!', "healing potion", VIOLET, false);
objects.push(object);
}
}
}
fn is_blocked(x: i32, y: i32, map: &Map, objects: &[Object]) -> bool {
@ -570,6 +594,24 @@ fn get_names_under_mouse(mouse: Mouse, objects: &[Object], fov_map: &FovMap) ->
names.join(", ")
}
fn pick_item_up(object_id: usize, game: &mut Game, objects: &mut Vec<Object>) {
if game.inventory.len() >= 26 {
game.messages.add(
format!(
"Your inventory is full, cannot pick up {}.",
objects[object_id].name
),
RED,
);
} else {
let item = objects.swap_remove(object_id);
game
.messages
.add(format!("You picked up a {}!", item.name), GREEN);
game.inventory.push(item);
}
}
fn render_all(tcod: &mut Tcod, game: &mut Game, objects: &[Object], fov_recompute: bool) {
if fov_recompute {
let player = &objects[PLAYER];
@ -718,6 +760,7 @@ fn main() {
// Generate map
map: make_map(&mut objects),
messages: Messages::new(),
inventory: vec![],
};
// Populate FOV map, according to generated map