test.gd 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. class_name Test
  2. extends Node2D
  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. class Circle2D:
  9. extends Node2D
  10. var center
  11. var radius
  12. var color
  13. func _draw():
  14. draw_circle(center, radius, color)
  15. var _drawn_nodes = []
  16. func _enter_tree():
  17. if not _enable_debug_collision:
  18. get_tree().debug_collisions_hint = false
  19. func _physics_process(_delta):
  20. if _wait_physics_ticks_counter > 0:
  21. _wait_physics_ticks_counter -= 1
  22. if _wait_physics_ticks_counter == 0:
  23. emit_signal("wait_done")
  24. func add_line(pos_start, pos_end, color):
  25. var line = Line2D.new()
  26. line.points = [pos_start, pos_end]
  27. line.width = 1.5
  28. line.default_color = color
  29. _drawn_nodes.push_back(line)
  30. add_child(line)
  31. func add_circle(pos, radius, color):
  32. var circle = Circle2D.new()
  33. circle.center = pos
  34. circle.radius = radius
  35. circle.color = color
  36. _drawn_nodes.push_back(circle)
  37. add_child(circle)
  38. func add_shape(shape, shape_transform, color):
  39. var collision = CollisionShape2D.new()
  40. collision.shape = shape
  41. collision.transform = shape_transform
  42. collision.modulate = color
  43. _drawn_nodes.push_back(collision)
  44. add_child(collision)
  45. func clear_drawn_nodes():
  46. for node in _drawn_nodes:
  47. node.queue_free()
  48. _drawn_nodes.clear()
  49. func create_rigidbody(shape, pickable = false, shape_transform = Transform2D.IDENTITY):
  50. var collision = CollisionShape2D.new()
  51. collision.shape = shape
  52. collision.transform = shape_transform
  53. var body = RigidBody2D.new()
  54. body.add_child(collision)
  55. if pickable:
  56. var script = load("res://utils/rigidbody_pick.gd")
  57. body.set_script(script)
  58. return body
  59. func create_rigidbody_box(size, pickable = false, use_icon = false, shape_transform = Transform2D.IDENTITY):
  60. var shape = RectangleShape2D.new()
  61. shape.size = size
  62. var body = create_rigidbody(shape, pickable, shape_transform)
  63. if use_icon:
  64. var texture = load("res://icon.webp")
  65. var icon = Sprite2D.new()
  66. icon.texture = texture
  67. icon.scale = size / texture.get_size()
  68. body.add_child(icon)
  69. return body
  70. func find_node(node_name):
  71. var nodes = find_children(node_name)
  72. if nodes.size() > 0:
  73. return nodes[0]
  74. return null
  75. func start_timer(timeout):
  76. if _timer == null:
  77. _timer = Timer.new()
  78. _timer.one_shot = true
  79. add_child(_timer)
  80. _timer.timeout.connect(self._on_timer_done)
  81. else:
  82. cancel_timer()
  83. _timer.start(timeout)
  84. _timer_started = true
  85. return _timer
  86. func cancel_timer():
  87. if _timer_started:
  88. _timer.paused = true
  89. _timer.emit_signal("timeout")
  90. _timer.paused = false
  91. func is_timer_canceled():
  92. return _timer and _timer.paused
  93. func wait_for_physics_ticks(tick_count):
  94. _wait_physics_ticks_counter = tick_count
  95. return self
  96. func _on_timer_done():
  97. _timer_started = false