test_joints.gd 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. extends Test
  2. const OPTION_JOINT_TYPE = "Joint Type/%s Joint (%d)"
  3. const OPTION_TEST_CASE_BODIES_COLLIDE = "Test case/Attached bodies collide"
  4. const OPTION_TEST_CASE_WORLD_ATTACHMENT = "Test case/No parent body"
  5. const OPTION_TEST_CASE_DYNAMIC_ATTACHMENT = "Test case/Parent body is dynamic (no gravity)"
  6. const OPTION_TEST_CASE_DESTROY_BODY = "Test case/Destroy attached body"
  7. const OPTION_TEST_CASE_CHANGE_POSITIONS = "Test case/Set body positions after added to scene"
  8. const BOX_SIZE = Vector2(64, 64)
  9. var _update_joint = false
  10. var _selected_joint = null
  11. var _joint_type = PinJoint2D
  12. var _bodies_collide = false
  13. var _world_attachement = false
  14. var _dynamic_attachement = false
  15. var _destroy_body = false
  16. var _change_positions = false
  17. var _joint_types = {}
  18. func _ready():
  19. var options = $Options
  20. var joints = $Joints
  21. for joint_index in range(joints.get_child_count()):
  22. var joint_node = joints.get_child(joint_index)
  23. joint_node.visible = false
  24. var joint_name = String(joint_node.name)
  25. var joint_short = joint_name.substr(0, joint_name.length() - 7)
  26. var option_name = OPTION_JOINT_TYPE % [joint_short, joint_index + 1]
  27. options.add_menu_item(option_name)
  28. _joint_types[option_name] = joint_node
  29. options.add_menu_item(OPTION_TEST_CASE_BODIES_COLLIDE, true, false)
  30. options.add_menu_item(OPTION_TEST_CASE_WORLD_ATTACHMENT, true, false)
  31. options.add_menu_item(OPTION_TEST_CASE_DYNAMIC_ATTACHMENT, true, false)
  32. options.add_menu_item(OPTION_TEST_CASE_DESTROY_BODY, true, false)
  33. options.add_menu_item(OPTION_TEST_CASE_CHANGE_POSITIONS, true, false)
  34. options.connect(&"option_selected", Callable(self, "_on_option_selected"))
  35. options.connect(&"option_changed", Callable(self, "_on_option_changed"))
  36. _selected_joint = _joint_types.values()[0]
  37. _update_joint = true
  38. func _process(_delta):
  39. if _update_joint:
  40. _update_joint = false
  41. await _create_joint()
  42. $LabelJointType.text = "Joint Type: " + String(_selected_joint.name)
  43. func _input(event):
  44. var key_event = event as InputEventKey
  45. if key_event and not key_event.pressed:
  46. var joint_index = key_event.keycode - KEY_1
  47. if joint_index >= 0 and joint_index < _joint_types.size():
  48. _selected_joint = _joint_types.values()[joint_index]
  49. _update_joint = true
  50. func _on_option_selected(option):
  51. if _joint_types.has(option):
  52. _selected_joint = _joint_types[option]
  53. _update_joint = true
  54. func _on_option_changed(option, checked):
  55. match option:
  56. OPTION_TEST_CASE_BODIES_COLLIDE:
  57. _bodies_collide = checked
  58. _update_joint = true
  59. OPTION_TEST_CASE_WORLD_ATTACHMENT:
  60. _world_attachement = checked
  61. _update_joint = true
  62. OPTION_TEST_CASE_DYNAMIC_ATTACHMENT:
  63. _dynamic_attachement = checked
  64. _update_joint = true
  65. OPTION_TEST_CASE_DESTROY_BODY:
  66. _destroy_body = checked
  67. _update_joint = true
  68. OPTION_TEST_CASE_CHANGE_POSITIONS:
  69. _change_positions = checked
  70. _update_joint = true
  71. func _create_joint():
  72. cancel_timer()
  73. var root = $Objects
  74. while root.get_child_count():
  75. var last_child_index = root.get_child_count() - 1
  76. var last_child = root.get_child(last_child_index)
  77. root.remove_child(last_child)
  78. last_child.queue_free()
  79. var child_body = create_rigidbody_box(BOX_SIZE, true, true)
  80. if _change_positions:
  81. root.add_child(child_body)
  82. child_body.position = Vector2(0.0, 40)
  83. else:
  84. child_body.position = Vector2(0.0, 40)
  85. root.add_child(child_body)
  86. var parent_body = null
  87. if not _world_attachement:
  88. parent_body = create_rigidbody_box(BOX_SIZE, true, true)
  89. if _dynamic_attachement:
  90. parent_body.gravity_scale = 0.0
  91. child_body.gravity_scale = 0.0
  92. else:
  93. parent_body.freeze = true
  94. if _change_positions:
  95. root.add_child(parent_body)
  96. parent_body.position = Vector2(0.0, -40)
  97. else:
  98. parent_body.position = Vector2(0.0, -40)
  99. root.add_child(parent_body)
  100. var joint = _selected_joint.duplicate()
  101. joint.visible = true
  102. joint.disable_collision = not _bodies_collide
  103. root.add_child(joint)
  104. if parent_body:
  105. joint.set_node_a(joint.get_path_to(parent_body))
  106. joint.set_node_b(joint.get_path_to(child_body))
  107. if _destroy_body:
  108. await start_timer(0.5).timeout
  109. if is_timer_canceled():
  110. return
  111. child_body.queue_free()