test_joints.gd 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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: Joint2D = null
  11. var _bodies_collide := false
  12. var _world_attachement := false
  13. var _dynamic_attachement := false
  14. var _destroy_body := false
  15. var _change_positions := false
  16. var _joint_types := {}
  17. func _ready() -> void:
  18. var options: OptionMenu = $Options
  19. var joints: Node2D = $Joints
  20. for joint_index in joints.get_child_count():
  21. var joint_node := joints.get_child(joint_index)
  22. joint_node.visible = false
  23. var joint_name := String(joint_node.name)
  24. var joint_short := joint_name.substr(0, joint_name.length() - 7)
  25. var option_name := OPTION_JOINT_TYPE % [joint_short, joint_index + 1]
  26. options.add_menu_item(option_name)
  27. _joint_types[option_name] = joint_node
  28. options.add_menu_item(OPTION_TEST_CASE_BODIES_COLLIDE, true, false)
  29. options.add_menu_item(OPTION_TEST_CASE_WORLD_ATTACHMENT, true, false)
  30. options.add_menu_item(OPTION_TEST_CASE_DYNAMIC_ATTACHMENT, true, false)
  31. options.add_menu_item(OPTION_TEST_CASE_DESTROY_BODY, true, false)
  32. options.add_menu_item(OPTION_TEST_CASE_CHANGE_POSITIONS, true, false)
  33. options.option_selected.connect(_on_option_selected)
  34. options.option_changed.connect(_on_option_changed)
  35. _selected_joint = _joint_types.values()[0]
  36. _update_joint = true
  37. func _process(_delta: float) -> void:
  38. if _update_joint:
  39. _update_joint = false
  40. await _create_joint()
  41. $LabelJointType.text = "Joint Type: " + String(_selected_joint.name)
  42. func _input(event: InputEvent) -> void:
  43. if event is InputEventKey and not event.pressed:
  44. var joint_index: int = event.keycode - KEY_1
  45. if joint_index >= 0 and joint_index < _joint_types.size():
  46. _selected_joint = _joint_types.values()[joint_index]
  47. _update_joint = true
  48. func _on_option_selected(option: String) -> void:
  49. if _joint_types.has(option):
  50. _selected_joint = _joint_types[option]
  51. _update_joint = true
  52. func _on_option_changed(option: String, checked: bool) -> void:
  53. match option:
  54. OPTION_TEST_CASE_BODIES_COLLIDE:
  55. _bodies_collide = checked
  56. _update_joint = true
  57. OPTION_TEST_CASE_WORLD_ATTACHMENT:
  58. _world_attachement = checked
  59. _update_joint = true
  60. OPTION_TEST_CASE_DYNAMIC_ATTACHMENT:
  61. _dynamic_attachement = checked
  62. _update_joint = true
  63. OPTION_TEST_CASE_DESTROY_BODY:
  64. _destroy_body = checked
  65. _update_joint = true
  66. OPTION_TEST_CASE_CHANGE_POSITIONS:
  67. _change_positions = checked
  68. _update_joint = true
  69. func _create_joint() -> void:
  70. cancel_timer()
  71. var root: Node2D = $Objects
  72. while root.get_child_count():
  73. var last_child_index := root.get_child_count() - 1
  74. var last_child := root.get_child(last_child_index)
  75. root.remove_child(last_child)
  76. last_child.queue_free()
  77. var child_body := create_rigidbody_box(BOX_SIZE, true, true)
  78. if _change_positions:
  79. root.add_child(child_body)
  80. child_body.position = Vector2(0.0, 40)
  81. else:
  82. child_body.position = Vector2(0.0, 40)
  83. root.add_child(child_body)
  84. var parent_body: PhysicsBody2D = null
  85. if not _world_attachement:
  86. parent_body = create_rigidbody_box(BOX_SIZE, true, true)
  87. if _dynamic_attachement:
  88. parent_body.gravity_scale = 0.0
  89. child_body.gravity_scale = 0.0
  90. else:
  91. parent_body.freeze = true
  92. if _change_positions:
  93. root.add_child(parent_body)
  94. parent_body.position = Vector2(0.0, -40)
  95. else:
  96. parent_body.position = Vector2(0.0, -40)
  97. root.add_child(parent_body)
  98. var joint := _selected_joint.duplicate()
  99. joint.visible = true
  100. joint.disable_collision = not _bodies_collide
  101. root.add_child(joint)
  102. if parent_body:
  103. joint.set_node_a(joint.get_path_to(parent_body))
  104. joint.set_node_b(joint.get_path_to(child_body))
  105. if _destroy_body:
  106. await start_timer(0.5).timeout
  107. if is_timer_canceled():
  108. return
  109. child_body.queue_free()