44 lines
927 B
GDScript
44 lines
927 B
GDScript
extends Node2D
|
|
|
|
signal liberated
|
|
|
|
@export var n_plant_needed: int
|
|
|
|
var is_liberated := false
|
|
var current_plants := 0
|
|
|
|
func _ready():
|
|
$AnimationPlayer.play("Closed")
|
|
update_count()
|
|
|
|
func _on_area_2d_area_entered(area: Area2D) -> void:
|
|
var plant = area.get_parent()
|
|
if plant is Plant and not is_liberated:
|
|
plant.grown.connect(tracked_plant_grew)
|
|
plant.died.connect(tracked_plant_died)
|
|
|
|
func tracked_plant_grew():
|
|
if is_liberated:
|
|
return
|
|
current_plants += 1
|
|
if current_plants == n_plant_needed:
|
|
liberate()
|
|
update_count()
|
|
|
|
func tracked_plant_died():
|
|
if is_liberated:
|
|
return
|
|
current_plants -= 1
|
|
update_count()
|
|
|
|
func update_count():
|
|
$PlantsCount.text = str(roundf((float(current_plants)/n_plant_needed)*100)) + "%"
|
|
|
|
func liberate():
|
|
is_liberated = true
|
|
liberated.emit()
|
|
$PlantsCount.visible = false
|
|
$AnimationPlayer.play("Finished")
|
|
await $AnimationPlayer.animation_finished
|
|
$AnimationPlayer.play("Open")
|