Enable unit selection and path visualization logic via Cursor interactions.
This commit is contained in:
@@ -23,24 +23,10 @@ var aStarGrid: AStarGrid2D:
|
|||||||
|
|
||||||
# Called when the node enters the scene tree for the first time.
|
# Called when the node enters the scene tree for the first time.
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
# add_child(_MovingMarker)
|
if $Player.is_node_ready():
|
||||||
# _MovingMarker.hide()
|
$Player.setup($Map.getGrid())
|
||||||
#
|
$GameBoard/Cursor.moved.connect($Player.setTarget)
|
||||||
# create unit at local position (50, 50) => eg mouse position
|
$GameBoard/Cursor.accept_pressed.connect($Player.selectedUnit)
|
||||||
_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]
|
|
||||||
|
|
||||||
$Player.setup($Map.getGrid())
|
|
||||||
|
|
||||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||||
func _process(delta: float) -> void:
|
func _process(delta: float) -> void:
|
||||||
@@ -58,6 +44,9 @@ func _input(event: InputEvent) -> void:
|
|||||||
_MovingMarker.position = GroundLayer.local_to_map(event.position)
|
_MovingMarker.position = GroundLayer.local_to_map(event.position)
|
||||||
_MovingMarker.show()
|
_MovingMarker.show()
|
||||||
|
|
||||||
|
func selectUnitAt(cell: Vector2i):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
func _getScreenCenter():
|
func _getScreenCenter():
|
||||||
return get_viewport().get_camera_2d().get_screen_center_position()
|
return get_viewport().get_camera_2d().get_screen_center_position()
|
||||||
|
|||||||
@@ -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="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://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="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="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="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://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" 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="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_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"]
|
[node name="Main" type="Node"]
|
||||||
script = ExtResource("1_qw60k")
|
script = ExtResource("1_qw60k")
|
||||||
grid = ExtResource("5_p6jpk")
|
grid = ExtResource("5_p6jpk")
|
||||||
@@ -32,13 +25,6 @@ grid = ExtResource("5_p6jpk")
|
|||||||
script = ExtResource("5_y3v7k")
|
script = ExtResource("5_y3v7k")
|
||||||
grid = ExtResource("5_p6jpk")
|
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"]
|
[node name="Cursor" type="Node2D" parent="GameBoard"]
|
||||||
position = Vector2(8, 8)
|
position = Vector2(8, 8)
|
||||||
script = ExtResource("7_y3v7k")
|
script = ExtResource("7_y3v7k")
|
||||||
@@ -56,6 +42,7 @@ scale = Vector2(2, 2)
|
|||||||
texture = SubResource("PlaceholderTexture2D_y3v7k")
|
texture = SubResource("PlaceholderTexture2D_y3v7k")
|
||||||
|
|
||||||
[node name="HUD" type="CanvasLayer" parent="."]
|
[node name="HUD" type="CanvasLayer" parent="."]
|
||||||
|
visible = false
|
||||||
|
|
||||||
[node name="GridDisplay" type="Control" parent="HUD"]
|
[node name="GridDisplay" type="Control" parent="HUD"]
|
||||||
unique_name_in_owner = true
|
unique_name_in_owner = true
|
||||||
@@ -77,21 +64,8 @@ layout_mode = 2
|
|||||||
layout_mode = 2
|
layout_mode = 2
|
||||||
text = "Show Grid"
|
text = "Show Grid"
|
||||||
|
|
||||||
[node name="Player" type="CharacterBody2D" parent="."]
|
[node name="Player" parent="." instance=ExtResource("9_hryqi")]
|
||||||
script = ExtResource("9_hryqi")
|
position = Vector2(8, 8)
|
||||||
|
|
||||||
[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")
|
|
||||||
|
|
||||||
[connection signal="accept_pressed" from="GameBoard/Cursor" to="." method="get_cell_information"]
|
[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"]
|
[connection signal="toggled" from="HUD/GridDisplay/MarginContainer/PanelContainer/ShowGrid" to="HUD/GridDisplay" method="toggle_grid_display"]
|
||||||
|
|
||||||
[editable path="GameBoard/Unit"]
|
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ extends CharacterBody2D
|
|||||||
var grid: AStarGrid2D
|
var grid: AStarGrid2D
|
||||||
var currentCell: Vector2i
|
var currentCell: Vector2i
|
||||||
var currentPoint: int
|
var currentPoint: int
|
||||||
|
var selected: bool:
|
||||||
|
set(v):
|
||||||
|
selected = v; $PathPrev.visible = selected;
|
||||||
var moving: bool:
|
var moving: bool:
|
||||||
set(v):
|
set(v):
|
||||||
moving = v; $PathPrev.visible = not moving; set_physics_process(moving)
|
moving = v; $PathPrev.visible = not moving; set_physics_process(moving)
|
||||||
@@ -11,9 +14,12 @@ var moving: bool:
|
|||||||
var targetCell: Vector2i
|
var targetCell: Vector2i
|
||||||
var movePts: Array
|
var movePts: Array
|
||||||
|
|
||||||
func _ready(): moving = false
|
func _ready():
|
||||||
|
selected = false
|
||||||
|
moving = false
|
||||||
|
|
||||||
func setup(_grid: AStarGrid2D):
|
func setup(_grid: AStarGrid2D):
|
||||||
|
selected = true
|
||||||
grid = _grid
|
grid = _grid
|
||||||
currentCell = pos_to_cell(global_position)
|
currentCell = pos_to_cell(global_position)
|
||||||
targetCell = currentCell
|
targetCell = currentCell
|
||||||
@@ -23,15 +29,18 @@ func pos_to_cell(pos: Vector2):
|
|||||||
|
|
||||||
func _input(event: InputEvent) -> void:
|
func _input(event: InputEvent) -> void:
|
||||||
if moving: return
|
if moving: return
|
||||||
if event is InputEventMouseButton:
|
|
||||||
if event.pressed and event.button_index == MOUSE_BUTTON_LEFT: startMove()
|
if event.is_action_pressed("SetMarker") and selected: startMove()
|
||||||
elif event is InputEventMouseMotion:
|
|
||||||
var target = Vector2i(pos_to_cell(event.position))
|
func setTarget(target: Vector2i):
|
||||||
if target != targetCell:
|
if !selected: return
|
||||||
movePts = grid.get_point_path(currentCell, target)
|
if moving: return
|
||||||
movePts = (movePts as Array).map(func (point): return point + grid.cell_size/2.0)
|
if target != targetCell:
|
||||||
$PathPrev.points = movePts
|
movePts = grid.get_point_path(currentCell, target)
|
||||||
targetCell = target
|
movePts = (movePts as Array).map(func (point): return point + grid.cell_size/2.0)
|
||||||
|
$PathPrev.points = movePts
|
||||||
|
targetCell = target
|
||||||
|
|
||||||
|
|
||||||
func startMove():
|
func startMove():
|
||||||
if movePts.is_empty(): return
|
if movePts.is_empty(): return
|
||||||
@@ -50,3 +59,9 @@ func _physics_process(delta: float) -> void:
|
|||||||
if (movePts[currentPoint+1] - global_position).length() < 4:
|
if (movePts[currentPoint+1] - global_position).length() < 4:
|
||||||
currentCell = pos_to_cell(global_position)
|
currentCell = pos_to_cell(global_position)
|
||||||
currentPoint += 1
|
currentPoint += 1
|
||||||
|
|
||||||
|
func selectUnit(cell: Vector2i):
|
||||||
|
if cell == currentCell:
|
||||||
|
selected = true
|
||||||
|
else:
|
||||||
|
selected = false
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ func _ready() -> void:
|
|||||||
|
|
||||||
# Pathing and Grid
|
# Pathing and Grid
|
||||||
set_process(false)
|
set_process(false)
|
||||||
_pathFollow.rotates = false
|
#_pathFollow.rotates = false
|
||||||
|
|
||||||
cell = grid.calculateGridCoordinates(position)
|
cell = grid.calculateGridCoordinates(position)
|
||||||
position = grid.calculateMapPosition(cell)
|
position = grid.calculateMapPosition(cell)
|
||||||
@@ -55,7 +55,8 @@ func _ready() -> void:
|
|||||||
# We create the curve resource here because creating it in the editor prevents
|
# We create the curve resource here because creating it in the editor prevents
|
||||||
# us from moving the unit.
|
# us from moving the unit.
|
||||||
if not Engine.is_editor_hint():
|
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.
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||||
func _process(delta: float) -> void:
|
func _process(delta: float) -> void:
|
||||||
|
|||||||
@@ -655,9 +655,7 @@ texture = ExtResource("5_ulevp")
|
|||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
|
||||||
shape = SubResource("RectangleShape2D_15sed")
|
shape = SubResource("RectangleShape2D_15sed")
|
||||||
|
|
||||||
[node name="Path2D" type="Path2D" parent="."]
|
[node name="PathPrev" type="Line2D" parent="."]
|
||||||
|
|
||||||
[node name="PathFollow2D" type="PathFollow2D" parent="Path2D"]
|
|
||||||
|
|
||||||
[connection signal="mouse_entered" from="Area2D" to="." method="_markUnit"]
|
[connection signal="mouse_entered" from="Area2D" to="." method="_markUnit"]
|
||||||
[connection signal="mouse_exited" from="Area2D" to="." method="_unMarkUnit"]
|
[connection signal="mouse_exited" from="Area2D" to="." method="_unMarkUnit"]
|
||||||
|
|||||||
Reference in New Issue
Block a user