133 lines
4.9 KiB
GDScript
133 lines
4.9 KiB
GDScript
class_name Minimap
|
|
extends Control
|
|
|
|
const CROP_RADIUS := 6
|
|
const UNKNOWN_COLOR := Color(0.035, 0.03, 0.025, 0.96)
|
|
const EXPLORED_COLOR := Color(0.24, 0.29, 0.25, 0.98)
|
|
const VISIBLE_COLOR := Color(0.72, 0.66, 0.51, 1.0)
|
|
const PLAYER_COLOR := Color(0.96, 0.78, 0.3, 1.0)
|
|
const THREAT_COLOR := Color(0.84, 0.22, 0.14, 1.0)
|
|
const COMPACT_SIZE := Vector2(396.0, 340.0)
|
|
const EXPANDED_SIZE := Vector2(520.0, 560.0)
|
|
const COMPACT_RAIL_INSET := 14.0
|
|
const EXPANDED_FRAME_INSET := 28.0
|
|
|
|
@onready var map_region: Control = $MapRegion
|
|
|
|
var expanded := false
|
|
var _crop_radius := CROP_RADIUS
|
|
var _map: GridMapState
|
|
var _player_cell := Vector2i.ZERO
|
|
var _visible_cells: Dictionary[Vector2i, bool] = {}
|
|
var _explored_cells: Dictionary[Vector2i, bool] = {}
|
|
var _threat_cells: Dictionary[Vector2i, bool] = {}
|
|
|
|
func _ready() -> void:
|
|
resized.connect(queue_redraw)
|
|
map_region.resized.connect(queue_redraw)
|
|
get_viewport().size_changed.connect(_on_viewport_size_changed)
|
|
call_deferred("_layout_in_hud")
|
|
|
|
func _on_viewport_size_changed() -> void:
|
|
# GameHud updates the rail rectangles from the same signal; defer until those
|
|
# rectangles have received their new responsive dimensions.
|
|
call_deferred("_layout_in_hud")
|
|
|
|
func set_state(map: GridMapState, player_cell: Vector2i, visible_cells: Dictionary[Vector2i, bool], explored_cells: Dictionary[Vector2i, bool], threat_cells: Dictionary[Vector2i, bool]) -> void:
|
|
_map = map
|
|
_player_cell = player_cell
|
|
_visible_cells = visible_cells.duplicate()
|
|
_explored_cells = explored_cells.duplicate()
|
|
_threat_cells = threat_cells.duplicate()
|
|
queue_redraw()
|
|
|
|
func get_camera_cell() -> Vector2i:
|
|
return _player_cell
|
|
|
|
func get_displayed_cells() -> Dictionary[Vector2i, bool]:
|
|
var displayed: Dictionary[Vector2i, bool] = {}
|
|
if _map == null:
|
|
return displayed
|
|
var radius := _crop_radius
|
|
for y in range(_player_cell.y - radius, _player_cell.y + radius + 1):
|
|
for x in range(_player_cell.x - radius, _player_cell.x + radius + 1):
|
|
var cell := Vector2i(x, y)
|
|
if cell.x >= 0 and cell.y >= 0 and cell.x < _map.size.x and cell.y < _map.size.y:
|
|
displayed[cell] = true
|
|
return displayed
|
|
|
|
func get_cell_visibility_state(cell: Vector2i) -> StringName:
|
|
if _visible_cells.has(cell):
|
|
return &"visible"
|
|
if _explored_cells.has(cell):
|
|
return &"explored"
|
|
return &"unknown"
|
|
|
|
func get_threat_marker_cells() -> Dictionary[Vector2i, bool]:
|
|
var threats: Dictionary[Vector2i, bool] = {}
|
|
var displayed := get_displayed_cells()
|
|
for cell in _threat_cells:
|
|
if _visible_cells.has(cell) and displayed.has(cell):
|
|
threats[cell] = true
|
|
return threats
|
|
|
|
## Returns the local rectangle exclusively available to map cells and markers.
|
|
func get_drawable_map_rect() -> Rect2:
|
|
if not is_instance_valid(map_region):
|
|
return Rect2()
|
|
var region_size := map_region.size
|
|
var square_side := minf(region_size.x, region_size.y)
|
|
return Rect2(map_region.position + (region_size - Vector2.ONE * square_side) * 0.5, Vector2.ONE * square_side)
|
|
|
|
func toggle_expanded() -> void:
|
|
expanded = not expanded
|
|
_crop_radius = CROP_RADIUS * 2 if expanded else CROP_RADIUS
|
|
_layout_in_hud()
|
|
queue_redraw()
|
|
|
|
func _layout_in_hud() -> void:
|
|
if not is_inside_tree():
|
|
return
|
|
var layout := get_node_or_null("../ResponsiveLayout") as Control
|
|
if layout == null:
|
|
return
|
|
var host := layout.get_node_or_null("PlayAreaFrame" if expanded else "MapSection") as Control
|
|
if host == null:
|
|
return
|
|
var requested_size := EXPANDED_SIZE if expanded else COMPACT_SIZE
|
|
var inset := EXPANDED_FRAME_INSET if expanded else COMPACT_RAIL_INSET
|
|
var available_size := host.size - Vector2.ONE * inset * 2.0
|
|
if not expanded:
|
|
# The section heading occupies its own clear line above the map.
|
|
available_size.y -= 28.0
|
|
var panel_size := Vector2(minf(requested_size.x, available_size.x), minf(requested_size.y, available_size.y))
|
|
size = panel_size
|
|
if expanded:
|
|
position = host.position + (host.size - panel_size) * 0.5
|
|
else:
|
|
position = host.position + Vector2((host.size.x - panel_size.x) * 0.5, 42.0)
|
|
|
|
func _draw() -> void:
|
|
if _map == null:
|
|
return
|
|
var drawable_rect := get_drawable_map_rect()
|
|
if drawable_rect.size.x <= 0.0 or drawable_rect.size.y <= 0.0:
|
|
return
|
|
var displayed := get_displayed_cells()
|
|
var threat_cells := get_threat_marker_cells()
|
|
var cells_across := _crop_radius * 2 + 1
|
|
var draw_cell_size := drawable_rect.size.x / float(cells_across)
|
|
for cell in displayed:
|
|
var local := cell - _player_cell + Vector2i(_crop_radius, _crop_radius)
|
|
var rect := Rect2(drawable_rect.position + Vector2(local) * draw_cell_size, Vector2.ONE * (draw_cell_size - 1.0))
|
|
var color := UNKNOWN_COLOR
|
|
if get_cell_visibility_state(cell) == &"visible":
|
|
color = VISIBLE_COLOR
|
|
elif get_cell_visibility_state(cell) == &"explored":
|
|
color = EXPLORED_COLOR
|
|
draw_rect(rect, color)
|
|
if cell == _player_cell:
|
|
draw_circle(rect.get_center(), draw_cell_size * 0.28, PLAYER_COLOR)
|
|
elif threat_cells.has(cell):
|
|
draw_circle(rect.get_center(), draw_cell_size * 0.25, THREAT_COLOR)
|