mirror of
https://github.com/Steffo99/hella-farm.git
synced 2024-11-26 01:54:23 +00:00
Make Sheep
s Draggable
with a CursorMagnet
Throwing and drag isn't implemented yet. Any ideas for how to do it with this framework? Might be a good idea to do it on a branch first...
This commit is contained in:
parent
d699ec9110
commit
3d17dcd3f0
8 changed files with 121 additions and 6 deletions
|
@ -2,11 +2,20 @@ extends Area2D
|
||||||
class_name Draggable
|
class_name Draggable
|
||||||
|
|
||||||
|
|
||||||
|
signal move(movement: Vector2)
|
||||||
signal dragged
|
signal dragged
|
||||||
signal dropped
|
signal dropped
|
||||||
|
|
||||||
|
|
||||||
var being_dragged: bool = false
|
@onready var mover: Node2D = $"MatchMousePosition"
|
||||||
|
|
||||||
|
var being_dragged: bool:
|
||||||
|
get:
|
||||||
|
return being_dragged
|
||||||
|
set(value):
|
||||||
|
being_dragged = value
|
||||||
|
mover.set_process(being_dragged)
|
||||||
|
mover.set_physics_process(being_dragged)
|
||||||
|
|
||||||
|
|
||||||
func drag():
|
func drag():
|
||||||
|
@ -16,3 +25,10 @@ func drag():
|
||||||
func drop():
|
func drop():
|
||||||
being_dragged = false
|
being_dragged = false
|
||||||
dropped.emit()
|
dropped.emit()
|
||||||
|
|
||||||
|
|
||||||
|
func _ready():
|
||||||
|
being_dragged = false
|
||||||
|
|
||||||
|
func _on_move(movement: Vector2):
|
||||||
|
move.emit(movement)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
[gd_scene load_steps=3 format=3 uid="uid://dijcjahkddudv"]
|
[gd_scene load_steps=4 format=3 uid="uid://dijcjahkddudv"]
|
||||||
|
|
||||||
[ext_resource type="Script" path="res://behaviours/draggable.gd" id="1_hdedq"]
|
[ext_resource type="Script" path="res://behaviours/draggable.gd" id="1_hdedq"]
|
||||||
|
[ext_resource type="PackedScene" uid="uid://bvrxvrjlo5130" path="res://behaviours/move_towards_mouse.tscn" id="2_gi6xd"]
|
||||||
|
|
||||||
[sub_resource type="CircleShape2D" id="CircleShape2D_vkph8"]
|
[sub_resource type="CircleShape2D" id="CircleShape2D_vkph8"]
|
||||||
radius = 8.0
|
radius = 8.0
|
||||||
|
@ -11,3 +12,9 @@ script = ExtResource("1_hdedq")
|
||||||
[node name="Shape" type="CollisionShape2D" parent="."]
|
[node name="Shape" type="CollisionShape2D" parent="."]
|
||||||
shape = SubResource("CircleShape2D_vkph8")
|
shape = SubResource("CircleShape2D_vkph8")
|
||||||
debug_color = Color(1, 1, 1, 0)
|
debug_color = Color(1, 1, 1, 0)
|
||||||
|
|
||||||
|
[node name="MatchMousePosition" parent="." instance=ExtResource("2_gi6xd")]
|
||||||
|
scale = Vector2(10000, 10000)
|
||||||
|
speed = 100000.0
|
||||||
|
|
||||||
|
[connection signal="move" from="MatchMousePosition" to="." method="_on_move"]
|
||||||
|
|
|
@ -27,8 +27,10 @@ func get_relative_mouse_position():
|
||||||
func _physics_process(delta: float) -> void:
|
func _physics_process(delta: float) -> void:
|
||||||
match state:
|
match state:
|
||||||
State.CAPTURED:
|
State.CAPTURED:
|
||||||
var direction: Vector2 = position.direction_to(get_relative_mouse_position())
|
var relative_mouse_position: Vector2 = get_relative_mouse_position()
|
||||||
var movement: Vector2 = direction * delta * speed
|
var direction: Vector2 = position.direction_to(relative_mouse_position)
|
||||||
|
var actual_speed: float = min(delta * speed, relative_mouse_position.length()) # Don't overshoot.
|
||||||
|
var movement: Vector2 = direction * actual_speed
|
||||||
move.emit(movement)
|
move.emit(movement)
|
||||||
|
|
||||||
func _on_capture_area_mouse_entered() -> void:
|
func _on_capture_area_mouse_entered() -> void:
|
||||||
|
|
|
@ -2,6 +2,14 @@ extends CharacterBody2D
|
||||||
class_name Sheep
|
class_name Sheep
|
||||||
|
|
||||||
|
|
||||||
|
@onready var draggable: Draggable = $"Draggable"
|
||||||
|
|
||||||
|
|
||||||
func _on_move(movement: Vector2) -> void:
|
func _on_move(movement: Vector2) -> void:
|
||||||
move_and_collide(movement)
|
if not draggable.being_dragged:
|
||||||
|
move_and_collide(movement)
|
||||||
|
|
||||||
|
|
||||||
|
func _on_drag_move(movement: Vector2) -> void:
|
||||||
|
if draggable.being_dragged:
|
||||||
|
move_and_collide(movement)
|
||||||
|
|
|
@ -25,5 +25,7 @@ speed = -20.0
|
||||||
can_detach = true
|
can_detach = true
|
||||||
|
|
||||||
[node name="Draggable" parent="." instance=ExtResource("3_8ku7r")]
|
[node name="Draggable" parent="." instance=ExtResource("3_8ku7r")]
|
||||||
|
scale = Vector2(2, 2)
|
||||||
|
|
||||||
[connection signal="move" from="MoveTowardsMouse" to="." method="_on_move"]
|
[connection signal="move" from="MoveTowardsMouse" to="." method="_on_move"]
|
||||||
|
[connection signal="move" from="Draggable" to="." method="_on_drag_move"]
|
||||||
|
|
54
scenes/game/cursor_magnet.gd
Normal file
54
scenes/game/cursor_magnet.gd
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
extends Area2D
|
||||||
|
class_name CursorMagnet
|
||||||
|
|
||||||
|
|
||||||
|
signal dragged(node: Draggable)
|
||||||
|
signal dropped(node: Draggable)
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
var dragging: Draggable = null
|
||||||
|
|
||||||
|
func drag():
|
||||||
|
var target = find_closest_target()
|
||||||
|
if target:
|
||||||
|
dragging = target
|
||||||
|
target.drag()
|
||||||
|
dragged.emit(target)
|
||||||
|
|
||||||
|
func drop():
|
||||||
|
if dragging:
|
||||||
|
var target = dragging
|
||||||
|
dragging = null
|
||||||
|
target.drop()
|
||||||
|
dropped.emit(target)
|
||||||
|
|
||||||
|
|
||||||
|
func _input(event: InputEvent) -> void:
|
||||||
|
if event is InputEventMouseButton:
|
||||||
|
if event.button_index == MOUSE_BUTTON_LEFT:
|
||||||
|
if event.pressed:
|
||||||
|
drag()
|
||||||
|
else:
|
||||||
|
drop()
|
||||||
|
|
||||||
|
func _on_dragged(node: Draggable) -> void:
|
||||||
|
Log.p(self, "Dragged: %s" % node)
|
||||||
|
|
||||||
|
func _on_dropped(node: Draggable) -> void:
|
||||||
|
Log.p(self, "Dropped: %s" % node)
|
||||||
|
|
||||||
|
func _on_move(movement: Vector2) -> void:
|
||||||
|
position += movement
|
23
scenes/game/cursor_magnet.tscn
Normal file
23
scenes/game/cursor_magnet.tscn
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
[gd_scene load_steps=4 format=3 uid="uid://7j1b55t8tafg"]
|
||||||
|
|
||||||
|
[ext_resource type="Script" path="res://scenes/game/cursor_magnet.gd" id="1_xk040"]
|
||||||
|
[ext_resource type="PackedScene" uid="uid://bvrxvrjlo5130" path="res://behaviours/move_towards_mouse.tscn" id="2_ljil3"]
|
||||||
|
|
||||||
|
[sub_resource type="CircleShape2D" id="CircleShape2D_6mkjn"]
|
||||||
|
radius = 16.0
|
||||||
|
|
||||||
|
[node name="CursorMagnet" type="Area2D"]
|
||||||
|
script = ExtResource("1_xk040")
|
||||||
|
|
||||||
|
[node name="Shape" type="CollisionShape2D" parent="."]
|
||||||
|
scale = Vector2(0.5, 0.5)
|
||||||
|
shape = SubResource("CircleShape2D_6mkjn")
|
||||||
|
debug_color = Color(1, 1, 1, 0.67451)
|
||||||
|
|
||||||
|
[node name="MoveTowardsMouse" parent="." instance=ExtResource("2_ljil3")]
|
||||||
|
scale = Vector2(10000, 10000)
|
||||||
|
speed = 100000.0
|
||||||
|
|
||||||
|
[connection signal="dragged" from="." to="." method="_on_dragged"]
|
||||||
|
[connection signal="dropped" from="." to="." method="_on_dropped"]
|
||||||
|
[connection signal="move" from="MoveTowardsMouse" to="." method="_on_move"]
|
|
@ -1,8 +1,9 @@
|
||||||
[gd_scene load_steps=8 format=3 uid="uid://cxj5aud02f40j"]
|
[gd_scene load_steps=9 format=3 uid="uid://cxj5aud02f40j"]
|
||||||
|
|
||||||
[ext_resource type="Script" path="res://scenes/game/main_game.gd" id="1_wiglu"]
|
[ext_resource type="Script" path="res://scenes/game/main_game.gd" id="1_wiglu"]
|
||||||
[ext_resource type="PackedScene" uid="uid://dm068vaseh45n" path="res://scenes/game/game_camera.tscn" id="2_db5xs"]
|
[ext_resource type="PackedScene" uid="uid://dm068vaseh45n" path="res://scenes/game/game_camera.tscn" id="2_db5xs"]
|
||||||
[ext_resource type="PackedScene" uid="uid://brvbtvt4em32" path="res://behaviours/counter.tscn" id="3_p6jw3"]
|
[ext_resource type="PackedScene" uid="uid://brvbtvt4em32" path="res://behaviours/counter.tscn" id="3_p6jw3"]
|
||||||
|
[ext_resource type="PackedScene" uid="uid://7j1b55t8tafg" path="res://scenes/game/cursor_magnet.tscn" id="3_xbolm"]
|
||||||
[ext_resource type="PackedScene" uid="uid://uoxwjpmgg27a" path="res://entities/gold.tscn" id="4_eu7q4"]
|
[ext_resource type="PackedScene" uid="uid://uoxwjpmgg27a" path="res://entities/gold.tscn" id="4_eu7q4"]
|
||||||
[ext_resource type="PackedScene" uid="uid://bc2bm8lbol18w" path="res://entities/sheep.tscn" id="5_63bwb"]
|
[ext_resource type="PackedScene" uid="uid://bc2bm8lbol18w" path="res://entities/sheep.tscn" id="5_63bwb"]
|
||||||
|
|
||||||
|
@ -26,6 +27,8 @@ debug_color = Color(1, 1, 1, 0)
|
||||||
|
|
||||||
[node name="GameCamera" parent="." instance=ExtResource("2_db5xs")]
|
[node name="GameCamera" parent="." instance=ExtResource("2_db5xs")]
|
||||||
|
|
||||||
|
[node name="CursorMagnet" parent="." instance=ExtResource("3_xbolm")]
|
||||||
|
|
||||||
[node name="GoldCounter" parent="." instance=ExtResource("3_p6jw3")]
|
[node name="GoldCounter" parent="." instance=ExtResource("3_p6jw3")]
|
||||||
|
|
||||||
[node name="Gold" parent="." instance=ExtResource("4_eu7q4")]
|
[node name="Gold" parent="." instance=ExtResource("4_eu7q4")]
|
||||||
|
|
Loading…
Reference in a new issue