def load_board(self, filename, lvl_number=0): """Load a saved board Load a Board saved on the disk as a JSON file. This method creates a new Board object, populate it with all the elements (except a Player) and then return it. If the filename argument is not an existing file, the open function is going to raise an exception. This method, load the board from the JSON file, populate it with all BoardItem included, check for sanity, init the board with BoardItemVoid and then associate the freshly created board to a lvl_number. It then create the NPCs and add them to the board. :param filename: The file to load :type filename: str :param lvl_number: The level number to associate the board to. Default is 0. :type lvl_number: int :returns: a newly created board (see :class:`gamelib.Board.Board`) Example:: mynewboard = game.load_board( 'awesome_level.json', 1 ) game.change_level( 1 ) """ with open(filename, 'r') as f: data = json.load(f) local_board = Board() data_keys = data.keys() if "name" in data_keys: local_board.name = data["name"] if 'size' in data_keys: local_board.size = data['size'] if 'player_starting_position' in data_keys: local_board.player_starting_position = data[ 'player_starting_position'] if 'ui_border_top' in data_keys: local_board.ui_border_top = data['ui_border_top'] if 'ui_border_bottom' in data_keys: local_board.ui_border_bottom = data['ui_border_bottom'] if 'ui_border_left' in data_keys: local_board.ui_border_left = data['ui_border_left'] if 'ui_border_right' in data_keys: local_board.ui_border_right = data['ui_border_right'] if 'ui_board_void_cell' in data_keys: local_board.ui_board_void_cell = data['ui_board_void_cell'] # Now we need to recheck for board sanity local_board.check_sanity() # and re-initialize the board (mainly to attribute a new model to the void cells as it's not dynamic). local_board.init_board() # Then add board to the game self.add_board(lvl_number, local_board) # Define an internal function to transform directions string into constants def _string_to_constant(s): if type(s) is int: return s elif s == "UP": return Constants.UP elif s == "DOWN": return Constants.DOWN elif s == "RIGHT": return Constants.RIGHT elif s == "LEFT": return Constants.LEFT elif s == "DRUP": return Constants.DRUP elif s == "DRDOWN": return Constants.DRDOWN elif s == "DLDOWN": return Constants.DLDOWN elif s == "DLUP": return Constants.DLUP def _ref2obj(ref): obj_keys = ref.keys() local_object = BoardItemVoid() if 'Wall' in ref['object']: local_object = Structures.Wall() elif 'Treasure' in ref['object']: local_object = Structures.Treasure() if 'value' in obj_keys: local_object.value = ref['value'] if 'size' in obj_keys: local_object._size = ref['size'] elif 'GenericStructure' in ref['object']: local_object = Structures.GenericStructure() if 'value' in obj_keys: local_object.value = ref['value'] if 'size' in obj_keys: local_object._size = ref['size'] if 'pickable' in obj_keys: local_object.set_pickable(ref['pickable']) if 'overlappable' in obj_keys: local_object.set_overlappable(ref['overlappable']) elif 'Door' in ref['object']: local_object = Structures.Door() if 'value' in obj_keys: local_object.value = ref['value'] if 'size' in obj_keys: local_object._size = ref['size'] if 'pickable' in obj_keys: local_object.set_pickable(ref['pickable']) if 'overlappable' in obj_keys: local_object.set_overlappable(ref['overlappable']) if 'restorable' in obj_keys: local_object.set_restorable(ref['restorable']) elif 'GenericActionableStructure' in ref['object']: local_object = Structures.GenericActionableStructure() if 'value' in obj_keys: local_object.value = ref['value'] if 'size' in obj_keys: local_object._size = ref['size'] if 'pickable' in obj_keys: local_object.set_pickable(ref['pickable']) if 'overlappable' in obj_keys: local_object.set_overlappable(ref['overlappable']) elif 'NPC' in ref['object']: local_object = NPC() if 'value' in obj_keys: local_object.value = ref['value'] if 'size' in obj_keys: local_object._size = ref['size'] if 'hp' in obj_keys: local_object.hp = ref['hp'] if 'max_hp' in obj_keys: local_object.max_hp = ref['max_hp'] if 'step' in obj_keys: local_object.step = ref['step'] if 'remaining_lives' in obj_keys: local_object.remaining_lives = ref['remaining_lives'] if 'attack_power' in obj_keys: local_object.attack_power = ref['attack_power'] if 'actuator' in obj_keys: if 'RandomActuator' in ref['actuator']['type']: local_object.actuator = RandomActuator(moveset=[]) if 'moveset' in ref['actuator'].keys(): for m in ref['actuator']['moveset']: local_object.actuator.moveset.append( _string_to_constant(m)) elif 'PathActuator' in ref['actuator']['type']: local_object.actuator = PathActuator(path=[]) if 'path' in ref['actuator'].keys(): for m in ref['actuator']['path']: local_object.actuator.path.append( _string_to_constant(m)) # Now what remains is what is common to all BoardItem if not isinstance(local_object, BoardItemVoid): if 'name' in obj_keys: local_object.name = ref['name'] if 'model' in obj_keys: local_object.model = ref['model'] if 'type' in obj_keys: local_object.type = ref['type'] return local_object # Now load the library if any if 'library' in data_keys: self.object_library = [] for e in data['library']: self.object_library.append(_ref2obj(e)) # Now let's place the good stuff on the board if 'map_data' in data_keys: for pos_x in data['map_data'].keys(): x = int(pos_x) for pos_y in data['map_data'][pos_x].keys(): y = int(pos_y) ref = data['map_data'][pos_x][pos_y] obj_keys = ref.keys() if 'object' in obj_keys: o = _ref2obj(ref) if not isinstance(o, NPC) and not isinstance( o, BoardItemVoid): local_board.place_item(o, x, y) elif isinstance(o, NPC): self.add_npc(lvl_number, o, x, y) else: Utils.warn( f'while loading the board in {filename}, at coordinates [{pos_x},{pos_y}] there is an entry without "object" attribute. NOT LOADED.' ) return local_board
size=[dg_width, dg_height], name="Random dungeon", ui_borders=Utils.WHITE_SQUARE, ui_board_void_cell=Utils.BLACK_SQUARE, DISPLAY_SIZE_WARNINGS=False, ) potential_starting_position = [] for r in range(dg.height): for c in range(dg.width): if dg.dungeon[r][c].get_ch() == "#": random_board.place_item(Wall(model=Sprites.WALL), r, c) elif dg.dungeon[r][c].get_ch() == "+": random_board.place_item(Door(model=Sprites.DOOR), r, c) elif dg.dungeon[r][c].get_ch() == ".": potential_starting_position.append([r, c]) random_board.player_starting_position = choice(potential_starting_position) print("done") print(f"starting position chosen: {random_board.player_starting_position}") # dg.print_map() # random_board.display() input("New random dungeon generated. Next?") g = Game() g.enable_partial_display = True # g.load_board('/home/arnaud/Code/Games/hgl-editor/Large_Dungeon.json', 1) g.player = Player(model=Sprites.MAGE) g.add_board(1, random_board) g.change_level(1) key = None