diff --git a/src/main.rs b/src/main.rs index 03537cf..9b3fb41 100644 --- a/src/main.rs +++ b/src/main.rs @@ -376,6 +376,19 @@ fn handle_keys(tcod: &mut Tcod, game: &mut Game, objects: &mut Vec) -> P DidntTakeTurn } + (Key { code: Text, .. }, "d", true) => { + // Show the inventory; if an item is selected, drop it + let inventory_index = inventory_menu( + &game.inventory, + "Press the key next to an item to drop it, or any other to cancel.\n", + &mut tcod.root, + ); + if let Some(inventory_index) = inventory_index { + drop_item(inventory_index, game, objects); + } + DidntTakeTurn + } + _ => DidntTakeTurn, } } @@ -1058,6 +1071,15 @@ fn target_monster( } } +fn drop_item(inventory_id: usize, game: &mut Game, objects: &mut Vec) { + let mut item = game.inventory.remove(inventory_id); + item.set_pos(objects[PLAYER].x, objects[PLAYER].y); + game + .messages + .add(format!("You dropped a {}.", item.name), YELLOW); + objects.push(item); +} + fn render_all(tcod: &mut Tcod, game: &mut Game, objects: &[Object], fov_recompute: bool) { if fov_recompute { let player = &objects[PLAYER];