The player now has two hands and a slot for a helmet and a chestplate. Accordingly, new classes of items have been added.

This commit is contained in:
eichhornchen
2021-01-06 10:46:36 +01:00
parent 601062237d
commit f3fe04e13a
4 changed files with 118 additions and 31 deletions

View File

@ -40,27 +40,53 @@ class Item(Entity):
Indicates what should be done when the item is used.
"""
def equip(self, armor: bool = False) -> None:
def equip(self) -> None:
"""
Indicates what should be done when the item is equipped.
"""
if armor:
if isinstance(self, Chestplate):
if self.held_by.equipped_armor:
self.held_by.equipped_armor.unequip()
self.held_by.remove_from_inventory(self)
self.held_by.equipped_armor = self
else:
if self.held_by.equipped_item:
self.held_by.equipped_item.unequip()
elif isinstance(self, Helmet):
if self.held_by.equipped_helmet:
self.held_by.equipped_helmet.unequip()
self.held_by.remove_from_inventory(self)
self.held_by.equipped_item = self
self.held_by.equipped_helmet = self
elif isinstance(self, Weapon):
if self.held_by.equipped_main:
if self.held_by.equipped_secondary:
self.held_by.equipped_secondary.unequip()
self.held_by.remove_from_inventory(self)
self.held_by.equipped_secondary = self
# For weapons, they are equipped as main only if main is empty.
else:
self.held_by.remove_from_inventory(self)
self.held_by.equipped_main = self
else:
# Other objects are only equipped as secondary.
if self.held_by.equipped_secondary:
self.held_by.equipped_secondary.unequip()
self.held_by.remove_from_inventory(self)
self.held_by.equipped_secondary = self
def unequip(self) -> None:
"""
Indicates what should be done when the item is unequipped.
"""
if isinstance(self, Chestplate):
self.held_by.equipped_armor = None
elif isinstance(self, Helmet):
self.held_by.equipped_helmet = None
elif isinstance(self, Weapon):
if self.held_by.equipped_main == self:
self.held_by.equipped_main = None
else:
self.held_by.equipped_secondary = None
else:
self.held_by.equipped_secondary = None
self.held_by.add_to_inventory(self)
self.held_by.equipped_item = None
def hold(self, holder: InventoryHolder) -> None:
"""
@ -81,7 +107,8 @@ class Item(Entity):
@staticmethod
def get_all_items() -> list:
return [BodySnatchPotion, Bomb, Heart, Shield, Sword]
return [BodySnatchPotion, Bomb, Heart, Shield, Sword,\
Chestplate, Helmet]
def be_sold(self, buyer: InventoryHolder, seller: InventoryHolder) -> bool:
"""
@ -226,7 +253,7 @@ class Weapon(Item):
d["damage"] = self.damage
return d
def equip(self, armor: bool = False) -> None:
def equip(self) -> None:
"""
When a weapon is equipped, the player gains strength.
"""
@ -252,15 +279,18 @@ class Sword(Weapon):
self.name = name
class Shield(Item):
class Armor(Item):
"""
Class of items that increase the player's constitution.
"""
constitution: int
def __init__(self, constitution: int = 2, *args, **kwargs):
super().__init__(name="shield", *args, **kwargs)
def __init__(self, constitution: int, *args, **kwargs):
super().__init__(*args, **kwargs)
self.constitution = constitution
def equip(self, armor: bool = True) -> None:
super().equip(armor)
def equip(self) -> None:
super().equip()
self.held_by.constitution += self.constitution
def unequip(self) -> None:
@ -272,6 +302,30 @@ class Shield(Item):
d["constitution"] = self.constitution
return d
class Shield(Armor):
"""
Class of shield items, they can be equipped in the other hand.
"""
def __init__(self, constitution: int = 2, *args, **kwargs):
super().__init__(name="shield", constitution=constitution, *args, **kwargs)
class Helmet(Armor):
"""
Class of helmet items, they can be equipped on the head.
"""
def __init__(self, constitution: int = 2, *args, **kwargs):
super().__init__(name="helmet", constitution=constitution, *args, **kwargs)
class Chestplate(Armor):
"""
Class of chestplate items, they can be equipped on the body.
"""
def __init__(self, constitution: int = 4, *args, **kwargs):
super().__init__(name="chestplate", constitution=constitution, *args, **kwargs)
class BodySnatchPotion(Item):
"""

View File

@ -17,15 +17,19 @@ class Player(InventoryHolder, FightingEntity):
current_xp: int = 0
max_xp: int = 10
paths: Dict[Tuple[int, int], Tuple[int, int]]
equipped_item: Optional[Item]
equipped_main: Optional[Item]
equipped_secondary: Optional[Item]
equipped_helmet: Optional[Item]
equipped_armor: Optional[Item]
def __init__(self, name: str = "player", maxhealth: int = 20,
strength: int = 5, intelligence: int = 1, charisma: int = 1,
dexterity: int = 1, constitution: int = 1, level: int = 1,
current_xp: int = 0, max_xp: int = 10, inventory: list = None,
hazel: int = 42, equipped_item: Optional[Item] = None,
hazel: int = 42, equipped_main: Optional[Item] = None,
equipped_armor: Optional[Item] = None, critical: int = 5,\
equipped_secondary: Optional[Item] = None, \
equipped_helmet: Optional[Item] = None, \
*args, **kwargs) -> None:
super().__init__(name=name, maxhealth=maxhealth, strength=strength,
intelligence=intelligence, charisma=charisma,
@ -36,12 +40,18 @@ class Player(InventoryHolder, FightingEntity):
self.inventory = self.translate_inventory(inventory or [])
self.paths = dict()
self.hazel = hazel
if isinstance(equipped_item, dict):
equipped_item = self.dict_to_item(equipped_item)
if isinstance(equipped_main, dict):
equipped_main = self.dict_to_item(equipped_main)
if isinstance(equipped_armor, dict):
equipped_armor = self.dict_to_item(equipped_armor)
self.equipped_item = equipped_item
if isinstance(equipped_secondary, dict):
equipped_secondary = self.dict_to_item(equipped_secondary)
if isinstance(equipped_helmet, dict):
equipped_helmet = self.dict_to_item(equipped_helmet)
self.equipped_main = equipped_main
self.equipped_armor = equipped_armor
self.equipped_secondary = equipped_secondary
self.equipped_helmet = equipped_helmet
def move(self, y: int, x: int) -> None:
"""
@ -79,10 +89,14 @@ class Player(InventoryHolder, FightingEntity):
"""
Remove the given item from the inventory, even if the item is equipped.
"""
if obj == self.equipped_item:
self.equipped_item = None
if obj == self.equipped_main:
self.equipped_main = None
elif obj == self.equipped_armor:
self.equipped_armor = None
elif obj == self.equipped_secondary:
self.equipped_secondary = None
elif obj == self.equipped_helmet:
self.equipped_helmet = None
else:
return super().remove_from_inventory(obj)
@ -165,8 +179,12 @@ class Player(InventoryHolder, FightingEntity):
d = super().save_state()
d["current_xp"] = self.current_xp
d["max_xp"] = self.max_xp
d["equipped_item"] = self.equipped_item.save_state()\
if self.equipped_item else None
d["equipped_main"] = self.equipped_main.save_state()\
if self.equipped_main else None
d["equipped_armor"] = self.equipped_armor.save_state()\
if self.equipped_armor else None
d["equipped_secondary"] = self.equipped_secondary.save_state()\
if self.equipped_secondary else None
d["equipped_helmet"] = self.equipped_helmet.save_state()\
if self.equipped_helmet else None
return d