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
|
from ..interfaces import Entity, FightingEntity, Map
|
||||||
|
|
||||||
|
|
||||||
class Item(Entity):
|
class Item(Entity):
|
||||||
held: bool
|
held: bool
|
||||||
|
hold_by: Optional["Player"]
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
@ -10,18 +14,30 @@ class Item(Entity):
|
||||||
|
|
||||||
def drop(self, y: int, x: int) -> None:
|
def drop(self, y: int, x: int) -> None:
|
||||||
self.held = False
|
self.held = False
|
||||||
|
self.hold_by = None
|
||||||
|
self.map.add_entity(self)
|
||||||
self.move(y, x)
|
self.move(y, x)
|
||||||
|
|
||||||
def hold(self) -> None:
|
def hold(self, player: "Player") -> None:
|
||||||
self.held = True
|
self.held = True
|
||||||
|
self.hold_by = player
|
||||||
|
self.map.remove_entity(self)
|
||||||
|
|
||||||
|
|
||||||
class Heart(Item):
|
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):
|
class Bomb(Item):
|
||||||
name = "bomb"
|
name: str = "bomb"
|
||||||
damage: int = 5
|
damage: int = 5
|
||||||
exploding: bool
|
exploding: bool
|
||||||
|
|
||||||
|
|
|
@ -47,6 +47,7 @@ class Player(FightingEntity):
|
||||||
self.current_xp += xp
|
self.current_xp += xp
|
||||||
self.level_up()
|
self.level_up()
|
||||||
|
|
||||||
|
# noinspection PyTypeChecker,PyUnresolvedReferences
|
||||||
def check_move(self, y: int, x: int, move_if_possible: bool = False) \
|
def check_move(self, y: int, x: int, move_if_possible: bool = False) \
|
||||||
-> bool:
|
-> bool:
|
||||||
"""
|
"""
|
||||||
|
@ -58,12 +59,14 @@ class Player(FightingEntity):
|
||||||
if self.dead:
|
if self.dead:
|
||||||
return False
|
return False
|
||||||
for entity in self.map.entities:
|
for entity in self.map.entities:
|
||||||
if entity.y == y and entity.x == x and \
|
if entity.y == y and entity.x == x:
|
||||||
isinstance(entity, FightingEntity):
|
if entity.is_fighting_entity():
|
||||||
self.hit(entity)
|
self.hit(entity)
|
||||||
if entity.dead:
|
if entity.dead:
|
||||||
self.add_xp(randint(3, 7))
|
self.add_xp(randint(3, 7))
|
||||||
return True
|
return True
|
||||||
|
elif entity.is_item():
|
||||||
|
entity.hold(self)
|
||||||
return super().check_move(y, x, move_if_possible)
|
return super().check_move(y, x, move_if_possible)
|
||||||
|
|
||||||
def recalculate_paths(self, max_distance: int = 8) -> None:
|
def recalculate_paths(self, max_distance: int = 8) -> None:
|
||||||
|
|
|
@ -192,6 +192,13 @@ class Entity:
|
||||||
"""
|
"""
|
||||||
return sqrt(self.distance_squared(other))
|
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
|
@staticmethod
|
||||||
def get_all_entity_classes():
|
def get_all_entity_classes():
|
||||||
from dungeonbattle.entities.items import Heart, Bomb
|
from dungeonbattle.entities.items import Heart, Bomb
|
||||||
|
|
Loading…
Reference in New Issue