Items can be dropped/equipped/used

This commit is contained in:
Yohann D'ANELLO 2020-12-04 16:53:27 +01:00
parent 0da7486750
commit c7545e53f7
4 changed files with 48 additions and 10 deletions

View File

@ -20,16 +20,26 @@ class Item(Entity):
self.held = held self.held = held
self.held_by = held_by self.held_by = held_by
def drop(self, y: int, x: int) -> None: def drop(self) -> None:
""" """
The item is dropped from the inventory onto the floor The item is dropped from the inventory onto the floor
""" """
if self.held: if self.held:
self.held_by.inventory.remove(self) self.held_by.inventory.remove(self)
self.map.add_entity(self)
self.move(self.held_by.y, self.held_by.x)
self.held = False self.held = False
self.held_by = None self.held_by = None
self.map.add_entity(self)
self.move(y, x) def use(self) -> None:
"""
Indicates what should be done when the item is used.
"""
def equip(self) -> None:
"""
Indicates what should be done when the item is equipped.
"""
def hold(self, player: "Player") -> None: def hold(self, player: "Player") -> None:
""" """
@ -80,6 +90,7 @@ class Bomb(Item):
A bomb item intended to deal damage to enemies at long range A bomb item intended to deal damage to enemies at long range
""" """
damage: int = 5 damage: int = 5
tick: int
exploding: bool exploding: bool
def __init__(self, damage: int = 5, exploding: bool = False, def __init__(self, damage: int = 5, exploding: bool = False,
@ -87,20 +98,29 @@ class Bomb(Item):
super().__init__(name="bomb", *args, **kwargs) super().__init__(name="bomb", *args, **kwargs)
self.damage = damage self.damage = damage
self.exploding = exploding self.exploding = exploding
self.tick = 5
def drop(self, x: int, y: int) -> None: def use(self) -> None:
super().drop(x, y) """
self.exploding = True When the bomb is used, throw it and explodes it.
"""
if self.held:
super().drop()
self.exploding = True
def act(self, m: Map) -> None: def act(self, m: Map) -> None:
""" """
Special exploding action of the bomb Special exploding action of the bomb
""" """
if self.exploding: if self.exploding:
for e in m.entities.copy(): if self.tick > 0:
if abs(e.x - self.x) + abs(e.y - self.y) <= 1 and \ self.tick -= 1
isinstance(e, FightingEntity): else:
e.take_damage(self, self.damage) for e in m.entities.copy():
if abs(e.x - self.x) + abs(e.y - self.y) <= 1 and \
isinstance(e, FightingEntity):
e.take_damage(self, self.damage)
m.entities.remove(self)
def save_state(self) -> dict: def save_state(self) -> dict:
""" """

View File

@ -38,6 +38,9 @@ class KeyValues(Enum):
RIGHT = auto() RIGHT = auto()
ENTER = auto() ENTER = auto()
INVENTORY = auto() INVENTORY = auto()
USE = auto()
EQUIP = auto()
DROP = auto()
SPACE = auto() SPACE = auto()
@staticmethod @staticmethod
@ -61,6 +64,12 @@ class KeyValues(Enum):
return KeyValues.ENTER return KeyValues.ENTER
elif key == settings.KEY_INVENTORY: elif key == settings.KEY_INVENTORY:
return KeyValues.INVENTORY return KeyValues.INVENTORY
elif key == settings.KEY_USE:
return KeyValues.USE
elif key == settings.KEY_EQUIP:
return KeyValues.EQUIP
elif key == settings.KEY_DROP:
return KeyValues.DROP
elif key == ' ': elif key == ' ':
return KeyValues.SPACE return KeyValues.SPACE
return None return None

View File

@ -123,6 +123,12 @@ class Game:
self.inventory_menu.go_up() self.inventory_menu.go_up()
elif key == KeyValues.DOWN: elif key == KeyValues.DOWN:
self.inventory_menu.go_down() self.inventory_menu.go_down()
elif key == KeyValues.USE:
self.inventory_menu.validate().use()
elif key == KeyValues.EQUIP:
self.inventory_menu.validate().equip()
elif key == KeyValues.DROP:
self.inventory_menu.validate().use()
def handle_key_pressed_main_menu(self, key: KeyValues) -> None: def handle_key_pressed_main_menu(self, key: KeyValues) -> None:
""" """

View File

@ -28,6 +28,9 @@ class Settings:
self.KEY_RIGHT_SECONDARY = ['KEY_RIGHT', 'Secondary key to move right'] self.KEY_RIGHT_SECONDARY = ['KEY_RIGHT', 'Secondary key to move right']
self.KEY_ENTER = ['\n', 'Key to validate a menu'] self.KEY_ENTER = ['\n', 'Key to validate a menu']
self.KEY_INVENTORY = ['i', 'Key used to open the inventory'] self.KEY_INVENTORY = ['i', 'Key used to open the inventory']
self.KEY_USE = ['u', 'Key used to use an item in the inventory']
self.KEY_EQUIP = ['e', 'Key used to equip an item in the inventory']
self.KEY_DROP = ['d', 'Key used to drop an item in the inventory']
self.TEXTURE_PACK = ['ascii', 'Texture pack'] self.TEXTURE_PACK = ['ascii', 'Texture pack']
self.LOCALE = [locale.getlocale()[0][:2], 'Language'] self.LOCALE = [locale.getlocale()[0][:2], 'Language']