We can equip items

This commit is contained in:
Yohann D'ANELLO 2020-12-18 17:30:03 +01:00
parent 330d78702a
commit fe769c4488
5 changed files with 47 additions and 15 deletions

View File

@ -20,7 +20,7 @@ class StatsDisplay(Display):
self.player = game.player self.player = game.player
def update_pad(self) -> None: def update_pad(self) -> None:
string2 = "Player -- LVL {}\nEXP {}/{}\nHP {}/{}"\ string2 = _("player").capitalize() + " -- LVL {}\nEXP {}/{}\nHP {}/{}"\
.format(self.player.level, self.player.current_xp, .format(self.player.level, self.player.current_xp,
self.player.max_xp, self.player.health, self.player.max_xp, self.player.health,
self.player.maxhealth) self.player.maxhealth)
@ -46,11 +46,20 @@ class StatsDisplay(Display):
printed_items.append(item) printed_items.append(item)
self.addstr(self.pad, 8, 0, inventory_str) self.addstr(self.pad, 8, 0, inventory_str)
self.addstr(self.pad, 9, 0, f"{self.pack.HAZELNUT} " if self.player.equipped_item:
self.addstr(self.pad, 9, 0,
_("Equipped item:") + " "
f"{self.pack[self.player.equipped_item.name.upper()]}")
if self.player.equipped_armor:
self.addstr(self.pad, 10, 0,
_("Equipped armor:") + " "
f"{self.pack[self.player.equipped_armor.name.upper()]}")
self.addstr(self.pad, 11, 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, 11, 0, _("YOU ARE DEAD"), curses.COLOR_RED, self.addstr(self.pad, 13, 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

@ -4,7 +4,6 @@
from random import choice, randint from random import choice, randint
from typing import Optional from typing import Optional
from .player import Player
from ..interfaces import Entity, FightingEntity, Map, InventoryHolder from ..interfaces import Entity, FightingEntity, Map, InventoryHolder
from ..translations import gettext as _ from ..translations import gettext as _
@ -45,15 +44,26 @@ class Item(Entity):
""" """
Indicates what should be done when the item is equipped. 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.equipped_item = self
self.held_by.remove_from_inventory(self)
def hold(self, player: InventoryHolder) -> None: def unequip(self) -> None:
"""
Indicates what should be done when the item is unequipped.
"""
self.held_by.add_to_inventory(self)
self.held_by.equipped_item = None
def hold(self, holder: InventoryHolder) -> None:
""" """
The item is taken from the floor and put into the inventory The item is taken from the floor and put into the inventory
""" """
self.held = True self.held = True
self.held_by = player self.held_by = holder
self.held_by.map.remove_entity(self) self.held_by.map.remove_entity(self)
player.add_to_inventory(self) holder.add_to_inventory(self)
def save_state(self) -> dict: def save_state(self) -> dict:
""" """
@ -116,7 +126,7 @@ class Bomb(Item):
""" """
damage: int = 5 damage: int = 5
exploding: bool exploding: bool
owner: Optional["Player"] owner: Optional["InventoryHolder"]
tick: int tick: int
def __init__(self, name: str = "bomb", damage: int = 5, def __init__(self, name: str = "bomb", damage: int = 5,

View File

@ -4,8 +4,9 @@
from functools import reduce from functools import reduce
from queue import PriorityQueue from queue import PriorityQueue
from random import randint from random import randint
from typing import Dict, Tuple from typing import Dict, Optional, Tuple
from .items import Item
from ..interfaces import FightingEntity, InventoryHolder from ..interfaces import FightingEntity, InventoryHolder
@ -16,12 +17,15 @@ 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_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, *args, **kwargs) \ hazel: int = 42, equipped_item: Optional[Item] = None,
equipped_armor: Optional[Item] = None, *args, **kwargs) \
-> None: -> 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,
@ -32,6 +36,12 @@ 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):
equipped_item = self.dict_to_item(equipped_item)
if isinstance(equipped_armor, dict):
equipped_armor = self.dict_to_item(equipped_armor)
self.equipped_item = equipped_item
self.equipped_armor = equipped_armor
def move(self, y: int, x: int) -> None: def move(self, y: int, x: int) -> None:
""" """

View File

@ -126,6 +126,7 @@ class Game:
self.map.tick() self.map.tick()
elif key == KeyValues.INVENTORY: elif key == KeyValues.INVENTORY:
self.state = GameMode.INVENTORY self.state = GameMode.INVENTORY
self.display_actions(DisplayActions.UPDATE)
elif key == KeyValues.SPACE: elif key == KeyValues.SPACE:
self.state = GameMode.MAINMENU self.state = GameMode.MAINMENU
elif key == KeyValues.CHAT: elif key == KeyValues.CHAT:

View File

@ -496,10 +496,10 @@ class InventoryHolder(Entity):
""" """
for i in range(len(inventory)): for i in range(len(inventory)):
if isinstance(inventory[i], dict): if isinstance(inventory[i], dict):
inventory[i] = self.dict_to_inventory(inventory[i]) inventory[i] = self.dict_to_item(inventory[i])
return inventory return inventory
def dict_to_inventory(self, item_dict: dict) -> Entity: def dict_to_item(self, item_dict: dict) -> Entity:
""" """
Translate a dict object that contains the state of an item Translate a dict object that contains the state of an item
into an item object. into an item object.
@ -522,12 +522,14 @@ class InventoryHolder(Entity):
""" """
Adds an object to inventory Adds an object to inventory
""" """
if obj not in self.inventory:
self.inventory.append(obj) self.inventory.append(obj)
def remove_from_inventory(self, obj: Any) -> None: def remove_from_inventory(self, obj: Any) -> None:
""" """
Removes an object from the inventory Removes an object from the inventory
""" """
if obj in self.inventory:
self.inventory.remove(obj) self.inventory.remove(obj)
def change_hazel_balance(self, hz: int) -> None: def change_hazel_balance(self, hz: int) -> None: