From 6e14678239663cc81e388e15299d6c7f06e83047 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 20 Apr 2024 04:54:33 +0200 Subject: [PATCH] Make `Sheep` throwable! --- behaviours/move_physics.gd | 39 +++++++++++++++++++ ...towards_physics.tscn => move_physics.tscn} | 2 +- behaviours/move_towards_physics.gd | 17 -------- entities/sheep.tscn | 19 ++++++--- 4 files changed, 54 insertions(+), 23 deletions(-) create mode 100644 behaviours/move_physics.gd rename behaviours/{move_towards_physics.tscn => move_physics.tscn} (60%) delete mode 100644 behaviours/move_towards_physics.gd diff --git a/behaviours/move_physics.gd b/behaviours/move_physics.gd new file mode 100644 index 0000000..b979b76 --- /dev/null +++ b/behaviours/move_physics.gd @@ -0,0 +1,39 @@ +extends Move +class_name MoveTowardsGravity + + +signal dragged(cursor: Cursor) +signal dropped +signal fallen + + +@export var acceleration = 2500.0 +@export var linear_damp = 50.0 + +var cursor: Cursor = null + +var velocity := Vector2.ZERO + +@onready var ticks_per_second = ProjectSettings.get_setting("physics/common/physics_ticks_per_second") + + +func drag(value: Cursor) -> void: + cursor = value + dragged.emit(value) + +func drop() -> void: + cursor = null + dropped.emit() + +func fall() -> void: + velocity = Vector2.ZERO + fallen.emit() + + +func _physics_process(delta: float) -> void: + if enabled: + if cursor: + var gap = cursor.global_position - global_position + velocity += gap * delta * acceleration + velocity *= 1.0 - linear_damp / ticks_per_second + move.emit(velocity * delta) diff --git a/behaviours/move_towards_physics.tscn b/behaviours/move_physics.tscn similarity index 60% rename from behaviours/move_towards_physics.tscn rename to behaviours/move_physics.tscn index bc2bedf..2a31113 100644 --- a/behaviours/move_towards_physics.tscn +++ b/behaviours/move_physics.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=2 format=3 uid="uid://rx24bppccih7"] -[ext_resource type="Script" path="res://behaviours/move_towards_physics.gd" id="1_u02i3"] +[ext_resource type="Script" path="res://behaviours/move_physics.gd" id="1_u02i3"] [node name="MoveTowardsPhysics" type="Node2D"] script = ExtResource("1_u02i3") diff --git a/behaviours/move_towards_physics.gd b/behaviours/move_towards_physics.gd deleted file mode 100644 index 22069cb..0000000 --- a/behaviours/move_towards_physics.gd +++ /dev/null @@ -1,17 +0,0 @@ -extends MoveTowards -class_name MoveTowardsGravity - - -# [field speed] is actually used as acceleration. Oh well. - -var velocity := Vector2.ZERO - - -func _physics_process(delta: float) -> void: - if enabled: - if target: - var gap = target.global_position - global_position - var norm = gap.normalized() - velocity += norm * delta * speed - Log.p(self, "Velocity: %s" % velocity) - move.emit(velocity * delta) diff --git a/entities/sheep.tscn b/entities/sheep.tscn index 79d5516..a393680 100644 --- a/entities/sheep.tscn +++ b/entities/sheep.tscn @@ -13,7 +13,7 @@ [ext_resource type="PackedScene" uid="uid://cbg5kgwxusvxf" path="res://behaviours/cursor_detector.tscn" id="13_5fkdr"] [ext_resource type="PackedScene" uid="uid://dti7l0d40hhgt" path="res://behaviours/target_picker.tscn" id="13_jf6em"] [ext_resource type="PackedScene" uid="uid://dijcjahkddudv" path="res://behaviours/draggable.tscn" id="14_0b1hs"] -[ext_resource type="PackedScene" uid="uid://rx24bppccih7" path="res://behaviours/move_towards_physics.tscn" id="14_3wojv"] +[ext_resource type="PackedScene" uid="uid://rx24bppccih7" path="res://behaviours/move_physics.tscn" id="14_3wojv"] [ext_resource type="PackedScene" uid="uid://c5pyp5hvthdof" path="res://behaviours/tracker_tracker.tscn" id="14_eqowb"] [sub_resource type="CircleShape2D" id="CircleShape2D_c5tcn"] @@ -164,7 +164,9 @@ debug_color = Color(1, 1, 0, 0) tracker = NodePath("..") [node name="MovementDrag" parent="." instance=ExtResource("14_3wojv")] -speed = 1200.0 +acceleration = 650.0 +linear_damp = 18.0 +enabled = false [node name="DragPriority" parent="MovementDrag" instance=ExtResource("11_0jlmk")] alternative_priority = 50 @@ -176,6 +178,10 @@ scale = Vector2(3, 3) shape = SubResource("CircleShape2D_c5tcn") debug_color = Color(1, 1, 1, 0) +[node name="FallTimer" type="Timer" parent="MovementDrag"] +wait_time = 0.5 +one_shot = true + [connection signal="ready" from="MovementSampler" to="MovementSampler" method="enable"] [connection signal="move" from="MovementIdle" to="." method="_on_move"] [connection signal="move_disabled" from="MovementIdle" to="MovementIdle/BoredTimer" method="stop"] @@ -201,11 +207,14 @@ debug_color = Color(1, 1, 1, 0) [connection signal="tracked" from="MovementRunFromHunter/HunterSense" to="MovementRunFromHunter/HunterSense/TargetPicker" method="set_target_if_null"] [connection signal="untracked" from="MovementRunFromHunter/HunterSense" to="MovementRunFromHunter/HunterSense/TargetPicker" method="clear_if_target"] [connection signal="target_changed" from="MovementRunFromHunter/HunterSense/TargetPicker" to="MovementRunFromHunter" method="set_target" unbinds=1] -[connection signal="changed_target" from="MovementDrag" to="MovementDrag/DragPriority" method="priority_conditional"] +[connection signal="dragged" from="MovementDrag" to="MovementDrag/DragPriority" method="priority_alternative" unbinds=1] +[connection signal="dropped" from="MovementDrag" to="MovementDrag/FallTimer" method="start"] +[connection signal="fallen" from="MovementDrag" to="MovementDrag/DragPriority" method="priority_default"] [connection signal="move" from="MovementDrag" to="." method="_on_move"] [connection signal="move_enabled" from="MovementDrag" to="MovementWander" method="clear_direction"] [connection signal="priority_changed_no_args" from="MovementDrag/DragPriority" to="MovementSampler" method="enable"] [connection signal="dragged" from="MovementDrag/Draggable" to="." method="_on_draggable_dragged"] -[connection signal="dragged" from="MovementDrag/Draggable" to="MovementDrag" method="set_target"] +[connection signal="dragged" from="MovementDrag/Draggable" to="MovementDrag" method="drag"] [connection signal="dropped" from="MovementDrag/Draggable" to="." method="_on_draggable_dropped"] -[connection signal="dropped" from="MovementDrag/Draggable" to="MovementDrag" method="clear_target"] +[connection signal="dropped" from="MovementDrag/Draggable" to="MovementDrag" method="drop"] +[connection signal="timeout" from="MovementDrag/FallTimer" to="MovementDrag" method="fall"]