diff --git a/scripts/domain/game_state.gd b/scripts/domain/game_state.gd index fd603d0..ed02d79 100644 --- a/scripts/domain/game_state.gd +++ b/scripts/domain/game_state.gd @@ -5,6 +5,9 @@ const TurnResolverType = preload("res://scripts/domain/turn_resolver.gd") const InteractionResolverType = preload("res://scripts/domain/interaction_resolver.gd") var player: ActorState +# Legacy renderer/game seams alias the active floor aggregate until their consumers +# move to FloorState directly in later world slices. +var floor_state: FloorState var enemies: Dictionary[StringName, ActorState] var map: GridMapState var visible_cells: Dictionary[Vector2i, bool] = {} @@ -12,11 +15,21 @@ var explored_cells: Dictionary[Vector2i, bool] = {} var exit_reached := false func _init(new_map: GridMapState, new_player: ActorState, new_enemies: Dictionary[StringName, ActorState]) -> void: - map = new_map player = new_player - enemies = new_enemies + floor_state = FloorState.new(new_map) + floor_state.entities = new_enemies + map = floor_state.map + enemies = floor_state.entities + explored_cells = floor_state.explored_cells update_exploration() +func bind_run_state(run_state: RunState) -> void: + run_state.set_floor_state(floor_state) + floor_state = run_state.current_floor_state() + map = floor_state.map + enemies = floor_state.entities + explored_cells = floor_state.explored_cells + func get_context_target() -> StringName: if exit_reached: return &"exit_reached" diff --git a/scripts/domain/grid_map_state.gd b/scripts/domain/grid_map_state.gd index 2041b81..0e7f5fb 100644 --- a/scripts/domain/grid_map_state.gd +++ b/scripts/domain/grid_map_state.gd @@ -6,6 +6,7 @@ enum CellKind { FLOOR, WALL, CLOSED_DOOR, CHEST, STAIRS } var size: Vector2i var _cells: Dictionary[Vector2i, CellKind] = {} +var _room_ids: Dictionary[Vector2i, StringName] = {} func _init(new_size: Vector2i, fill: CellKind = CellKind.FLOOR) -> void: size = new_size @@ -20,6 +21,17 @@ func set_cell(cell: Vector2i, kind: CellKind) -> void: func get_cell(cell: Vector2i) -> CellKind: return _cells.get(cell, CellKind.WALL) +func set_room_id(cell: Vector2i, room_id: StringName) -> void: + if not is_inside(cell): + return + if room_id.is_empty(): + _room_ids.erase(cell) + else: + _room_ids[cell] = room_id + +func get_room_id(cell: Vector2i) -> StringName: + return _room_ids.get(cell, &"") + func is_inside(cell: Vector2i) -> bool: return cell.x >= 0 and cell.y >= 0 and cell.x < size.x and cell.y < size.y @@ -30,3 +42,26 @@ func is_walkable(cell: Vector2i) -> bool: func is_opaque(cell: Vector2i) -> bool: var kind := get_cell(cell) return kind == CellKind.WALL or kind == CellKind.CLOSED_DOOR or kind == CellKind.CHEST + +func to_dict() -> Dictionary: + var tiles: Array[Dictionary] = [] + for y in range(size.y): + for x in range(size.x): + var cell := Vector2i(x, y) + tiles.append(TileState.new(cell, get_cell(cell), get_room_id(cell)).to_dict()) + return { + "size": [size.x, size.y], + "tiles": tiles, + } + +static func from_dict(data: Dictionary) -> GridMapState: + var serialized_size: Array = data.get("size", [0, 0]) + var map := GridMapState.new( + Vector2i(int(serialized_size[0]), int(serialized_size[1])), + CellKind.WALL + ) + for tile_data: Dictionary in data.get("tiles", []): + var tile := TileState.from_dict(tile_data) + map.set_cell(tile.cell, tile.kind) + map.set_room_id(tile.cell, tile.room_id) + return map diff --git a/scripts/domain/run/run_state.gd b/scripts/domain/run/run_state.gd index e6470ef..14b71d5 100644 --- a/scripts/domain/run/run_state.gd +++ b/scripts/domain/run/run_state.gd @@ -6,6 +6,7 @@ var settings: RunSettings var player_name := "Sans-Nom" var base_archetype: StringName = &"vanguard" var current_floor := 0 +var floors: Dictionary[int, FloorState] = {} var current_turn := 0 var log: Array[GameEvent] = [] @@ -13,6 +14,12 @@ func _init(new_seed: String, new_settings: RunSettings = null) -> void: settings = new_settings if new_settings != null else RunSettings.new(new_seed) seed = settings.seed +func set_floor_state(floor: FloorState) -> void: + floors[floor.floor_number] = floor + +func current_floor_state() -> FloorState: + return floors.get(current_floor) + func next_turn() -> int: current_turn += 1 return current_turn diff --git a/scripts/domain/world/floor_state.gd b/scripts/domain/world/floor_state.gd new file mode 100644 index 0000000..2217eec --- /dev/null +++ b/scripts/domain/world/floor_state.gd @@ -0,0 +1,106 @@ +class_name FloorState +extends RefCounted + +var seed: int +var floor_number: int +var map: GridMapState +var rooms: Array[RoomState] = [] +var entrance := Vector2i.ZERO +var exit := Vector2i.ZERO +var entities: Dictionary[StringName, ActorState] = {} +var chests: Dictionary[Vector2i, Dictionary] = {} +var ground_items: Dictionary[Vector2i, Array] = {} +var events: Array[Dictionary] = [] +var explored_cells: Dictionary[Vector2i, bool] = {} +var metadata: Dictionary = {} + +func _init(new_map: GridMapState, new_seed: int = 0, new_floor_number: int = 0) -> void: + map = new_map + seed = new_seed + floor_number = new_floor_number + +func to_dict() -> Dictionary: + var serialized_rooms: Array[Dictionary] = [] + for room in rooms: + serialized_rooms.append(room.to_dict()) + var serialized_entities: Array[Dictionary] = [] + for entity in entities.values(): + serialized_entities.append(_actor_to_dict(entity)) + return { + "seed": seed, + "floor_number": floor_number, + "map": map.to_dict(), + "rooms": serialized_rooms, + "entrance": _cell_to_array(entrance), + "exit": _cell_to_array(exit), + "entities": serialized_entities, + "chests": _cell_dictionary_to_array(chests), + "ground_items": _cell_dictionary_to_array(ground_items), + "events": events.duplicate(true), + "explored_cells": _explored_cells_to_array(), + "metadata": metadata.duplicate(true), + } + +static func from_dict(data: Dictionary) -> FloorState: + var floor := FloorState.new( + GridMapState.from_dict(data.get("map", {})), + int(data.get("seed", 0)), + int(data.get("floor_number", 0)) + ) + for room_data: Dictionary in data.get("rooms", []): + floor.rooms.append(RoomState.from_dict(room_data)) + floor.entrance = _array_to_cell(data.get("entrance", [0, 0])) + floor.exit = _array_to_cell(data.get("exit", [0, 0])) + for entity_data: Dictionary in data.get("entities", []): + var entity := _actor_from_dict(entity_data) + floor.entities[entity.id] = entity + for chest_data: Dictionary in data.get("chests", []): + floor.chests[_array_to_cell(chest_data.get("cell", [0, 0]))] = chest_data.get("value", {}).duplicate(true) + for item_data: Dictionary in data.get("ground_items", []): + floor.ground_items[_array_to_cell(item_data.get("cell", [0, 0]))] = item_data.get("value", []).duplicate(true) + for event_data: Dictionary in data.get("events", []): + floor.events.append(event_data.duplicate(true)) + for cell_data: Array in data.get("explored_cells", []): + floor.explored_cells[_array_to_cell(cell_data)] = true + floor.metadata = data.get("metadata", {}).duplicate(true) + return floor + +func _explored_cells_to_array() -> Array[Array]: + var serialized: Array[Array] = [] + for cell in explored_cells: + serialized.append(_cell_to_array(cell)) + return serialized + +static func _cell_dictionary_to_array(values: Dictionary) -> Array[Dictionary]: + var serialized: Array[Dictionary] = [] + for cell in values: + serialized.append({ + "cell": _cell_to_array(cell), + "value": values[cell].duplicate(true), + }) + return serialized + +static func _actor_to_dict(actor: ActorState) -> Dictionary: + return { + "id": String(actor.id), + "cell": _cell_to_array(actor.cell), + "hit_points": actor.hit_points, + "max_hit_points": actor.max_hit_points, + "attack_damage": actor.attack_damage, + } + +static func _actor_from_dict(data: Dictionary) -> ActorState: + var actor := ActorState.new( + StringName(data.get("id", "")), + _array_to_cell(data.get("cell", [0, 0])), + int(data.get("max_hit_points", data.get("hit_points", 0))), + int(data.get("attack_damage", 0)) + ) + actor.hit_points = int(data.get("hit_points", actor.max_hit_points)) + return actor + +static func _cell_to_array(cell: Vector2i) -> Array[int]: + return [cell.x, cell.y] + +static func _array_to_cell(serialized: Array) -> Vector2i: + return Vector2i(int(serialized[0]), int(serialized[1])) diff --git a/scripts/domain/world/floor_state.gd.uid b/scripts/domain/world/floor_state.gd.uid new file mode 100644 index 0000000..833526f --- /dev/null +++ b/scripts/domain/world/floor_state.gd.uid @@ -0,0 +1 @@ +uid://m5uvxlfrnl07 diff --git a/scripts/domain/world/room_state.gd b/scripts/domain/world/room_state.gd new file mode 100644 index 0000000..708d9a7 --- /dev/null +++ b/scripts/domain/world/room_state.gd @@ -0,0 +1,35 @@ +class_name RoomState +extends RefCounted + +var id: StringName +var bounds: Rect2i +var kind: StringName +var metadata: Dictionary = {} + +func _init(new_id: StringName, new_bounds: Rect2i, new_kind: StringName = &"", new_metadata: Dictionary = {}) -> void: + id = new_id + bounds = new_bounds + kind = new_kind + metadata = new_metadata.duplicate(true) + +func to_dict() -> Dictionary: + return { + "id": String(id), + "bounds": [bounds.position.x, bounds.position.y, bounds.size.x, bounds.size.y], + "kind": String(kind), + "metadata": metadata.duplicate(true), + } + +static func from_dict(data: Dictionary) -> RoomState: + var serialized_bounds: Array = data.get("bounds", [0, 0, 0, 0]) + return RoomState.new( + StringName(data.get("id", "")), + Rect2i( + int(serialized_bounds[0]), + int(serialized_bounds[1]), + int(serialized_bounds[2]), + int(serialized_bounds[3]) + ), + StringName(data.get("kind", "")), + data.get("metadata", {}).duplicate(true) + ) diff --git a/scripts/domain/world/room_state.gd.uid b/scripts/domain/world/room_state.gd.uid new file mode 100644 index 0000000..4613f35 --- /dev/null +++ b/scripts/domain/world/room_state.gd.uid @@ -0,0 +1 @@ +uid://twk2tqdb0tfc diff --git a/scripts/domain/world/tile_state.gd b/scripts/domain/world/tile_state.gd new file mode 100644 index 0000000..5cbd89d --- /dev/null +++ b/scripts/domain/world/tile_state.gd @@ -0,0 +1,26 @@ +class_name TileState +extends RefCounted + +var cell: Vector2i +var kind: GridMapState.CellKind +var room_id: StringName = &"" + +func _init(new_cell: Vector2i, new_kind: GridMapState.CellKind, new_room_id: StringName = &"") -> void: + cell = new_cell + kind = new_kind + room_id = new_room_id + +func to_dict() -> Dictionary: + return { + "cell": [cell.x, cell.y], + "kind": int(kind), + "room_id": String(room_id), + } + +static func from_dict(data: Dictionary) -> TileState: + var coordinates: Array = data.get("cell", [0, 0]) + return TileState.new( + Vector2i(int(coordinates[0]), int(coordinates[1])), + int(data.get("kind", GridMapState.CellKind.WALL)) as GridMapState.CellKind, + StringName(data.get("room_id", "")) + ) diff --git a/scripts/domain/world/tile_state.gd.uid b/scripts/domain/world/tile_state.gd.uid new file mode 100644 index 0000000..76026f2 --- /dev/null +++ b/scripts/domain/world/tile_state.gd.uid @@ -0,0 +1 @@ +uid://4a8dcxs3fm4b diff --git a/scripts/game/game.gd b/scripts/game/game.gd index fdbf1f5..84f5415 100644 --- a/scripts/game/game.gd +++ b/scripts/game/game.gd @@ -64,6 +64,7 @@ func _initialize_run_state() -> void: run_state = RunStateType.new(settings.seed, settings) run_state.player_name = pending_player_name run_state.base_archetype = pending_base_archetype + game_state.bind_run_state(run_state) if pending_run_settings != null: var game_started := GameEventType.new( run_state.next_turn(), diff --git a/tests/unit/test_floor_state.gd b/tests/unit/test_floor_state.gd new file mode 100644 index 0000000..871903d --- /dev/null +++ b/tests/unit/test_floor_state.gd @@ -0,0 +1,60 @@ +extends GutTest + +const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd") +const RoomStateType = preload("res://scripts/domain/world/room_state.gd") +const FloorStateType = preload("res://scripts/domain/world/floor_state.gd") +const ActorStateType = preload("res://scripts/domain/actor_state.gd") +const GameStateType = preload("res://scripts/domain/game_state.gd") +const RunStateType = preload("res://scripts/domain/run/run_state.gd") + +func test_floor_state_round_trip_preserves_rooms_tiles_and_floor_collections() -> void: + var map := GridMapStateType.new(Vector2i(8, 6), GridMapStateType.CellKind.WALL) + map.set_cell(Vector2i(2, 2), GridMapStateType.CellKind.FLOOR) + map.set_cell(Vector2i(6, 4), GridMapStateType.CellKind.STAIRS) + map.set_room_id(Vector2i(2, 2), &"entry") + map.set_room_id(Vector2i(6, 4), &"exit") + var floor := FloorStateType.new(map, 8675309, 3) + floor.rooms.append(RoomStateType.new(&"entry", Rect2i(1, 1, 3, 3), &"entrance", {"cleared": true})) + floor.rooms.append(RoomStateType.new(&"exit", Rect2i(5, 3, 2, 2), &"combat")) + floor.entrance = Vector2i(2, 2) + floor.exit = Vector2i(6, 4) + floor.entities[&"goblin"] = ActorStateType.new(&"goblin", Vector2i(5, 4), 6, 2) + floor.chests[Vector2i(3, 2)] = {"opened": false, "loot": [{"id": "potion_minor", "quantity": 2}]} + floor.ground_items[Vector2i(4, 2)] = [{"id": "gold", "quantity": 10}] + floor.events.append({"id": "entry_event", "resolved": false}) + floor.explored_cells[Vector2i(2, 2)] = true + floor.metadata = {"theme": "ruins", "difficulty": 2} + + var restored := FloorStateType.from_dict(floor.to_dict()) + + assert_eq(restored.seed, 8675309) + assert_eq(restored.floor_number, 3) + assert_eq(restored.rooms.size(), 2) + assert_eq(restored.rooms[0].id, &"entry") + assert_eq(restored.rooms[0].bounds, Rect2i(1, 1, 3, 3)) + assert_eq(restored.rooms[0].metadata, {"cleared": true}) + assert_eq(restored.map.get_cell(Vector2i(6, 4)), GridMapStateType.CellKind.STAIRS) + assert_eq(restored.map.get_room_id(Vector2i(2, 2)), &"entry") + assert_eq(restored.map.get_room_id(Vector2i(6, 4)), &"exit") + assert_eq(restored.entrance, Vector2i(2, 2)) + assert_eq(restored.exit, Vector2i(6, 4)) + assert_eq(restored.entities[&"goblin"].cell, Vector2i(5, 4)) + assert_eq(restored.chests[Vector2i(3, 2)], {"opened": false, "loot": [{"id": "potion_minor", "quantity": 2}]}) + assert_eq(restored.ground_items[Vector2i(4, 2)], [{"id": "gold", "quantity": 10}]) + assert_eq(restored.events, [{"id": "entry_event", "resolved": false}]) + assert_true(restored.explored_cells.has(Vector2i(2, 2))) + assert_eq(restored.metadata, {"theme": "ruins", "difficulty": 2}) + +func test_game_state_uses_the_run_current_floor_state_without_breaking_legacy_map_access() -> void: + var legacy_map := GridMapStateType.new(Vector2i(4, 4)) + var player := ActorStateType.new(&"player", Vector2i(1, 1), 12, 3) + var goblin := ActorStateType.new(&"goblin", Vector2i(2, 1), 6, 2) + var state := GameStateType.new(legacy_map, player, {&"goblin": goblin}) + var run := RunStateType.new("floor-adapter") + + state.bind_run_state(run) + + assert_same(run.current_floor_state(), state.floor_state) + assert_same(state.map, state.floor_state.map) + assert_same(state.enemies, state.floor_state.entities) + assert_same(state.explored_cells, state.floor_state.explored_cells) diff --git a/tests/unit/test_floor_state.gd.uid b/tests/unit/test_floor_state.gd.uid new file mode 100644 index 0000000..d2632b1 --- /dev/null +++ b/tests/unit/test_floor_state.gd.uid @@ -0,0 +1 @@ +uid://b4tha8i0sv1ds