test_moving_platform.gd 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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() -> void:
  27. var options: OptionMenu = $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(_on_option_selected)
  47. options.option_changed.connect(_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: InputEvent) -> void:
  56. if event is InputEventKey and not event.pressed:
  57. var _index: int = event.keycode - KEY_1
  58. if _index >= 0 and _index < _key_list.size():
  59. spawn_body_index(_index)
  60. func _on_option_selected(option: String) -> void:
  61. if _body_scene.has(option):
  62. spawn_body_key(option)
  63. else:
  64. match option:
  65. SHAPE_CAPSULE:
  66. _current_shape = _shapes[SHAPE_CAPSULE]
  67. spawn_body_index(_current_body_index)
  68. SHAPE_BOX:
  69. _current_shape = _shapes[SHAPE_BOX]
  70. spawn_body_index(_current_body_index)
  71. SHAPE_CYLINDER:
  72. _current_shape = _shapes[SHAPE_CYLINDER]
  73. spawn_body_index(_current_body_index)
  74. SHAPE_SPHERE:
  75. _current_shape = _shapes[SHAPE_SPHERE]
  76. spawn_body_index(_current_body_index)
  77. SHAPE_CONVEX:
  78. _current_shape = _shapes[SHAPE_CONVEX]
  79. spawn_body_index(_current_body_index)
  80. func _on_option_changed(option: String, checked: bool) -> void:
  81. match option:
  82. OPTION_SLOPE:
  83. _slope = checked
  84. spawn_body_index(_current_body_index)
  85. OPTION_SNAP:
  86. _snap = checked
  87. spawn_body_index(_current_body_index)
  88. OPTION_FRICTION:
  89. _friction = checked
  90. spawn_body_index(_current_body_index)
  91. OPTION_ROUGH:
  92. _rough = checked
  93. spawn_body_index(_current_body_index)
  94. OPTION_PROCESS_PHYSICS:
  95. _animation_physics = checked
  96. spawn_body_index(_current_body_index)
  97. func spawn_body_index(body_index: int) -> void:
  98. if _current_body:
  99. _current_body.queue_free()
  100. _current_body_index = body_index
  101. _current_body_key = _key_list[body_index]
  102. var body_parent := $Bodies
  103. var body: PhysicsBody3D = _body_scene[_key_list[body_index]].instantiate()
  104. _current_body = body
  105. init_body()
  106. body_parent.add_child(body)
  107. start_test()
  108. func spawn_body_key(body_key: String) -> void:
  109. if _current_body:
  110. _current_body.queue_free()
  111. _current_body_key = body_key
  112. _current_body_index = _key_list.find(body_key)
  113. var body_parent := $Bodies
  114. var body: PhysicsBody3D = _body_scene[body_key].instantiate()
  115. _current_body = body
  116. init_body()
  117. body_parent.add_child(body)
  118. start_test()
  119. func init_body() -> void:
  120. if _current_body is CharacterBody3D:
  121. _current_body._stop_on_slopes = _slope
  122. _current_body.use_snap = _snap
  123. elif _current_body is RigidBody3D:
  124. _current_body.physics_material_override.rough = _rough
  125. _current_body.physics_material_override.friction = 1.0 if _friction else 0.0
  126. for shape in _current_body.get_children():
  127. if shape is CollisionShape3D:
  128. if shape.name != _current_shape:
  129. shape.queue_free()
  130. func start_test() -> void:
  131. var animation_player: AnimationPlayer = $Platforms/MovingPlatform/AnimationPlayer
  132. animation_player.stop()
  133. if _animation_physics:
  134. animation_player.playback_process_mode = AnimationPlayer.ANIMATION_PROCESS_PHYSICS
  135. else:
  136. animation_player.playback_process_mode = AnimationPlayer.ANIMATION_PROCESS_IDLE
  137. animation_player.play("Move")
  138. $LabelBodyType.text = "Body Type: " + _body_type[_current_body_index] + " \nCollision Shape: " + _current_shape
  139. func get_packed_scene(node: Node) -> PackedScene:
  140. for child in node.get_children():
  141. child.owner = node
  142. var packed_scene := PackedScene.new()
  143. packed_scene.pack(node)
  144. return packed_scene