Enable unit selection and path visualization logic via Cursor interactions.

This commit is contained in:
gdz
2025-12-26 23:11:33 +01:00
parent c4b2d6ff94
commit 2f82eb80e4
5 changed files with 42 additions and 65 deletions

View File

@@ -4,6 +4,9 @@ extends CharacterBody2D
var grid: AStarGrid2D
var currentCell: Vector2i
var currentPoint: int
var selected: bool:
set(v):
selected = v; $PathPrev.visible = selected;
var moving: bool:
set(v):
moving = v; $PathPrev.visible = not moving; set_physics_process(moving)
@@ -11,9 +14,12 @@ var moving: bool:
var targetCell: Vector2i
var movePts: Array
func _ready(): moving = false
func _ready():
selected = false
moving = false
func setup(_grid: AStarGrid2D):
selected = true
grid = _grid
currentCell = pos_to_cell(global_position)
targetCell = currentCell
@@ -23,15 +29,18 @@ func pos_to_cell(pos: Vector2):
func _input(event: InputEvent) -> void:
if moving: return
if event is InputEventMouseButton:
if event.pressed and event.button_index == MOUSE_BUTTON_LEFT: startMove()
elif event is InputEventMouseMotion:
var target = Vector2i(pos_to_cell(event.position))
if target != targetCell:
movePts = grid.get_point_path(currentCell, target)
movePts = (movePts as Array).map(func (point): return point + grid.cell_size/2.0)
$PathPrev.points = movePts
targetCell = target
if event.is_action_pressed("SetMarker") and selected: startMove()
func setTarget(target: Vector2i):
if !selected: return
if moving: return
if target != targetCell:
movePts = grid.get_point_path(currentCell, target)
movePts = (movePts as Array).map(func (point): return point + grid.cell_size/2.0)
$PathPrev.points = movePts
targetCell = target
func startMove():
if movePts.is_empty(): return
@@ -50,3 +59,9 @@ func _physics_process(delta: float) -> void:
if (movePts[currentPoint+1] - global_position).length() < 4:
currentCell = pos_to_cell(global_position)
currentPoint += 1
func selectUnit(cell: Vector2i):
if cell == currentCell:
selected = true
else:
selected = false