Add shields to be more protected, see #48

This commit is contained in:
Yohann D'ANELLO 2020-12-18 20:01:52 +01:00
parent 9aa684fb77
commit c01307202a
2 changed files with 39 additions and 13 deletions

View File

@ -70,6 +70,7 @@ TexturePack.ASCII_PACK = TexturePack(
MERCHANT='M',
PLAYER='@',
RABBIT='Y',
SHIELD='D',
SUNFLOWER='I',
SWORD='\u2020',
TEDDY_BEAR='8',
@ -96,6 +97,7 @@ TexturePack.SQUIRREL_PACK = TexturePack(
PLAYER='🐿️ ',
MERCHANT='🦜',
RABBIT='🐇',
SHIELD='🛡️ ',
SUNFLOWER='🌻',
SWORD='🗡️ ',
TEDDY_BEAR='🧸',

View File

@ -40,14 +40,20 @@ class Item(Entity):
Indicates what should be done when the item is used.
"""
def equip(self) -> None:
def equip(self, armor: bool = False) -> None:
"""
Indicates what should be done when the item is equipped.
"""
if self.held_by.equipped_item:
self.held_by.equipped_item.unequip()
self.held_by.remove_from_inventory(self)
self.held_by.equipped_item = self
if armor:
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()
self.held_by.remove_from_inventory(self)
self.held_by.equipped_item = self
def unequip(self) -> None:
"""
@ -75,7 +81,7 @@ class Item(Entity):
@staticmethod
def get_all_items() -> list:
return [BodySnatchPotion, Bomb, Heart, Sword]
return [BodySnatchPotion, Bomb, Heart, Shield, Sword]
def be_sold(self, buyer: InventoryHolder, seller: InventoryHolder) -> bool:
"""
@ -225,20 +231,17 @@ class Sword(Weapon):
"""
A basic weapon
"""
strength: int
def __init__(self, name: str = "sword", price: int = 20, strength: int = 3,
def __init__(self, name: str = "sword", price: int = 20,
*args, **kwargs):
super().__init__(name=name, price=price, *args, **kwargs)
self.name = name
self.strength = strength
def equip(self) -> None:
def equip(self, armor: bool = False) -> None:
"""
When a sword is equipped, the player gains strength.
"""
super().equip()
self.held_by.strength += self.strength
self.held_by.strength += self.damage
def unequip(self) -> None:
"""
@ -246,7 +249,28 @@ class Sword(Weapon):
:return:
"""
super().unequip()
self.held_by.strength -= self.strength
self.held_by.strength -= self.damage
class Shield(Item):
constitution: int
def __init__(self, constitution: int = 2, *args, **kwargs):
super().__init__(name="shield", *args, **kwargs)
self.constitution = constitution
def equip(self, armor: bool = True) -> None:
super().equip(armor)
self.held_by.constitution += self.constitution
def unequip(self) -> None:
super().unequip()
self.held_by.constitution -= self.constitution
def save_state(self) -> dict:
d = super().save_state()
d["constitution"] = self.constitution
return d
class BodySnatchPotion(Item):