Files

73 lines
1.9 KiB
GDScript

extends Node3D
@export var required_weight: float = 5.0
@export var active_color: Color = Color(0, 1, 0)
@export var inactive_color: Color = Color(1, 0, 0)
@export var text_on_activation: String
@onready var area: Area3D = $Area3D
@onready var mesh: MeshInstance3D = $MeshInstance3D
@onready var static_body: StaticBody3D = $StaticBody3D
signal activated
signal deactivated
var current_weight: float = 0.0
var is_active: bool = false
func _ready():
mesh.material_override = StandardMaterial3D.new()
mesh.material_override.albedo_color = inactive_color
func _on_body_entered(body: Node3D) -> void:
print("body entered")
print(body)
var w = _get_body_weight(body)
if w > 0:
current_weight += w
_check_weight()
func _on_body_exited(body: Node3D) -> void:
var w = _get_body_weight(body)
if w > 0:
current_weight -= w
_check_weight()
func _get_body_weight(body: Node3D) -> float:
if body.has_method("get_weight"):
return body.get_weight()
elif body is RigidBody3D:
return body.mass
return 0.0
func _check_weight() -> void:
var active_now = current_weight >= required_weight
if active_now != is_active:
is_active = active_now
_update_visuals()
if is_active:
_on_activated()
else:
_on_deactivated()
func _update_visuals():
mesh.material_override.albedo_color = active_color if is_active else inactive_color
var target_pos_y = -0.05 if is_active else 0.0
var tween = get_tree().create_tween()
# Bewege sowohl das sichtbare Mesh als auch den Kollisionskörper nach unten
tween.tween_property(mesh, "position:y", target_pos_y, 0.2)\
.set_trans(Tween.TRANS_SINE).set_ease(Tween.EASE_OUT)
tween.tween_property(static_body, "position:y", target_pos_y, 0.2)\
.set_trans(Tween.TRANS_SINE).set_ease(Tween.EASE_OUT)
func _on_activated():
print("Pressure block activated! Total weight =", current_weight)
activated.emit()
func _on_deactivated():
print("Pressure block deactivated!")
deactivated.emit()