test_character_tilemap.gd 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. extends TestCharacter
  2. const OPTION_TEST_CASE_ALL = "Test Cases/TEST ALL (0)"
  3. const OPTION_TEST_CASE_JUMP_ONE_WAY_RIGID = "Test Cases/Jump through one-way tiles (Rigid Body)"
  4. const OPTION_TEST_CASE_JUMP_ONE_WAY_CHARACTER = "Test Cases/Jump through one-way tiles (Character Body)"
  5. const OPTION_TEST_CASE_JUMP_ONE_WAY_CORNER_RIGID = "Test Cases/Jump through one-way corner (Rigid Body)"
  6. const OPTION_TEST_CASE_JUMP_ONE_WAY_CORNER_CHARACTER = "Test Cases/Jump through one-way corner (Character Body)"
  7. const OPTION_TEST_CASE_FALL_ONE_WAY_CHARACTER = "Test Cases/Fall and pushed on one-way tiles (Character Body)"
  8. var _test_jump_one_way = false
  9. var _test_jump_one_way_corner = false
  10. var _test_fall_one_way = false
  11. var _extra_body: PhysicsBody2D = null
  12. var _failed_reason = ""
  13. func _ready():
  14. super._ready()
  15. options.add_menu_item(OPTION_TEST_CASE_ALL)
  16. options.add_menu_item(OPTION_TEST_CASE_JUMP_ONE_WAY_RIGID)
  17. options.add_menu_item(OPTION_TEST_CASE_JUMP_ONE_WAY_CHARACTER)
  18. options.add_menu_item(OPTION_TEST_CASE_JUMP_ONE_WAY_CORNER_RIGID)
  19. options.add_menu_item(OPTION_TEST_CASE_JUMP_ONE_WAY_CORNER_CHARACTER)
  20. options.add_menu_item(OPTION_TEST_CASE_FALL_ONE_WAY_CHARACTER)
  21. func _input(event):
  22. super._input(event)
  23. var key_event = event as InputEventKey
  24. if key_event and not key_event.pressed:
  25. if key_event.keycode == KEY_0:
  26. await _on_option_selected(OPTION_TEST_CASE_ALL)
  27. func _on_option_selected(option):
  28. match option:
  29. OPTION_TEST_CASE_ALL:
  30. await _test_all()
  31. OPTION_TEST_CASE_JUMP_ONE_WAY_RIGID:
  32. await _start_test_case(option)
  33. return
  34. OPTION_TEST_CASE_JUMP_ONE_WAY_CHARACTER:
  35. await _start_test_case(option)
  36. return
  37. OPTION_TEST_CASE_JUMP_ONE_WAY_CORNER_RIGID:
  38. await _start_test_case(option)
  39. return
  40. OPTION_TEST_CASE_JUMP_ONE_WAY_CORNER_CHARACTER:
  41. await _start_test_case(option)
  42. return
  43. OPTION_TEST_CASE_FALL_ONE_WAY_CHARACTER:
  44. await _start_test_case(option)
  45. return
  46. super._on_option_selected(option)
  47. func _start_test_case(option):
  48. Log.print_log("* Starting " + option)
  49. match option:
  50. OPTION_TEST_CASE_JUMP_ONE_WAY_RIGID:
  51. _body_type = E_BodyType.RIGID_BODY
  52. _test_jump_one_way_corner = false
  53. await _start_jump_one_way()
  54. OPTION_TEST_CASE_JUMP_ONE_WAY_CHARACTER:
  55. _body_type = E_BodyType.CHARACTER_BODY
  56. _test_jump_one_way_corner = false
  57. await _start_jump_one_way()
  58. OPTION_TEST_CASE_JUMP_ONE_WAY_CORNER_RIGID:
  59. _body_type = E_BodyType.RIGID_BODY
  60. _test_jump_one_way_corner = true
  61. await _start_jump_one_way()
  62. OPTION_TEST_CASE_JUMP_ONE_WAY_CORNER_CHARACTER:
  63. _body_type = E_BodyType.CHARACTER_BODY
  64. _test_jump_one_way_corner = true
  65. await _start_jump_one_way()
  66. OPTION_TEST_CASE_FALL_ONE_WAY_CHARACTER:
  67. _body_type = E_BodyType.CHARACTER_BODY
  68. await _start_fall_one_way()
  69. _:
  70. Log.print_error("Invalid test case.")
  71. func _test_all():
  72. Log.print_log("* TESTING ALL...")
  73. # RigidBody tests.
  74. await _start_test_case(OPTION_TEST_CASE_JUMP_ONE_WAY_RIGID)
  75. if is_timer_canceled():
  76. return
  77. await _start_test_case(OPTION_TEST_CASE_JUMP_ONE_WAY_CORNER_RIGID)
  78. if is_timer_canceled():
  79. return
  80. # CharacterBody tests.
  81. await _start_test_case(OPTION_TEST_CASE_JUMP_ONE_WAY_CHARACTER)
  82. if is_timer_canceled():
  83. return
  84. await _start_test_case(OPTION_TEST_CASE_JUMP_ONE_WAY_CORNER_CHARACTER)
  85. if is_timer_canceled():
  86. return
  87. await _start_test_case(OPTION_TEST_CASE_FALL_ONE_WAY_CHARACTER)
  88. if is_timer_canceled():
  89. return
  90. Log.print_log("* Done.")
  91. func _set_result(test_passed):
  92. var result = ""
  93. if test_passed:
  94. result = "PASSED"
  95. else:
  96. result = "FAILED"
  97. if not test_passed and not _failed_reason.is_empty():
  98. result += _failed_reason
  99. else:
  100. result += "."
  101. Log.print_log("Test %s" % result)
  102. func _start_test():
  103. if _extra_body:
  104. _body_parent.remove_child(_extra_body)
  105. _extra_body.queue_free()
  106. _extra_body = null
  107. super._start_test()
  108. if _test_jump_one_way:
  109. _test_jump_one_way = false
  110. _moving_body._initial_velocity = Vector2(600, -1000)
  111. if _test_jump_one_way_corner:
  112. _moving_body.position.x = 147.0
  113. $JumpTargetArea2D.visible = true
  114. $JumpTargetArea2D/CollisionShape2D.disabled = false
  115. if _test_fall_one_way:
  116. _test_fall_one_way = false
  117. _moving_body.position.y = 350.0
  118. _moving_body._gravity_force = 100.0
  119. _moving_body._motion_speed = 0.0
  120. _moving_body._jump_force = 0.0
  121. _extra_body = _moving_body.duplicate()
  122. _extra_body._gravity_force = 100.0
  123. _extra_body._motion_speed = 0.0
  124. _extra_body._jump_force = 0.0
  125. _extra_body.position -= Vector2(0.0, 200.0)
  126. _body_parent.add_child(_extra_body)
  127. $FallTargetArea2D.visible = true
  128. $FallTargetArea2D/CollisionShape2D.disabled = false
  129. func _start_jump_one_way():
  130. _test_jump_one_way = true
  131. _start_test()
  132. await start_timer(1.5).timeout
  133. if is_timer_canceled():
  134. return
  135. _finalize_jump_one_way()
  136. func _start_fall_one_way():
  137. _test_fall_one_way = true
  138. _start_test()
  139. await start_timer(1.0).timeout
  140. if is_timer_canceled():
  141. return
  142. _finalize_fall_one_way()
  143. func _finalize_jump_one_way():
  144. var passed = true
  145. if not $JumpTargetArea2D.overlaps_body(_moving_body):
  146. passed = false
  147. _failed_reason = ": the body wasn't able to jump all the way through."
  148. _set_result(passed)
  149. $JumpTargetArea2D.visible = false
  150. $JumpTargetArea2D/CollisionShape2D.disabled = true
  151. func _finalize_fall_one_way():
  152. var passed = true
  153. if $FallTargetArea2D.overlaps_body(_moving_body):
  154. passed = false
  155. _failed_reason = ": the body was pushed through the one-way collision."
  156. _set_result(passed)
  157. $FallTargetArea2D.visible = false
  158. $FallTargetArea2D/CollisionShape2D.disabled = true