test_character.gd 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. extends Test
  2. class_name TestCharacter
  3. enum E_BodyType {
  4. CHARACTER_BODY,
  5. CHARACTER_BODY_RAY,
  6. RIGID_BODY,
  7. RIGID_BODY_RAY,
  8. }
  9. const OPTION_OBJECT_TYPE_CHARACTER = "Object type/Character body (1)"
  10. const OPTION_OBJECT_TYPE_CHARACTER_RAY = "Object type/Character body with ray (2)"
  11. const OPTION_OBJECT_TYPE_RIGID_BODY = "Object type/Rigid body (3)"
  12. const OPTION_OBJECT_TYPE_RIGID_BODY_RAY = "Object type/Rigid body with ray (4)"
  13. const OPTION_MOVE_CHARACTER_SNAP = "Move Options/Use snap (Character only)"
  14. const OPTION_MOVE_CHARACTER_STOP_ON_SLOPE = "Move Options/Use stop on slope (Character only)"
  15. const OPTION_MOVE_CHARACTER_FLOOR_ONLY = "Move Options/Move on floor only (Character only)"
  16. const OPTION_MOVE_CHARACTER_CONSTANT_SPEED = "Move Options/Use constant speed (Character only)"
  17. @export var _initial_velocity = Vector2.ZERO
  18. @export var _constant_velocity = Vector2.ZERO
  19. @export var _motion_speed = 400.0
  20. @export var _gravity_force = 50.0
  21. @export var _jump_force = 1000.0
  22. @export var _snap_distance = 0.0
  23. @export var _floor_max_angle = 45.0
  24. @export var _body_type: E_BodyType = 0
  25. @onready var options = $Options
  26. var _use_snap = true
  27. var _use_stop_on_slope = true
  28. var _use_floor_only = true
  29. var _use_constant_speed = false
  30. var _body_parent: Node = null
  31. var _character_body_template = null
  32. var _character_body_ray_template = null
  33. var _rigid_body_template = null
  34. var _rigid_body_ray_template = null
  35. var _moving_body: PhysicsBody2D = null
  36. func _ready():
  37. options.option_selected.connect(self._on_option_selected)
  38. options.option_changed.connect(self._on_option_changed)
  39. _character_body_template = find_child("CharacterBody2D")
  40. if _character_body_template:
  41. _body_parent = _character_body_template.get_parent()
  42. _body_parent.remove_child(_character_body_template)
  43. var enabled = _body_type == E_BodyType.CHARACTER_BODY
  44. options.add_menu_item(OPTION_OBJECT_TYPE_CHARACTER, true, enabled, true)
  45. _character_body_ray_template = find_child("CharacterBodyRay2D")
  46. if _character_body_ray_template:
  47. _body_parent = _character_body_ray_template.get_parent()
  48. _body_parent.remove_child(_character_body_ray_template)
  49. var enabled = _body_type == E_BodyType.CHARACTER_BODY_RAY
  50. options.add_menu_item(OPTION_OBJECT_TYPE_CHARACTER_RAY, true, enabled, true)
  51. _rigid_body_template = find_child("RigidBody2D")
  52. if _rigid_body_template:
  53. _body_parent = _rigid_body_template.get_parent()
  54. _body_parent.remove_child(_rigid_body_template)
  55. var enabled = _body_type == E_BodyType.RIGID_BODY
  56. options.add_menu_item(OPTION_OBJECT_TYPE_RIGID_BODY, true, enabled, true)
  57. _rigid_body_ray_template = find_child("RigidBodyRay2D")
  58. if _rigid_body_ray_template:
  59. _body_parent = _rigid_body_ray_template.get_parent()
  60. _body_parent.remove_child(_rigid_body_ray_template)
  61. var enabled = _body_type == E_BodyType.RIGID_BODY_RAY
  62. options.add_menu_item(OPTION_OBJECT_TYPE_RIGID_BODY_RAY, true, enabled, true)
  63. options.add_menu_item(OPTION_MOVE_CHARACTER_SNAP, true, _use_snap)
  64. options.add_menu_item(OPTION_MOVE_CHARACTER_STOP_ON_SLOPE, true, _use_stop_on_slope)
  65. options.add_menu_item(OPTION_MOVE_CHARACTER_FLOOR_ONLY, true, _use_floor_only)
  66. options.add_menu_item(OPTION_MOVE_CHARACTER_CONSTANT_SPEED, true, _use_constant_speed)
  67. var floor_slider = find_child("FloorMaxAngle")
  68. if floor_slider:
  69. floor_slider.get_node("HSlider").value = _floor_max_angle
  70. _start_test()
  71. func _process(_delta):
  72. var label_floor = $LabelFloor
  73. if _moving_body:
  74. if _moving_body.is_on_floor():
  75. label_floor.text = "ON FLOOR"
  76. label_floor.self_modulate = Color.GREEN
  77. else:
  78. label_floor.text = "OFF FLOOR"
  79. label_floor.self_modulate = Color.RED
  80. else:
  81. label_floor.visible = false
  82. func _input(event):
  83. var key_event = event as InputEventKey
  84. if key_event and not key_event.pressed:
  85. if key_event.keycode == KEY_1:
  86. if _character_body_template:
  87. _on_option_selected(OPTION_OBJECT_TYPE_CHARACTER)
  88. elif key_event.keycode == KEY_2:
  89. if _character_body_ray_template:
  90. _on_option_selected(OPTION_OBJECT_TYPE_CHARACTER_RAY)
  91. elif key_event.keycode == KEY_3:
  92. if _rigid_body_template:
  93. _on_option_selected(OPTION_OBJECT_TYPE_RIGID_BODY)
  94. elif key_event.keycode == KEY_4:
  95. if _rigid_body_ray_template:
  96. _on_option_selected(OPTION_OBJECT_TYPE_RIGID_BODY_RAY)
  97. func _exit_tree():
  98. if _character_body_template:
  99. _character_body_template.free()
  100. if _character_body_ray_template:
  101. _character_body_ray_template.free()
  102. if _rigid_body_template:
  103. _rigid_body_template.free()
  104. if _rigid_body_ray_template:
  105. _rigid_body_ray_template.free()
  106. func _on_option_selected(option):
  107. match option:
  108. OPTION_OBJECT_TYPE_CHARACTER:
  109. _body_type = E_BodyType.CHARACTER_BODY
  110. _start_test()
  111. OPTION_OBJECT_TYPE_CHARACTER_RAY:
  112. _body_type = E_BodyType.CHARACTER_BODY_RAY
  113. _start_test()
  114. OPTION_OBJECT_TYPE_RIGID_BODY:
  115. _body_type = E_BodyType.RIGID_BODY
  116. _start_test()
  117. OPTION_OBJECT_TYPE_RIGID_BODY_RAY:
  118. _body_type = E_BodyType.RIGID_BODY_RAY
  119. _start_test()
  120. func _on_option_changed(option, checked):
  121. match option:
  122. OPTION_MOVE_CHARACTER_SNAP:
  123. _use_snap = checked
  124. if _moving_body and _moving_body is CharacterBody2D:
  125. _moving_body._snap = _snap_distance if _use_snap else 0.0
  126. OPTION_MOVE_CHARACTER_STOP_ON_SLOPE:
  127. _use_stop_on_slope = checked
  128. if _moving_body and _moving_body is CharacterBody2D:
  129. _moving_body._stop_on_slope = _use_stop_on_slope
  130. OPTION_MOVE_CHARACTER_FLOOR_ONLY:
  131. _use_floor_only = checked
  132. if _moving_body and _moving_body is CharacterBody2D:
  133. _moving_body._move_on_floor_only = _use_floor_only
  134. OPTION_MOVE_CHARACTER_CONSTANT_SPEED:
  135. _use_constant_speed = checked
  136. if _moving_body and _moving_body is CharacterBody2D:
  137. _moving_body._constant_speed = _use_constant_speed
  138. func _update_floor_max_angle(value):
  139. if (value == _floor_max_angle):
  140. return
  141. _floor_max_angle = value
  142. if _moving_body and _moving_body is CharacterBody2D:
  143. _moving_body._floor_max_angle = _floor_max_angle
  144. func _start_test():
  145. cancel_timer()
  146. if _moving_body:
  147. _body_parent.remove_child(_moving_body)
  148. _moving_body.queue_free()
  149. _moving_body = null
  150. var test_label = "Testing: "
  151. var template = null
  152. match _body_type:
  153. E_BodyType.CHARACTER_BODY:
  154. template = _character_body_template
  155. E_BodyType.CHARACTER_BODY_RAY:
  156. template = _character_body_ray_template
  157. E_BodyType.RIGID_BODY:
  158. template = _rigid_body_template
  159. E_BodyType.RIGID_BODY_RAY:
  160. template = _rigid_body_ray_template
  161. test_label += String(template.name)
  162. _moving_body = template.duplicate()
  163. _body_parent.add_child(_moving_body)
  164. _moving_body._initial_velocity = _initial_velocity
  165. _moving_body._constant_velocity = _constant_velocity
  166. _moving_body._motion_speed = _motion_speed
  167. _moving_body._gravity_force = _gravity_force
  168. _moving_body._jump_force = _jump_force
  169. _moving_body._floor_max_angle = _floor_max_angle
  170. if _moving_body is CharacterBody2D:
  171. _moving_body._snap = _snap_distance if _use_snap else 0.0
  172. _moving_body._stop_on_slope = _use_stop_on_slope
  173. _moving_body._move_on_floor_only = _use_floor_only
  174. _moving_body._constant_speed = _use_constant_speed
  175. $LabelTestType.text = test_label