Open History to see the last commit
1import XCTest
2@testable import ExpeditionCore
3
4final class JumpAnimationTests: XCTestCase {
5 func actor() -> ExplorerInteraction {
6 var value = ExplorerInteraction(position: .init(x: 300, y: 0), bounds: .init(minimum: .init(x: 24, y: 0), maximum: .init(x: 976, y: 724)))
7 value.autonomous = true
8 value.updateTerrain(.init(windows: [], viewport: .init(x: 0, y: 0, width: 1000, height: 800)))
9 value.setKeyboardControl(true)
10 return value
11 }
12 func testFeetStaySupportedUntilLegPushCompletesThenLiftStarts() {
13 var value = actor()
14 value.controlJump()
15 XCTAssertEqual(value.state, .preparingJump)
16 for _ in 0..<14 {
17 value.advance(by: 1.0 / 120)
18 XCTAssertEqual(value.position.y, 0)
19 XCTAssertNotNil(value.attachment)
20 XCTAssertEqual(value.keyboardJumpPose?.leftFoot.y, 0)
21 XCTAssertEqual(value.keyboardJumpPose?.rightFoot.y, 0)
22 value.controlJump() // Repeats cannot restart the preparation.
23 }
24 value.advance(by: 1.0 / 120)
25 XCTAssertEqual(value.state, .jumping)
26 XCTAssertNil(value.attachment)
27 XCTAssertEqual(value.position.y, 0)
28 value.advance(by: 1.0 / 120)
29 XCTAssertGreaterThan(value.position.y, 2)
30 }
31 func testCountermovementExtendsLegsBeforeAirborneTuck() {
32 let crouch = JumpAnimation.preparation(at: JumpAnimation.compressionDuration)
33 let launch = JumpAnimation.preparation(at: JumpAnimation.preparationDuration)
34 XCTAssertLessThan(crouch.hips.y, ExpeditionConfiguration.standingHipHeight - 6)
35 XCTAssertGreaterThan(launch.hips.y, crouch.hips.y + 7)
36 let firstAir = JumpAnimation.airborne(elapsed: 0, verticalSpeed: JumpAnimation.launchSpeed, landingDistance: 0)
37 XCTAssertEqual(firstAir.hips, launch.hips)
38 XCTAssertEqual(firstAir.leftFoot, launch.leftFoot)
39 let apex = JumpAnimation.airborne(elapsed: 0.475, verticalSpeed: 0, landingDistance: 68)
40 XCTAssertGreaterThan(apex.leftFoot.y, launch.leftFoot.y + 5)
41 let reaching = JumpAnimation.airborne(elapsed: 0.95, verticalSpeed: -285, landingDistance: 1)
42 XCTAssertEqual(reaching.leftFoot.y, 0, accuracy: 0.01)
43 }
44 func testJumpPosesStayInsideLimbReach() {
45 for tick in 0...240 {
46 let pose = JumpAnimation.previewPose(at: Double(tick) / 120)
47 for (root, foot) in [(WorldPoint(x: pose.hips.x - 7, y: pose.hips.y), pose.leftFoot), (.init(x: pose.hips.x + 7, y: pose.hips.y), pose.rightFoot)] {
48 let ankle = WorldPoint(x: foot.x, y: foot.y + ExpeditionConfiguration.ankleHeight)
49 XCTAssertFalse(TwoBoneIK.solve(root: root, target: ankle, upper: 13.5, lower: 13.5).isClamped, "leg at \(tick)")
50 }
51 for (root, hand) in [(WorldPoint(x: pose.hips.x - 10, y: pose.hips.y + 20), pose.leftHand), (.init(x: pose.hips.x + 10, y: pose.hips.y + 20), pose.rightHand)] {
52 XCTAssertFalse(TwoBoneIK.solve(root: root, target: hand, upper: 12, lower: 12).isClamped, "arm at \(tick)")
53 }
54 }
55 }
56 func testAirSteeringPreservesMomentumOnReleaseAndLandingIsOneContact() {
57 var value = actor()
58 value.setControlDirection(horizontal: 1, vertical: 0)
59 value.controlJump()
60 for _ in 0..<30 { value.advance(by: 1.0 / 120) }
61 value.setControlDirection(horizontal: 0, vertical: 0)
62 let x = value.position.x
63 for _ in 0..<12 { value.advance(by: 1.0 / 120) }
64 XCTAssertGreaterThan(value.position.x - x, 6)
65 for _ in 0..<240 { value.advance(by: 1.0 / 120) }
66 XCTAssertEqual(value.landingCount, 1)
67 XCTAssertGreaterThan(value.lastLandingSpeed, 275)
68 XCTAssertEqual(value.position.y, 0)
69 }
70 func testMovingOrLosingSupportDuringWindupDoesNotLaunchFromStaleFloor() {
71 let frame = WorldRect(x: 200, y: 20, width: 300, height: 200)
72 var value = actor()
73 value.beginDrag(at: value.position)
74 value.drag(to: .init(x: 300, y: 220))
75 value.endDrag(on: .init(windows: [.init(id: 1, frame: frame)], viewport: .init(x: 0, y: 0, width: 1000, height: 800)))
76 value.controlJump()
77 value.advance(by: 0.05)
78 value.updateTerrain(.init(windows: [.init(id: 1, frame: .init(x: 220, y: 40, width: 300, height: 200))], viewport: .init(x: 0, y: 0, width: 1000, height: 800)))
79 XCTAssertEqual(value.position, .init(x: 320, y: 240))
80 value.updateTerrain(.init(windows: [], viewport: .init(x: 0, y: 0, width: 1000, height: 800)))
81 value.advance(by: 0.05)
82 XCTAssertNil(value.attachment)
83 XCTAssertLessThan(value.position.y, 240)
84 XCTAssertEqual(value.state, .recovering)
85 XCTAssertTrue(value.keyboardControlEnabled)
86 }
87 func testCeilingContactStopsTheRiseWithoutHovering() {
88 var value = actor()
89 value.updateBounds(.init(minimum: .init(x: 24, y: 0), maximum: .init(x: 976, y: 20)))
90 value.updateTerrain(.init(windows: [], viewport: .init(x: 0, y: 0, width: 1000, height: 800)))
91 value.controlJump()
92 for _ in 0..<96 { value.advance(by: 1.0 / 120) }
93 XCTAssertEqual(value.position.y, 0)
94 XCTAssertEqual(value.landingCount, 1)
95 }
96
97}