78 lines
1.9 KiB
GDScript
78 lines
1.9 KiB
GDScript
extends Node2D
|
|
|
|
class_name Unit
|
|
|
|
### 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 = preload("res://Scenes/Unit/marker.tscn")
|
|
var marker
|
|
|
|
@onready var _readyToSelectMarker = $ReadyToSelectMarker
|
|
@onready var _selectedMarker = $SelectedMarker
|
|
|
|
var _selected = false
|
|
var _readyToSelect = false
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
marker = marker_scene.instantiate()
|
|
marker.hide()
|
|
|
|
_readyToSelectMarker.hide()
|
|
_selectedMarker.hide()
|
|
|
|
# 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")
|
|
|
|
# Can't select from myself since (global_)position is alway 0,0
|
|
print("Event position: ", event.position)
|
|
print("My position is", position)
|
|
print("My global_position is", global_position)
|
|
|
|
if event.position == global_position:
|
|
_setSelected(true)
|
|
print_debug("Unit selected")
|
|
|
|
# We combine it with the fact that it is already marked (@see _markUnit)
|
|
if _readyToSelect: _selectUnit()
|
|
else: _deselectUnit()
|
|
|
|
|
|
if event.is_action_pressed("SetMarker") and _selected:
|
|
marker.global_position = event.position
|
|
marker.show()
|
|
print_debug("Setting marker")
|
|
|
|
func _getUnitPosition():
|
|
return $AnimatedSprite2D.global_position
|
|
|
|
func _setSelected(selected: bool):
|
|
_selected = selected
|
|
|
|
func _selectUnit():
|
|
_setSelected(true)
|
|
_selectedMarker.show()
|
|
|
|
func _deselectUnit():
|
|
_setSelected(false)
|
|
_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()
|