2020-11-27 15:33:17 +00:00
|
|
|
# Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-12-04 16:10:23 +00:00
|
|
|
from random import randint
|
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-12-04 16:10:23 +00:00
|
|
|
from ..translations import gettext as _
|
2020-11-06 14:33:26 +00:00
|
|
|
|
2020-10-23 14:51:48 +00:00
|
|
|
|
|
|
|
class Item(Entity):
|
2020-11-18 11:27:59 +00:00
|
|
|
"""
|
|
|
|
A class for items
|
|
|
|
"""
|
2020-11-06 14:33:26 +00:00
|
|
|
held: bool
|
2020-11-18 13:54:21 +00:00
|
|
|
held_by: Optional[Player]
|
2020-10-23 14:51:48 +00:00
|
|
|
|
2020-11-18 13:54:21 +00:00
|
|
|
def __init__(self, held: bool = False, held_by: Optional[Player] = None,
|
|
|
|
*args, **kwargs):
|
2020-11-06 14:33:26 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
2020-11-18 13:54:21 +00:00
|
|
|
self.held = held
|
|
|
|
self.held_by = held_by
|
2020-11-06 14:33:26 +00:00
|
|
|
|
2020-12-04 15:53:27 +00:00
|
|
|
def drop(self) -> None:
|
2020-11-18 11:27:59 +00:00
|
|
|
"""
|
|
|
|
The item is dropped from the inventory onto the floor
|
|
|
|
"""
|
2020-11-11 22:41:06 +00:00
|
|
|
if self.held:
|
|
|
|
self.held_by.inventory.remove(self)
|
2020-12-04 15:53:27 +00:00
|
|
|
self.map.add_entity(self)
|
|
|
|
self.move(self.held_by.y, self.held_by.x)
|
2020-11-11 22:41:06 +00:00
|
|
|
self.held = False
|
|
|
|
self.held_by = None
|
2020-12-04 15:53:27 +00:00
|
|
|
|
|
|
|
def use(self) -> None:
|
|
|
|
"""
|
|
|
|
Indicates what should be done when the item is used.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def equip(self) -> None:
|
|
|
|
"""
|
|
|
|
Indicates what should be done when the item is equipped.
|
|
|
|
"""
|
2020-11-06 14:33:26 +00:00
|
|
|
|
2020-11-11 15:47:19 +00:00
|
|
|
def hold(self, player: "Player") -> None:
|
2020-11-18 11:27:59 +00:00
|
|
|
"""
|
|
|
|
The item is taken from the floor and put into the inventory
|
|
|
|
"""
|
2020-10-23 14:51:48 +00:00
|
|
|
self.held = True
|
2020-11-11 15:57:09 +00:00
|
|
|
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-18 23:10:37 +00:00
|
|
|
def save_state(self) -> dict:
|
2020-11-18 21:42:46 +00:00
|
|
|
"""
|
|
|
|
Saves the state of the entity into a dictionary
|
|
|
|
"""
|
|
|
|
d = super().save_state()
|
|
|
|
d["held"] = self.held
|
2020-11-18 23:10:37 +00:00
|
|
|
return d
|
2020-11-18 21:42:46 +00:00
|
|
|
|
2020-11-06 14:33:26 +00:00
|
|
|
|
2020-11-11 15:23:27 +00:00
|
|
|
class Heart(Item):
|
2020-11-18 11:27:59 +00:00
|
|
|
"""
|
|
|
|
A heart item to return health to the player
|
|
|
|
"""
|
2020-11-18 13:54:21 +00:00
|
|
|
healing: int
|
|
|
|
|
|
|
|
def __init__(self, healing: int = 5, *args, **kwargs):
|
|
|
|
super().__init__(name="heart", *args, **kwargs)
|
|
|
|
self.healing = healing
|
2020-11-11 15:47:19 +00:00
|
|
|
|
|
|
|
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-11-19 00:11:11 +00:00
|
|
|
def save_state(self) -> dict:
|
|
|
|
"""
|
|
|
|
Saves the state of the header into a dictionary
|
|
|
|
"""
|
|
|
|
d = super().save_state()
|
|
|
|
d["healing"] = self.healing
|
|
|
|
return d
|
|
|
|
|
2020-11-11 15:23:27 +00:00
|
|
|
|
2020-10-23 16:02:57 +00:00
|
|
|
class Bomb(Item):
|
2020-11-18 11:27:59 +00:00
|
|
|
"""
|
2020-11-18 13:54:21 +00:00
|
|
|
A bomb item intended to deal damage to enemies at long range
|
2020-11-18 11:27:59 +00:00
|
|
|
"""
|
2020-11-06 14:33:26 +00:00
|
|
|
damage: int = 5
|
|
|
|
exploding: bool
|
2020-12-04 17:16:46 +00:00
|
|
|
owner: Optional["Player"]
|
|
|
|
tick: int
|
2020-10-23 16:02:57 +00:00
|
|
|
|
2020-11-18 13:54:21 +00:00
|
|
|
def __init__(self, damage: int = 5, exploding: bool = False,
|
|
|
|
*args, **kwargs):
|
|
|
|
super().__init__(name="bomb", *args, **kwargs)
|
|
|
|
self.damage = damage
|
|
|
|
self.exploding = exploding
|
2020-12-04 16:10:23 +00:00
|
|
|
self.tick = 4
|
2020-12-04 17:16:46 +00:00
|
|
|
self.owner = None
|
2020-10-23 16:02:57 +00:00
|
|
|
|
2020-12-04 15:53:27 +00:00
|
|
|
def use(self) -> None:
|
|
|
|
"""
|
|
|
|
When the bomb is used, throw it and explodes it.
|
|
|
|
"""
|
|
|
|
if self.held:
|
2020-12-04 16:10:23 +00:00
|
|
|
self.owner = self.held_by
|
2020-12-04 15:53:27 +00:00
|
|
|
super().drop()
|
|
|
|
self.exploding = True
|
2020-11-06 14:33:26 +00:00
|
|
|
|
|
|
|
def act(self, m: Map) -> None:
|
2020-11-18 11:27:59 +00:00
|
|
|
"""
|
|
|
|
Special exploding action of the bomb
|
|
|
|
"""
|
2020-10-23 16:02:57 +00:00
|
|
|
if self.exploding:
|
2020-12-04 15:53:27 +00:00
|
|
|
if self.tick > 0:
|
2020-12-04 16:10:23 +00:00
|
|
|
# The bomb will explode in <tick> moves
|
2020-12-04 15:53:27 +00:00
|
|
|
self.tick -= 1
|
|
|
|
else:
|
2020-12-04 16:10:23 +00:00
|
|
|
# The bomb is exploding.
|
|
|
|
# Each entity that is close to the bomb takes damages.
|
|
|
|
# The player earn XP if the entity was killed.
|
|
|
|
log_message = _("Bomb is exploding.")
|
2020-12-04 15:53:27 +00:00
|
|
|
for e in m.entities.copy():
|
2020-12-04 16:10:23 +00:00
|
|
|
if abs(e.x - self.x) + abs(e.y - self.y) <= 3 and \
|
2020-12-04 15:53:27 +00:00
|
|
|
isinstance(e, FightingEntity):
|
2020-12-04 16:10:23 +00:00
|
|
|
log_message += " " + e.take_damage(self, self.damage)
|
|
|
|
if e.dead:
|
|
|
|
self.owner.add_xp(randint(3, 7))
|
|
|
|
m.logs.add_message(log_message)
|
2020-12-04 15:53:27 +00:00
|
|
|
m.entities.remove(self)
|
2020-11-19 00:11:11 +00:00
|
|
|
|
|
|
|
def save_state(self) -> dict:
|
|
|
|
"""
|
|
|
|
Saves the state of the bomb into a dictionary
|
|
|
|
"""
|
|
|
|
d = super().save_state()
|
|
|
|
d["exploding"] = self.exploding
|
|
|
|
d["damage"] = self.damage
|
|
|
|
return d
|