42 lines
1.1 KiB
GDScript
42 lines
1.1 KiB
GDScript
extends KinematicBody2D
|
|
|
|
var free_slots = []
|
|
var used_slots = []
|
|
|
|
func _ready():
|
|
free_slots = [$Olive, $Olive2, $Olive3, $Olive4, $Olive5]
|
|
|
|
# Something has entered the collection area.
|
|
# If it's an olive and we have space, pick it up!
|
|
func _on_CollectionArea_area_entered(area: Area2D):
|
|
if area.is_in_group("olive_collectible"):
|
|
var olive_slot = free_slots.pop_back()
|
|
if olive_slot == null:
|
|
return
|
|
|
|
area.remove_from_group("olive_collectible")
|
|
|
|
self.call_deferred("_collect_olive", area, olive_slot)
|
|
|
|
func _collect_olive(olive: Area2D, olive_slot: Node2D):
|
|
# new olive found!
|
|
var olive_gpos = olive.global_position
|
|
olive.get_parent().remove_child(olive)
|
|
self.add_child_below_node(olive_slot, olive)
|
|
olive.global_position = olive_gpos
|
|
olive.harvest_into_wheelbarrow(olive_slot.position, olive_slot.rotation, self, olive_slot)
|
|
|
|
|
|
func slot_entry_complete(slot: Node2D):
|
|
used_slots.push_back(slot)
|
|
slot.visible = true
|
|
|
|
func on_press_area_entered(press: PhysicsBody2D):
|
|
var num_olives = 0
|
|
for olive in used_slots:
|
|
olive.visible = false
|
|
free_slots.push_back(olive)
|
|
num_olives += 1
|
|
used_slots = []
|
|
press.add_fresh_olives(num_olives)
|