1
Fork 0
mirror of https://github.com/Steffo99/hella-farm.git synced 2024-11-26 01:54:23 +00:00
hella-farm/scenes/game/cursor.gd

36 lines
904 B
GDScript3
Raw Normal View History

2024-04-20 00:23:01 +00:00
extends Area2D
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-20 00:23:01 +00:00
signal dragged(node: Draggable)
signal dropped(node: Draggable)
2024-04-19 02:30:35 +00:00
@onready var game := MainGame.get_via_group(self)
static func get_via_group(node: Node) -> MainGame:
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:
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
func _physics_process(_delta: float) -> void:
2024-04-20 00:23:01 +00:00
position += (game.camera.get_global_mouse_position() - global_position)