Skip to content

Commit

Permalink
Fix locations on web build
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
doctor-g committed Feb 2, 2024
1 parent 8f77164 commit 846b21c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
22 changes: 12 additions & 10 deletions project/locations/game_map.gd
Original file line number Diff line number Diff line change
@@ -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]

Expand All @@ -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()

Expand Down
14 changes: 9 additions & 5 deletions project/test/integration/game_map_test.gd
Original file line number Diff line number Diff line change
@@ -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)

3 changes: 1 addition & 2 deletions project/test/integration/simple_story_validity_test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion project/ui/main_scene.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 846b21c

Please sign in to comment.