Skip to content

Commit 2e7cd70

Browse files
committed
add functionality for remove item from backpack
1 parent 4d43c14 commit 2e7cd70

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ <h1>BACKPACK</h1>
728728

729729
<div class="footer">
730730
<button id="item-use-btn">Use (R)</button>
731-
<button id="item-drop-btn">Drop (X)</button>
731+
<button id="item-drop-btn">Remove (X)</button>
732732
<button id="inventory-close-btn">Close (I)</button>
733733
</div>
734734
</div>

src/backpack.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,19 @@ export class Backpack {
119119
this.decreaseItemQty(selectedItem);
120120
}
121121

122-
dropItem() {}
122+
dropItem() {
123+
if (!selectedItem) return;
124+
const ind = this.inventory.findIndex(
125+
(item) => item.itemName === selectedItem
126+
);
127+
128+
if (ind != -1) {
129+
this.inventory.splice(ind, 1);
130+
selectedItem = null;
131+
this.saveInventoryState();
132+
this.render();
133+
}
134+
}
123135

124136
render() {
125137
const backpack = document.getElementById('backpack-content');
@@ -170,6 +182,8 @@ export class Backpack {
170182
}
171183
} else if (e.key == 'r') {
172184
this.useItem();
185+
} else if (e.key == 'x') {
186+
this.dropItem()
173187
}
174188
}
175189

0 commit comments

Comments
 (0)