squirrel-battle/dungeonbattle/entities/items.py

62 lines
1.6 KiB
Python

from typing import Optional
from .player import Player
from ..interfaces import Entity, FightingEntity, Map
class Item(Entity):
held: bool
hold_by: Optional["Player"]
can_be_in_inventory: bool = False
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.held = False
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, player: "Player") -> None:
self.held = True
self.hold_by = player
self.map.remove_entity(self)
if self.can_be_in_inventory:
player.inventory.append(self)
class Heart(Item):
name: str = "heart"
can_be_in_inventory: bool = False
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: str = "bomb"
damage: int = 5
exploding: bool
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.exploding = False
def drop(self, x: int, y: int) -> None:
super().drop(x, y)
self.exploding = True
def act(self, m: Map) -> None:
if self.exploding:
for e in m.entities:
if abs(e.x - self.x) + abs(e.y - self.y) <= 1 and \
isinstance(e, FightingEntity):
e.take_damage(self, self.damage)