test_character_tilemap.gd 5.5 KB

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