test_joints.gd 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 = 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", self, "_on_option_selected")
  35. options.connect("option_changed", 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. _create_joint()
  42. $LabelJointType.text = "Joint Type: " + _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.scancode - 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. child_body.mode = RigidBody2D.MODE_RIGID
  81. if _change_positions:
  82. root.add_child(child_body)
  83. child_body.position = Vector2(0.0, 40)
  84. else:
  85. child_body.position = Vector2(0.0, 40)
  86. root.add_child(child_body)
  87. var parent_body = null
  88. if not _world_attachement:
  89. parent_body = create_rigidbody_box(BOX_SIZE, true, true)
  90. if _dynamic_attachement:
  91. parent_body.mode = RigidBody2D.MODE_RIGID
  92. parent_body.gravity_scale = 0.0
  93. child_body.gravity_scale = 0.0
  94. else:
  95. parent_body.mode = RigidBody2D.MODE_STATIC
  96. if _change_positions:
  97. root.add_child(parent_body)
  98. parent_body.position = Vector2(0.0, -40)
  99. else:
  100. parent_body.position = Vector2(0.0, -40)
  101. root.add_child(parent_body)
  102. var joint = _selected_joint.duplicate()
  103. joint.visible = true
  104. joint.disable_collision = not _bodies_collide
  105. if parent_body:
  106. joint.node_a = parent_body.get_path()
  107. joint.node_b = child_body.get_path()
  108. root.add_child(joint)
  109. if _destroy_body:
  110. yield(start_timer(0.5), "timeout")
  111. if is_timer_canceled():
  112. return
  113. child_body.queue_free()