75 lines
2.1 KiB
GDScript
75 lines
2.1 KiB
GDScript
extends Node2D
|
|
|
|
|
|
const CELL_SIZE: int = 32
|
|
|
|
@onready var map: TileMap = $TileMap
|
|
@onready var marker_from: Node2D = $MarkerFrom
|
|
@onready var marker_to: Node2D = $MarkerTo
|
|
@onready var path_line: Line2D = $PathLine
|
|
@onready var check_box: CheckBox = $CheckBox
|
|
|
|
var astar: MultilevelAStar
|
|
var used_rect: Rect2i
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
# mapa začne na -1, -1. tukaj premaknem da se vidi vse na mapi
|
|
# (saj verjetno obstaja boljši način ma ga ne poznam - mogoče kakšna kamera)
|
|
position = Vector2i.ONE * CELL_SIZE
|
|
|
|
used_rect = map.get_used_rect()
|
|
print(used_rect)
|
|
|
|
astar = MultilevelAStar.new(map)
|
|
|
|
mark_units()
|
|
|
|
marker_from.modulate = Color.html("#ff0000ff")
|
|
marker_to.modulate = Color.html("#00ff00ff")
|
|
|
|
marker_from.position = Vector2i(-1, -1) * CELL_SIZE
|
|
marker_to.position = (used_rect.position + used_rect.size - Vector2i.ONE) * CELL_SIZE
|
|
|
|
find_path()
|
|
|
|
|
|
func mark_units() -> void:
|
|
for unit in $Units.get_tree().get_nodes_in_group("unit"):
|
|
astar.set_unit(unit.position / CELL_SIZE)
|
|
|
|
|
|
func _unhandled_input(event: InputEvent) -> void:
|
|
if event is InputEventMouseButton:
|
|
if event.pressed:
|
|
var cell := map.local_to_map(map.get_local_mouse_position())
|
|
if map.get_used_rect().has_point(cell):
|
|
if event.button_index == 1:
|
|
marker_to.position = Vector2(cell.x * CELL_SIZE, cell.y * CELL_SIZE)
|
|
find_path();
|
|
elif event.button_index == 2:
|
|
marker_from.position = Vector2(cell.x * CELL_SIZE, cell.y * CELL_SIZE)
|
|
find_path();
|
|
|
|
|
|
func find_path() -> void:
|
|
path_line.clear_points()
|
|
|
|
var from: Vector2i = Vector2i(marker_from.position / CELL_SIZE)
|
|
var to: Vector2i = Vector2i(marker_to.position / CELL_SIZE)
|
|
|
|
var arr = astar.find_path(from, to, check_box.button_pressed) # vrne Variant: null ali Array
|
|
#print(arr)
|
|
#print(arr.size())
|
|
|
|
if arr.size() > 1: # if the start and the end are the same there is no line to draw
|
|
# add new path
|
|
for vec in arr:
|
|
#print("%s, %s" % [vec.x, vec.y])
|
|
path_line.add_point(vec * CELL_SIZE + Vector2i(16, 16))
|
|
|
|
|
|
func _on_check_box_pressed() -> void:
|
|
find_path()
|