From b222f9a7ffc92cc9da8e832c64097b20f357bc8c Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 20 Apr 2024 02:23:01 +0200 Subject: [PATCH] Refactor `Cursor` to be an `Area2D` --- behaviours/cursor_detector.gd | 4 ++-- behaviours/cursor_detector.tscn | 4 ++-- scenes/game/cursor.gd | 21 +++++++++++++++++++-- scenes/game/cursor.tscn | 3 +-- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/behaviours/cursor_detector.gd b/behaviours/cursor_detector.gd index 599201b..a80f04f 100644 --- a/behaviours/cursor_detector.gd +++ b/behaviours/cursor_detector.gd @@ -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) diff --git a/behaviours/cursor_detector.tscn b/behaviours/cursor_detector.tscn index cf22039..d5cf13a 100644 --- a/behaviours/cursor_detector.tscn +++ b/behaviours/cursor_detector.tscn @@ -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"] diff --git a/scenes/game/cursor.gd b/scenes/game/cursor.gd index 7a8909f..23f78e2 100644 --- a/scenes/game/cursor.gd +++ b/scenes/game/cursor.gd @@ -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) diff --git a/scenes/game/cursor.tscn b/scenes/game/cursor.tscn index 664d3fb..4a4dfbf 100644 --- a/scenes/game/cursor.tscn +++ b/scenes/game/cursor.tscn @@ -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="."]