From fe769c4488a360d6562ec3bcdf9399fc99aac723 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 18 Dec 2020 17:30:03 +0100 Subject: [PATCH] We can equip items --- squirrelbattle/display/statsdisplay.py | 17 +++++++++++++---- squirrelbattle/entities/items.py | 20 +++++++++++++++----- squirrelbattle/entities/player.py | 14 ++++++++++++-- squirrelbattle/game.py | 1 + squirrelbattle/interfaces.py | 10 ++++++---- 5 files changed, 47 insertions(+), 15 deletions(-) diff --git a/squirrelbattle/display/statsdisplay.py b/squirrelbattle/display/statsdisplay.py index ef358bb..00f6675 100644 --- a/squirrelbattle/display/statsdisplay.py +++ b/squirrelbattle/display/statsdisplay.py @@ -20,7 +20,7 @@ class StatsDisplay(Display): self.player = game.player def update_pad(self) -> None: - string2 = "Player -- LVL {}\nEXP {}/{}\nHP {}/{}"\ + string2 = _("player").capitalize() + " -- LVL {}\nEXP {}/{}\nHP {}/{}"\ .format(self.player.level, self.player.current_xp, self.player.max_xp, self.player.health, self.player.maxhealth) @@ -46,11 +46,20 @@ class StatsDisplay(Display): printed_items.append(item) self.addstr(self.pad, 8, 0, inventory_str) - self.addstr(self.pad, 9, 0, f"{self.pack.HAZELNUT} " - f"x{self.player.hazel}") + 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}") 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) def display(self) -> None: diff --git a/squirrelbattle/entities/items.py b/squirrelbattle/entities/items.py index 865a703..9719e3a 100644 --- a/squirrelbattle/entities/items.py +++ b/squirrelbattle/entities/items.py @@ -4,7 +4,6 @@ from random import choice, randint from typing import Optional -from .player import Player from ..interfaces import Entity, FightingEntity, Map, InventoryHolder from ..translations import gettext as _ @@ -45,15 +44,26 @@ class Item(Entity): """ 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 """ self.held = True - self.held_by = player + self.held_by = holder self.held_by.map.remove_entity(self) - player.add_to_inventory(self) + holder.add_to_inventory(self) def save_state(self) -> dict: """ @@ -116,7 +126,7 @@ class Bomb(Item): """ damage: int = 5 exploding: bool - owner: Optional["Player"] + owner: Optional["InventoryHolder"] tick: int def __init__(self, name: str = "bomb", damage: int = 5, diff --git a/squirrelbattle/entities/player.py b/squirrelbattle/entities/player.py index 19c8348..d7c9e28 100644 --- a/squirrelbattle/entities/player.py +++ b/squirrelbattle/entities/player.py @@ -4,8 +4,9 @@ from functools import reduce from queue import PriorityQueue from random import randint -from typing import Dict, Tuple +from typing import Dict, Optional, Tuple +from .items import Item from ..interfaces import FightingEntity, InventoryHolder @@ -16,12 +17,15 @@ class Player(InventoryHolder, FightingEntity): current_xp: int = 0 max_xp: int = 10 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, strength: int = 5, intelligence: int = 1, charisma: int = 1, dexterity: int = 1, constitution: int = 1, level: int = 1, 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: super().__init__(name=name, maxhealth=maxhealth, strength=strength, intelligence=intelligence, charisma=charisma, @@ -32,6 +36,12 @@ class Player(InventoryHolder, FightingEntity): self.inventory = self.translate_inventory(inventory or []) self.paths = dict() 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: """ diff --git a/squirrelbattle/game.py b/squirrelbattle/game.py index 0553d2e..e0f3df9 100644 --- a/squirrelbattle/game.py +++ b/squirrelbattle/game.py @@ -126,6 +126,7 @@ class Game: self.map.tick() elif key == KeyValues.INVENTORY: self.state = GameMode.INVENTORY + self.display_actions(DisplayActions.UPDATE) elif key == KeyValues.SPACE: self.state = GameMode.MAINMENU elif key == KeyValues.CHAT: diff --git a/squirrelbattle/interfaces.py b/squirrelbattle/interfaces.py index 94025bd..c5d9e0e 100644 --- a/squirrelbattle/interfaces.py +++ b/squirrelbattle/interfaces.py @@ -496,10 +496,10 @@ class InventoryHolder(Entity): """ for i in range(len(inventory)): if isinstance(inventory[i], dict): - inventory[i] = self.dict_to_inventory(inventory[i]) + inventory[i] = self.dict_to_item(inventory[i]) 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 into an item object. @@ -522,13 +522,15 @@ class InventoryHolder(Entity): """ Adds an object to inventory """ - self.inventory.append(obj) + if obj not in self.inventory: + self.inventory.append(obj) def remove_from_inventory(self, obj: Any) -> None: """ Removes an object from the inventory """ - self.inventory.remove(obj) + if obj in self.inventory: + self.inventory.remove(obj) def change_hazel_balance(self, hz: int) -> None: """