Trying new stuff with the camera and grid. I want to be able to move a unit but there is a problem with the grid at the moment. I am now trying to implement a new way of handling the camera, maybe then I can move on to moving units...

This commit is contained in:
gdz
2025-12-15 02:20:03 +01:00
parent 4f9d5fc8bd
commit 67b6198412
17 changed files with 588 additions and 51 deletions

View File

@@ -0,0 +1,87 @@
[gd_scene load_steps=11 format=3 uid="uid://b1d6lktijxy3s"]
[ext_resource type="Script" uid="uid://c8ocnhejcdc77" path="res://unit.gd" id="1_astap"]
[ext_resource type="Texture2D" uid="uid://cgvyfsuri6vmx" path="res://Graphics/TileMaps/kenney_rpgUrbanKit/Tilemap/tilemap.png" id="2_fhoaw"]
[ext_resource type="Texture2D" uid="uid://dlaevn54qcvej" path="res://Graphics/TileMaps/kenney_rpgUrbanKit/Tiles/tile_0267.png" id="3_fhoaw"]
[sub_resource type="AtlasTexture" id="AtlasTexture_4o1a4"]
atlas = ExtResource("2_fhoaw")
region = Rect2(408, 51, 16, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_hn0wa"]
atlas = ExtResource("2_fhoaw")
region = Rect2(408, 68, 16, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_jt11o"]
atlas = ExtResource("2_fhoaw")
region = Rect2(408, 85, 16, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_e33ge"]
atlas = ExtResource("2_fhoaw")
region = Rect2(408, 68, 16, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_px3ay"]
atlas = ExtResource("2_fhoaw")
region = Rect2(408, 85, 16, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_6ceyn"]
atlas = ExtResource("2_fhoaw")
region = Rect2(34, 255, 16, 16)
[sub_resource type="SpriteFrames" id="SpriteFrames_lgeeq"]
animations = [{
"frames": [],
"loop": true,
"name": &"default",
"speed": 5.0
}, {
"frames": [{
"duration": 1.0,
"texture": SubResource("AtlasTexture_4o1a4")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_hn0wa")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_jt11o")
}],
"loop": true,
"name": &"down",
"speed": 5.0
}, {
"frames": [{
"duration": 1.0,
"texture": SubResource("AtlasTexture_e33ge")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_px3ay")
}],
"loop": true,
"name": &"idle",
"speed": 5.0
}, {
"frames": [{
"duration": 1.0,
"texture": SubResource("AtlasTexture_6ceyn")
}, {
"duration": 1.0,
"texture": null
}],
"loop": true,
"name": &"selected",
"speed": 5.0
}]
[node name="Unit" type="Path2D"]
script = ExtResource("1_astap")
[node name="PathFollow2D" type="PathFollow2D" parent="."]
rotates = false
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="PathFollow2D"]
visible = false
sprite_frames = SubResource("SpriteFrames_lgeeq")
animation = &"down"
[node name="Sprite2D" type="Sprite2D" parent="PathFollow2D"]
texture = ExtResource("3_fhoaw")

View File

@@ -2,36 +2,60 @@ extends Node2D
class_name Unit
signal walk_finished
@export var grid: Resource
var _spawnPosition: Vector2
### Marker
# For now the marker will be spawned and deleted by the unit.
# Later it will be handled by the main scene.
# Load marker scene.
# var marker_scene: PackedScene = preload("res://Scenes/Unit/marker.tscn")
# var marker
## WE NOW USE A SIMPLE SPRITE2D FOR THE MARKER
@onready var _readyToSelectMarker: Sprite2D = $ReadyToSelectMarker
@onready var _selectedMarker: Sprite2D = $SelectedMarker
#@onready var _movingMarker: Sprite2D = $MovingMarker
var _readyToSelect: bool = false
var _selected: bool = false
#var _moving: bool = false
var isSelected := false:
set(value):
isSelected = value
if isSelected: _selectUnit()
else: _deselectUnit()
var TargetPosition: Vector2
var _isMoving := false:
set(value):
_isMoving = value
set_process(_isMoving)
## Unit Data
### Distance in cells
@export var moveRange := 6
### Speed along the path
@export var moveSpeed := 600.0
# Coordinates of the current cell the cursor moved to
var cell := Vector2.ZERO:
set(value):
# When changing the cell's value, we don't want to allow coordinates outside the grid.
cell = grid.clamp(value)
@onready var _path: Path2D = $Path2D
@onready var _pathFollow: PathFollow2D = $Path2D/PathFollow2D
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
# marker = marker_scene.instantiate()
# marker.hide()
# Spawning
global_position = _spawnPosition
_readyToSelectMarker.hide()
_selectedMarker.hide()
# _movingMarker.hide()
# Pathing and Grid
set_process(false)
_pathFollow.rotates = false
cell = grid.calculateGridCoordinates(position)
position = grid.calculateMapPosition(cell)
# 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()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
@@ -42,31 +66,13 @@ func _input(event: InputEvent):
print_debug("Action is Select")
# We combine it with the fact that it is already marked (@see _markUnit)
if _readyToSelect: _selectUnit()
else: _deselectUnit()
# if event.is_action_pressed("SetMarker"):
# print("Action is SetMarker")
# if _selected:
# print("Setting marker to ", event.position)
# #marker.global_position = event.position
## marker.show()
## _movingMarker.position = get_global_mouse_position()
## _movingMarker.show()
func _getUnitPosition():
return $AnimatedSprite2D.global_position
func _setSelected(selected: bool):
_selected = selected
if _readyToSelect: isSelected = true
else: isSelected = false
func _selectUnit():
_setSelected(true)
_selectedMarker.show()
func _deselectUnit():
_setSelected(false)
_selectedMarker.hide()
func _gets_selected(viewport: Node, event: InputEvent, shape_index: int):
@@ -82,4 +88,4 @@ func _unMarkUnit():
_readyToSelectMarker.hide()
func moveToTarget():
pass

View File

@@ -1,7 +1,8 @@
[gd_scene load_steps=92 format=3 uid="uid://dy7rltpxyqyw7"]
[gd_scene load_steps=93 format=3 uid="uid://dy7rltpxyqyw7"]
[ext_resource type="Script" uid="uid://dpu6c0bpm0dvl" path="res://Scenes/Unit/unit.gd" id="1_15sed"]
[ext_resource type="Texture2D" uid="uid://cgvyfsuri6vmx" path="res://Graphics/TileMaps/kenney_rpgUrbanKit/Tilemap/tilemap.png" id="1_hgpyh"]
[ext_resource type="Resource" uid="uid://bpf7mj7w5kftq" path="res://Resource/Grid.tres" id="2_jbdwb"]
[ext_resource type="Texture2D" uid="uid://bprproedmlhtr" path="res://Graphics/TileMaps/kenney_rpgUrbanKit/Tiles/tile_0168.png" id="3_ladk0"]
[ext_resource type="Texture2D" uid="uid://b7ra2w7rdeqij" path="res://Graphics/TileMaps/kenney_rpgUrbanKit/Tiles/tile_0169.png" id="4_iuf4a"]
[ext_resource type="Texture2D" uid="uid://cxtkb8rqq0j6r" path="res://Graphics/TileMaps/kenney_rpgUrbanKit/Tiles/tile_0407.png" id="5_ulevp"]
@@ -628,6 +629,7 @@ size = Vector2(16, 16)
[node name="Unit" type="Node2D"]
texture_filter = 1
script = ExtResource("1_15sed")
grid = ExtResource("2_jbdwb")
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."]
sprite_frames = SubResource("SpriteFrames_7f253")
@@ -653,5 +655,9 @@ 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"]
[connection signal="mouse_entered" from="Area2D" to="." method="_markUnit"]
[connection signal="mouse_exited" from="Area2D" to="." method="_unMarkUnit"]