From c12ff464e2913db5ab557f69a12f1c5640f594ad Mon Sep 17 00:00:00 2001 From: Lorenzo Rossi Date: Mon, 15 Apr 2024 02:45:18 +0200 Subject: [PATCH] Add eat_target and imp behaviour --- behaviours/eat_target.gd | 39 ++++++++++++ behaviours/eat_target.tscn | 22 +++++++ behaviours/hunt_target.gd | 7 ++- behaviours/hunt_target.tscn | 2 + behaviours/move_towards_mouse.gd | 40 +++++++++++- behaviours/move_towards_target.gd | 23 ++++++- behaviours/move_towards_target.tscn | 9 +-- entities/cloud.tscn | 94 ++++++++++++++--------------- entities/imp.gd | 22 ++++++- entities/imp.tscn | 18 +++--- main.tscn | 2 +- scenes/game/main_game.tscn | 12 ++-- 12 files changed, 218 insertions(+), 72 deletions(-) create mode 100644 behaviours/eat_target.gd create mode 100644 behaviours/eat_target.tscn diff --git a/behaviours/eat_target.gd b/behaviours/eat_target.gd new file mode 100644 index 0000000..d873940 --- /dev/null +++ b/behaviours/eat_target.gd @@ -0,0 +1,39 @@ +extends Node2D +class_name EatTarget + + +signal eaten(target: Node2D) +signal move(movement: Vector2) + +@export var tag: StringName: + get: + return tag + set(value): + tag = value + if hunt_target != null: + hunt_target.tag = value + if eater != null: + eater.tag = value + + +@onready var hunt_target: HuntTarget = $"HuntTarget" +@onready var eater: Eater = $"Eater" +@onready var move_towards: MoveTowardsTarget = $"MoveTowardsTarget" + +func _ready(): + hunt_target.tag = tag + eater.tag = tag + +func _on_target_selected(body: Node2D) -> void: + move_towards.target = body + +func _on_target_abandoned(_body: Node2D) -> void: + move_towards.target = null + + +func _on_eater_eaten(edible): + eaten.emit(edible) + + +func _on_move_towards_target_move(movement): + move.emit(movement) diff --git a/behaviours/eat_target.tscn b/behaviours/eat_target.tscn new file mode 100644 index 0000000..ee94530 --- /dev/null +++ b/behaviours/eat_target.tscn @@ -0,0 +1,22 @@ +[gd_scene load_steps=5 format=3 uid="uid://dfitiajuuvdko"] + +[ext_resource type="Script" path="res://behaviours/eat_target.gd" id="1_tlnlt"] +[ext_resource type="PackedScene" uid="uid://dxmodn8mbvw0i" path="res://behaviours/hunt_target.tscn" id="2_c8wdv"] +[ext_resource type="PackedScene" uid="uid://jg7qkbswgqjc" path="res://behaviours/eater.tscn" id="3_ir2yh"] +[ext_resource type="PackedScene" uid="uid://kceb2v2dm0qn" path="res://behaviours/move_towards_target.tscn" id="4_1qc2q"] + +[node name="EatTarget" type="Node2D"] +script = ExtResource("1_tlnlt") + +[node name="HuntTarget" parent="." instance=ExtResource("2_c8wdv")] +tag = &"" +give_up_secs = 5.0 + +[node name="Eater" parent="." instance=ExtResource("3_ir2yh")] + +[node name="MoveTowardsTarget" parent="." instance=ExtResource("4_1qc2q")] + +[connection signal="target_abandoned" from="HuntTarget" to="." method="_on_target_abandoned"] +[connection signal="target_selected" from="HuntTarget" to="." method="_on_target_selected"] +[connection signal="eaten" from="Eater" to="." method="_on_eater_eaten"] +[connection signal="move" from="MoveTowardsTarget" to="." method="_on_move_towards_target_move"] diff --git a/behaviours/hunt_target.gd b/behaviours/hunt_target.gd index de97456..cdd7f9c 100644 --- a/behaviours/hunt_target.gd +++ b/behaviours/hunt_target.gd @@ -11,7 +11,8 @@ signal target_abandoned(target: Node2D) return tag set(value): tag = value - hunter.tag = value + if hunter != null: + hunter.tag = value @export var give_up_secs: float = 5.0 @@ -22,6 +23,9 @@ var target: Node2D = null func pick_new_target(): + if hunter.possible_targets.is_empty(): + return + var idx = Random.rng.randi_range(0, len(hunter.possible_targets) - 1) target = hunter.possible_targets[idx] target_selected.emit(target) @@ -37,6 +41,7 @@ func _on_hunter_tracked(_body: Node2D): func _on_hunter_untracked(body: Node2D): if body == target: target = null + target_abandoned.emit(body) pick_new_target() func _on_give_up_timer_timeout() -> void: diff --git a/behaviours/hunt_target.tscn b/behaviours/hunt_target.tscn index 31b6aa4..e9e33e2 100644 --- a/behaviours/hunt_target.tscn +++ b/behaviours/hunt_target.tscn @@ -5,6 +5,8 @@ [node name="HuntTarget" type="Node2D"] script = ExtResource("1_1ex7u") +tag = null +give_up_secs = null [node name="Hunter" parent="." instance=ExtResource("2_vjdtc")] diff --git a/behaviours/move_towards_mouse.gd b/behaviours/move_towards_mouse.gd index 4abc5de..4bbcee7 100644 --- a/behaviours/move_towards_mouse.gd +++ b/behaviours/move_towards_mouse.gd @@ -1,8 +1,46 @@ -extends MoveTowards +extends Node2D class_name MoveTowardsMouse + +signal move(movement: Vector2) +signal detached +signal captured + @onready var game := MainGame.get_ancestor(self) +@export var speed: float = 100.0 +@export var can_detach: bool = false + + +enum State { DETACHED, CAPTURED } + +var state: State = State.DETACHED func get_followed_global_position(): return game.camera.get_global_mouse_position() + + +func get_followed_mouse_position(): + var global_followed_position: Vector2 = get_followed_global_position() + var relative_followed_position: Vector2 = global_followed_position - global_position + return relative_followed_position + + +func _physics_process(delta: float) -> void: + match state: + State.CAPTURED: + var relative_followed_position: Vector2 = get_followed_mouse_position() + var direction: Vector2 = position.direction_to(relative_followed_position) + var actual_speed: float = min(delta * speed, relative_followed_position.length()) # Don't overshoot. + var movement: Vector2 = direction * actual_speed + move.emit(movement) + +func _on_capture_area_mouse_entered() -> void: + state = State.CAPTURED + captured.emit() + +func _on_capture_area_mouse_exited() -> void: + if can_detach: + state = State.DETACHED + detached.emit() + diff --git a/behaviours/move_towards_target.gd b/behaviours/move_towards_target.gd index eadfaf2..c519780 100644 --- a/behaviours/move_towards_target.gd +++ b/behaviours/move_towards_target.gd @@ -1,8 +1,29 @@ -extends MoveTowards +extends Node2D class_name MoveTowardsTarget + +signal move(movement: Vector2) + + +@export var speed: float = 100.0 @export var target: Node2D func get_followed_global_position(): return target.global_position + + +func get_followed_mouse_position(): + var global_followed_position: Vector2 = get_followed_global_position() + var relative_followed_position: Vector2 = global_followed_position - global_position + return relative_followed_position + + +func _physics_process(delta: float) -> void: + if target == null: + return + var relative_followed_position: Vector2 = get_followed_mouse_position() + var direction: Vector2 = position.direction_to(relative_followed_position) + var actual_speed: float = min(delta * speed, relative_followed_position.length()) # Don't overshoot. + var movement: Vector2 = direction * actual_speed + move.emit(movement) diff --git a/behaviours/move_towards_target.tscn b/behaviours/move_towards_target.tscn index 433b6e8..d04e146 100644 --- a/behaviours/move_towards_target.tscn +++ b/behaviours/move_towards_target.tscn @@ -1,13 +1,6 @@ -[gd_scene load_steps=3 format=3 uid="uid://kceb2v2dm0qn"] +[gd_scene load_steps=2 format=3 uid="uid://kceb2v2dm0qn"] [ext_resource type="Script" path="res://behaviours/move_towards_target.gd" id="1_703gy"] -[ext_resource type="PackedScene" uid="uid://cbg5kgwxusvxf" path="res://behaviours/hover_detector.tscn" id="2_nq6qb"] [node name="MoveTowardsTarget" type="Node2D"] script = ExtResource("1_703gy") - -[node name="CaptureArea" parent="." instance=ExtResource("2_nq6qb")] -collision_mask = 24 - -[connection signal="mouse_entered" from="CaptureArea" to="." method="_on_capture_area_mouse_entered"] -[connection signal="mouse_exited" from="CaptureArea" to="." method="_on_capture_area_mouse_exited"] diff --git a/entities/cloud.tscn b/entities/cloud.tscn index 454b64c..318129b 100644 --- a/entities/cloud.tscn +++ b/entities/cloud.tscn @@ -6,53 +6,6 @@ [sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_08frh"] -[sub_resource type="Animation" id="Animation_mqmwg"] -resource_name = "fade_out" -tracks/0/type = "bezier" -tracks/0/imported = false -tracks/0/enabled = true -tracks/0/path = NodePath("Sprite:modulate:r") -tracks/0/interp = 1 -tracks/0/loop_wrap = true -tracks/0/keys = { -"handle_modes": PackedInt32Array(0, 0), -"points": PackedFloat32Array(1, -0.25, 0, 0.25, 0, 1, -0.25, 0, 0.25, 0), -"times": PackedFloat32Array(0, 1) -} -tracks/1/type = "bezier" -tracks/1/imported = false -tracks/1/enabled = true -tracks/1/path = NodePath("Sprite:modulate:g") -tracks/1/interp = 1 -tracks/1/loop_wrap = true -tracks/1/keys = { -"handle_modes": PackedInt32Array(0, 0), -"points": PackedFloat32Array(1, -0.25, 0, 0.25, 0, 1, -0.25, 0, 0.25, 0), -"times": PackedFloat32Array(0, 1) -} -tracks/2/type = "bezier" -tracks/2/imported = false -tracks/2/enabled = true -tracks/2/path = NodePath("Sprite:modulate:b") -tracks/2/interp = 1 -tracks/2/loop_wrap = true -tracks/2/keys = { -"handle_modes": PackedInt32Array(0, 0), -"points": PackedFloat32Array(1, -0.25, 0, 0.25, 0, 1, -0.25, 0, 0.25, 0), -"times": PackedFloat32Array(0, 1) -} -tracks/3/type = "bezier" -tracks/3/imported = false -tracks/3/enabled = true -tracks/3/path = NodePath("Sprite:modulate:a") -tracks/3/interp = 1 -tracks/3/loop_wrap = true -tracks/3/keys = { -"handle_modes": PackedInt32Array(0, 0), -"points": PackedFloat32Array(1, -0.25, 0, 0.25, 0, 0, -0.25, 0, 0.25, 0), -"times": PackedFloat32Array(0, 1) -} - [sub_resource type="Animation" id="Animation_rrrnd"] length = 0.001 tracks/0/type = "bezier" @@ -100,6 +53,53 @@ tracks/3/keys = { "times": PackedFloat32Array(0) } +[sub_resource type="Animation" id="Animation_mqmwg"] +resource_name = "fade_out" +tracks/0/type = "bezier" +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/path = NodePath("Sprite:modulate:r") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/keys = { +"handle_modes": PackedInt32Array(0, 0), +"points": PackedFloat32Array(1, -0.25, 0, 0.25, 0, 1, -0.25, 0, 0.25, 0), +"times": PackedFloat32Array(0, 1) +} +tracks/1/type = "bezier" +tracks/1/imported = false +tracks/1/enabled = true +tracks/1/path = NodePath("Sprite:modulate:g") +tracks/1/interp = 1 +tracks/1/loop_wrap = true +tracks/1/keys = { +"handle_modes": PackedInt32Array(0, 0), +"points": PackedFloat32Array(1, -0.25, 0, 0.25, 0, 1, -0.25, 0, 0.25, 0), +"times": PackedFloat32Array(0, 1) +} +tracks/2/type = "bezier" +tracks/2/imported = false +tracks/2/enabled = true +tracks/2/path = NodePath("Sprite:modulate:b") +tracks/2/interp = 1 +tracks/2/loop_wrap = true +tracks/2/keys = { +"handle_modes": PackedInt32Array(0, 0), +"points": PackedFloat32Array(1, -0.25, 0, 0.25, 0, 1, -0.25, 0, 0.25, 0), +"times": PackedFloat32Array(0, 1) +} +tracks/3/type = "bezier" +tracks/3/imported = false +tracks/3/enabled = true +tracks/3/path = NodePath("Sprite:modulate:a") +tracks/3/interp = 1 +tracks/3/loop_wrap = true +tracks/3/keys = { +"handle_modes": PackedInt32Array(0, 0), +"points": PackedFloat32Array(1, -0.25, 0, 0.25, 0, 0, -0.25, 0, 0.25, 0), +"times": PackedFloat32Array(0, 1) +} + [sub_resource type="AnimationLibrary" id="AnimationLibrary_fs8h8"] _data = { "RESET": SubResource("Animation_rrrnd"), diff --git a/entities/imp.gd b/entities/imp.gd index 79fc1c9..52c63e4 100644 --- a/entities/imp.gd +++ b/entities/imp.gd @@ -1,6 +1,10 @@ extends CharacterBody2D class_name Imp +@export var skull_chance: float = 0.2 + +@onready var skull_spawner: Spawner = $"SkullSpawner" +@onready var draggable: Draggable = $"Draggable" # Called when the node enters the scene tree for the first time. func _ready() -> void: @@ -8,5 +12,21 @@ func _ready() -> void: # Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta: float) -> void: +func _process(_delta: float) -> void: pass + + +func _on_eat_target_eaten(_target: Edible): + if Random.rng.randf() < skull_chance: + skull_spawner.spawn() + + +func _on_draggable_move(movement:Vector2): + if draggable.being_dragged: + print("move", movement) + move_and_collide(movement) + +func _on_move(movement:Vector2): + if not draggable.being_dragged: + print("move", movement) + move_and_collide(movement) \ No newline at end of file diff --git a/entities/imp.tscn b/entities/imp.tscn index f61df06..9193784 100644 --- a/entities/imp.tscn +++ b/entities/imp.tscn @@ -1,9 +1,10 @@ -[gd_scene load_steps=7 format=3 uid="uid://4d3ksr3171x4"] +[gd_scene load_steps=8 format=3 uid="uid://4d3ksr3171x4"] [ext_resource type="Script" path="res://entities/imp.gd" id="1_dixpc"] -[ext_resource type="PackedScene" uid="uid://dxmodn8mbvw0i" path="res://behaviours/hunt_target.tscn" id="3_6bq0i"] [ext_resource type="PackedScene" uid="uid://dijcjahkddudv" path="res://behaviours/draggable.tscn" id="3_4528r"] -[ext_resource type="PackedScene" uid="uid://kceb2v2dm0qn" path="res://behaviours/move_towards_target.tscn" id="4_ofu70"] +[ext_resource type="PackedScene" uid="uid://dfitiajuuvdko" path="res://behaviours/eat_target.tscn" id="3_iybf3"] +[ext_resource type="PackedScene" uid="uid://cyrg770fsetyu" path="res://behaviours/spawner.tscn" id="4_d8lgm"] +[ext_resource type="PackedScene" uid="uid://uoxwjpmgg27a" path="res://entities/gold.tscn" id="5_yrfoq"] [sub_resource type="CircleShape2D" id="CircleShape2D_c5tcn"] radius = 16.0 @@ -31,11 +32,12 @@ scale = Vector2(0.5, 0.5) [node name="DropSound" type="AudioStreamPlayer2D" parent="Draggable"] scale = Vector2(0.5, 0.5) -[node name="HuntTarget" parent="." instance=ExtResource("3_6bq0i")] +[node name="EatTarget" parent="." instance=ExtResource("3_iybf3")] tag = &"Sheep" -[node name="MoveTowardsTarget" parent="." instance=ExtResource("4_ofu70")] +[node name="SkullSpawner" parent="." instance=ExtResource("4_d8lgm")] +scene = ExtResource("5_yrfoq") -[connection signal="dragged" from="Draggable" to="." method="_on_draggable_dragged"] -[connection signal="dropped" from="Draggable" to="." method="_on_draggable_dropped"] -[connection signal="move" from="Draggable" to="." method="_on_drag_move"] +[connection signal="move" from="Draggable" to="." method="_on_draggable_move"] +[connection signal="eaten" from="EatTarget" to="." method="_on_eat_target_eaten"] +[connection signal="move" from="EatTarget" to="." method="_on_move"] diff --git a/main.tscn b/main.tscn index 1525501..1d2db12 100644 --- a/main.tscn +++ b/main.tscn @@ -1,7 +1,7 @@ [gd_scene load_steps=4 format=3 uid="uid://b38wkla8e7rmo"] [ext_resource type="Script" path="res://main.gd" id="1_jyg3q"] -[ext_resource type="Material" uid="uid://dpumcyplql08q" path="res://postprocess.material" id="2_3o1ux"] +[ext_resource type="Material" path="res://postprocess.material" id="2_3o1ux"] [ext_resource type="PackedScene" uid="uid://bkm7id1wdwywg" path="res://scenes/menu/safe_margin_container.tscn" id="2_ah6n8"] [node name="Main" type="Node"] diff --git a/scenes/game/main_game.tscn b/scenes/game/main_game.tscn index e3a9a82..d5e0851 100644 --- a/scenes/game/main_game.tscn +++ b/scenes/game/main_game.tscn @@ -1,7 +1,7 @@ -[gd_scene load_steps=13 format=3 uid="uid://cxj5aud02f40j"] +[gd_scene load_steps=14 format=3 uid="uid://cxj5aud02f40j"] [ext_resource type="Script" path="res://scenes/game/main_game.gd" id="1_wiglu"] -[ext_resource type="PackedScene" path="res://behaviours/spawner.tscn" id="2_bx37o"] +[ext_resource type="PackedScene" uid="uid://cyrg770fsetyu" path="res://behaviours/spawner.tscn" id="2_bx37o"] [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://eqb3vfbvjefp" path="res://entities/cloud.tscn" id="3_whlmm"] @@ -9,6 +9,7 @@ [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="Texture2D" uid="uid://dyvkvmw7wxwe5" path="res://icon.svg" id="9_8kxq4"] +[ext_resource type="PackedScene" uid="uid://4d3ksr3171x4" path="res://entities/imp.tscn" id="10_26rf6"] [sub_resource type="TileSet" id="TileSet_g2dkm"] @@ -72,12 +73,12 @@ position = Vector2(379, -58) position = Vector2(456, -7) [node name="Sheep" parent="." instance=ExtResource("5_63bwb")] -position = Vector2(-135, -134) +position = Vector2(-273, -86) [node name="SpawnedEntities" type="Node2D" parent="."] [node name="Cloud" parent="." instance=ExtResource("3_whlmm")] -position = Vector2(-3, -112) +position = Vector2(-62, -239) [node name="Node2D" type="Node2D" parent="."] position = Vector2(590, 317) @@ -93,4 +94,7 @@ texture = ExtResource("9_8kxq4") scale = Vector2(0.0886132, 0.0886132) shape = SubResource("CircleShape2D_3du4x") +[node name="Imp" parent="." instance=ExtResource("10_26rf6")] +position = Vector2(-275, 10) + [connection signal="mouse_entered" from="Node2D/Area2D" to="CloudSpawner" method="spawn"]