Open History to see the last commit
1import XCTest
2@testable import ExpeditionCore
3
4final class RunningAndFastWorkTests: XCTestCase {
5 let layout = TerrainSnapshot(windows: [.init(id: 1, frame: .init(x: 300, y: 20, width: 300, height: 350))], viewport: .init(x: 0, y: 0, width: 1500, height: 900))
6 func actor() -> ExplorerInteraction {
7 var value = ExplorerInteraction(position: .init(x: 100, y: 0), bounds: .init(minimum: .init(x: 24, y: 0), maximum: .init(x: 1476, y: 824)))
8 value.autonomous = true; value.updateTerrain(layout); value.setKeyboardControl(true)
9 return value
10 }
11 func advance(_ value: inout ExplorerInteraction, seconds: Double) {
12 for _ in 0..<Int(seconds * 120) { value.advance(by: 1.0 / 120) }
13 }
14 func testShiftRunsAtTheSameSpeedAsTheAnimatedStrideAndReleasesToWalk() {
15 var value = actor(); value.setControlDirection(horizontal: 1, vertical: 0); value.setControlFastPace(true)
16 advance(&value, seconds: 1)
17 XCTAssertEqual(value.state, .running)
18 XCTAssertEqual(value.position.x, 100 + RunningGait.speed, accuracy: 0.01)
19 XCTAssertEqual(value.runningTime, 1, accuracy: 0.0001)
20 value.setControlFastPace(false)
21 let x = value.position.x; advance(&value, seconds: 1)
22 XCTAssertEqual(value.state, .walking)
23 XCTAssertEqual(value.position.x - x, 80, accuracy: 0.01)
24 value.setKeyboardControl(false)
25 XCTAssertFalse(value.controlFastPace)
26 }
27 func testRunningHasAFlightPhaseAndPlantedSolesDoNotSlide() {
28 var flightFrames = 0
29 for tick in 0..<600 {
30 let t = Double(tick) / 600, next = t + 0.00001
31 let pose = RunningGait.pose(at: t), after = RunningGait.pose(at: next)
32 if pose.leftFoot.y > 0 && pose.rightFoot.y > 0 { flightFrames += 1 }
33 for (a, b) in [(pose.leftFoot, after.leftFoot), (pose.rightFoot, after.rightFoot)] where a.y == 0 && b.y == 0 {
34 XCTAssertEqual(b.x - a.x + RunningGait.distance(from: t, to: next), 0, accuracy: 0.00001)
35 }
36 for (root, foot) in [(WorldPoint(x: -7, y: pose.hips.y), pose.leftFoot), (.init(x: 7, y: pose.hips.y), pose.rightFoot)] {
37 XCTAssertFalse(TwoBoneIK.solve(root: root, target: .init(x: foot.x, y: foot.y + 3), upper: 13.5, lower: 13.5).isClamped)
38 }
39 }
40 XCTAssertGreaterThan(flightFrames, 100)
41 }
42 func testFastRopeWorkChangesMotorClockWithoutSpeedingGravityOrSwingTime() {
43 let anchor = RopeAnchor(windowID: 1, edge: .front, offset: 150), guide = anchor.resolve(in: layout)!
44 var normal = RopeSession(anchor: anchor, guide: guide, feet: .init(x: 450, y: 100), mode: .climbing)
45 var fast = normal
46 for _ in 0..<300 {
47 normal.advance(by: 1.0 / 120, guide: guide, minimumLength: 25)
48 fast.advance(by: 1.0 / 120, guide: guide, minimumLength: 25, workRate: 1.8)
49 }
50 XCTAssertEqual(fast.pullTime, normal.pullTime * 1.8, accuracy: 0.00001)
51 XCTAssertEqual(fast.age, normal.age, accuracy: 0.00001)
52 XCTAssertEqual((237 - fast.body.length) / (237 - normal.body.length), 1.8, accuracy: 0.001)
53 var a = RopeSession(anchor: anchor, guide: guide, feet: .init(x: 350, y: 100), mode: .swinging)
54 a.climbsAfterSwing = false; var b = a
55 for _ in 0..<120 {
56 a.advance(by: 1.0 / 120, guide: guide, minimumLength: 25)
57 b.advance(by: 1.0 / 120, guide: guide, minimumLength: 25, workRate: 1.8)
58 }
59 XCTAssertEqual(a.body.position, b.body.position)
60 XCTAssertEqual(a.body.velocity, b.body.velocity)
61 }
62 func testFastDescentPaysOutFasterAndKeepsTheSameRope() {
63 let anchor = RopeAnchor(windowID: 1, edge: .front, offset: 150), guide = anchor.resolve(in: layout)!
64 var a = RopeSession(anchor: anchor, guide: guide, feet: .init(x: 450, y: 250), mode: .descending), b = a
65 let initial = a.body.length
66 for _ in 0..<120 {
67 a.advance(by: 1.0 / 120, guide: guide, minimumLength: 25)
68 b.advance(by: 1.0 / 120, guide: guide, minimumLength: 25, workRate: 1.8)
69 }
70 XCTAssertEqual(a.body.length - initial, 48, accuracy: 0.001)
71 XCTAssertEqual(b.body.length - initial, 86.4, accuracy: 0.001)
72 XCTAssertEqual(a.anchor, b.anchor)
73 }
74 func testOptionalIdleRunningAndFastWorkDoNotEnableExplorationByThemselves() {
75 var value = actor(); value.setKeyboardControl(false); value.setIdleExploration(false)
76 value.setRunWhenExploring(true); value.setFasterRopeWork(true)
77 advance(&value, seconds: 5)
78 XCTAssertEqual(value.position.x, 100)
79 XCTAssertEqual(value.ropeWorkRate, 1.8)
80 let destination = TerrainAttachment(owner: .screen, edge: .floor, offset: 700)
81 XCTAssertTrue(value.follow([.init(from: value.attachment!, to: destination, kind: .walking)]))
82 advance(&value, seconds: 1)
83 XCTAssertEqual(value.state, .running)
84 XCTAssertEqual(value.position.x, 268, accuracy: 0.01)
85 value.setIdleExploration(false)
86 let point = value.position; advance(&value, seconds: 5)
87 XCTAssertEqual(value.position, point)
88 }
89 func testRunMomentumCarriesIntoJumpWithoutChangingItsHeight() {
90 var walk = actor(), run = actor()
91 walk.setControlDirection(horizontal: 1, vertical: 0)
92 run.setControlDirection(horizontal: 1, vertical: 0); run.setControlFastPace(true)
93 walk.controlJump(); run.controlJump()
94 advance(&walk, seconds: 0.6); advance(&run, seconds: 0.6)
95 XCTAssertEqual(walk.position.y, run.position.y, accuracy: 0.001)
96 XCTAssertGreaterThan(run.position.x - 100, (walk.position.x - 100) * 2)
97 }
98}