Items can be dropped/equipped/used
This commit is contained in:
@ -20,16 +20,26 @@ class Item(Entity):
|
||||
self.held = held
|
||||
self.held_by = held_by
|
||||
|
||||
def drop(self, y: int, x: int) -> None:
|
||||
def drop(self) -> None:
|
||||
"""
|
||||
The item is dropped from the inventory onto the floor
|
||||
"""
|
||||
if self.held:
|
||||
self.held_by.inventory.remove(self)
|
||||
self.map.add_entity(self)
|
||||
self.move(self.held_by.y, self.held_by.x)
|
||||
self.held = False
|
||||
self.held_by = None
|
||||
self.map.add_entity(self)
|
||||
self.move(y, x)
|
||||
|
||||
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.
|
||||
"""
|
||||
|
||||
def hold(self, player: "Player") -> None:
|
||||
"""
|
||||
@ -80,6 +90,7 @@ class Bomb(Item):
|
||||
A bomb item intended to deal damage to enemies at long range
|
||||
"""
|
||||
damage: int = 5
|
||||
tick: int
|
||||
exploding: bool
|
||||
|
||||
def __init__(self, damage: int = 5, exploding: bool = False,
|
||||
@ -87,20 +98,29 @@ class Bomb(Item):
|
||||
super().__init__(name="bomb", *args, **kwargs)
|
||||
self.damage = damage
|
||||
self.exploding = exploding
|
||||
self.tick = 5
|
||||
|
||||
def drop(self, x: int, y: int) -> None:
|
||||
super().drop(x, y)
|
||||
self.exploding = True
|
||||
def use(self) -> None:
|
||||
"""
|
||||
When the bomb is used, throw it and explodes it.
|
||||
"""
|
||||
if self.held:
|
||||
super().drop()
|
||||
self.exploding = True
|
||||
|
||||
def act(self, m: Map) -> None:
|
||||
"""
|
||||
Special exploding action of the bomb
|
||||
"""
|
||||
if self.exploding:
|
||||
for e in m.entities.copy():
|
||||
if abs(e.x - self.x) + abs(e.y - self.y) <= 1 and \
|
||||
isinstance(e, FightingEntity):
|
||||
e.take_damage(self, self.damage)
|
||||
if self.tick > 0:
|
||||
self.tick -= 1
|
||||
else:
|
||||
for e in m.entities.copy():
|
||||
if abs(e.x - self.x) + abs(e.y - self.y) <= 1 and \
|
||||
isinstance(e, FightingEntity):
|
||||
e.take_damage(self, self.damage)
|
||||
m.entities.remove(self)
|
||||
|
||||
def save_state(self) -> dict:
|
||||
"""
|
||||
|
Reference in New Issue
Block a user