Files
turnbasedstrategygame/Scenes/Unit/unit.gd

92 lines
2.2 KiB
GDScript

extends Node2D
class_name Unit
signal walk_finished
@export var grid: Resource
var _spawnPosition: Vector2
@onready var _readyToSelectMarker: Sprite2D = $ReadyToSelectMarker
@onready var _selectedMarker: Sprite2D = $SelectedMarker
var _readyToSelect: 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:
# Spawning
global_position = _spawnPosition
_readyToSelectMarker.hide()
_selectedMarker.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:
pass
func _input(event: InputEvent):
if event.is_action_pressed("Select"):
print_debug("Action is Select")
# We combine it with the fact that it is already marked (@see _markUnit)
if _readyToSelect: isSelected = true
else: isSelected = false
func _selectUnit():
_selectedMarker.show()
func _deselectUnit():
_selectedMarker.hide()
func _gets_selected(viewport: Node, event: InputEvent, shape_index: int):
if event.is_action_pressed("Select") and event.position == position:
_selectUnit()
func _markUnit():
_readyToSelect = true
_readyToSelectMarker.show()
func _unMarkUnit():
_readyToSelect = false
_readyToSelectMarker.hide()
func moveToTarget():
pass