test.gd 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. class_name Test
  2. extends Node
  3. signal wait_done()
  4. @export var _enable_debug_collision = true
  5. var _timer
  6. var _timer_started = false
  7. var _wait_physics_ticks_counter = 0
  8. var _drawn_nodes = []
  9. func _enter_tree():
  10. if not _enable_debug_collision:
  11. get_tree().debug_collisions_hint = false
  12. func _physics_process(_delta):
  13. if _wait_physics_ticks_counter > 0:
  14. _wait_physics_ticks_counter -= 1
  15. if _wait_physics_ticks_counter == 0:
  16. emit_signal("wait_done")
  17. func add_sphere(pos, radius, color):
  18. var sphere = MeshInstance3D.new()
  19. var sphere_mesh = SphereMesh.new()
  20. sphere_mesh.radius = radius
  21. sphere_mesh.height = radius * 2.0
  22. sphere.mesh = sphere_mesh
  23. var material = StandardMaterial3D.new()
  24. material.flags_unshaded = true
  25. material.albedo_color = color
  26. sphere.set_surface_override_material(0, material)
  27. _drawn_nodes.push_back(sphere)
  28. add_child(sphere)
  29. sphere.global_transform.origin = pos
  30. func add_shape(shape, transform, color):
  31. var debug_mesh = shape.get_debug_mesh()
  32. var mesh_instance = MeshInstance3D.new()
  33. mesh_instance.transform = transform
  34. mesh_instance.mesh = debug_mesh
  35. var material = StandardMaterial3D.new()
  36. material.flags_unshaded = true
  37. material.albedo_color = color
  38. mesh_instance.set_surface_override_material(0, material)
  39. add_child(mesh_instance)
  40. _drawn_nodes.push_back(mesh_instance)
  41. func clear_drawn_nodes():
  42. for node in _drawn_nodes:
  43. remove_child(node)
  44. node.queue_free()
  45. _drawn_nodes.clear()
  46. func create_rigidbody(shape, pickable = false, transform = Transform3D.IDENTITY):
  47. var collision = CollisionShape3D.new()
  48. collision.shape = shape
  49. collision.transform = transform
  50. var body = RigidBody3D.new()
  51. body.add_child(collision)
  52. if pickable:
  53. var script = load("res://utils/rigidbody_pick.gd")
  54. body.set_script(script)
  55. return body
  56. func create_rigidbody_box(size, pickable = false, transform = Transform3D.IDENTITY):
  57. var shape = BoxShape3D.new()
  58. shape.size = size
  59. return create_rigidbody(shape, pickable, transform)
  60. func start_timer(timeout):
  61. if _timer == null:
  62. _timer = Timer.new()
  63. _timer.one_shot = true
  64. add_child(_timer)
  65. _timer.connect(&"timeout", Callable(self, "_on_timer_done"))
  66. else:
  67. cancel_timer()
  68. _timer.start(timeout)
  69. _timer_started = true
  70. return _timer
  71. func cancel_timer():
  72. if _timer_started:
  73. _timer.paused = true
  74. _timer.emit_signal("timeout")
  75. _timer.paused = false
  76. func is_timer_canceled():
  77. return _timer.paused
  78. func wait_for_physics_ticks(tick_count):
  79. _wait_physics_ticks_counter = tick_count
  80. return self
  81. func _on_timer_done():
  82. _timer_started = false