52 lines
2.2 KiB
GDScript
52 lines
2.2 KiB
GDScript
## LevelManager.gd
|
|||
|
|
# Manages the current level's data, visual representation, and navigation graph.
|
||
|
|
# Responsible for generating, painting, and initializing the level, as well as providing pathfinding and coordinate helpers.
|
||
|
|
extends Node
|
||
|
|
|
||
|
|
# The source of truth for the current level's layout (MapData resource).
|
||
|
|
var _data: MapData
|
||
|
|
# The TileMapLayer node where the map is visually rendered.
|
||
|
|
var _layer: TileMapLayer
|
||
|
|
|
||
|
|
# Service objects for map generation, painting, and pathfinding.
|
||
|
|
# MapGenerator and MapPainter are stateless; PathfindingHandler manages the navigation graph.
|
||
|
|
var _generator: MapGenerator = MapGenerator.new()
|
||
|
|
var _painter: MapPainter = MapPainter.new()
|
||
|
|
var _pathfinding: PathfindingHandler = PathfindingHandler.new()
|
||
|
|
|
||
|
|
# Orchestrates the creation of a new level.
|
||
|
|
# 1. Generates the map data.
|
||
|
|
# 2. Paints the map visually.
|
||
|
|
# 3. Builds the navigation graph for AI/pathfinding.
|
||
|
|
# 4. Spawns the player at the correct location.
|
||
|
|
func initialize(settings: MapSettings, layer: TileMapLayer, player: CharacterBody2D) -> void:
|
||
|
|
_layer = layer
|
||
|
|
|
||
|
|
# 1. Generate the abstract map data (rooms, corridors, etc.).
|
||
|
|
# This avoids side effects on the settings resource.
|
||
|
|
_data = _generator.generate(settings)
|
||
|
|
|
||
|
|
# 2. Paint the visual layer for the player to see.
|
||
|
|
# This does not modify the underlying map data.
|
||
|
|
_painter.paint(_data, _layer)
|
||
|
|
|
||
|
|
# 3. Build the AStar graph for AI and navigation.
|
||
|
|
# Reads directly from the grid stored in _data.
|
||
|
|
_pathfinding.build(_data)
|
||
|
|
|
||
|
|
# 4. Finalize world state by spawning the player at the spawn point.
|
||
|
|
# Uses the TileMapLayer to convert grid coordinates to world pixel positions.
|
||
|
|
player.position = _layer.map_to_local(_data.spawn_point)
|
||
|
|
|
||
|
|
# Public API for other systems (like AI) to query the navigation grid.
|
||
|
|
# Encapsulates the PathfindingHandler so callers don't need to know about it directly.
|
||
|
|
func find_path(from: Vector2i, to: Vector2i) -> Array[Vector2i]:
|
||
|
|
return _pathfinding.find_path(from, to)
|
||
|
|
|
||
|
|
# Coordinate conversion helpers — lets the player and other systems convert between world and tile coordinates without touching the TileMapLayer directly.
|
||
|
|
func world_to_tile(world_pos: Vector2) -> Vector2i:
|
||
|
|
return _layer.local_to_map(world_pos)
|
||
|
|
|
||
|
|
func tile_to_world(tile: Vector2i) -> Vector2:
|
||
|
|
return _layer.map_to_local(tile)
|