2024-04-20 00:23:01 +00:00
|
|
|
extends Area2D
|
2024-04-15 23:44:06 +00:00
|
|
|
class_name Cursor
|
|
|
|
|
2024-04-17 03:26:13 +00:00
|
|
|
## A [CharacterBody2D] syncing its position with the mouse on each physics timestep.
|
2024-04-15 23:44:06 +00:00
|
|
|
|
|
|
|
|
2024-04-20 00:23:01 +00:00
|
|
|
signal dragged(node: Draggable)
|
|
|
|
signal dropped(node: Draggable)
|
|
|
|
|
|
|
|
|
2024-04-24 02:55:31 +00:00
|
|
|
@onready var game: MainGame = MainGame.get_via_group(self)
|
2024-04-24 02:56:44 +00:00
|
|
|
@onready var gold_display: GoldDisplay = %"GoldDisplay"
|
2024-05-01 04:26:30 +00:00
|
|
|
@onready var cheat_gold_spawner: Spawner = %"CheatGoldSpawner"
|
2024-04-15 23:44:06 +00:00
|
|
|
|
2024-04-20 00:50:47 +00:00
|
|
|
var dragging: Draggable = null
|
|
|
|
|
2024-04-15 23:44:06 +00:00
|
|
|
|
2024-04-24 02:55:31 +00:00
|
|
|
static func get_via_group(node: Node) -> Cursor:
|
2024-04-20 00:17:40 +00:00
|
|
|
var result = node.get_tree().get_nodes_in_group("cursor")
|
|
|
|
if result.is_empty():
|
|
|
|
return null
|
|
|
|
return result[0]
|
|
|
|
|
|
|
|
|
2024-04-20 00:23:01 +00:00
|
|
|
func find_closest_target() -> Draggable:
|
|
|
|
var bodies = get_overlapping_bodies()
|
|
|
|
var min_distance: float = INF
|
|
|
|
var to_drag: Node = null
|
|
|
|
for body in bodies:
|
2024-04-20 00:23:45 +00:00
|
|
|
for target in body.find_children("*", "Draggable", true, false):
|
2024-04-20 00:23:01 +00:00
|
|
|
var distance = position.distance_to(target.position)
|
|
|
|
if distance < min_distance:
|
|
|
|
min_distance = distance
|
|
|
|
to_drag = target
|
|
|
|
return to_drag
|
|
|
|
|
2024-04-20 00:50:47 +00:00
|
|
|
func drag():
|
|
|
|
var target = find_closest_target()
|
|
|
|
if target and not dragging:
|
|
|
|
dragging = target
|
2024-04-20 02:11:14 +00:00
|
|
|
target.drag(self)
|
2024-04-20 00:50:47 +00:00
|
|
|
dragged.emit(target)
|
|
|
|
|
|
|
|
func drop():
|
|
|
|
if dragging:
|
|
|
|
var target = dragging
|
|
|
|
dragging = null
|
2024-04-30 02:49:00 +00:00
|
|
|
if is_instance_valid(target):
|
|
|
|
target.drop()
|
|
|
|
dropped.emit(target)
|
|
|
|
else:
|
|
|
|
dropped.emit(null)
|
2024-04-20 00:50:47 +00:00
|
|
|
|
|
|
|
func log_dragging() -> void:
|
|
|
|
Log.p(self, "Dragging: %s" % dragging)
|
|
|
|
|
|
|
|
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
|
|
if event is InputEventMouseButton:
|
|
|
|
if event.button_index == MOUSE_BUTTON_LEFT:
|
|
|
|
if event.pressed:
|
|
|
|
drag()
|
|
|
|
else:
|
|
|
|
drop()
|
2024-04-26 00:15:39 +00:00
|
|
|
|
2024-04-20 00:23:01 +00:00
|
|
|
|
2024-04-15 23:44:06 +00:00
|
|
|
func _physics_process(_delta: float) -> void:
|
2024-04-20 00:23:01 +00:00
|
|
|
position += (game.camera.get_global_mouse_position() - global_position)
|
2024-05-01 04:26:30 +00:00
|
|
|
if Input.is_action_pressed(&"cheat_gold"):
|
|
|
|
cheat_gold_spawner.spawn()
|