Exemple #1
0
    def test_clear_cell(self):
        self.board = pgl_engine.Board(name="test_board",
                                      size=[10, 10],
                                      player_starting_position=[5, 5])
        self.placed_item = pgl_board_items.BoardItem()
        self.board.place_item(item=self.placed_item, row=1, column=1)
        self.assertIsInstance(self.board.item(1, 1), pgl_board_items.BoardItem)

        self.board.clear_cell(1, 1)
        self.assertIsInstance(self.board.item(1, 1),
                              pgl_board_items.BoardItemVoid)
        self.assertIsNone(self.board.clear_cell(1, 1, 10))
        r = self.board.height - 1
        c = self.board.width - 1
        self.board.place_item(pgl_board_items.Door(name="door_layer_0"), r, c,
                              0)
        self.board.place_item(pgl_board_items.Door(name="door_layer_1"), r, c,
                              1)
        self.board.place_item(pgl_board_items.Door(name="door_layer_2"), r, c,
                              2)
        self.board.place_item(pgl_board_items.Door(name="door_layer_3"), r, c,
                              3)
        self.assertIsNone(self.board.clear_cell(r, c, 2))
        self.assertEqual(self.board.layers(r, c), 3)
        self.board.place_item(pgl_board_items.Door(name="door_layer_3bis"), r,
                              c, 3, False)
        self.assertIsNone(self.board.clear_cell(r, c, 1))
        self.assertEqual(self.board.layers(r, c), 4)
 def test_create_default_boardItem(self):
     self.boardItem = board_items.BoardItem()
     self.assertEqual(self.boardItem.name, "Board item")
     self.assertEqual(self.boardItem.type, "item")
     self.assertEqual(self.boardItem.pos, [None, None, None])
     self.assertEqual(self.boardItem.model, "*")
     self.assertEqual(self.boardItem.__str__(), self.boardItem.__repr__())
Exemple #3
0
 def test_default_boarditem_implementation(self):
     bi = board_items.BoardItem()
     with self.assertRaises(NotImplementedError):
         bi.can_move()
     with self.assertRaises(NotImplementedError):
         bi.pickable()
     with self.assertRaises(NotImplementedError):
         bi.overlappable()
     with self.assertRaises(NotImplementedError):
         bi.inventory_space()
Exemple #4
0
    def test_clear_cell(self):
        self.board = pgl_engine.Board(
            name="test_board", size=[10, 10], player_starting_position=[5, 5]
        )
        self.placed_item = pgl_board_items.BoardItem()
        self.board.place_item(item=self.placed_item, row=1, column=1)
        self.assertIsInstance(self.board.item(1, 1), pgl_board_items.BoardItem)

        self.board.clear_cell(1, 1)
        self.assertIsInstance(self.board.item(1, 1), pgl_board_items.BoardItemVoid)
Exemple #5
0
    def test_item(self):
        self.board = pgl_engine.Board(name="test_board",
                                      size=[10, 10],
                                      player_starting_position=[5, 5])
        self.placed_item = pgl_board_items.BoardItem()

        self.board.place_item(self.placed_item, 1, 1)
        self.returned_item = self.board.item(1, 1)
        self.assertEqual(self.placed_item, self.returned_item)

        with self.assertRaises(
                pgl_base.PglOutOfBoardBoundException) as excinfo:
            self.board.item(15, 15)
        self.assertTrue(
            "out of the board boundaries" in str(excinfo.exception))
        sprix = gfx_core.Sprixel(bg_color=gfx_core.Color(45, 45, 45))
        sprix.is_bg_transparent = True
        self.board.place_item(
            pgl_board_items.Door(sprixel=gfx_core.Sprixel(
                bg_color=gfx_core.Color(15, 15, 15))),
            5,
            5,
        )

        i = pgl_board_items.NPC(sprixel=sprix)
        self.assertIsNone(self.board.place_item(i, 5, 5))
        self.assertIsNone(
            self.board.place_item(
                pgl_board_items.ComplexNPC(
                    base_item_type=pgl_board_items.Movable),
                8,
                8,
                8,
            ))
        self.assertIsNone(self.board.place_item(pgl_board_items.Tile(), 8, 2))
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            self.board.place_item(1, 1, 1)
        with self.assertRaises(pgl_base.PglOutOfBoardBoundException):
            self.board.place_item(i, 100, 100)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            self.board.remove_item(1)
        # Let's try to break things
        j = pgl_board_items.NPC()
        j.store_position(2, 2)
        with self.assertRaises(pgl_base.PglException) as e:
            self.board.remove_item(j)
        self.assertEqual(e.exception.error, "invalid_item")
        self.assertTrue(self.board.remove_item(i))
        b = pgl_engine.Board()
        i = pgl_board_items.ComplexNPC(sprite=gfx_core.Sprite(
            sprixels=[[gfx_core.Sprixel("#"),
                       gfx_core.Sprixel("#")]]))
        self.assertIsNone(b.place_item(i, 5, 5, 0))
        self.assertTrue(b.remove_item(i))
    def test_custom_boardItem(self):
        self.boardItem = board_items.BoardItem(
            name="test_boardItem",
            item_type="test_type",
            pos=[10, 10],
            model="test_model",
        )
        self.assertEqual(self.boardItem.name, "test_boardItem")
        self.assertEqual(self.boardItem.type, "test_type")
        self.assertEqual(self.boardItem.pos, [10, 10])
        self.assertEqual(self.boardItem.model, "test_model")
        self.assertEqual(self.boardItem.__repr__(), "test_model\x1b[0m")
        self.assertIsNone(self.boardItem.display())
        self.assertIn("'model' = 'test_model'", self.boardItem.debug_info())
        self.assertEqual(self.boardItem.position_as_vector().row, 10)
        self.assertEqual(self.boardItem.position_as_vector().column, 10)
        self.assertEqual(self.boardItem.row, 10)
        self.assertEqual(self.boardItem.column, 10)
        self.assertEqual(self.boardItem.width, 1)
        self.assertEqual(self.boardItem.height, 1)
        bi = board_items.BoardItem(
            name="test_boardItem",
            item_type="test_type",
            pos=[10, 10],
            model="test_model",
        )
        self.assertTrue(self.boardItem.collides_with(bi))
        bi.store_position(8, 9)
        self.assertFalse(self.boardItem.collides_with(bi))
        with self.assertRaises(Exception) as context:
            self.boardItem.collides_with(12)

        self.assertAlmostEqual(self.boardItem.distance_to(bi), 2.23606797749979)
        self.assertTrue("require a BoardItem as parameter" in str(context.exception))
        with self.assertRaises(Exception) as context:
            self.boardItem.distance_to(12)

        self.assertTrue("require a BoardItem as parameter" in str(context.exception))

        bi = board_items.BoardItem(sprixel=gfx_core.Sprixel("-"))
        self.assertEqual(bi.__str__(), gfx_core.Sprixel("-").__repr__())
 def test_default_boarditem_implementation(self):
     bi = board_items.BoardItem()
     self.assertEqual(bi.inventory_space, 1)
     self.assertEqual(bi.size, [1, 1])
     self.assertEqual(bi.restorable(), False)
     self.assertEqual(bi.overlappable(), False)
     self.assertEqual(bi.pickable(), False)
     self.assertEqual(bi.type, "item")
     self.assertEqual(bi.name, "Board item")
     self.assertEqual(bi.can_move(), False)
     self.assertEqual(bi.sprixel.model, "*")
     self.assertEqual(bi.sprixel.is_bg_transparent, True)
     self.assertIsNone(bi.value)
     bi = board_items.BoardItem(
         overlappable=None, pickable=None, restorable=None, can_move=None
     )
     self.assertFalse(bi.overlappable())
     self.assertFalse(bi.restorable())
     self.assertFalse(bi.pickable())
     self.assertFalse(bi.can_move())
     bi = board_items.BoardItem(overlappable=True, pickable=True, restorable=True)
     self.assertFalse(bi.pickable())
     bi = board_items.BoardItem(overlappable=False, pickable=True, restorable=True)
     self.assertFalse(bi.restorable())
     bi.sprixel = None
     self.assertEqual(str(bi), "")
     self.assertEqual(bi.inventory_space, 1)
     bi.inventory_space = 2
     self.assertEqual(bi.inventory_space, 2)
     with self.assertRaises(base.PglInvalidTypeException):
         bi.inventory_space = "2"
     with self.assertRaises(base.PglInvalidTypeException):
         bi.set_overlappable("False")
     self.assertIsNone(bi.set_can_move(True))
     with self.assertRaises(base.PglInvalidTypeException):
         bi.set_can_move("True")
     with self.assertRaises(base.PglInvalidTypeException):
         bi.set_pickable("False")
    def test_screen_buffer(self):
        sprites_panda = SpriteCollection.load_json_file("tests/panda.spr")
        b = engine.Board(size=[20, 20])
        s = engine.Screen()
        # This is a dirty hack for CircleCI as it returns a 0x0 screen size.
        if s.width <= 0 or s.height <= 0:
            s._display_buffer = np.array(
                [[Sprixel(" ") for i in range(0, 50, 1)] for j in range(0, 50, 1)]
            )
            s._screen_buffer = np.array(
                [[Sprixel(" ") for i in range(0, 50, 1)] for j in range(0, 50, 1)]
            )
        # Because CircleCI return a console with no size (most probably because we are
        # not attached to any terminal), we need to make sure that the partial display
        # tests work in that environment too
        screen_width = 0
        screen_height = 0
        if s.width <= 0:
            screen_width = 50
        else:
            screen_width = s.width
        if s.height <= 0:
            screen_height = 50
        else:
            screen_height = s.height
        self.assertEqual(s.vcenter, int(s.height / 2))
        self.assertEqual(s.hcenter, int(s.width / 2))
        b.place_item(board_items.Tile(sprite=sprites_panda["panda"]), 0, 0)
        self.assertIsInstance(b.render_cell(1, 1), Sprixel)
        b.item(19, 19).model = "@"
        b.item(19, 19).sprixel = None
        self.assertIsInstance(b.render_cell(19, 19), Sprixel)
        self.assertEqual(b.render_cell(19, 19), Sprixel())
        with self.assertRaises(base.PglOutOfBoardBoundException):
            b.render_cell(50, 50)
        self.assertIsNone(s.clear_buffers())
        self.assertIsNone(s.clear_screen_buffer())
        # And again after clear buffers.
        if s.width <= 0 or s.height <= 0:
            s._display_buffer = np.array(
                [[Sprixel(" ") for i in range(0, 50, 1)] for j in range(0, 50, 1)]
            )
            s._screen_buffer = np.array(
                [[Sprixel(" ") for i in range(0, 50, 1)] for j in range(0, 50, 1)]
            )
        self.assertTrue(s._is_dirty)
        self.assertTrue(functions.pgl_isinstance(s.buffer, "numpy.ndarray"))
        self.assertIsNone(s.update())
        b = engine.Board(size=[1, 1])
        b.place_item(board_items.Wall(model="##"), 0, 0)
        self.assertIsNone(s.place("test", 0, 0))
        self.assertIsNone(s.place(b, 1, 0))
        t = base.Text("test 2")
        self.assertIsNone(s.place(t, 2, 0))
        self.assertIsNone(s.place(sprites_panda["panda"], 0, 5))
        self.assertIsNone(s.place(TB(), 3, 0))
        self.assertIsNone(s.place(board_items.BoardItem(model="##"), 10, 0))
        self.assertIsNone(
            s.place(
                board_items.Tile(
                    sprixels=[
                        [Sprixel("##"), Sprixel("##")],
                        [Sprixel("##"), Sprixel("##")],
                    ]
                ),
                4,
                0,
            )
        )
        with self.assertRaises(base.PglInvalidTypeException):
            s.place(None, 0, 0)
        with self.assertRaises(base.PglInvalidTypeException):
            s.place(1, 0, 0)
        s.update()
        t.text = "update"
        self.assertIsNone(
            s.place(sprites_panda["panda"], screen_height - 2, screen_width - 2)
        )
        self.assertIsNone(s.place("test", 1, screen_width - 2))
        s.update()
        self.assertIsNone(s.render())  # Should not render
        self.assertFalse(s.need_rendering)
        s.trigger_rendering()
        self.assertTrue(s.need_rendering)
        # Now testing partial display
        camera = board_items.Camera()
        camera.row = 0
        camera.column = 0

        b = engine.Board(
            size=[screen_width * 2, screen_height * 2],
            enable_partial_display=True,
            partial_display_viewport=[
                int(screen_height / 2) - 1,
                int(screen_width / 2) - 1,
            ],
            partial_display_focus=camera,
            DISPLAY_SIZE_WARNINGS=False,
        )
        for row in range(0, b.height):
            for col in range(0, b.width):
                b.place_item(
                    board_items.Wall(
                        sprixel=Sprixel(" ", Color(row * 4, col, int((row + col) / 2))),
                    ),
                    row,
                    col,
                )
        self.assertIsNone(s.place(b, 0, 0, 2))
        s.trigger_rendering()
        self.assertIsNone(s.update())
        b.partial_display_viewport = [
            int(screen_height * 3) - 1,
            int(screen_width * 3) - 1,
        ]
        camera.row += 1
        with self.assertRaises(IndexError):
            s.force_render()
            s.update()
        b.partial_display_viewport = [
            int(screen_height / 2) - 1,
            int(screen_width / 2) - 1,
        ]
        camera.row = b.height - 1
        camera.column = b.width - 1
        self.assertIsNone(s.trigger_rendering())
        self.assertIsNone(s.update())
        camera = board_items.Tile(
            sprite=Sprite(
                sprixels=[[Sprixel("+"), Sprixel("+")], [Sprixel("+"), Sprixel("+")]]
            )
        )
        b.partial_display_focus = camera
        # Please never do that in real life...
        camera.pos = [1, 1]
        self.assertIsNone(s.trigger_rendering())
        self.assertIsNone(s.render())
        self.assertIsNone(s.update())
        # This will succeed but the str type cannot benefit from deferred rendering.
        self.assertIsNone(s.place("test delete", 0, 0, 2))
        self.assertIsNone(s.update())
        self.assertIsNone(s.delete(0, 0))
        self.assertIsNone(s.update())
    def test_screen_buffer(self):
        sprites_panda = SpriteCollection.load_json_file("tests/panda.spr")
        b = engine.Board(size=[20, 20])
        s = engine.Screen(50, 50)
        # Because CircleCI return a console with no size (most probably because we are
        # not attached to any terminal), we need to make sure that the partial display
        # tests work in that environment too
        screen_width = 0
        screen_height = 0
        if s.width <= 0:
            screen_width = 50
        else:
            screen_width = s.width
        if s.height <= 0:
            screen_height = 50
        else:
            screen_height = s.height
        self.assertEqual(s.vcenter, int(s.height / 2))
        self.assertEqual(s.hcenter, int(s.width / 2))
        b.place_item(board_items.Tile(sprite=sprites_panda["panda"]), 0, 0)
        self.assertIsInstance(b.render_cell(1, 1), Sprixel)
        b.item(19, 19).model = "@"
        b.item(19, 19).sprixel = None
        self.assertIsInstance(b.render_cell(19, 19), Sprixel)
        self.assertEqual(b.render_cell(19, 19), Sprixel())
        b.place_item(board_items.Door(), 19, 19)
        b.place_item(
            board_items.Door(sprixel=Sprixel(
                "*", Color(125, 125, 0), is_bg_transparent=False)),
            19,
            19,
        )
        b.place_item(
            board_items.Door(sprixel=Sprixel("#", is_bg_transparent=True)), 19,
            19)
        b.place_item(
            board_items.NPC(sprixel=Sprixel("$", is_bg_transparent=True)), 19,
            19)
        self.assertEqual(b.layers(19, 19), 4)
        b.place_item(
            board_items.BoardItemVoid(sprixel=Sprixel(is_bg_transparent=True)),
            19, 19)
        b.place_item(
            board_items.BoardItemVoid(sprixel=Sprixel(is_bg_transparent=True)),
            19, 19)
        self.assertIsInstance(b.render_cell(19, 19), Sprixel)
        b._clean_layers(19, 19)
        self.assertEqual(b.layers(19, 19), 3)
        b._clean_layers(18, 19)
        self.assertEqual(b.layers(18, 19), 1)
        with self.assertRaises(base.PglOutOfBoardBoundException):
            b.render_cell(50, 50)
        self.assertIsNone(s.clear_buffers())
        self.assertIsNone(s.clear_screen_buffer())
        self.assertTrue(s._is_dirty)
        self.assertTrue(functions.pgl_isinstance(s.buffer, "numpy.ndarray"))
        self.assertIsNone(s.update())
        b = engine.Board(size=[1, 1])
        b.place_item(board_items.Wall(model="##"), 0, 0)
        self.assertIsNone(s.place("test", 0, 0))
        self.assertIsNone(s.place(b, 1, 0))
        self.assertIsInstance(s.get(1, 0), engine.Board)
        t = base.Text("test 2")
        self.assertIsNone(s.place(t, 2, 0))
        self.assertIsNone(s.place(sprites_panda["panda"], 0, 5))
        self.assertIsNone(s.place(TB(), 3, 0))
        self.assertIsNone(s.place(board_items.BoardItem(model="##"), 10, 0))
        self.assertIsNone(
            s.place(
                board_items.Tile(sprite=Sprite(sprixels=[
                    [Sprixel("##"), Sprixel("##")],
                    [Sprixel("##"), Sprixel("##")],
                ])),
                4,
                0,
            ))
        self.assertIsNone(
            s.place(
                Sprite(sprixels=[
                    [Sprixel("##"), Sprixel("##")],
                    [Sprixel("###"), Sprixel("##")],
                ]),
                8,
                0,
            ))
        s.force_render()
        with self.assertRaises(base.PglInvalidTypeException):
            s.place(None, 0, 0)
        with self.assertRaises(base.PglInvalidTypeException):
            s.place(1, 0, 0)
        with self.assertRaises(base.PglException):
            s.place(TB(), 400, 0)
        with self.assertRaises(base.PglException):
            s.place(TB(), 0, 400)
        s.force_update()
        t.text = "update"
        self.assertIsNone(
            s.place(sprites_panda["panda"], screen_height - 2,
                    screen_width - 2))
        self.assertIsNone(s.place("test", 1, screen_width - 2))
        s.update()
        self.assertIsNone(s.render())  # Should not render
        self.assertFalse(s.need_rendering)
        s.trigger_rendering()
        self.assertTrue(s.need_rendering)
        # Now testing partial display
        camera = board_items.Camera()
        camera.row = 0
        camera.column = 0

        b = engine.Board(
            size=[screen_width * 2, screen_height * 2],
            enable_partial_display=True,
            partial_display_viewport=[
                int(screen_height / 2) - 1,
                int(screen_width / 2) - 1,
            ],
            partial_display_focus=camera,
            DISPLAY_SIZE_WARNINGS=False,
        )
        for row in range(0, b.height):
            for col in range(0, b.width):
                b.place_item(
                    board_items.Wall(sprixel=Sprixel(
                        " ", Color(row * 4, col, int((row + col) / 2))), ),
                    row,
                    col,
                )
        self.assertIsNone(s.place(b, 0, 0, 2))
        s.trigger_rendering()
        self.assertIsNone(s.update())
        b.partial_display_viewport = [
            int(screen_height / 2) - 1,
            int(screen_width / 2) - 1,
        ]
        camera.row = b.height - 1
        camera.column = b.width - 1
        self.assertIsNone(s.trigger_rendering())
        self.assertIsNone(s.update())
        camera = board_items.Tile(
            sprite=Sprite(sprixels=[[Sprixel("+"), Sprixel("+")],
                                    [Sprixel("+"), Sprixel("+")]]))
        b.partial_display_focus = camera
        # Please never do that in real life...
        camera.pos = [1, 1]
        self.assertIsNone(s.trigger_rendering())
        self.assertIsNone(s.render())
        self.assertIsNone(s.update())
        # This will succeed but the str type cannot benefit from deferred rendering.
        self.assertIsNone(s.place("test delete", 0, 0, 2))
        self.assertIsNone(s.update())
        self.assertIsNone(s.delete(0, 0))
        self.assertIsNone(s.update())
        self.assertIsNone(
            functions.render_string_to_buffer(
                "hello",
                s._display_buffer,
                0,
                0,
                s._display_buffer.shape[0],
                s._display_buffer.shape[1],
            ))
        # This test has nothing to do here... but I'm lazy.
        self.assertEqual(5, functions.clamp(5, 0, 10))
        self.assertEqual(0, functions.clamp(-5, 0, 10))
        self.assertEqual(10, functions.clamp(15, 0, 10))
        # This one either and it's even worst: it's going to disappear!
        t = base.Text("this is a text\non multiple lines")
        t.render_to_buffer(
            s._display_buffer,
            s._display_buffer.shape[0] - 1,
            s._display_buffer.shape[1] - 5,
            s._display_buffer.shape[0],
            s._display_buffer.shape[1],
        )
        s.update()
        t = base.Text(
            "this is a text",
            Color(0, 0, 0),
            Color(255, 255, 255),
            font=Font("8bits"),
        )
        t.render_to_buffer(
            s._display_buffer,
            s._display_buffer.shape[0] - 1,
            s._display_buffer.shape[1] - 5,
            s._display_buffer.shape[0],
            s._display_buffer.shape[1],
        )
        s.update()