Merge branch 'fix-explosions' into 'master'

Fix explosions

Closes #80

See merge request ynerant/squirrel-battle!79
This commit is contained in:
ynerant 2021-01-16 00:46:04 +01:00
commit 99b749aaa2
4 changed files with 14 additions and 5 deletions

View File

@ -232,14 +232,19 @@ class Explosion(Item):
"""
When a bomb explodes, the explosion is displayed.
"""
def __init__(self, *args, **kwargs):
living_ticks: int
def __init__(self, living_ticks: int = 2, *args, **kwargs):
super().__init__(name="explosion", *args, **kwargs)
self.living_ticks = living_ticks
def act(self, m: Map) -> None:
"""
The bomb disappears after exploding.
"""
m.remove_entity(self)
self.living_ticks -= 1
if self.living_ticks <= 0:
m.remove_entity(self)
def hold(self, player: InventoryHolder) -> None:
"""

View File

@ -848,7 +848,6 @@ class InventoryHolder(Entity):
for i in range(len(inventory)):
if isinstance(inventory[i], dict):
inventory[i] = self.dict_to_item(inventory[i])
inventory[i].held_by = self
return inventory
def dict_to_item(self, item_dict: dict) -> Entity:
@ -859,7 +858,9 @@ class InventoryHolder(Entity):
entity_classes = self.get_all_entity_classes_in_a_dict()
item_class = entity_classes[item_dict["type"]]
return item_class(**item_dict)
item = item_class(**item_dict)
item.held_by = self
return item
def save_state(self) -> dict:
"""
@ -875,6 +876,7 @@ class InventoryHolder(Entity):
Adds an object to the inventory.
"""
if obj not in self.inventory:
obj.held_by = self
self.inventory.append(obj)
def remove_from_inventory(self, obj: Any) -> None:

View File

@ -209,7 +209,8 @@ class TestEntities(unittest.TestCase):
self.assertNotIn(explosion, self.player.inventory)
self.assertIsNone(explosion.held_by)
# The explosion disappears after one tick
# The explosion disappears after two ticks
explosion.act(self.map)
explosion.act(self.map)
self.assertNotIn(explosion, self.map.entities)

View File

@ -255,6 +255,7 @@ class TestGame(unittest.TestCase):
self.game.map.add_entity(explosion)
self.assertIn(explosion, self.game.map.entities)
self.game.handle_key_pressed(KeyValues.WAIT)
self.game.handle_key_pressed(KeyValues.WAIT)
self.assertNotIn(explosion, self.game.map.entities)
rabbit = Rabbit()