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):
"""