def test_patrol(self): a = actuators.PatrolActuator(path=[constants.UP, constants.DOWN]) data = a.serialize() self.assertIsNotNone(data) self.assertEqual(data["path"], [constants.UP, constants.DOWN]) al = actuators.PatrolActuator.load(data) self.assertEqual(al.path, [constants.UP, constants.DOWN]) self.assertEqual(a.state, al.state) a.pause() self.assertEqual(a.next_move(), constants.NO_DIR) a.start() a.next_move() self.assertEqual(a.next_move(), constants.DOWN) self.assertEqual(a.next_move(), constants.UP) self.assertEqual(a.next_move(), constants.DOWN) a.set_path([constants.LEFT, constants.RIGHT]) self.assertEqual(a.next_move(), constants.LEFT) self.assertEqual(a.next_move(), constants.RIGHT) self.assertEqual(a.next_move(), constants.LEFT) self.assertEqual(a.next_move(), constants.RIGHT) a.set_path([ constants.DLDOWN, constants.DLUP, constants.DRDOWN, constants.DRUP ]) self.assertEqual(a.next_move(), constants.DLDOWN) self.assertEqual(a.next_move(), constants.DLUP) self.assertEqual(a.next_move(), constants.DRDOWN) self.assertEqual(a.next_move(), constants.DRUP) self.assertEqual(a.next_move(), constants.DLDOWN) self.assertEqual(a.next_move(), constants.DLUP) self.assertEqual(a.next_move(), constants.DRDOWN) self.assertEqual(a.next_move(), constants.DRUP)
def test_patrol(self): a = actuators.PatrolActuator(path=[constants.UP, constants.DOWN]) a.next_move() self.assertEqual(a.next_move(), constants.DOWN) self.assertEqual(a.next_move(), constants.UP) self.assertEqual(a.next_move(), constants.DOWN) a.set_path([constants.LEFT, constants.RIGHT]) self.assertEqual(a.next_move(), constants.LEFT) self.assertEqual(a.next_move(), constants.RIGHT) self.assertEqual(a.next_move(), constants.LEFT) self.assertEqual(a.next_move(), constants.RIGHT) a.set_path([constants.DLDOWN, constants.DLUP, constants.DRDOWN, constants.DRUP]) self.assertEqual(a.next_move(), constants.DLDOWN) self.assertEqual(a.next_move(), constants.DLUP) self.assertEqual(a.next_move(), constants.DRDOWN) self.assertEqual(a.next_move(), constants.DRUP) self.assertEqual(a.next_move(), constants.DLDOWN) self.assertEqual(a.next_move(), constants.DLUP) self.assertEqual(a.next_move(), constants.DRDOWN) self.assertEqual(a.next_move(), constants.DRUP)
def test_tools_function(self): b = engine.Board() g = engine.Game() g.player = constants.NO_PLAYER self.assertIsNone(g.add_board(1, b)) self.assertIsNone(g.change_level(1)) self.assertIsNone( g.add_npc(1, board_items.NPC(value=10, inventory_space=1), 1, 1)) tmp_npc = g.get_board(1).item(1, 1) tmp_npc.actuator = actuators.PathFinder(game=g, actuated_object=tmp_npc) tmp_npc.actuator.set_destination(2, 5) tmp_npc.actuator.find_path() self.assertIsNone( b.place_item(board_items.Door(value=10, inventory_space=1), 1, 2)) self.assertIsNone( b.place_item(board_items.Wall(value=10, inventory_space=1), 1, 3)) self.assertIsNone( b.place_item( board_items.GenericStructure(value=10, inventory_space=1), 1, 4)) self.assertIsNone( b.place_item( board_items.GenericActionableStructure(value=10, inventory_space=1), 1, 5, )) self.assertIsNone( b.place_item( board_items.Door(value=10, inventory_space=1, sprixel=core.Sprixel("#")), 2, 2, )) self.assertIsNone( b.place_item(board_items.Treasure(value=10, inventory_space=1), 1, 6)) with self.assertRaises(base.PglInvalidTypeException): g.neighbors("2") with self.assertRaises(base.PglInvalidTypeException): g.neighbors(2, "crash") g.object_library.append(board_items.NPC()) self.assertIsNone( g.save_board(1, "test-pygamelib.engine.Game.lvl1.json")) with self.assertRaises(base.PglInvalidTypeException): g.save_board("1", "test-pygamelib.engine.Game.lvl1.json") with self.assertRaises(base.PglInvalidTypeException): g.save_board(1, 1) with self.assertRaises(base.PglInvalidLevelException): g.save_board(11, "test-pygamelib.engine.Game.lvl1.json") self.assertIsInstance( g.load_board("test-pygamelib.engine.Game.lvl1.json", 1), engine.Board) self.assertEqual(g._string_to_constant("UP"), constants.UP) self.assertEqual(g._string_to_constant("DOWN"), constants.DOWN) self.assertEqual(g._string_to_constant("LEFT"), constants.LEFT) self.assertEqual(g._string_to_constant("RIGHT"), constants.RIGHT) self.assertEqual(g._string_to_constant("DRUP"), constants.DRUP) self.assertEqual(g._string_to_constant("DRDOWN"), constants.DRDOWN) self.assertEqual(g._string_to_constant("DLUP"), constants.DLUP) self.assertEqual(g._string_to_constant("DLDOWN"), constants.DLDOWN) with self.assertRaises(base.PglInvalidTypeException): g.delete_level() with self.assertRaises(base.PglInvalidLevelException): g.delete_level(42) g.delete_level(1) g.delete_all_levels() self.assertIsNone(g.current_board()) bi = board_items.Door( value=10, inventory_space=0, pickable=False, overlappable=True, restorable=True, ) obj = engine.Game._ref2obj(bi.serialize()) self.assertIsInstance(obj, board_items.Door) bi = board_items.Treasure( value=10, inventory_space=0, pickable=False, overlappable=True, restorable=True, ) obj = engine.Game._ref2obj(bi.serialize()) self.assertIsInstance(obj, board_items.Treasure) bi = board_items.GenericActionableStructure( value=10, inventory_space=0, pickable=False, overlappable=True, restorable=True, ) obj = engine.Game._ref2obj(bi.serialize()) self.assertIsInstance(obj, board_items.GenericActionableStructure) bi = board_items.NPC( value=10, inventory_space=10, pickable=False, overlappable=True, restorable=True, ) bi.actuator = actuators.PathActuator(path=[constants.UP]) obj = engine.Game._ref2obj(bi.serialize()) self.assertIsInstance(obj, board_items.NPC) bi.actuator = actuators.PatrolActuator(path=[constants.UP]) obj = engine.Game._ref2obj(bi.serialize()) self.assertIsInstance(obj.actuator, actuators.PatrolActuator)