squirrel-battle/dungeonbattle/entities/items.py

79 lines
2.0 KiB
Python
Raw Normal View History

2020-11-11 15:47:19 +00:00
from typing import Optional
from .player import Player
2020-11-06 14:33:26 +00:00
from ..interfaces import Entity, FightingEntity, Map
2020-10-23 14:51:48 +00:00
class Item(Entity):
"""
A class for items
"""
2020-11-06 14:33:26 +00:00
held: bool
held_by: Optional["Player"]
2020-10-23 14:51:48 +00:00
def __init__(self, *args, **kwargs):
2020-11-06 14:33:26 +00:00
super().__init__(*args, **kwargs)
2020-10-23 14:51:48 +00:00
self.held = False
2020-11-06 14:33:26 +00:00
2020-11-06 15:13:28 +00:00
def drop(self, y: int, x: int) -> None:
"""
The item is dropped from the inventory onto the floor
"""
if self.held:
self.held_by.inventory.remove(self)
self.held = False
self.held_by = None
2020-11-11 15:47:19 +00:00
self.map.add_entity(self)
2020-11-06 15:13:28 +00:00
self.move(y, x)
2020-11-06 14:33:26 +00:00
2020-11-11 15:47:19 +00:00
def hold(self, player: "Player") -> None:
"""
The item is taken from the floor and put into the inventory
"""
2020-10-23 14:51:48 +00:00
self.held = True
self.held_by = player
2020-11-11 15:47:19 +00:00
self.map.remove_entity(self)
2020-11-11 16:15:28 +00:00
player.inventory.append(self)
2020-10-23 16:02:57 +00:00
2020-11-06 14:33:26 +00:00
2020-11-11 15:23:27 +00:00
class Heart(Item):
"""
A heart item to return health to the player
"""
2020-11-11 15:47:19 +00:00
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)
2020-11-11 16:15:28 +00:00
self.map.remove_entity(self)
2020-11-11 15:23:27 +00:00
2020-10-23 16:02:57 +00:00
class Bomb(Item):
"""
A bomb item intended to deal damage to ennemies at long range
"""
2020-11-11 15:47:19 +00:00
name: str = "bomb"
2020-11-06 14:33:26 +00:00
damage: int = 5
exploding: bool
2020-10-23 16:02:57 +00:00
def __init__(self, *args, **kwargs):
2020-11-06 14:33:26 +00:00
super().__init__(*args, **kwargs)
2020-10-23 16:02:57 +00:00
self.exploding = False
2020-11-06 14:33:26 +00:00
def drop(self, x: int, y: int) -> None:
super().drop(x, y)
2020-10-23 16:02:57 +00:00
self.exploding = True
2020-11-06 14:33:26 +00:00
def act(self, m: Map) -> None:
"""
Special exploding action of the bomb
"""
2020-10-23 16:02:57 +00:00
if self.exploding:
2020-11-06 14:33:26 +00:00
for e in m.entities:
if abs(e.x - self.x) + abs(e.y - self.y) <= 1 and \
isinstance(e, FightingEntity):
2020-10-23 16:02:57 +00:00
e.take_damage(self, self.damage)