Interact with items
This commit is contained in:
parent
2b5d82db57
commit
f11fb31c28
@ -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
|
||||
|
||||
|
@ -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:
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user