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

@ -47,20 +47,28 @@ class StatsDisplay(Display):
printed_items.append(item) printed_items.append(item)
self.addstr(self.pad, 9, 0, inventory_str) self.addstr(self.pad, 9, 0, inventory_str)
if self.player.equipped_item: if self.player.equipped_main:
self.addstr(self.pad, 10, 0, self.addstr(self.pad, 10, 0,
_("Equipped item:") + " " _("Equipped main:") + " "
f"{self.pack[self.player.equipped_item.name.upper()]}") f"{self.pack[self.player.equipped_main.name.upper()]}")
if self.player.equipped_armor: if self.player.equipped_secondary:
self.addstr(self.pad, 11, 0, self.addstr(self.pad, 11, 0,
_("Equipped armor:") + " " _("Equipped secondary:") + " "
f"{self.pack[self.player.equipped_secondary.name.upper()]}")
if self.player.equipped_armor:
self.addstr(self.pad, 12, 0,
_("Equipped chestplate:") + " "
f"{self.pack[self.player.equipped_armor.name.upper()]}") f"{self.pack[self.player.equipped_armor.name.upper()]}")
if self.player.equipped_helmet:
self.addstr(self.pad, 13, 0,
_("Equipped helmet:") + " "
f"{self.pack[self.player.equipped_helmet.name.upper()]}")
self.addstr(self.pad, 12, 0, f"{self.pack.HAZELNUT} " self.addstr(self.pad, 14, 0, f"{self.pack.HAZELNUT} "
f"x{self.player.hazel}") f"x{self.player.hazel}")
if self.player.dead: if self.player.dead:
self.addstr(self.pad, 14, 0, _("YOU ARE DEAD"), curses.COLOR_RED, self.addstr(self.pad, 15, 0, _("YOU ARE DEAD"), curses.COLOR_RED,
bold=True, blink=True, standout=True) bold=True, blink=True, standout=True)
def display(self) -> None: def display(self) -> None:

View File

@ -31,6 +31,9 @@ class TexturePack:
TIGER: str TIGER: str
WALL: str WALL: str
EAGLE: str EAGLE: str
SHIELD: str
CHESTPLATE: str
HELMET: str
ASCII_PACK: "TexturePack" ASCII_PACK: "TexturePack"
SQUIRREL_PACK: "TexturePack" SQUIRREL_PACK: "TexturePack"
@ -78,6 +81,8 @@ TexturePack.ASCII_PACK = TexturePack(
TIGER='n', TIGER='n',
WALL='#', WALL='#',
EAGLE='µ', EAGLE='µ',
CHESTPLATE='(',
HELMET='0',
) )
TexturePack.SQUIRREL_PACK = TexturePack( TexturePack.SQUIRREL_PACK = TexturePack(
@ -106,4 +111,6 @@ TexturePack.SQUIRREL_PACK = TexturePack(
TIGER='🐅', TIGER='🐅',
WALL='🧱', WALL='🧱',
EAGLE='🦅', EAGLE='🦅',
CHESTPLATE='🦺',
HELMET='⛑️',
) )

View File

@ -40,27 +40,53 @@ class Item(Entity):
Indicates what should be done when the item is used. 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. Indicates what should be done when the item is equipped.
""" """
if armor: if isinstance(self, Chestplate):
if self.held_by.equipped_armor: if self.held_by.equipped_armor:
self.held_by.equipped_armor.unequip() self.held_by.equipped_armor.unequip()
self.held_by.remove_from_inventory(self) self.held_by.remove_from_inventory(self)
self.held_by.equipped_armor = self self.held_by.equipped_armor = self
else: elif isinstance(self, Helmet):
if self.held_by.equipped_item: if self.held_by.equipped_helmet:
self.held_by.equipped_item.unequip() self.held_by.equipped_helmet.unequip()
self.held_by.remove_from_inventory(self) 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: def unequip(self) -> None:
""" """
Indicates what should be done when the item is unequipped. 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.add_to_inventory(self)
self.held_by.equipped_item = None
def hold(self, holder: InventoryHolder) -> None: def hold(self, holder: InventoryHolder) -> None:
""" """
@ -81,7 +107,8 @@ class Item(Entity):
@staticmethod @staticmethod
def get_all_items() -> list: 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: def be_sold(self, buyer: InventoryHolder, seller: InventoryHolder) -> bool:
""" """
@ -226,7 +253,7 @@ class Weapon(Item):
d["damage"] = self.damage d["damage"] = self.damage
return d return d
def equip(self, armor: bool = False) -> None: def equip(self) -> None:
""" """
When a weapon is equipped, the player gains strength. When a weapon is equipped, the player gains strength.
""" """
@ -252,15 +279,18 @@ class Sword(Weapon):
self.name = name self.name = name
class Shield(Item): class Armor(Item):
"""
Class of items that increase the player's constitution.
"""
constitution: int constitution: int
def __init__(self, constitution: int = 2, *args, **kwargs): def __init__(self, constitution: int, *args, **kwargs):
super().__init__(name="shield", *args, **kwargs) super().__init__(*args, **kwargs)
self.constitution = constitution self.constitution = constitution
def equip(self, armor: bool = True) -> None: def equip(self) -> None:
super().equip(armor) super().equip()
self.held_by.constitution += self.constitution self.held_by.constitution += self.constitution
def unequip(self) -> None: def unequip(self) -> None:
@ -272,6 +302,30 @@ class Shield(Item):
d["constitution"] = self.constitution d["constitution"] = self.constitution
return d 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): class BodySnatchPotion(Item):
""" """

View File

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