2024-04-14 05:21:24 +00:00
|
|
|
extends Area2D
|
2024-04-14 04:26:14 +00:00
|
|
|
class_name Draggable
|
|
|
|
|
|
|
|
|
2024-04-14 05:46:06 +00:00
|
|
|
signal move(movement: Vector2)
|
2024-04-14 04:26:14 +00:00
|
|
|
signal dragged
|
|
|
|
signal dropped
|
|
|
|
|
2024-04-15 23:00:49 +00:00
|
|
|
@export var state_holder: StateHolder
|
2024-04-14 04:26:14 +00:00
|
|
|
|
2024-04-14 05:46:06 +00:00
|
|
|
@onready var mover: Node2D = $"MatchMousePosition"
|
|
|
|
|
2024-04-15 23:00:49 +00:00
|
|
|
var is_being_dragged: bool:
|
2024-04-14 05:46:06 +00:00
|
|
|
get:
|
2024-04-15 23:00:49 +00:00
|
|
|
return state_holder.state == 'dragged'
|
2024-04-14 04:26:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
func drag():
|
2024-04-15 23:00:49 +00:00
|
|
|
if not state_holder.propose_state('dragged', 100, _on_dropped):
|
|
|
|
return false
|
|
|
|
|
|
|
|
_update_mover_process()
|
2024-04-14 04:26:14 +00:00
|
|
|
dragged.emit()
|
|
|
|
|
|
|
|
func drop():
|
2024-04-15 23:00:49 +00:00
|
|
|
state_holder.remove_state('dragged')
|
|
|
|
|
|
|
|
func _on_dropped():
|
2024-04-14 04:26:14 +00:00
|
|
|
dropped.emit()
|
2024-04-14 05:46:06 +00:00
|
|
|
|
2024-04-15 23:00:49 +00:00
|
|
|
func _update_mover_process():
|
|
|
|
var value = is_being_dragged
|
|
|
|
mover.set_process(value)
|
|
|
|
mover.set_physics_process(value)
|
2024-04-14 05:46:06 +00:00
|
|
|
|
|
|
|
func _ready():
|
2024-04-15 23:00:49 +00:00
|
|
|
if state_holder == null:
|
|
|
|
state_holder = StateHolder.get_default(self)
|
|
|
|
_update_mover_process()
|
2024-04-14 05:46:06 +00:00
|
|
|
|
|
|
|
func _on_move(movement: Vector2):
|
2024-04-15 23:00:49 +00:00
|
|
|
if is_being_dragged:
|
|
|
|
move.emit(movement)
|