test_character.gd 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. extends Test
  2. class_name TestCharacter
  3. enum E_BodyType {
  4. RIGID_BODY,
  5. KINEMATIC_BODY,
  6. KINEMATIC_BODY_RAY_SHAPE,
  7. }
  8. const OPTION_OBJECT_TYPE_RIGIDBODY = "Object type/Rigid body (1)"
  9. const OPTION_OBJECT_TYPE_KINEMATIC = "Object type/Kinematic body (2)"
  10. const OPTION_OBJECT_TYPE_KINEMATIC_RAYSHAPE = "Object type/Kinematic body with ray shape (3)"
  11. const OPTION_MOVE_KINEMATIC_SNAP = "Move Options/Use snap (Kinematic only)"
  12. const OPTION_MOVE_KINEMATIC_STOP_ON_SLOPE = "Move Options/Use stop on slope (Kinematic only)"
  13. export(Vector2) var _initial_velocity = Vector2.ZERO
  14. export(Vector2) var _constant_velocity = Vector2.ZERO
  15. export(float) var _motion_speed = 400.0
  16. export(float) var _gravity_force = 50.0
  17. export(float) var _jump_force = 1000.0
  18. export(float) var _snap_distance = 0.0
  19. export(float) var _floor_max_angle = 45.0
  20. export(E_BodyType) var _body_type = 0
  21. onready var options = $Options
  22. var _use_snap = true
  23. var _use_stop_on_slope = true
  24. var _body_parent = null
  25. var _rigid_body_template = null
  26. var _kinematic_body_template = null
  27. var _kinematic_body_ray_template = null
  28. var _moving_body = null
  29. func _ready():
  30. options.connect("option_selected", self, "_on_option_selected")
  31. options.connect("option_changed", self, "_on_option_changed")
  32. _rigid_body_template = find_node("RigidBody2D")
  33. if _rigid_body_template:
  34. _body_parent = _rigid_body_template.get_parent()
  35. _body_parent.remove_child(_rigid_body_template)
  36. var enabled = _body_type == E_BodyType.RIGID_BODY
  37. options.add_menu_item(OPTION_OBJECT_TYPE_RIGIDBODY, true, enabled, true)
  38. _kinematic_body_template = find_node("KinematicBody2D")
  39. if _kinematic_body_template:
  40. _body_parent = _kinematic_body_template.get_parent()
  41. _body_parent.remove_child(_kinematic_body_template)
  42. var enabled = _body_type == E_BodyType.KINEMATIC_BODY
  43. options.add_menu_item(OPTION_OBJECT_TYPE_KINEMATIC, true, enabled, true)
  44. _kinematic_body_ray_template = find_node("KinematicBodyRay2D")
  45. if _kinematic_body_ray_template:
  46. _body_parent = _kinematic_body_ray_template.get_parent()
  47. _body_parent.remove_child(_kinematic_body_ray_template)
  48. var enabled = _body_type == E_BodyType.KINEMATIC_BODY_RAY_SHAPE
  49. options.add_menu_item(OPTION_OBJECT_TYPE_KINEMATIC_RAYSHAPE, true, enabled, true)
  50. options.add_menu_item(OPTION_MOVE_KINEMATIC_SNAP, true, _use_snap)
  51. options.add_menu_item(OPTION_MOVE_KINEMATIC_STOP_ON_SLOPE, true, _use_stop_on_slope)
  52. _start_test()
  53. func _process(_delta):
  54. var label_floor = $LabelFloor
  55. if _moving_body:
  56. if _moving_body.is_on_floor():
  57. label_floor.text = "ON FLOOR"
  58. label_floor.self_modulate = Color.green
  59. else:
  60. label_floor.text = "OFF FLOOR"
  61. label_floor.self_modulate = Color.red
  62. else:
  63. label_floor.visible = false
  64. func _input(event):
  65. var key_event = event as InputEventKey
  66. if key_event and not key_event.pressed:
  67. if key_event.scancode == KEY_1:
  68. if _rigid_body_template:
  69. _on_option_selected(OPTION_OBJECT_TYPE_RIGIDBODY)
  70. elif key_event.scancode == KEY_2:
  71. if _kinematic_body_template:
  72. _on_option_selected(OPTION_OBJECT_TYPE_KINEMATIC)
  73. elif key_event.scancode == KEY_3:
  74. if _kinematic_body_ray_template:
  75. _on_option_selected(OPTION_OBJECT_TYPE_KINEMATIC_RAYSHAPE)
  76. func _exit_tree():
  77. if _rigid_body_template:
  78. _rigid_body_template.free()
  79. if _kinematic_body_template:
  80. _kinematic_body_template.free()
  81. if _kinematic_body_ray_template:
  82. _kinematic_body_ray_template.free()
  83. func _on_option_selected(option):
  84. match option:
  85. OPTION_OBJECT_TYPE_RIGIDBODY:
  86. _body_type = E_BodyType.RIGID_BODY
  87. _start_test()
  88. OPTION_OBJECT_TYPE_KINEMATIC:
  89. _body_type = E_BodyType.KINEMATIC_BODY
  90. _start_test()
  91. OPTION_OBJECT_TYPE_KINEMATIC_RAYSHAPE:
  92. _body_type = E_BodyType.KINEMATIC_BODY_RAY_SHAPE
  93. _start_test()
  94. func _on_option_changed(option, checked):
  95. match option:
  96. OPTION_MOVE_KINEMATIC_SNAP:
  97. _use_snap = checked
  98. _start_test()
  99. OPTION_MOVE_KINEMATIC_STOP_ON_SLOPE:
  100. _use_stop_on_slope = checked
  101. _start_test()
  102. func _start_test():
  103. cancel_timer()
  104. if _moving_body:
  105. _body_parent.remove_child(_moving_body)
  106. _moving_body.queue_free()
  107. _moving_body = null
  108. var test_label = "Testing: "
  109. var template = null
  110. match _body_type:
  111. E_BodyType.RIGID_BODY:
  112. template = _rigid_body_template
  113. E_BodyType.KINEMATIC_BODY:
  114. template = _kinematic_body_template
  115. E_BodyType.KINEMATIC_BODY_RAY_SHAPE:
  116. template = _kinematic_body_ray_template
  117. test_label += template.name
  118. _moving_body = template.duplicate()
  119. _body_parent.add_child(_moving_body)
  120. _moving_body._initial_velocity = _initial_velocity
  121. _moving_body._constant_velocity = _constant_velocity
  122. _moving_body._motion_speed = _motion_speed
  123. _moving_body._gravity_force = _gravity_force
  124. _moving_body._jump_force = _jump_force
  125. if _moving_body is KinematicBody2D:
  126. if _use_snap:
  127. _moving_body._snap = Vector2(0, _snap_distance)
  128. _moving_body._stop_on_slope = _use_stop_on_slope
  129. _moving_body._floor_max_angle = _floor_max_angle
  130. $LabelTestType.text = test_label