84 lines
3.1 KiB
GDScript
84 lines
3.1 KiB
GDScript
class_name Minimap
|
|
extends Control
|
|
|
|
const CROP_RADIUS := 6
|
|
const CELL_SIZE := 12.0
|
|
const UNKNOWN_COLOR := Color(0.025, 0.04, 0.09, 0.94)
|
|
const EXPLORED_COLOR := Color(0.18, 0.32, 0.48, 0.96)
|
|
const VISIBLE_COLOR := Color(0.72, 0.84, 0.9, 1.0)
|
|
const PLAYER_COLOR := Color(1.0, 0.92, 0.32, 1.0)
|
|
const THREAT_COLOR := Color(0.95, 0.42, 0.18, 1.0)
|
|
|
|
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 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] = {}
|
|
for cell in _threat_cells:
|
|
if _visible_cells.has(cell) and get_displayed_cells().has(cell):
|
|
threats[cell] = true
|
|
return threats
|
|
|
|
func toggle_expanded() -> void:
|
|
expanded = not expanded
|
|
_crop_radius = CROP_RADIUS * 2 if expanded else CROP_RADIUS
|
|
set_anchors_and_offsets_preset(Control.PRESET_CENTER_TOP if expanded else Control.PRESET_TOP_RIGHT)
|
|
size = Vector2(312, 312) if expanded else Vector2(168, 168)
|
|
position = Vector2(484, 72) if expanded else Vector2(-188, 184)
|
|
queue_redraw()
|
|
|
|
func _draw() -> void:
|
|
if _map == null:
|
|
return
|
|
var displayed := get_displayed_cells()
|
|
var cells_across := _crop_radius * 2 + 1
|
|
var draw_cell_size := size.x / float(cells_across)
|
|
for cell in displayed:
|
|
var local := cell - _player_cell + Vector2i(cells_across / 2, cells_across / 2)
|
|
var rect := Rect2(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 get_threat_marker_cells().has(cell):
|
|
draw_circle(rect.get_center(), draw_cell_size * 0.25, THREAT_COLOR)
|