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

@@ -23,24 +23,10 @@ var aStarGrid: AStarGrid2D:
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
# add_child(_MovingMarker)
# _MovingMarker.hide()
#
# create unit at local position (50, 50) => eg mouse position
_createUnit(GroundLayer.local_to_map(Vector2i(50, 50)))
# create unit at map position (50, 50) => tile x = 50, y = 50 in the map
_createUnit(GroundLayer.map_to_local(Vector2i(25,25)))
for unit in _Units:
add_child(unit)
# $CameraController.position = get_viewport().get_camera_2d().get_screen_center_position()
var firstUnit: Node2D = _Units[0]
# if firstUnit.is_node_ready():
# UnitCamera.target = _Units[0].get_path_to(get_parent())
# UnitCamera.target_node = _Units[0]
if $Player.is_node_ready():
$Player.setup($Map.getGrid())
$GameBoard/Cursor.moved.connect($Player.setTarget)
$GameBoard/Cursor.accept_pressed.connect($Player.selectedUnit)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
@@ -58,6 +44,9 @@ func _input(event: InputEvent) -> void:
_MovingMarker.position = GroundLayer.local_to_map(event.position)
_MovingMarker.show()
func selectUnitAt(cell: Vector2i):
pass
func _getScreenCenter():
return get_viewport().get_camera_2d().get_screen_center_position()

View File

@@ -1,23 +1,16 @@
[gd_scene load_steps=13 format=3 uid="uid://d05j5yuhlsxp0"]
[gd_scene load_steps=10 format=3 uid="uid://d05j5yuhlsxp0"]
[ext_resource type="PackedScene" uid="uid://cywuuce71rmgb" path="res://Scenes/Map/map.tscn" id="1_1r6ip"]
[ext_resource type="Script" uid="uid://btdvxp8ckmeb3" path="res://Scenes/Main/main.gd" id="1_qw60k"]
[ext_resource type="Script" uid="uid://14cwbxcvt5dx" path="res://Scenes/Main/game_board.gd" id="4_5yls4"]
[ext_resource type="Resource" uid="uid://bpf7mj7w5kftq" path="res://Resource/Grid.tres" id="5_p6jpk"]
[ext_resource type="Script" uid="uid://dukn3yshfepum" path="res://Scenes/Main/grid_debug.gd" id="5_y3v7k"]
[ext_resource type="PackedScene" uid="uid://b1d6lktijxy3s" path="res://Scenes/Unit/move/unit.tscn" id="6_2a143"]
[ext_resource type="Script" uid="uid://cidjtc27oj1gn" path="res://Scenes/Main/cursor.gd" id="7_y3v7k"]
[ext_resource type="Script" uid="uid://dtme43jtijpok" path="res://Scenes/Main/grid_display.gd" id="8_y3v7k"]
[ext_resource type="Script" uid="uid://dm40sxvvnbdrn" path="res://Scenes/Main/player.gd" id="9_hryqi"]
[ext_resource type="Script" path="res://Scenes/Main/grid_display.gd" id="8_y3v7k"]
[ext_resource type="PackedScene" uid="uid://ct0rg5jelnb3x" path="res://Scenes/Unit/unit_base.tscn" id="9_hryqi"]
[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_y3v7k"]
[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_imbg2"]
size = Vector2(16, 16)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_hfg1r"]
size = Vector2(32, 32)
[node name="Main" type="Node"]
script = ExtResource("1_qw60k")
grid = ExtResource("5_p6jpk")
@@ -32,13 +25,6 @@ grid = ExtResource("5_p6jpk")
script = ExtResource("5_y3v7k")
grid = ExtResource("5_p6jpk")
[node name="Unit" parent="GameBoard" instance=ExtResource("6_2a143")]
position = Vector2(400, 224)
grid = ExtResource("5_p6jpk")
[node name="Sprite2D" parent="GameBoard/Unit/PathFollow2D" index="1"]
texture = null
[node name="Cursor" type="Node2D" parent="GameBoard"]
position = Vector2(8, 8)
script = ExtResource("7_y3v7k")
@@ -56,6 +42,7 @@ scale = Vector2(2, 2)
texture = SubResource("PlaceholderTexture2D_y3v7k")
[node name="HUD" type="CanvasLayer" parent="."]
visible = false
[node name="GridDisplay" type="Control" parent="HUD"]
unique_name_in_owner = true
@@ -77,21 +64,8 @@ layout_mode = 2
layout_mode = 2
text = "Show Grid"
[node name="Player" type="CharacterBody2D" parent="."]
script = ExtResource("9_hryqi")
[node name="PathPrev" type="Line2D" parent="Player"]
top_level = true
width = 2.0
default_color = Color(0, 0.3372549, 0.3372549, 1)
[node name="Sprite2D" type="Sprite2D" parent="Player"]
texture = SubResource("PlaceholderTexture2D_imbg2")
[node name="CollisionShape2D" type="CollisionShape2D" parent="Player"]
shape = SubResource("RectangleShape2D_hfg1r")
[node name="Player" parent="." instance=ExtResource("9_hryqi")]
position = Vector2(8, 8)
[connection signal="accept_pressed" from="GameBoard/Cursor" to="." method="get_cell_information"]
[connection signal="toggled" from="HUD/GridDisplay/MarginContainer/PanelContainer/ShowGrid" to="HUD/GridDisplay" method="toggle_grid_display"]
[editable path="GameBoard/Unit"]

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,16 +29,19 @@ 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 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
currentPoint = 0; moving = true
@@ -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

View File

@@ -47,7 +47,7 @@ func _ready() -> void:
# Pathing and Grid
set_process(false)
_pathFollow.rotates = false
#_pathFollow.rotates = false
cell = grid.calculateGridCoordinates(position)
position = grid.calculateMapPosition(cell)
@@ -55,7 +55,8 @@ func _ready() -> void:
# We create the curve resource here because creating it in the editor prevents
# us from moving the unit.
if not Engine.is_editor_hint():
_path.curve = Curve2D.new()
pass
#_path.curve = Curve2D.new()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:

View File

@@ -655,9 +655,7 @@ texture = ExtResource("5_ulevp")
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
shape = SubResource("RectangleShape2D_15sed")
[node name="Path2D" type="Path2D" parent="."]
[node name="PathFollow2D" type="PathFollow2D" parent="Path2D"]
[node name="PathPrev" type="Line2D" parent="."]
[connection signal="mouse_entered" from="Area2D" to="." method="_markUnit"]
[connection signal="mouse_exited" from="Area2D" to="." method="_unMarkUnit"]