123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- extends TestCharacter
- const OPTION_TEST_CASE_ALL = "Test Cases/TEST ALL (0)"
- const OPTION_TEST_CASE_JUMP_ONE_WAY_RIGID = "Test Cases/Jump through one-way tiles (Rigid Body)"
- const OPTION_TEST_CASE_JUMP_ONE_WAY_CHARACTER = "Test Cases/Jump through one-way tiles (Character Body)"
- const OPTION_TEST_CASE_JUMP_ONE_WAY_CORNER_RIGID = "Test Cases/Jump through one-way corner (Rigid Body)"
- const OPTION_TEST_CASE_JUMP_ONE_WAY_CORNER_CHARACTER = "Test Cases/Jump through one-way corner (Character Body)"
- const OPTION_TEST_CASE_FALL_ONE_WAY_CHARACTER = "Test Cases/Fall and pushed on one-way tiles (Character Body)"
- var _test_jump_one_way = false
- var _test_jump_one_way_corner = false
- var _test_fall_one_way = false
- var _extra_body: PhysicsBody2D = null
- var _failed_reason = ""
- func _ready():
- super._ready()
- options.add_menu_item(OPTION_TEST_CASE_ALL)
- options.add_menu_item(OPTION_TEST_CASE_JUMP_ONE_WAY_RIGID)
- options.add_menu_item(OPTION_TEST_CASE_JUMP_ONE_WAY_CHARACTER)
- options.add_menu_item(OPTION_TEST_CASE_JUMP_ONE_WAY_CORNER_RIGID)
- options.add_menu_item(OPTION_TEST_CASE_JUMP_ONE_WAY_CORNER_CHARACTER)
- options.add_menu_item(OPTION_TEST_CASE_FALL_ONE_WAY_CHARACTER)
- func _input(event):
- super._input(event)
- var key_event = event as InputEventKey
- if key_event and not key_event.pressed:
- if key_event.keycode == KEY_0:
- await _on_option_selected(OPTION_TEST_CASE_ALL)
- func _on_option_selected(option):
- match option:
- OPTION_TEST_CASE_ALL:
- await _test_all()
- OPTION_TEST_CASE_JUMP_ONE_WAY_RIGID:
- await _start_test_case(option)
- return
- OPTION_TEST_CASE_JUMP_ONE_WAY_CHARACTER:
- await _start_test_case(option)
- return
- OPTION_TEST_CASE_JUMP_ONE_WAY_CORNER_RIGID:
- await _start_test_case(option)
- return
- OPTION_TEST_CASE_JUMP_ONE_WAY_CORNER_CHARACTER:
- await _start_test_case(option)
- return
- OPTION_TEST_CASE_FALL_ONE_WAY_CHARACTER:
- await _start_test_case(option)
- return
- super._on_option_selected(option)
- func _start_test_case(option):
- Log.print_log("* Starting " + option)
- match option:
- OPTION_TEST_CASE_JUMP_ONE_WAY_RIGID:
- _body_type = E_BodyType.RIGID_BODY
- _test_jump_one_way_corner = false
- await _start_jump_one_way()
- OPTION_TEST_CASE_JUMP_ONE_WAY_CHARACTER:
- _body_type = E_BodyType.CHARACTER_BODY
- _test_jump_one_way_corner = false
- await _start_jump_one_way()
- OPTION_TEST_CASE_JUMP_ONE_WAY_CORNER_RIGID:
- _body_type = E_BodyType.RIGID_BODY
- _test_jump_one_way_corner = true
- await _start_jump_one_way()
- OPTION_TEST_CASE_JUMP_ONE_WAY_CORNER_CHARACTER:
- _body_type = E_BodyType.CHARACTER_BODY
- _test_jump_one_way_corner = true
- await _start_jump_one_way()
- OPTION_TEST_CASE_FALL_ONE_WAY_CHARACTER:
- _body_type = E_BodyType.CHARACTER_BODY
- await _start_fall_one_way()
- _:
- Log.print_error("Invalid test case.")
- func _test_all():
- Log.print_log("* TESTING ALL...")
- # RigidBody tests.
- await _start_test_case(OPTION_TEST_CASE_JUMP_ONE_WAY_RIGID)
- if is_timer_canceled():
- return
- await _start_test_case(OPTION_TEST_CASE_JUMP_ONE_WAY_CORNER_RIGID)
- if is_timer_canceled():
- return
- # CharacterBody tests.
- await _start_test_case(OPTION_TEST_CASE_JUMP_ONE_WAY_CHARACTER)
- if is_timer_canceled():
- return
- await _start_test_case(OPTION_TEST_CASE_JUMP_ONE_WAY_CORNER_CHARACTER)
- if is_timer_canceled():
- return
- await _start_test_case(OPTION_TEST_CASE_FALL_ONE_WAY_CHARACTER)
- if is_timer_canceled():
- return
- Log.print_log("* Done.")
- func _set_result(test_passed):
- var result = ""
- if test_passed:
- result = "PASSED"
- else:
- result = "FAILED"
- if not test_passed and not _failed_reason.is_empty():
- result += _failed_reason
- else:
- result += "."
- Log.print_log("Test %s" % result)
- func _start_test():
- if _extra_body:
- _body_parent.remove_child(_extra_body)
- _extra_body.queue_free()
- _extra_body = null
- super._start_test()
- if _test_jump_one_way:
- _test_jump_one_way = false
- _moving_body._initial_velocity = Vector2(600, -1000)
- if _test_jump_one_way_corner:
- _moving_body.position.x = 147.0
- $JumpTargetArea2D.visible = true
- $JumpTargetArea2D/CollisionShape2D.disabled = false
- if _test_fall_one_way:
- _test_fall_one_way = false
- _moving_body.position.y = 350.0
- _moving_body._gravity_force = 100.0
- _moving_body._motion_speed = 0.0
- _moving_body._jump_force = 0.0
- _extra_body = _moving_body.duplicate()
- _extra_body._gravity_force = 100.0
- _extra_body._motion_speed = 0.0
- _extra_body._jump_force = 0.0
- _extra_body.position -= Vector2(0.0, 200.0)
- _body_parent.add_child(_extra_body)
- $FallTargetArea2D.visible = true
- $FallTargetArea2D/CollisionShape2D.disabled = false
- func _start_jump_one_way():
- _test_jump_one_way = true
- _start_test()
- await start_timer(1.5).timeout
- if is_timer_canceled():
- return
- _finalize_jump_one_way()
- func _start_fall_one_way():
- _test_fall_one_way = true
- _start_test()
- await start_timer(1.0).timeout
- if is_timer_canceled():
- return
- _finalize_fall_one_way()
- func _finalize_jump_one_way():
- var passed = true
- if not $JumpTargetArea2D.overlaps_body(_moving_body):
- passed = false
- _failed_reason = ": the body wasn't able to jump all the way through."
- _set_result(passed)
- $JumpTargetArea2D.visible = false
- $JumpTargetArea2D/CollisionShape2D.disabled = true
- func _finalize_fall_one_way():
- var passed = true
- if $FallTargetArea2D.overlaps_body(_moving_body):
- passed = false
- _failed_reason = ": the body was pushed through the one-way collision."
- _set_result(passed)
- $FallTargetArea2D.visible = false
- $FallTargetArea2D/CollisionShape2D.disabled = true
|