Merge branch 'exploding_bomb' into 'master'

Add a small bomb exploding animation

Closes #31

See merge request ynerant/squirrel-battle!47
This commit is contained in:
ynerant 2020-12-12 16:53:26 +01:00
commit 158338637a
3 changed files with 42 additions and 1 deletions

View File

@ -62,6 +62,7 @@ TexturePack.ASCII_PACK = TexturePack(
BODY_SNATCH_POTION='S', BODY_SNATCH_POTION='S',
BOMB='o', BOMB='o',
EMPTY=' ', EMPTY=' ',
EXPLOSION='%',
FLOOR='.', FLOOR='.',
HAZELNUT='¤', HAZELNUT='¤',
HEART='', HEART='',
@ -87,6 +88,7 @@ TexturePack.SQUIRREL_PACK = TexturePack(
BODY_SNATCH_POTION='🔀', BODY_SNATCH_POTION='🔀',
BOMB='💣', BOMB='💣',
EMPTY=' ', EMPTY=' ',
EXPLOSION='💥',
FLOOR='██', FLOOR='██',
HAZELNUT='🌰', HAZELNUT='🌰',
HEART='💜', HEART='💜',

View File

@ -158,6 +158,10 @@ class Bomb(Item):
m.logs.add_message(log_message) m.logs.add_message(log_message)
m.entities.remove(self) m.entities.remove(self)
# Add sparkles where the bomb exploded.
explosion = Explosion(y=self.y, x=self.x)
self.map.add_entity(explosion)
def save_state(self) -> dict: def save_state(self) -> dict:
""" """
Saves the state of the bomb into a dictionary Saves the state of the bomb into a dictionary
@ -168,6 +172,26 @@ class Bomb(Item):
return d return d
class Explosion(Item):
"""
When a bomb explodes, the explosion is displayed.
"""
def __init__(self, *args, **kwargs):
super().__init__(name="explosion", *args, **kwargs)
def act(self, m: Map) -> None:
"""
The explosion instant dies.
"""
m.remove_entity(self)
def hold(self, player: InventoryHolder) -> None:
"""
The player can't hold any explosion.
"""
pass
class Weapon(Item): class Weapon(Item):
""" """
Non-throwable items that improve player damage Non-throwable items that improve player damage

View File

@ -3,7 +3,8 @@
import unittest import unittest
from squirrelbattle.entities.items import BodySnatchPotion, Bomb, Heart, Item from squirrelbattle.entities.items import BodySnatchPotion, Bomb, Heart, Item, \
Explosion
from squirrelbattle.entities.monsters import Tiger, Hedgehog, Rabbit, TeddyBear from squirrelbattle.entities.monsters import Tiger, Hedgehog, Rabbit, TeddyBear
from squirrelbattle.entities.player import Player from squirrelbattle.entities.player import Player
from squirrelbattle.interfaces import Entity, Map from squirrelbattle.interfaces import Entity, Map
@ -138,6 +139,20 @@ class TestEntities(unittest.TestCase):
self.assertTrue(teddy_bear.dead) self.assertTrue(teddy_bear.dead)
bomb_state = item.save_state() bomb_state = item.save_state()
self.assertEqual(bomb_state["damage"], item.damage) self.assertEqual(bomb_state["damage"], item.damage)
explosions = self.map.find_entities(Explosion)
self.assertTrue(explosions)
explosion = explosions[0]
self.assertEqual(explosion.y, item.y)
self.assertEqual(explosion.x, item.x)
# The player can't hold the explosion
explosion.hold(self.player)
self.assertNotIn(explosion, self.player.inventory)
self.assertFalse(explosion.held)
# The explosion disappears after one tick
explosion.act(self.map)
self.assertNotIn(explosion, self.map.entities)
def test_hearts(self) -> None: def test_hearts(self) -> None:
""" """