73 lines
1.9 KiB
GDScript
73 lines
1.9 KiB
GDScript
extends Node2D
|
|
|
|
var progress: float = 0.0
|
|
var growing: bool = false
|
|
export var grow_rate: float = 0.05
|
|
|
|
# TODO 50 is debug
|
|
const GROW_RATE = 0.05
|
|
|
|
func _ready():
|
|
reset()
|
|
start_growing()
|
|
grow_rate = rand_range(0.03, 0.07)
|
|
|
|
func set_progress(new_progress: float):
|
|
progress = new_progress
|
|
$SpriteOlive.scale = Vector2(1, 1) * max(progress, 0)
|
|
|
|
if progress >= 1.0:
|
|
$Tween.interpolate_property($SpriteOlive, "modulate", $SpriteOlive.modulate, Color.white, 2.5, Tween.TRANS_QUAD, Tween.EASE_IN_OUT)
|
|
$Tween.start()
|
|
|
|
func reset():
|
|
set_progress(rand_range(-0.5, 0.0))
|
|
$SpriteOlive.modulate = Color(0.4, 0.4, 0.4)
|
|
$Tween.remove_all()
|
|
|
|
func start_growing():
|
|
growing = true
|
|
|
|
func _process(delta):
|
|
if growing and progress < 1.0:
|
|
set_progress(min(1.0, progress + GROW_RATE * delta))
|
|
|
|
func drop_olive():
|
|
if progress < 1.0:
|
|
return false
|
|
|
|
## Calculate where the olive needs to go.
|
|
|
|
# tree is my parent.
|
|
# world is the tree's parent.
|
|
var new_parent = self.get_parent().get_parent()
|
|
|
|
# we want to be at ground level
|
|
var target_y = self.get_parent().position.y + rand_range(-8, 8)
|
|
var target_x = self.get_parent().position.x + (self.position.x * rand_range(1.25, 2.0))
|
|
var target_pos = Vector2(target_x, target_y)
|
|
|
|
var new_olive = preload("res://projectiles/OliveProjectile.tscn").instance()
|
|
new_olive.position = self.global_position
|
|
new_olive.rotation = self.global_rotation
|
|
|
|
new_parent.add_child_below_node(self.get_parent(), new_olive)
|
|
|
|
new_olive.shoot_to(target_pos, rand_range(0.5, 1.5))
|
|
grow_rate = max(rand_range(0.9, 1.1) * grow_rate, 0.025)
|
|
self.reset()
|
|
|
|
return true
|
|
|
|
func calculate_food_on_bud(in_time: float) -> float:
|
|
return clamp(progress + in_time * grow_rate, 0.0, 1.0)
|
|
|
|
func eat_food_from_bud(limit: float) -> float:
|
|
if limit >= progress:
|
|
var eaten = max(0.0, progress)
|
|
self.reset()
|
|
return eaten
|
|
else:
|
|
set_progress(progress - limit)
|
|
return limit
|