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_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
"""
if self.held:
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_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:
"""
@ -80,6 +90,7 @@ class Bomb(Item):
A bomb item intended to deal damage to enemies at long range
"""
damage: int = 5
tick: int
exploding: bool
def __init__(self, damage: int = 5, exploding: bool = False,
@ -87,20 +98,29 @@ class Bomb(Item):
super().__init__(name="bomb", *args, **kwargs)
self.damage = damage
self.exploding = exploding
self.tick = 5
def drop(self, x: int, y: int) -> None:
super().drop(x, y)
self.exploding = True
def use(self) -> None:
"""
When the bomb is used, throw it and explodes it.
"""
if self.held:
super().drop()
self.exploding = True
def act(self, m: Map) -> None:
"""
Special exploding action of the bomb
"""
if self.exploding:
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)
if self.tick > 0:
self.tick -= 1
else:
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:
"""

View File

@ -38,6 +38,9 @@ class KeyValues(Enum):
RIGHT = auto()
ENTER = auto()
INVENTORY = auto()
USE = auto()
EQUIP = auto()
DROP = auto()
SPACE = auto()
@staticmethod
@ -61,6 +64,12 @@ class KeyValues(Enum):
return KeyValues.ENTER
elif key == settings.KEY_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 == ' ':
return KeyValues.SPACE
return None

View File

@ -123,6 +123,12 @@ class Game:
self.inventory_menu.go_up()
elif key == KeyValues.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:
"""

View File

@ -28,6 +28,9 @@ class Settings:
self.KEY_RIGHT_SECONDARY = ['KEY_RIGHT', 'Secondary key to move right']
self.KEY_ENTER = ['\n', 'Key to validate a menu']
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.LOCALE = [locale.getlocale()[0][:2], 'Language']