Make slugs choose the juiciest tree

main
Olivier 'reivilibre' 2023-01-09 02:55:34 +00:00
parent 880481c4b3
commit b6b38133db
7 changed files with 105 additions and 11 deletions

View File

@ -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

View File

@ -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 )

View File

@ -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

View File

@ -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 )

View File

@ -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)

BIN
sfx/slimy_walk.wav (Stored with Git LFS) Normal file

Binary file not shown.

23
sfx/slimy_walk.wav.import Normal file
View File

@ -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