test_character_pixels.gd 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 = [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. 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):
  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):
  34. super._input(event)
  35. var key_event = event as InputEventKey
  36. if key_event and not key_event.pressed:
  37. if key_event.keycode == KEY_0:
  38. await _on_option_selected(OPTION_TEST_CASE_ALL)
  39. func _on_option_selected(option):
  40. match option:
  41. OPTION_TEST_CASE_ALL:
  42. await _test_all()
  43. OPTION_TEST_CASE_DETECT_FLOOR_NO_SNAP:
  44. await _start_test_case(option)
  45. return
  46. OPTION_TEST_CASE_DETECT_FLOOR_MOTION_CHANGES:
  47. await _start_test_case(option)
  48. return
  49. super._on_option_selected(option)
  50. func _start_test_case(option):
  51. Log.print_log("* Starting " + option)
  52. match option:
  53. OPTION_TEST_CASE_DETECT_FLOOR_NO_SNAP:
  54. _test_floor_detection = true
  55. _test_motion_changes = false
  56. _use_snap = false
  57. _body_type = E_BodyType.CHARACTER_BODY
  58. _start_test()
  59. await start_timer(1.0).timeout
  60. if is_timer_canceled():
  61. return
  62. _set_result(not _floor_lost)
  63. OPTION_TEST_CASE_DETECT_FLOOR_MOTION_CHANGES:
  64. _test_floor_detection = true
  65. _test_motion_changes = true
  66. _use_snap = false
  67. _body_type = E_BodyType.CHARACTER_BODY
  68. _start_test()
  69. await start_timer(4.0).timeout
  70. if is_timer_canceled():
  71. _test_motion_changes = false
  72. return
  73. _test_motion_changes = false
  74. _moving_body._constant_velocity = Vector2.ZERO
  75. _set_result(not _floor_lost)
  76. _:
  77. Log.print_error("Invalid test case.")
  78. func _test_all():
  79. Log.print_log("* TESTING ALL...")
  80. # Test floor detection with no snapping.
  81. await _start_test_case(OPTION_TEST_CASE_DETECT_FLOOR_NO_SNAP)
  82. if is_timer_canceled():
  83. return
  84. # Test floor detection with no snapping.
  85. # In this test case, motion alternates different speeds.
  86. await _start_test_case(OPTION_TEST_CASE_DETECT_FLOOR_MOTION_CHANGES)
  87. if is_timer_canceled():
  88. return
  89. Log.print_log("* Done.")
  90. func _set_result(test_passed):
  91. var result = ""
  92. if test_passed:
  93. result = "PASSED"
  94. else:
  95. result = "FAILED"
  96. if not test_passed and not _failed_reason.is_empty():
  97. result += _failed_reason
  98. else:
  99. result += "."
  100. Log.print_log("Test %s" % result)
  101. func _start_test():
  102. super._start_test()
  103. _failed_reason = ""
  104. _floor_detected = false
  105. _floor_lost = false
  106. if _test_floor_detection:
  107. _failed_reason = ": floor was not detected consistently."
  108. if _test_motion_changes:
  109. # Always use the same seed for reproducible results.
  110. seed(123456789)
  111. _moving_body._gravity_force = 0.0
  112. _moving_body._motion_speed = 0.0
  113. _moving_body._jump_force = 0.0
  114. else:
  115. _moving_body._initial_velocity = Vector2(30, 0)
  116. _test_floor_detection = false
  117. else:
  118. _test_motion_changes = false