ButtonTrigger.gd 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. extends Area3D
  2. @export var on_enter := {}
  3. @export var on_exit := {}
  4. var activated_on_start = false
  5. @onready var timer = $Timer
  6. signal activated(on_start)
  7. signal deactivated
  8. var first_time = true
  9. func _process(delta: float) -> void:
  10. if first_time:
  11. #this is a bit ugly, but calls the signal activated with on_start true, when sonething is on it
  12. var layers = 32
  13. for mov in get_tree().get_nodes_in_group("movable"):
  14. for i in layers:
  15. if get_collision_mask_value(i+1) and mov.get_collision_layer_value(i+1):
  16. if abs((mov.position - position).length()) <= 1.9:
  17. print (str(mov.name) + " is closer <= 2")
  18. activated_on_start = true
  19. emit_signal("activated", true)
  20. first_time = false
  21. func _ready() -> void:
  22. for o in on_enter:
  23. var node = get_node(o)
  24. if node != null and node.has_method(on_enter[o]):
  25. print ("node: " + str(node.name) + ", " + str(on_enter[o]))
  26. self.activated.connect(Callable(node, on_enter[o]))
  27. elif node != null:
  28. print (node.name)
  29. for o in on_exit:
  30. var node = get_node(o)
  31. if node != null and node.has_method(on_exit[o]):
  32. print ("node: " + str(node.name) + ", " + str(on_exit[o]))
  33. self.deactivated.connect(Callable(node, on_exit[o]))
  34. func _on_ButtonTrigger_body_entered(body: Node3D) -> void:
  35. if timer.get_time_left() == 0:
  36. emit_signal("activated", false)
  37. func _on_ButtonTrigger_body_exited(body: Node3D) -> void:
  38. if timer.get_time_left() == 0:
  39. if get_overlapping_bodies().size() == 0:
  40. emit_signal("deactivated")
  41. else:
  42. #else something exited and is still overlapping bodies
  43. if body.is_in_group("tool"):
  44. emit_signal("deactivated")
  45. func _on_Timer_timeout() -> void:
  46. first_time = false