From b6b38133db7d38727284aa1935063c584b600177 Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Mon, 9 Jan 2023 02:55:34 +0000 Subject: [PATCH] Make slugs choose the juiciest tree --- characters/pests/Slug.gd | 54 ++++++++++++++++++++++++++++-- characters/pests/Slug.tscn | 12 ++++--- scenery/facilities/OliveTree.gd | 19 ++++++++--- scenery/facilities/OliveTree.tscn | 2 +- scenery/facilities/OliveTreeBud.gd | 3 ++ sfx/slimy_walk.wav | 3 ++ sfx/slimy_walk.wav.import | 23 +++++++++++++ 7 files changed, 105 insertions(+), 11 deletions(-) create mode 100644 sfx/slimy_walk.wav create mode 100644 sfx/slimy_walk.wav.import diff --git a/characters/pests/Slug.gd b/characters/pests/Slug.gd index 29eb8b5..65c39cb 100644 --- a/characters/pests/Slug.gd +++ b/characters/pests/Slug.gd @@ -8,13 +8,35 @@ onready var _agent: NavigationAgent2D = $NavigationAgent2D var target: Node2D = null +var eaten_olives: int = 0 +var hp: float = 0 + + # Called when the node enters the scene tree for the first time. func _ready(): call_deferred("_find_target") func _find_target() -> void: - target = find_parent("YSort").get_node("Player") - _agent.set_target_location(target.global_position) + if eaten_olives >= 5: + # go in for the kill + target = find_parent("YSort").get_node("Player") + else: + var trees := get_tree().get_nodes_in_group("olive_trees") + var best_tree = null + var best_tree_juiciness = -1 + for tree in trees: + var tree_distsq = self.global_position.distance_squared_to(tree.global_position) + var tree_food = tree.calculate_food(sqrt(tree_distsq) / SPEED) + var tree_juiciness = tree_food / tree_distsq + + if tree_juiciness > best_tree_juiciness: + best_tree = tree + best_tree_juiciness = tree_juiciness + + target = best_tree + + if target != null: + _agent.set_target_location(target.global_position) func _at_target() -> void: print("at target") @@ -36,9 +58,37 @@ func _physics_process(delta: float) -> void: if _velocity.length_squared() > 16: if $AnimatedSprite.animation != "crawl": $AnimatedSprite.play("crawl") + if not $SlimyWalkSfx.playing: + $SlimyWalkSfx.play() else: if $AnimatedSprite.animation != "still": $AnimatedSprite.play("still") + $SlimyWalkSfx.playing = false #print(_velocity) var _vel := move_and_slide(_velocity) + +func _on_hp_updated() -> void: + if hp == eaten_olives: + self.modulate.a = 1.0 + else: + self.modulate.a = clamp(sqrt(hp / eaten_olives), 0.0, 1.0) + +func _on_eaten_olives_updated() -> void: + self.scale = Vector2(1, 1) * (1.0 + eaten_olives * 0.1) + var non_red := 1.0 - (0.1 * eaten_olives) + self.modulate.g = non_red + self.modulate.b = non_red + +func interact(_player: Node2D) -> bool: + self.hp -= 1 + if self.hp < 0: + # the slug is dead! + $SlimyWalkSfx.stop() + target = null + # TODO sfx + self.queue_free() + else: + self._on_hp_updated() + + return true diff --git a/characters/pests/Slug.tscn b/characters/pests/Slug.tscn index a545fbc..ef4ab97 100644 --- a/characters/pests/Slug.tscn +++ b/characters/pests/Slug.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=10 format=2] +[gd_scene load_steps=11 format=2] [ext_resource path="res://characters/pests/slug_frame0005.png" type="Texture" id=1] [ext_resource path="res://characters/pests/slug_frame0004.png" type="Texture" id=2] @@ -7,13 +7,14 @@ [ext_resource path="res://characters/pests/slug_frame0002.png" type="Texture" id=5] [ext_resource path="res://characters/pests/slug_frame0001.png" type="Texture" id=6] [ext_resource path="res://characters/pests/Slug.gd" type="Script" id=7] +[ext_resource path="res://sfx/slimy_walk.wav" type="AudioStream" id=8] [sub_resource type="SpriteFrames" id=1] animations = [ { -"frames": [ ExtResource( 4 ), ExtResource( 6 ), ExtResource( 5 ), ExtResource( 3 ), ExtResource( 2 ), ExtResource( 1 ) ], +"frames": [ ExtResource( 4 ), ExtResource( 6 ), ExtResource( 5 ), ExtResource( 3 ), ExtResource( 2 ), ExtResource( 1 ), ExtResource( 3 ), ExtResource( 5 ), ExtResource( 6 ), ExtResource( 4 ) ], "loop": true, "name": "crawl", -"speed": 5.0 +"speed": 7.0 }, { "frames": [ ExtResource( 4 ) ], "loop": true, @@ -26,7 +27,7 @@ radius = 6.0 height = 40.0 [node name="Slug" type="KinematicBody2D"] -collision_layer = 0 +collision_layer = 2 collision_mask = 0 script = ExtResource( 7 ) @@ -45,3 +46,6 @@ shape = SubResource( 2 ) path_desired_distance = 8.0 target_desired_distance = 12.0 path_max_distance = 64.0 + +[node name="SlimyWalkSfx" type="AudioStreamPlayer2D" parent="."] +stream = ExtResource( 8 ) diff --git a/scenery/facilities/OliveTree.gd b/scenery/facilities/OliveTree.gd index 99696da..3800816 100644 --- a/scenery/facilities/OliveTree.gd +++ b/scenery/facilities/OliveTree.gd @@ -1,10 +1,7 @@ extends StaticBody2D -# Declare member variables here. Examples: -# var a = 2 -# var b = "text" - +var has_been_munched: bool = false # Called when the node enters the scene tree for the first time. func _ready(): @@ -23,3 +20,17 @@ func interact(_player): $FallSfx.pitch_scale = rand_range(0.9, 1.5) $FallSfx.play() break + +# Calculate some measure of food that makes this tree attractive to pests. +# Calculate it in a given time. +func calculate_food(in_time: float) -> float: + var food = 0 + if not has_been_munched: + # munching a tree is worth 2 foods + food += 2 + + for child in self.get_children(): + if child.has_method("drop_olive"): + food += child.calculate_food_on_bud(in_time) + + return food diff --git a/scenery/facilities/OliveTree.tscn b/scenery/facilities/OliveTree.tscn index 5aa812b..e263100 100644 --- a/scenery/facilities/OliveTree.tscn +++ b/scenery/facilities/OliveTree.tscn @@ -9,7 +9,7 @@ radius = 28.0 height = 34.0 -[node name="OliveTree" type="StaticBody2D" groups=["interactable"]] +[node name="OliveTree" type="StaticBody2D" groups=["interactable", "olive_trees"]] collision_layer = 3 script = ExtResource( 3 ) diff --git a/scenery/facilities/OliveTreeBud.gd b/scenery/facilities/OliveTreeBud.gd index f288903..5bafa16 100644 --- a/scenery/facilities/OliveTreeBud.gd +++ b/scenery/facilities/OliveTreeBud.gd @@ -58,3 +58,6 @@ func drop_olive(): self.reset() return true + +func calculate_food_on_bud(in_time: float) -> float: + return clamp(progress + in_time * grow_rate, 0.0, 1.0) diff --git a/sfx/slimy_walk.wav b/sfx/slimy_walk.wav new file mode 100644 index 0000000..0239412 --- /dev/null +++ b/sfx/slimy_walk.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e21975f8f09b6312f901d1a7dd589f6f54b27e37d9f14a0f3045c2db31bc7dd9 +size 212770 diff --git a/sfx/slimy_walk.wav.import b/sfx/slimy_walk.wav.import new file mode 100644 index 0000000..a7a4bb3 --- /dev/null +++ b/sfx/slimy_walk.wav.import @@ -0,0 +1,23 @@ +[remap] + +importer="wav" +type="AudioStreamSample" +path="res://.import/slimy_walk.wav-79debdca905c40e38b83ce06cb03b684.sample" + +[deps] + +source_file="res://sfx/slimy_walk.wav" +dest_files=[ "res://.import/slimy_walk.wav-79debdca905c40e38b83ce06cb03b684.sample" ] + +[params] + +force/8_bit=false +force/mono=false +force/max_rate=false +force/max_rate_hz=44100 +edit/trim=false +edit/normalize=false +edit/loop_mode=0 +edit/loop_begin=0 +edit/loop_end=-1 +compress/mode=0