test_character_pixels.gd 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. extends TestCharacter
  2. const OPTION_TEST_CASE_ALL = "Test Cases/TEST ALL (0)"
  3. const OPTION_TEST_CASE_DETECT_FLOOR_NO_SNAP = "Test Cases/Floor detection (Kinematic Body)"
  4. const OPTION_TEST_CASE_DETECT_FLOOR_MOTION_CHANGES = "Test Cases/Floor detection with motion changes (Kinematic Body)"
  5. const MOTION_CHANGES_DIR = Vector2(1.0, 1.0)
  6. const MOTION_CHANGES_SPEEDS = [0.5, 1.0, 2.0, 5.0, 10.0, 20.0, 50.0]
  7. var _test_floor_detection = false
  8. var _test_motion_changes = false
  9. var _floor_detected = false
  10. var _floor_lost = false
  11. var _failed_reason = ""
  12. func _ready():
  13. options.add_menu_item(OPTION_TEST_CASE_ALL)
  14. options.add_menu_item(OPTION_TEST_CASE_DETECT_FLOOR_NO_SNAP)
  15. options.add_menu_item(OPTION_TEST_CASE_DETECT_FLOOR_MOTION_CHANGES)
  16. func _physics_process(_delta):
  17. if _moving_body:
  18. if _moving_body.is_on_floor():
  19. _floor_detected = true
  20. elif _floor_detected:
  21. _floor_lost = true
  22. if _test_motion_changes:
  23. Log.print_log("Floor lost.")
  24. if _test_motion_changes:
  25. var speed_count = MOTION_CHANGES_SPEEDS.size()
  26. var speed_index = randi() % speed_count
  27. var speed = MOTION_CHANGES_SPEEDS[speed_index]
  28. var velocity = speed * MOTION_CHANGES_DIR
  29. _moving_body._constant_velocity = velocity
  30. #Log.print_log("Velocity: %s" % velocity)
  31. func _input(event):
  32. var key_event = event as InputEventKey
  33. if key_event and not key_event.pressed:
  34. if key_event.scancode == KEY_0:
  35. _on_option_selected(OPTION_TEST_CASE_ALL)
  36. func _on_option_selected(option):
  37. match option:
  38. OPTION_TEST_CASE_ALL:
  39. _test_all()
  40. OPTION_TEST_CASE_DETECT_FLOOR_NO_SNAP:
  41. _start_test_case(option)
  42. return
  43. OPTION_TEST_CASE_DETECT_FLOOR_MOTION_CHANGES:
  44. _start_test_case(option)
  45. return
  46. ._on_option_selected(option)
  47. func _start_test_case(option):
  48. Log.print_log("* Starting " + option)
  49. match option:
  50. OPTION_TEST_CASE_DETECT_FLOOR_NO_SNAP:
  51. _test_floor_detection = true
  52. _test_motion_changes = false
  53. _use_snap = false
  54. _body_type = E_BodyType.KINEMATIC_BODY
  55. _start_test()
  56. yield(start_timer(1.0), "timeout")
  57. if is_timer_canceled():
  58. return
  59. _set_result(not _floor_lost)
  60. OPTION_TEST_CASE_DETECT_FLOOR_MOTION_CHANGES:
  61. _test_floor_detection = true
  62. _test_motion_changes = true
  63. _use_snap = false
  64. _body_type = E_BodyType.KINEMATIC_BODY
  65. _start_test()
  66. yield(start_timer(4.0), "timeout")
  67. if is_timer_canceled():
  68. _test_motion_changes = false
  69. return
  70. _test_motion_changes = false
  71. _moving_body._constant_velocity = Vector2.ZERO
  72. _set_result(not _floor_lost)
  73. _:
  74. Log.print_error("Invalid test case.")
  75. func _test_all():
  76. Log.print_log("* TESTING ALL...")
  77. # Test floor detection with no snapping.
  78. yield(_start_test_case(OPTION_TEST_CASE_DETECT_FLOOR_NO_SNAP), "completed")
  79. if is_timer_canceled():
  80. return
  81. # Test floor detection with no snapping.
  82. # In this test case, motion alternates different speeds.
  83. yield(_start_test_case(OPTION_TEST_CASE_DETECT_FLOOR_MOTION_CHANGES), "completed")
  84. if is_timer_canceled():
  85. return
  86. Log.print_log("* Done.")
  87. func _set_result(test_passed):
  88. var result = ""
  89. if test_passed:
  90. result = "PASSED"
  91. else:
  92. result = "FAILED"
  93. if not test_passed and not _failed_reason.empty():
  94. result += _failed_reason
  95. else:
  96. result += "."
  97. Log.print_log("Test %s" % result)
  98. func _start_test():
  99. ._start_test()
  100. _failed_reason = ""
  101. _floor_detected = false
  102. _floor_lost = false
  103. if _test_floor_detection:
  104. _failed_reason = ": floor was not detected consistently."
  105. if _test_motion_changes:
  106. # Always use the same seed for reproducible results.
  107. rand_seed(123456789)
  108. _moving_body._gravity_force = 0.0
  109. _moving_body._motion_speed = 0.0
  110. _moving_body._jump_force = 0.0
  111. else:
  112. _moving_body._initial_velocity = Vector2(30, 0)
  113. _test_floor_detection = false
  114. else:
  115. _test_motion_changes = false