Skip to content
0.2.0-dev Download ZIP
157 lines ·5.4 KB Raw
1using System.Numerics;
2using EvilEngine.Formats.Animations;
3using EvilEngine.Formats.Maps;
4using EvilEngine.Formats.Models;
5using EvilEngine.Host.Gait;
6
7namespace EvilEngine.Host.Tests;
8
9public sealed class BodyGaitTests
10{
11 private const double Tick = 0.015;
12
13 private static readonly Dictionary<string, Animation> Set = new()
14 {
15 [GaitSlots.Idle] = Clip("idle", 9), [GaitSlots.Walk] = Clip("walk", 1, 2), [GaitSlots.Run] = Clip("run", 1, 5),
16 [GaitSlots.CrouchIdle] = Clip("cidle", 6), [GaitSlots.Jump] = Clip("jump", 1.8f, loop: false)
17 };
18
19 private static Clip Clip(string name, float length, float move = 0, bool loop = true)
20 {
21 return new Clip(name, [ClipNode.Rest("root", -1, Vector3.Zero, Quaternion.Identity)],
22 [new ClipTrack(0, ModelChannelPath.Translation, [0, length], [0, 0, 0, 0, 0, 0])], null, 30, loop)
23 {
24 Move = move > 0 ? new Vector3(0, 0, move) : null, Fade = new Vector2(0.3f, 0.2f)
25 };
26 }
27
28 private static BodyGait Gait()
29 {
30 return new BodyGait(GaitRules.Standard, slot => Set.GetValueOrDefault(slot));
31 }
32
33 private static GaitInput Moving(double x, double z, double yaw = 0, bool crouching = false)
34 {
35 return new GaitInput(new MapVector(x, 0, z), true, crouching, false, yaw, 0);
36 }
37
38 [Fact]
39 public void WalksRunsAndCrouchesByHorizontalSpeed()
40 {
41 var gait = Gait();
42
43 gait.Update(Moving(0, 0), Tick);
44 Assert.Equal("idle", gait.Current?.Name);
45 gait.Update(Moving(0, -3), Tick);
46 Assert.Equal("walk", gait.Current?.Name);
47 gait.Update(Moving(0, -5.08), Tick);
48 Assert.Equal("run", gait.Current?.Name);
49 gait.Update(Moving(0, -1, crouching: true), Tick);
50 Assert.Equal(GaitSlots.CrouchWalk, gait.Slot);
51 Assert.Equal("cidle", gait.Current?.Name);
52 }
53
54 [Fact]
55 public void CarriesTheCycleAcrossAChangeAndFadesTheOldSequenceOut()
56 {
57 var gait = Gait();
58 gait.Update(Moving(0, -3), Tick);
59 gait.Cycle = 0.4;
60
61 gait.Update(Moving(0, -5), Tick);
62
63 Assert.Equal("run", gait.Current?.Name);
64 Assert.InRange(gait.Cycle, 0.4, 0.45);
65 var fade = Assert.Single(gait.Fades);
66 Assert.Equal("walk", fade.Animation.Name);
67 Assert.Equal(0.2, fade.Duration, 6);
68 Assert.Equal(1, fade.Weight);
69 for (var i = 0; i < 14; i++)
70 gait.Update(Moving(0, -5), Tick);
71 Assert.Empty(gait.Fades);
72 }
73
74 [Fact]
75 public void ScalesTheMovePosesToTheEdgesGroundSpeedAndSetsTheRate()
76 {
77 var gait = Gait();
78
79 gait.Update(Moving(0, -1), Tick);
80 Assert.Equal(0.5f, gait.MoveX, 4);
81 Assert.Equal(0, gait.MoveY, 4);
82 Assert.Equal(0.5, gait.Rate, 4);
83 gait.Update(Moving(1.5, -1.5), Tick);
84 Assert.Equal(1, gait.MoveX, 4);
85 Assert.Equal(1, gait.MoveY, 4);
86 gait.Update(Moving(0, 0), Tick);
87 Assert.Equal(1, gait.Rate);
88 }
89
90 [Fact]
91 public void TurnsAStillBodyIn45DegreeStepsOnceTheViewIsMoreThan45Off()
92 {
93 var gait = Gait();
94 gait.Update(Moving(0, 0), Tick);
95
96 for (var i = 0; i < 100; i++)
97 gait.Update(Moving(0, 0, 30), Tick);
98 Assert.Equal(0, gait.FeetYaw, 6);
99 for (var i = 0; i < 100; i++)
100 gait.Update(Moving(0, 0, 60), Tick);
101 Assert.Equal(45, gait.GoalYaw, 6);
102 Assert.Equal(45, gait.FeetYaw, 6);
103 Assert.Equal(15, gait.AimYaw, 4);
104 }
105
106 [Fact]
107 public void JumpsFromCycleZeroAndLandsOnceMoreThanAFifthOfASecondHasPassed()
108 {
109 var gait = Gait();
110 gait.Update(Moving(0, 0), Tick);
111 gait.Cycle = 0.6;
112
113 gait.Update(new GaitInput(new MapVector(0, 5, 0), false, false, false, 0, 0) { Jumped = true }, Tick);
114 Assert.Equal("jump", gait.Current?.Name);
115 Assert.Equal(0, gait.Cycle);
116 Assert.Equal(2, gait.Rate);
117 for (var i = 0; i < 5; i++)
118 gait.Update(Moving(0, 0), Tick);
119 Assert.Equal("jump", gait.Current?.Name);
120 for (var i = 0; i < 12; i++)
121 gait.Update(Moving(0, 0), Tick);
122 Assert.Equal("idle", gait.Current?.Name);
123 Assert.InRange(gait.Cycle, 0, 0.01);
124 Assert.NotNull(gait.LandedAt);
125 }
126
127 [Fact]
128 public void PlaysTheLandingGestureThroughOnceAndHoldsTheNoclipOneUntilNoclipEndsInALanding()
129 {
130 var land = Clip("land", 0.6f, loop: false);
131 var noclip = Clip("noclip", 0.3f);
132 var gait = new BodyGait(GaitRules.Standard, slot => slot switch
133 {
134 GaitSlots.Landing => land,
135 GaitSlots.Noclip => noclip,
136 _ => Set.GetValueOrDefault(slot)
137 });
138 gait.Update(new GaitInput(new MapVector(0, -3, 0), false, false, false, 0, 0), Tick);
139
140 gait.Update(Moving(0, 0), Tick);
141 var landing = Assert.Single(gait.Gestures);
142 Assert.Equal((2, "land", 0.0), (landing.Slot, landing.Animation.Name, landing.Cycle));
143 gait.Update(Moving(0, 0), Tick);
144 Assert.Equal(0.025, landing.Cycle, 6);
145 for (var i = 0; i < 40; i++)
146 gait.Update(Moving(0, 0), Tick);
147 Assert.Empty(gait.Gestures);
148
149 var flying = new GaitInput(new MapVector(0, 0, 0), false, false, true, 0, 0);
150 for (var i = 0; i < 40; i++)
151 gait.Update(flying, Tick);
152 var held = Assert.Single(gait.Gestures);
153 Assert.Equal((6, 1.0), (held.Slot, held.Cycle));
154 gait.Update(Moving(0, 0), Tick);
155 Assert.Equal(2, Assert.Single(gait.Gestures).Slot);
156 }
157}