Skip to content

Commit

Permalink
Wearing or dropping a worn item unequips it
Browse files Browse the repository at this point in the history
  • Loading branch information
Shoes01 committed Apr 10, 2019
1 parent 22bd803 commit 4613a05
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 24 deletions.
2 changes: 1 addition & 1 deletion _data.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@
'heal': libtcod.light_blue,
'max_hp': libtcod.light_blue,
'wear': libtcod.light_blue,
'wear_already': libtcod.dark_red,
'wear_already': libtcod.light_blue,
'wear_fail': libtcod.dark_red
}
21 changes: 15 additions & 6 deletions processors/drop.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import esper

from components.actor.drop import DropComponent
from components.actor.equipment import EquipmentComponent
from components.actor.inventory import InventoryComponent
from components.item.pickedup import PickedupComponent
from components.game.popup import PopupComponent
Expand All @@ -12,7 +13,7 @@ def __init__(self):
super().__init__()

def process(self):
for ent, (drop, inv, pos) in self.world.get_components(DropComponent, InventoryComponent, PositionComponent):
for ent, (drop, eqp, inv, pos) in self.world.get_components(DropComponent, EquipmentComponent, InventoryComponent, PositionComponent):

if drop.item_id is None:
# Create popup menu for player to choose from.
Expand All @@ -21,10 +22,7 @@ def process(self):
# Present the player with a list of items.
n = 97
for item in self.world.component_for_entity(ent, InventoryComponent).inventory:
name = self.world.component_for_entity(item, NameComponent).name
char = chr(n)
result = {'action': {'drop': item}}
choices.append((name, char, result))
choices.append(self.generate_choices(chr(n), eqp, item))
n += 1

choices.append(('Nevermind', 'ESC', {'event': {'cancel': True}}))
Expand All @@ -41,4 +39,15 @@ def process(self):
# Remove the player from the item.
self.world.remove_component(item, PickedupComponent)
item_pos = self.world.component_for_entity(item, PositionComponent)
item_pos.x, item_pos.y = pos.x, pos.y
item_pos.x, item_pos.y = pos.x, pos.y

def generate_choices(self, char, eqp, item):
name = self.world.component_for_entity(item, NameComponent).name
result = None
if item in eqp.equipment:
name += ' (worn)'
result = {'action': {'wear': item}}
else:
result = {'action': {'drop': item}}

return (name, char, result)
15 changes: 10 additions & 5 deletions processors/inventory.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import esper

from components.actor.open_inv import OpenInventoryComponent
from components.actor.equipment import EquipmentComponent
from components.actor.inventory import InventoryComponent
from components.actor.open_inv import OpenInventoryComponent
from components.game.popup import PopupComponent
from components.name import NameComponent

Expand All @@ -10,7 +11,7 @@ def __init__(self):
super().__init__()

def process(self):
for ent, (inv, op_inv) in self.world.get_components(InventoryComponent, OpenInventoryComponent):
for ent, (eqp, inv, op_inv) in self.world.get_components(EquipmentComponent, InventoryComponent, OpenInventoryComponent):

# Create popup menu for player to choose from.
title = 'Inventory'
Expand All @@ -19,16 +20,20 @@ def process(self):
n = 97
for item in inv.inventory:
name = self.world.component_for_entity(item, NameComponent).name
drop_or_wear = 'drop'
if item in eqp.equipment:
name += ' (worn)'
drop_or_wear = 'wear'
char = chr(n)
result = self.generate_results(item, name)
result = self.generate_results(item, name, drop_or_wear)
choices.append((name, char, result))
n += 1

choices.append(('Nevermind', 'ESC', {'event': {'cancel': True}}))
self.world.component_for_entity(1, PopupComponent).menus.append( (title, choices) )
self.world.remove_component(ent, OpenInventoryComponent)

def generate_results(self, item, name):
def generate_results(self, item, name, drop_or_wear):
title = name
choices = [
(
Expand All @@ -39,7 +44,7 @@ def generate_results(self, item, name):
(
'Drop',
'd',
{'action': {'drop': item}}
{'action': {drop_or_wear: item}}
),
(
'Wear',
Expand Down
4 changes: 2 additions & 2 deletions processors/sub/message_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ def render_message_log(console_bundle, world):
if _wear:
name, turn = _wear

console.print(0, 0 + dy, '(Turn %s) Your equip your %s.' % (turn, name), LOG_COLORS['wear'])
console.print(0, 0 + dy, '(Turn %s) You equip your %s.' % (turn, name), LOG_COLORS['wear'])

if _wear_already:
name, turn = _wear_already

console.print(0, 0 + dy, '(Turn %s) You have already equipped your %s!' % (turn, name), LOG_COLORS['wear_already'])
console.print(0, 0 + dy, '(Turn %s) You unequip your %s.' % (turn, name), LOG_COLORS['wear_already'])

if _wear_fail:
name, turn = _wear_fail
Expand Down
23 changes: 13 additions & 10 deletions processors/wearable.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self):
super().__init__()

def process(self):
for ent, (wear) in self.world.get_component(WearComponent):
for ent, (eqp, wear) in self.world.get_components(EquipmentComponent, WearComponent):

if wear.item_id is None:
# Create popup menu for player to choose from.
Expand All @@ -23,10 +23,7 @@ def process(self):
# Present the player with a list of items from their inventory that they may consume.
n = 97
for item in self.world.component_for_entity(ent, InventoryComponent).inventory:
name = self.world.component_for_entity(item, NameComponent).name
char = chr(n)
result = {'action': {'wear': item}}
choices.append((name, char, result))
choices.append(self.generate_choice(chr(n), eqp, item))
n += 1

choices.append(('Nevermind', 'ESC', {'event': {'cancel': True}}))
Expand All @@ -36,13 +33,19 @@ def process(self):
else:
# Wear the item.
item = self.world.component_for_entity(ent, WearComponent).item_id
equipment_component = self.world.component_for_entity(ent, EquipmentComponent)
if item in equipment_component.equipment:
if item in eqp.equipment:
self.world.component_for_entity(1, MessageLogComponent).messages.append({'wear_already': (self.world.component_for_entity(item, NameComponent).name, self.world.component_for_entity(1, TurnCountComponent).turn_count)})
self.world.remove_component(ent, WearComponent)
eqp.equipment.remove(item)
elif self.world.has_component(item, WearableComponent):
self.world.component_for_entity(1, MessageLogComponent).messages.append({'wear': (self.world.component_for_entity(item, NameComponent).name, self.world.component_for_entity(1, TurnCountComponent).turn_count)})
equipment_component.equipment.append(item)
eqp.equipment.append(item)
else:
self.world.component_for_entity(1, MessageLogComponent).messages.append({'wear_fail': (self.world.component_for_entity(item, NameComponent).name, self.world.component_for_entity(1, TurnCountComponent).turn_count)})
self.world.remove_component(ent, WearComponent)
self.world.remove_component(ent, WearComponent)

def generate_choice(self, char, eqp, item):
name = self.world.component_for_entity(item, NameComponent).name
if item in eqp.equipment:
name += ' (worn)'
result = {'action': {'wear': item}}
return (name, char, result)

0 comments on commit 4613a05

Please sign in to comment.