test_character_pixels.gd 3.9 KB

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