From 1d26b02bc53a24d2e7bcd96ae57b72d94dfcade6 Mon Sep 17 00:00:00 2001 From: Raphael Fernandes Date: Fri, 15 Nov 2024 21:05:59 +0100 Subject: [PATCH 1/2] Load game map improved --- rpg/load_game_map.py | 68 +++++++------------------------------------- 1 file changed, 10 insertions(+), 58 deletions(-) diff --git a/rpg/load_game_map.py b/rpg/load_game_map.py index 083919e2..378050ed 100644 --- a/rpg/load_game_map.py +++ b/rpg/load_game_map.py @@ -1,6 +1,3 @@ -""" -Load maps -""" import json import os from collections import OrderedDict @@ -29,27 +26,16 @@ def load_map(map_name): """ Load a map """ - game_map = GameMap() game_map.map_layers = OrderedDict() - game_map.light_layer = LightLayer(100, 100) # List of blocking sprites - layer_options = { - "trees_blocking": { - "use_spatial_hash": True, - }, - "misc_blocking": { - "use_spatial_hash": True, - }, - "bridges": { - "use_spatial_hash": True, - }, - "water_blocking": { - "use_spatial_hash": True, - }, + "trees_blocking": {"use_spatial_hash": True}, + "misc_blocking": {"use_spatial_hash": True}, + "bridges": {"use_spatial_hash": True}, + "water_blocking": {"use_spatial_hash": True}, } # Read in the tiled map @@ -95,7 +81,7 @@ def load_map(map_name): ) character_sprite.position = shape elif isinstance(shape, list) and len(shape[0]) == 2: - # Rect or polygon. + # Rect or polygon location = [shape[0][0], shape[0][1]] character_sprite = PathFollowingSprite( f":characters:{character_data['images']}" @@ -127,10 +113,7 @@ def load_map(map_name): if isinstance(shape, list) and len(shape) == 2: # Point - if "radius" in light_object.properties: - radius = light_object.properties["radius"] - else: - radius = 150 + radius = light_object.properties.get("radius", 150) mode = "soft" color = light_object.properties["color"] color = (color.red, color.green, color.blue) @@ -140,17 +123,12 @@ def load_map(map_name): else: print("Failed to add light") else: - # Hack - x = 0 - y = 0 - radius = 1 - mode = "soft" - color = arcade.csscolor.WHITE + # Hack: Add default light if no lights are found + x, y, radius, mode, color = 0, 0, 1, "soft", arcade.csscolor.WHITE dummy_light = Light(x, y, radius, color, mode) game_map.light_layer.add(dummy_light) print("Added default light") - # Get all the tiled sprite lists # Get all the tiled sprite lists game_map.map_layers = my_map.sprite_lists @@ -162,12 +140,11 @@ def load_map(map_name): game_map.properties = my_map.properties - # Any layer with '_blocking' in it, will be a wall + # Add wall list for blocking layers game_map.scene.add_sprite_list("wall_list", use_spatial_hash=True) for layer, sprite_list in game_map.map_layers.items(): if "_blocking" in layer: game_map.scene.remove_sprite_list_by_object(sprite_list) - game_map.scene["wall_list"].extend(sprite_list) return game_map @@ -178,35 +155,10 @@ def load_maps(): Load all the Tiled maps from a directory. (Must use the .json extension.) """ - - # Directory to pull maps from mypath = "resources/maps" if load_maps.map_file_names is None: - # Dictionary to hold all our maps load_maps.map_list = {} - # Pull names of all json files in that path - load_maps.map_file_names = [ - f[:-5] - for f in os.listdir(mypath) - if isfile(join(mypath, f)) and f.endswith(".json") - ] - load_maps.map_file_names.sort() - load_maps.file_count = len(load_maps.map_file_names) - - # Loop and load each file - map_name = load_maps.map_file_names.pop(0) - load_maps.map_list[map_name] = load_map(f"resources/maps/{map_name}.json") - - files_left = load_maps.file_count - len(load_maps.map_file_names) - progress = 100 * files_left / load_maps.file_count - - done = len(load_maps.map_file_names) == 0 - return done, progress, load_maps.map_list - - -load_maps.map_file_names = None -load_maps.map_list = None -load_maps.file_count = None + # Pull names of From 3bfe62c6ce96a6c483fa0a061a353479a55a0cd6 Mon Sep 17 00:00:00 2001 From: Raphael Fernandes Date: Sat, 16 Nov 2024 00:38:08 +0100 Subject: [PATCH 2/2] Improvments --- rpg/load_game_map.py | 107 +++++++++++++++++++++---------------------- 1 file changed, 53 insertions(+), 54 deletions(-) diff --git a/rpg/load_game_map.py b/rpg/load_game_map.py index 378050ed..0bd4e3e4 100644 --- a/rpg/load_game_map.py +++ b/rpg/load_game_map.py @@ -1,3 +1,6 @@ +""" +Load maps +""" import json import os from collections import OrderedDict @@ -26,121 +29,96 @@ def load_map(map_name): """ Load a map """ + game_map = GameMap() game_map.map_layers = OrderedDict() game_map.light_layer = LightLayer(100, 100) # List of blocking sprites layer_options = { - "trees_blocking": {"use_spatial_hash": True}, - "misc_blocking": {"use_spatial_hash": True}, - "bridges": {"use_spatial_hash": True}, - "water_blocking": {"use_spatial_hash": True}, + layer: {"use_spatial_hash": True} + for layer in ["trees_blocking", "misc_blocking", "bridges", "water_blocking"] } # Read in the tiled map print(f"Loading map: {map_name}") - my_map = arcade.tilemap.load_tilemap( - map_name, scaling=TILE_SCALING, layer_options=layer_options - ) - + my_map = arcade.tilemap.load_tilemap(map_name, scaling=TILE_SCALING, layer_options=layer_options) game_map.scene = arcade.Scene.from_tilemap(my_map) - if "characters" in my_map.object_lists: - f = open("resources/data/characters_dictionary.json") + # Load character dictionary only once + with open("resources/data/characters_dictionary.json") as f: character_dictionary = json.load(f) + + # Load characters from map objects + if "characters" in my_map.object_lists: character_object_list = my_map.object_lists["characters"] for character_object in character_object_list: - if "type" not in character_object.properties: - print( - f"No 'type' field for character in map {map_name}. {character_object.properties}" - ) + print(f"No 'type' field for character in map {map_name}. {character_object.properties}") continue character_type = character_object.properties["type"] if character_type not in character_dictionary: - print( - f"Unable to find '{character_type}' in characters_dictionary.json." - ) + print(f"Unable to find '{character_type}' in characters_dictionary.json.") continue character_data = character_dictionary[character_type] shape = character_object.shape + character_sprite = None + # Handle different character shapes if isinstance(shape, list) and len(shape) == 2: # Point - if character_object.properties.get("movement") == "random": - character_sprite = RandomWalkingSprite( - f":characters:{character_data['images']}", game_map.scene - ) - else: - character_sprite = CharacterSprite( - f":characters:{character_data['images']}" - ) + character_sprite = RandomWalkingSprite( + f":characters:{character_data['images']}", game_map.scene + ) if character_object.properties.get("movement") == "random" else CharacterSprite( + f":characters:{character_data['images']}" + ) character_sprite.position = shape elif isinstance(shape, list) and len(shape[0]) == 2: # Rect or polygon location = [shape[0][0], shape[0][1]] - character_sprite = PathFollowingSprite( - f":characters:{character_data['images']}" - ) + character_sprite = PathFollowingSprite(f":characters:{character_data['images']}") character_sprite.position = location - path = [] - for point in shape: - location = [point[0], point[1]] - path.append(location) - character_sprite.path = path + character_sprite.path = [[point[0], point[1]] for point in shape] else: - print( - f"Unknown shape type for character with shape '{shape}' in map {map_name}." - ) + print(f"Unknown shape type for character with shape '{shape}' in map {map_name}.") continue print(f"Adding character {character_type} at {character_sprite.position}") game_map.scene.add_sprite("characters", character_sprite) + # Handle lights if "lights" in my_map.object_lists: lights_object_list = my_map.object_lists["lights"] - for light_object in lights_object_list: if "color" not in light_object.properties: print(f"No color for light in map {map_name}.") continue shape = light_object.shape - if isinstance(shape, list) and len(shape) == 2: - # Point radius = light_object.properties.get("radius", 150) - mode = "soft" - color = light_object.properties["color"] - color = (color.red, color.green, color.blue) - light = Light(shape[0], shape[1], radius, color, mode) + color = (light_object.properties["color"].red, light_object.properties["color"].green, light_object.properties["color"].blue) + light = Light(shape[0], shape[1], radius, color, "soft") game_map.light_layer.add(light) print("Added light", color, "radius", radius) else: print("Failed to add light") else: - # Hack: Add default light if no lights are found - x, y, radius, mode, color = 0, 0, 1, "soft", arcade.csscolor.WHITE - dummy_light = Light(x, y, radius, color, mode) + # Default light if no lights are specified + dummy_light = Light(0, 0, 1, arcade.csscolor.WHITE, "soft") game_map.light_layer.add(dummy_light) print("Added default light") - # Get all the tiled sprite lists + # Set map layers and properties game_map.map_layers = my_map.sprite_lists - - # Define the size of the map, in tiles game_map.map_size = my_map.width, my_map.height - - # Set the background color game_map.background_color = my_map.background_color - game_map.properties = my_map.properties - # Add wall list for blocking layers + # Add all '_blocking' layers as walls game_map.scene.add_sprite_list("wall_list", use_spatial_hash=True) for layer, sprite_list in game_map.map_layers.items(): if "_blocking" in layer: @@ -155,10 +133,31 @@ def load_maps(): Load all the Tiled maps from a directory. (Must use the .json extension.) """ + + # Directory to pull maps from mypath = "resources/maps" if load_maps.map_file_names is None: # Dictionary to hold all our maps load_maps.map_list = {} - # Pull names of + # Pull names of all json files in that path + load_maps.map_file_names = sorted( + [f[:-5] for f in os.listdir(mypath) if isfile(join(mypath, f)) and f.endswith(".json")] + ) + load_maps.file_count = len(load_maps.map_file_names) + + # Load each file + map_name = load_maps.map_file_names.pop(0) + load_maps.map_list[map_name] = load_map(f"resources/maps/{map_name}.json") + + files_left = load_maps.file_count - len(load_maps.map_file_names) + progress = 100 * files_left / load_maps.file_count + + done = len(load_maps.map_file_names) == 0 + return done, progress, load_maps.map_list + + +load_maps.map_file_names = None +load_maps.map_list = None +load_maps.file_count = None