test_moving_platform.gd 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. extends Test
  2. const OPTION_BODY_TYPE = "Body Type/%s (%d)"
  3. const OPTION_SLOPE = "Physics options/Stop on slope (Character only)"
  4. const OPTION_SNAP = "Physics options/Use snap (Character only)"
  5. const OPTION_FRICTION = "Physics options/Friction (Rigid only)"
  6. const OPTION_ROUGH = "Physics options/Rough (Rigid only)"
  7. const OPTION_PROCESS_PHYSICS = "Physics options/AnimationPlayer physics process mode"
  8. const SHAPE_CAPSULE = "Collision shapes/Capsule"
  9. const SHAPE_BOX = "Collision shapes/Box"
  10. const SHAPE_CYLINDER = "Collision shapes/Cylinder"
  11. const SHAPE_SPHERE = "Collision shapes/Sphere"
  12. const SHAPE_CONVEX = "Collision shapes/Convex"
  13. var _slope = false
  14. var _snap = false
  15. var _friction = false
  16. var _rough = false
  17. var _animation_physics = false
  18. var _body_scene = {}
  19. var _key_list = []
  20. var _current_body_index = 0
  21. var _current_body_key = ""
  22. var _current_body: PhysicsBody3D = null
  23. var _body_type = ["CharacterBody3D", "RigidBody"]
  24. var _shapes = {}
  25. var _current_shape = ""
  26. func _ready():
  27. var options = $Options
  28. var bodies = $Bodies.get_children()
  29. for i in bodies.size():
  30. var body = bodies[i]
  31. var option_name = OPTION_BODY_TYPE % [body.name, i + 1]
  32. options.add_menu_item(option_name)
  33. _key_list.append(option_name)
  34. _body_scene[option_name] = get_packed_scene(body)
  35. body.queue_free()
  36. options.add_menu_item(SHAPE_CAPSULE)
  37. options.add_menu_item(SHAPE_BOX)
  38. options.add_menu_item(SHAPE_CYLINDER)
  39. options.add_menu_item(SHAPE_SPHERE)
  40. options.add_menu_item(SHAPE_CONVEX)
  41. options.add_menu_item(OPTION_SLOPE, true, false)
  42. options.add_menu_item(OPTION_SNAP, true, false)
  43. options.add_menu_item(OPTION_FRICTION, true, false)
  44. options.add_menu_item(OPTION_ROUGH, true, false)
  45. options.add_menu_item(OPTION_PROCESS_PHYSICS, true, false)
  46. options.option_selected.connect(self._on_option_selected)
  47. options.option_changed.connect(self._on_option_changed)
  48. _shapes[SHAPE_CAPSULE] = "Capsule"
  49. _shapes[SHAPE_BOX] = "Box"
  50. _shapes[SHAPE_CYLINDER] = "Cylinder"
  51. _shapes[SHAPE_SPHERE] = "Sphere"
  52. _shapes[SHAPE_CONVEX] = "Convex"
  53. _current_shape = _shapes[SHAPE_CAPSULE]
  54. spawn_body_index(_current_body_index)
  55. func _input(event):
  56. var key_event = event as InputEventKey
  57. if key_event and not key_event.pressed:
  58. var _index = key_event.keycode - KEY_1
  59. if _index >= 0 and _index < _key_list.size():
  60. spawn_body_index(_index)
  61. func _on_option_selected(option):
  62. if _body_scene.has(option):
  63. spawn_body_key(option)
  64. else:
  65. match option:
  66. SHAPE_CAPSULE:
  67. _current_shape = _shapes[SHAPE_CAPSULE]
  68. spawn_body_index(_current_body_index)
  69. SHAPE_BOX:
  70. _current_shape = _shapes[SHAPE_BOX]
  71. spawn_body_index(_current_body_index)
  72. SHAPE_CYLINDER:
  73. _current_shape = _shapes[SHAPE_CYLINDER]
  74. spawn_body_index(_current_body_index)
  75. SHAPE_SPHERE:
  76. _current_shape = _shapes[SHAPE_SPHERE]
  77. spawn_body_index(_current_body_index)
  78. SHAPE_CONVEX:
  79. _current_shape = _shapes[SHAPE_CONVEX]
  80. spawn_body_index(_current_body_index)
  81. func _on_option_changed(option, checked):
  82. match option:
  83. OPTION_SLOPE:
  84. _slope = checked
  85. spawn_body_index(_current_body_index)
  86. OPTION_SNAP:
  87. _snap = checked
  88. spawn_body_index(_current_body_index)
  89. OPTION_FRICTION:
  90. _friction = checked
  91. spawn_body_index(_current_body_index)
  92. OPTION_ROUGH:
  93. _rough = checked
  94. spawn_body_index(_current_body_index)
  95. OPTION_PROCESS_PHYSICS:
  96. _animation_physics = checked
  97. spawn_body_index(_current_body_index)
  98. func spawn_body_index(body_index):
  99. if _current_body:
  100. _current_body.queue_free()
  101. _current_body_index = body_index
  102. _current_body_key = _key_list[body_index]
  103. var body_parent = $Bodies
  104. var body = _body_scene[_key_list[body_index]].instantiate()
  105. _current_body = body
  106. init_body()
  107. body_parent.add_child(body)
  108. start_test()
  109. func spawn_body_key(body_key):
  110. if _current_body:
  111. _current_body.queue_free()
  112. _current_body_key = body_key
  113. _current_body_index = _key_list.find(body_key)
  114. var body_parent = $Bodies
  115. var body = _body_scene[body_key].instantiate()
  116. _current_body = body
  117. init_body()
  118. body_parent.add_child(body)
  119. start_test()
  120. func init_body():
  121. if _current_body is CharacterBody3D:
  122. _current_body._stop_on_slopes = _slope
  123. _current_body.use_snap = _snap
  124. elif _current_body is RigidBody3D:
  125. _current_body.physics_material_override.rough = _rough
  126. _current_body.physics_material_override.friction = 1.0 if _friction else 0.0
  127. for shape in _current_body.get_children():
  128. if shape is CollisionShape3D:
  129. if shape.name != _current_shape:
  130. shape.queue_free()
  131. func start_test():
  132. var animation_player = $Platforms/MovingPlatform/AnimationPlayer
  133. animation_player.stop()
  134. if _animation_physics:
  135. animation_player.playback_process_mode = AnimationPlayer.ANIMATION_PROCESS_PHYSICS
  136. else:
  137. animation_player.playback_process_mode = AnimationPlayer.ANIMATION_PROCESS_IDLE
  138. animation_player.play("Move")
  139. $LabelBodyType.text = "Body Type: " + _body_type[_current_body_index] + " \nCollision Shape: " + _current_shape
  140. func get_packed_scene(node):
  141. for child in node.get_children():
  142. child.owner = node
  143. var packed_scene = PackedScene.new()
  144. packed_scene.pack(node)
  145. return packed_scene