From 846b21c341937c0ddee8db4e3b9d2785b9d275ab Mon Sep 17 00:00:00 2001 From: Paul Gestwicki Date: Fri, 2 Feb 2024 13:45:42 -0500 Subject: [PATCH] Fix locations on web build The way we were loading location images worked locally but not in the web build. This approach works, though it requires to have the locations listed in the GameMap configuration. The integration test has been updated to check whether the number of pngs in the locations folder matches the registered locations in the GameMap in hopes that this prevents errors later. --- project/locations/game_map.gd | 22 ++++++++++--------- project/test/integration/game_map_test.gd | 14 +++++++----- .../integration/simple_story_validity_test.gd | 3 +-- project/ui/main_scene.gd | 1 - 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/project/locations/game_map.gd b/project/locations/game_map.gd index e586e2d..1343bdf 100644 --- a/project/locations/game_map.gd +++ b/project/locations/game_map.gd @@ -1,10 +1,22 @@ ## Holds all the available locations class_name GameMap extends RefCounted +const _RESOURCE_PATHS : Array[String] = [ + "res://locations/classroom.png", + "res://locations/hallway.png", +] + ## Maps location names to their textures. ## The keys are plain location names, like "hallway". var _dictionary := {} +func _init(): + for resource_path in _RESOURCE_PATHS: + var file_name := resource_path.substr(resource_path.rfind("/")+1) + var key := file_name.substr(0, file_name.rfind(".")) + _dictionary[key] = load(resource_path) + + func get_by_name(name:String)->Texture: return _dictionary[name] @@ -13,16 +25,6 @@ func has_location(name:String)->bool: return _dictionary.keys().has(name) -## Load all of the png images in the given folder as locations -func load(path:String)->void: - for file_name in DirAccess.get_files_at(path): - if file_name.ends_with(".png"): - var resource_path := path + file_name - var texture := load(resource_path) - var key := file_name.substr(0, file_name.find(".png")) - _dictionary[key] = texture - - func pick_random() -> Texture: return _dictionary.values().pick_random() diff --git a/project/test/integration/game_map_test.gd b/project/test/integration/game_map_test.gd index 065b16c..cd19974 100644 --- a/project/test/integration/game_map_test.gd +++ b/project/test/integration/game_map_test.gd @@ -1,10 +1,14 @@ extends GutTest -func test_load() -> void: +const LOCATIONS_PATH := "res://locations/" + +func test_load__all_files_in_folder_are_locations() -> void: var map := GameMap.new() - map.load("res://test/integration/test_locations/") - assert_eq(map.size(), 1, "One location should be loaded from the test locations.") - assert_true(map.has_location("test_location"), "The map should have loaded the test location") - assert_not_null(map.get_by_name("test_location"), "The location texture can be loaded by name") + var files_in_directory := 0 + for file_name in DirAccess.get_files_at(LOCATIONS_PATH): + if file_name.ends_with(".png"): + files_in_directory += 1 + + assert_eq(map.size(), files_in_directory, "Number of locations matches number of .png images in %s" % LOCATIONS_PATH) diff --git a/project/test/integration/simple_story_validity_test.gd b/project/test/integration/simple_story_validity_test.gd index 4e618cf..dc68f62 100644 --- a/project/test/integration/simple_story_validity_test.gd +++ b/project/test/integration/simple_story_validity_test.gd @@ -156,8 +156,7 @@ func test_all_story_options_end_story_exists(): func test_locations(): - var game_map := GameMap.new() - game_map.load("res://locations/") + var game_map :GameMap = autofree(GameMap.new()) for story_path in story_paths: var story := _load_simple_story(story_path) if "location" in story: diff --git a/project/ui/main_scene.gd b/project/ui/main_scene.gd index f141309..1ea05ca 100644 --- a/project/ui/main_scene.gd +++ b/project/ui/main_scene.gd @@ -35,7 +35,6 @@ func _ready(): # Initialize the world world = World.new() world.cast.load_cast(_CAST_PATH) - world.game_map.load(_LOCATIONS_PATH) _game_screen.world = world ## Load all the stories in _STARTING_STORY_PATH