1
Fork 0
mirror of https://github.com/Steffo99/hella-farm.git synced 2024-11-24 17:14:23 +00:00

Refactor Cursor to be an Area2D

This commit is contained in:
Steffo 2024-04-20 02:23:01 +02:00
parent 3d9233f01f
commit b222f9a7ff
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
4 changed files with 24 additions and 8 deletions

View file

@ -10,12 +10,12 @@ signal cursor_exited(cursor: Cursor)
var mouse_inside = false
func _on_body_entered(body: Node2D) -> void:
func _on_area_entered(body: Area2D) -> void:
if body is Cursor:
mouse_inside = true
cursor_entered.emit(body)
func _on_body_exited(body: Node2D) -> void:
func _on_area_exited(body: Area2D) -> void:
if body is Cursor:
mouse_inside = true
cursor_exited.emit(body)

View file

@ -9,5 +9,5 @@ input_pickable = false
monitorable = false
script = ExtResource("1_4hcxj")
[connection signal="body_entered" from="." to="." method="_on_body_entered"]
[connection signal="body_exited" from="." to="." method="_on_body_exited"]
[connection signal="area_entered" from="." to="." method="_on_area_entered"]
[connection signal="area_exited" from="." to="." method="_on_area_exited"]

View file

@ -1,9 +1,13 @@
extends CharacterBody2D
extends Area2D
class_name Cursor
## A [CharacterBody2D] syncing its position with the mouse on each physics timestep.
signal dragged(node: Draggable)
signal dropped(node: Draggable)
@onready var game := MainGame.get_via_group(self)
@ -14,5 +18,18 @@ static func get_via_group(node: Node) -> MainGame:
return result[0]
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", "Draggable", false, false):
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:
move_and_collide(game.camera.get_global_mouse_position() - global_position)
position += (game.camera.get_global_mouse_position() - global_position)

View file

@ -5,10 +5,9 @@
[sub_resource type="CircleShape2D" id="CircleShape2D_j2mj5"]
radius = 4.0
[node name="Cursor" type="CharacterBody2D" groups=["cursor"]]
[node name="Cursor" type="Area2D" groups=["cursor"]]
collision_layer = 64
collision_mask = 0
motion_mode = 1
script = ExtResource("1_1og6v")
[node name="Shape" type="CollisionShape2D" parent="."]