0.2.0-dev Download ZIP
128 lines ·5.1 KB Raw
| 1 | using EvilEngine.Formats; |
| 2 | using EvilEngine.Formats.Collision; |
| 3 | using EvilEngine.Formats.Maps; |
| 4 | using EvilEngine.Formats.Records; |
| 5 | using EvilEngine.Host.Movement; |
| 6 | using EvilEngine.Host.Sessions; |
| 7 | using EvilEngine.Host.Steps; |
| 8 | |
| 9 | namespace EvilEngine.Host.Tests; |
| 10 | |
| 11 | public sealed class StepSoundsTests |
| 12 | { |
| 13 | private static readonly ContentId Map = ContentId.Parse("gmod:test"); |
| 14 | |
| 15 | private static readonly SurfaceRecord[] Surfaces = |
| 16 | [ |
| 17 | Surface("chainlink", "metal", ("step.left", "chainlink.stepleft")), |
| 18 | Surface("default", "default", ("step.left", "default.stepleft"), ("step.right", "default.stepright")), |
| 19 | Surface("metal", "metal", ("step.left", "metal.stepleft"), ("step.right", "metal.stepright")), |
| 20 | Surface("quietmetal", "metal"), |
| 21 | Surface("silent", "default") |
| 22 | ]; |
| 23 | |
| 24 | private static ContentId Id(string name) |
| 25 | { |
| 26 | return ContentId.Create("gmod", name); |
| 27 | } |
| 28 | |
| 29 | private static SurfaceRecord Surface(string name, string surfaceClass, params (string Slot, string Sound)[] sounds) |
| 30 | { |
| 31 | return new SurfaceRecord(Id($"surfaces/{name}")) |
| 32 | { |
| 33 | Class = surfaceClass, Sounds = sounds.ToDictionary(pair => pair.Slot, pair => Id(pair.Sound)) |
| 34 | }; |
| 35 | } |
| 36 | |
| 37 | private static StepSounds Sounds(Func<ContentId, ContentId?>? materials = null) |
| 38 | { |
| 39 | return new StepSounds(Surfaces, materials ?? (_ => null)); |
| 40 | } |
| 41 | |
| 42 | [Fact] |
| 43 | public void PlaysTheSurfacesOwnSlot() |
| 44 | { |
| 45 | Assert.Equal(Id("metal.stepright"), |
| 46 | Sounds().Sound(new CollisionSurface(Id("surfaces/metal"), null), StepSlots.Right, "gmod")); |
| 47 | } |
| 48 | |
| 49 | [Fact] |
| 50 | public void FallsToTheFirstSurfaceOfItsClassWithTheSlotThenToDefault() |
| 51 | { |
| 52 | var sounds = Sounds(); |
| 53 | |
| 54 | Assert.Equal(Id("chainlink.stepleft"), |
| 55 | sounds.Sound(new CollisionSurface(Id("surfaces/quietmetal"), null), StepSlots.Left, "gmod")); |
| 56 | Assert.Equal(Id("metal.stepright"), |
| 57 | sounds.Sound(new CollisionSurface(Id("surfaces/quietmetal"), null), StepSlots.Right, "gmod")); |
| 58 | Assert.Equal(Id("default.stepleft"), |
| 59 | sounds.Sound(new CollisionSurface(Id("surfaces/silent"), null), StepSlots.Left, "gmod")); |
| 60 | Assert.Equal(Id("default.stepright"), |
| 61 | sounds.Sound(new CollisionSurface(Id("surfaces/unknown"), null), StepSlots.Right, "gmod")); |
| 62 | } |
| 63 | |
| 64 | [Fact] |
| 65 | public void TakesAFacesMaterialsSurfaceAndDefaultWithNeither() |
| 66 | { |
| 67 | var asked = new List<ContentId>(); |
| 68 | var sounds = Sounds(material => |
| 69 | { |
| 70 | asked.Add(material); |
| 71 | return material == Id("materials/metal") ? Id("surfaces/metal") : null; |
| 72 | }); |
| 73 | |
| 74 | Assert.Equal(Id("metal.stepleft"), |
| 75 | sounds.Sound(new CollisionSurface(null, Id("materials/metal")), StepSlots.Left, "gmod")); |
| 76 | Assert.Equal(Id("metal.stepleft"), |
| 77 | sounds.Sound(new CollisionSurface(null, Id("materials/metal")), StepSlots.Left, "gmod")); |
| 78 | Assert.Equal(Id("default.stepleft"), |
| 79 | sounds.Sound(new CollisionSurface(null, Id("materials/plain")), StepSlots.Left, "gmod")); |
| 80 | Assert.Equal(Id("default.stepleft"), sounds.Sound(CollisionSurface.None, StepSlots.Left, "gmod")); |
| 81 | Assert.Null(sounds.Sound(CollisionSurface.None, StepSlots.Left, "other")); |
| 82 | Assert.Equal([Id("materials/metal"), Id("materials/plain")], asked); |
| 83 | } |
| 84 | |
| 85 | [Fact] |
| 86 | public void NoSurfacesPlayNothing() |
| 87 | { |
| 88 | Assert.Null(StepSounds.None.Sound(new CollisionSurface(Id("surfaces/metal"), null), StepSlots.Left, "gmod")); |
| 89 | } |
| 90 | |
| 91 | private static List<Snapshot> Walk(StepSounds sounds) |
| 92 | { |
| 93 | var floor = new HullBoxWorld().Box(new MapVector(-100, -1, -100), new MapVector(100, 0, 100)); |
| 94 | var spawn = new SpawnPoint(null, new MapTransform(MapVector.Zero, MapVector.Zero)); |
| 95 | var server = new ServerSession(new SessionStart(Map, floor, [spawn], -50) { StepSounds = sounds }); |
| 96 | var snapshots = new List<Snapshot>(); |
| 97 | var bot = server.AddBot(brain: _ => new MoveCommand(1, 0, 0, 0, 0, MoveButtons.Run)); |
| 98 | bot.Arrived = snapshots.Add; |
| 99 | for (var tick = 0; tick < 200; tick++) |
| 100 | server.Tick(); |
| 101 | return snapshots; |
| 102 | } |
| 103 | |
| 104 | [Fact] |
| 105 | public void SendsEachTicksStepsAsSoundEventsFromTheBody() |
| 106 | { |
| 107 | var snapshots = Walk(Sounds()); |
| 108 | |
| 109 | var events = snapshots.SelectMany(snapshot => snapshot.Sounds.Select(sound => (snapshot, sound))).ToArray(); |
| 110 | Assert.True(events.Length >= 5, $"{events.Length} step sounds"); |
| 111 | Assert.All(events, entry => |
| 112 | { |
| 113 | var body = Assert.Single(entry.snapshot.Bodies); |
| 114 | Assert.Equal(body.Body, entry.sound.Emitter); |
| 115 | Assert.Contains(entry.sound.Sound, new[] { Id("default.stepleft"), Id("default.stepright") }); |
| 116 | Assert.Contains(entry.sound.Volume, new double?[] { 0.2, 0.5 }); |
| 117 | }); |
| 118 | Assert.Equal(0.5, events[^1].sound.Volume); |
| 119 | Assert.Equal(Id("default.stepright"), events[0].sound.Sound); |
| 120 | Assert.Equal(Id("default.stepleft"), events[1].sound.Sound); |
| 121 | } |
| 122 | |
| 123 | [Fact] |
| 124 | public void SendsNoSoundsWithoutSurfaces() |
| 125 | { |
| 126 | Assert.All(Walk(StepSounds.None), snapshot => Assert.Empty(snapshot.Sounds)); |
| 127 | } |
| 128 | } |