Skip to content

Commit 9b0b799

Browse files
committed
finished adding ability to drop items
1 parent 69f33e3 commit 9b0b799

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/gui.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,13 @@ pub enum ItemMenuResult {
137137
Selected,
138138
}
139139

140-
pub fn show_inventory(gs: &mut State, ctx: &mut BTerm) -> (ItemMenuResult, Option<Entity>) {
140+
pub fn show_inventory(
141+
gs: &mut State,
142+
ctx: &mut BTerm,
143+
title: impl Into<String>,
144+
) -> (ItemMenuResult, Option<Entity>) {
141145
const ESCAPE_MSG: &str = "ESCAPE to cancel";
146+
let title_str = title.into();
142147
let entities = gs.ecs.entities();
143148
let names = gs.ecs.read_storage::<Name>();
144149
let backpack = gs.ecs.read_storage::<InBackpack>();
@@ -153,7 +158,7 @@ pub fn show_inventory(gs: &mut State, ctx: &mut BTerm) -> (ItemMenuResult, Optio
153158
.fold((0, 0), |(size, max_length), (_item, name)| {
154159
(size + 1, max_length.max(name.name.len()))
155160
});
156-
let box_width = max(max_item_name_length, ESCAPE_MSG.len()) + 3 + 1;
161+
let box_width = max(max(max_item_name_length, ESCAPE_MSG.len()), title_str.len()) + 4;
157162

158163
// (start at: mid height - half of item size):
159164
let x_init = 15;
@@ -172,7 +177,7 @@ pub fn show_inventory(gs: &mut State, ctx: &mut BTerm) -> (ItemMenuResult, Optio
172177
y_box_init,
173178
RGB::named(YELLOW),
174179
RGB::named(BLACK),
175-
"Inventory",
180+
title_str,
176181
);
177182
ctx.print_color(
178183
x_init + 3,

src/main.rs

+26-4
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl GameState for State {
127127
newrunstate = RunState::AwaitingInput;
128128
}
129129
RunState::ShowInventory => {
130-
let result = show_inventory(self, ctx);
130+
let result = show_inventory(self, ctx, "Inventory: Use Item");
131131
match result.0 {
132132
gui::ItemMenuResult::Cancel => newrunstate = RunState::AwaitingInput,
133133
gui::ItemMenuResult::NoResponse => {}
@@ -147,16 +147,38 @@ impl GameState for State {
147147
let names = self.ecs.read_storage::<Name>();
148148
let mut gamelog = self.ecs.fetch_mut::<gamelog::GameLog>();
149149
gamelog.entries.push(format!(
150-
"You try to use the {}, but it isn't written yet!",
150+
"You use the {}.",
151151
names.get(item_entity).unwrap().name,
152152
));
153153
newrunstate = RunState::PlayerTurn;
154154
}
155155
}
156156
}
157157
RunState::ShowDropItem => {
158-
let todo = true;
159-
todo!()
158+
let result = gui::show_inventory(self, ctx, "Inventory: Drop Item");
159+
match result.0 {
160+
gui::ItemMenuResult::Cancel => newrunstate = RunState::AwaitingInput,
161+
gui::ItemMenuResult::NoResponse => {}
162+
gui::ItemMenuResult::Selected => {
163+
let item_entity = result
164+
.1
165+
.unwrap_or_else(|| panic!("Item selected but not found!"));
166+
let mut intent = self.ecs.write_storage::<EventWantsToDropItem>();
167+
intent
168+
.insert(
169+
get_player_unwrap(&self.ecs, PLAYER_NAME),
170+
EventWantsToDropItem { item: item_entity },
171+
)
172+
.unwrap_or_else(|_| panic!("Tried to drop item but failed!"));
173+
let names = self.ecs.read_storage::<Name>();
174+
let mut gamelog = self.ecs.fetch_mut::<gamelog::GameLog>();
175+
gamelog.entries.push(format!(
176+
"You drop the {}.",
177+
names.get(item_entity).unwrap().name,
178+
));
179+
newrunstate = RunState::PlayerTurn;
180+
}
181+
}
160182
}
161183
}
162184
{

0 commit comments

Comments
 (0)