From f11fb31c28826eaa9366b348ea435a3b3f6796de Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Wed, 11 Nov 2020 16:47:19 +0100 Subject: [PATCH] Interact with items --- dungeonbattle/entities/items.py | 22 +++++++++++++++++++--- dungeonbattle/entities/player.py | 15 +++++++++------ dungeonbattle/interfaces.py | 7 +++++++ 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/dungeonbattle/entities/items.py b/dungeonbattle/entities/items.py index 676f517..4284f42 100644 --- a/dungeonbattle/entities/items.py +++ b/dungeonbattle/entities/items.py @@ -1,8 +1,12 @@ +from typing import Optional + +from .player import Player from ..interfaces import Entity, FightingEntity, Map class Item(Entity): held: bool + hold_by: Optional["Player"] def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -10,18 +14,30 @@ class Item(Entity): def drop(self, y: int, x: int) -> None: self.held = False + self.hold_by = None + self.map.add_entity(self) self.move(y, x) - def hold(self) -> None: + def hold(self, player: "Player") -> None: self.held = True + self.hold_by = player + self.map.remove_entity(self) class Heart(Item): - name = "heart" + name: str = "heart" + healing: int = 5 + + def hold(self, player: "Player") -> None: + """ + When holding a heart, heal the player and don't put item in inventory. + """ + player.health = min(player.maxhealth, player.health + self.healing) + return super().hold(player) class Bomb(Item): - name = "bomb" + name: str = "bomb" damage: int = 5 exploding: bool diff --git a/dungeonbattle/entities/player.py b/dungeonbattle/entities/player.py index 2ab3a43..7dcaf2f 100644 --- a/dungeonbattle/entities/player.py +++ b/dungeonbattle/entities/player.py @@ -47,6 +47,7 @@ class Player(FightingEntity): self.current_xp += xp self.level_up() + # noinspection PyTypeChecker,PyUnresolvedReferences def check_move(self, y: int, x: int, move_if_possible: bool = False) \ -> bool: """ @@ -58,12 +59,14 @@ class Player(FightingEntity): if self.dead: return False for entity in self.map.entities: - if entity.y == y and entity.x == x and \ - isinstance(entity, FightingEntity): - self.hit(entity) - if entity.dead: - self.add_xp(randint(3, 7)) - return True + if entity.y == y and entity.x == x: + if entity.is_fighting_entity(): + self.hit(entity) + if entity.dead: + self.add_xp(randint(3, 7)) + return True + elif entity.is_item(): + entity.hold(self) return super().check_move(y, x, move_if_possible) def recalculate_paths(self, max_distance: int = 8) -> None: diff --git a/dungeonbattle/interfaces.py b/dungeonbattle/interfaces.py index 20f8f3d..90e1bb7 100644 --- a/dungeonbattle/interfaces.py +++ b/dungeonbattle/interfaces.py @@ -192,6 +192,13 @@ class Entity: """ return sqrt(self.distance_squared(other)) + def is_fighting_entity(self) -> bool: + return isinstance(self, FightingEntity) + + def is_item(self) -> bool: + from dungeonbattle.entities.items import Item + return isinstance(self, Item) + @staticmethod def get_all_entity_classes(): from dungeonbattle.entities.items import Heart, Bomb