77 lines
2.2 KiB
GDScript
77 lines
2.2 KiB
GDScript
extends StaticBody3D
|
|
class_name Balloon
|
|
|
|
@export var pop_up_text = ""
|
|
@export var wait_time = 4.0
|
|
@export var image_path = "res://assets/Images/spiel_ballon_text2.png"
|
|
@onready var label_3d: Label3D = $Label3D
|
|
@onready var balloon_model: MeshInstance3D = $Balloon_model
|
|
@onready var collision_shape_3d: CollisionShape3D = $CollisionShape3D
|
|
@onready var area_3d: Area3D = $Area3D
|
|
signal balloon_popped
|
|
|
|
func _ready() -> void:
|
|
for child in get_children():
|
|
if child is RigidBody3D:
|
|
child.freeze = true
|
|
child.gravity_scale = 0.0
|
|
$label.texture = load(image_path)
|
|
|
|
func _on_area_3d_body_entered(body: Node3D) -> void:
|
|
if not body.is_in_group("throwable"):
|
|
return
|
|
$AudioStreamPlayer.play()
|
|
balloon_popped.emit()
|
|
# hide balloon, animation, make label visible
|
|
balloon_model.visible = false
|
|
$AnimationPlayer.play("pop")
|
|
collision_shape_3d.call_deferred("queue_free")
|
|
area_3d.call_deferred("queue_free")
|
|
label_3d.text = pop_up_text
|
|
label_3d.visible = true
|
|
|
|
# --- let objects hanging from balloons fall ---
|
|
for child in get_children():
|
|
print(child.name)
|
|
if child is not RigidBody3D and child is not Area3D:
|
|
print(child)
|
|
continue # Label und Ballon ignorieren
|
|
var global_tf = child.global_transform
|
|
|
|
remove_child(child)
|
|
get_tree().current_scene.add_child(child) # zurück zur Welt
|
|
child.global_transform = global_tf
|
|
if child is Area3D and child.name.contains('Battery'):
|
|
print('battery falling')
|
|
print(child.position)
|
|
child.position.y = child.position.y - child.new_y_diff
|
|
print(child.position)
|
|
child.set_picture()
|
|
|
|
# if RigidBody3D: activate physics again
|
|
if child is RigidBody3D:
|
|
child.freeze = false
|
|
child.gravity_scale = 1.0
|
|
|
|
# start coroutine for delay + fade out
|
|
await fade_and_destroy()
|
|
|
|
func fade_and_destroy() -> void:
|
|
# wait 10 seconds
|
|
await get_tree().create_timer(wait_time).timeout
|
|
|
|
# create tween and play it
|
|
var tween = create_tween()
|
|
tween.tween_property($label, "modulate:a", 0.0, 0.5) # 2 Sekunden FadeOut
|
|
|
|
# wait til tween is finished
|
|
await tween.finished
|
|
|
|
# delete node
|
|
queue_free()
|
|
|
|
|
|
func _on_animation_player_animation_finished(anim_name: StringName) -> void:
|
|
if anim_name == 'pop':
|
|
$popped_balloon.visible = false
|