2020-11-06 15:13:28 +00:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from dungeonbattle.entities.items import Bomb, Item
|
2020-11-10 20:47:36 +00:00
|
|
|
from dungeonbattle.entities.monsters import Hedgehog
|
2020-11-06 15:13:28 +00:00
|
|
|
from dungeonbattle.entities.player import Player
|
|
|
|
from dungeonbattle.interfaces import Entity, Map
|
|
|
|
|
|
|
|
|
|
|
|
class TestEntities(unittest.TestCase):
|
|
|
|
def setUp(self) -> None:
|
|
|
|
"""
|
|
|
|
Load example map that can be used in tests.
|
|
|
|
"""
|
2020-11-10 19:37:01 +00:00
|
|
|
self.map = Map.load("resources/example_map.txt")
|
2020-11-06 15:13:28 +00:00
|
|
|
|
|
|
|
def test_basic_entities(self) -> None:
|
|
|
|
"""
|
|
|
|
Test some random stuff with basic entities.
|
|
|
|
"""
|
|
|
|
entity = Entity()
|
|
|
|
entity.move(42, 64)
|
|
|
|
self.assertEqual(entity.y, 42)
|
|
|
|
self.assertEqual(entity.x, 64)
|
|
|
|
self.assertIsNone(entity.act(self.map))
|
|
|
|
|
2020-11-10 21:59:02 +00:00
|
|
|
other_entity = Entity()
|
|
|
|
other_entity.move(45, 68)
|
|
|
|
self.assertEqual(entity.distance_squared(other_entity), 25)
|
|
|
|
self.assertEqual(entity.distance(other_entity), 5)
|
|
|
|
|
2020-11-06 15:13:28 +00:00
|
|
|
def test_fighting_entities(self) -> None:
|
|
|
|
"""
|
|
|
|
Test some random stuff with fighting entities.
|
|
|
|
"""
|
2020-11-10 20:47:36 +00:00
|
|
|
entity = Hedgehog()
|
2020-11-10 21:44:53 +00:00
|
|
|
self.map.add_entity(entity)
|
2020-11-06 15:13:28 +00:00
|
|
|
self.assertEqual(entity.maxhealth, 10)
|
|
|
|
self.assertEqual(entity.maxhealth, entity.health)
|
|
|
|
self.assertEqual(entity.strength, 3)
|
|
|
|
self.assertIsNone(entity.hit(entity))
|
|
|
|
self.assertFalse(entity.dead)
|
|
|
|
self.assertIsNone(entity.hit(entity))
|
|
|
|
self.assertFalse(entity.dead)
|
|
|
|
self.assertIsNone(entity.hit(entity))
|
|
|
|
self.assertFalse(entity.dead)
|
|
|
|
self.assertIsNone(entity.hit(entity))
|
|
|
|
self.assertTrue(entity.dead)
|
|
|
|
|
2020-11-10 21:44:53 +00:00
|
|
|
entity = Hedgehog()
|
|
|
|
self.map.add_entity(entity)
|
|
|
|
entity.move(2, 6)
|
|
|
|
self.map.tick()
|
|
|
|
self.assertFalse(entity.y == 2 and entity.x == 6)
|
|
|
|
|
2020-11-06 15:13:28 +00:00
|
|
|
def test_items(self) -> None:
|
|
|
|
"""
|
|
|
|
Test some random stuff with items.
|
|
|
|
"""
|
|
|
|
item = Item()
|
|
|
|
self.assertFalse(item.held)
|
|
|
|
item.hold()
|
|
|
|
self.assertTrue(item.held)
|
|
|
|
item.drop(42, 42)
|
|
|
|
self.assertEqual(item.y, 42)
|
|
|
|
self.assertEqual(item.x, 42)
|
|
|
|
|
|
|
|
def test_bombs(self) -> None:
|
|
|
|
"""
|
|
|
|
Test some random stuff with bombs.
|
|
|
|
"""
|
|
|
|
item = Bomb()
|
2020-11-10 20:47:36 +00:00
|
|
|
hedgehog = Hedgehog()
|
2020-11-06 16:59:19 +00:00
|
|
|
self.map.add_entity(item)
|
2020-11-10 20:47:36 +00:00
|
|
|
self.map.add_entity(hedgehog)
|
|
|
|
hedgehog.health = 2
|
|
|
|
hedgehog.move(41, 42)
|
2020-11-06 15:13:28 +00:00
|
|
|
item.act(self.map)
|
2020-11-10 20:47:36 +00:00
|
|
|
self.assertFalse(hedgehog.dead)
|
2020-11-06 15:13:28 +00:00
|
|
|
item.drop(42, 42)
|
|
|
|
self.assertEqual(item.y, 42)
|
|
|
|
self.assertEqual(item.x, 42)
|
|
|
|
item.act(self.map)
|
2020-11-10 20:47:36 +00:00
|
|
|
self.assertTrue(hedgehog.dead)
|
2020-11-06 15:13:28 +00:00
|
|
|
|
|
|
|
def test_players(self) -> None:
|
|
|
|
"""
|
|
|
|
Test some random stuff with players.
|
|
|
|
"""
|
|
|
|
player = Player()
|
2020-11-06 17:03:30 +00:00
|
|
|
self.map.add_entity(player)
|
|
|
|
player.move(1, 6)
|
2020-11-06 15:13:28 +00:00
|
|
|
self.assertEqual(player.strength, 5)
|
|
|
|
self.assertEqual(player.health, player.maxhealth)
|
|
|
|
self.assertEqual(player.maxhealth, 20)
|
2020-11-06 17:03:30 +00:00
|
|
|
|
|
|
|
# Test movements and ensure that collisions are working
|
|
|
|
self.assertFalse(player.move_up())
|
|
|
|
self.assertTrue(player.move_left())
|
|
|
|
self.assertFalse(player.move_left())
|
|
|
|
for i in range(8):
|
|
|
|
self.assertTrue(player.move_down())
|
|
|
|
self.assertFalse(player.move_down())
|
|
|
|
self.assertTrue(player.move_right())
|
|
|
|
self.assertTrue(player.move_right())
|
|
|
|
self.assertTrue(player.move_right())
|
|
|
|
self.assertFalse(player.move_right())
|
|
|
|
self.assertTrue(player.move_down())
|
|
|
|
self.assertTrue(player.move_down())
|
2020-11-06 20:23:17 +00:00
|
|
|
|
|
|
|
player.add_xp(70)
|
|
|
|
self.assertEqual(player.current_xp, 10)
|
|
|
|
self.assertEqual(player.max_xp, 40)
|
|
|
|
self.assertEqual(player.level, 4)
|