using System; using System.Collections.Generic; using Svelto.Tasks; using Svelto.Tasks.Enumerators; using Unity.Mathematics; using TechbloxModdingAPI.App; using TechbloxModdingAPI.Blocks; using TechbloxModdingAPI.Tests; using TechbloxModdingAPI.Utility; namespace TechbloxModdingAPI.Players { #if TEST /// /// Player test cases. Not accessible in release versions. /// [APITestClass] public static class PlayerTests { [APITestCase(TestType.EditMode)] public static void ExistsTest() { if (!Assert.Equal(Player.Exists(PlayerType.Local), true, "Local player does not exist.", "Local player detected.")) return; Assert.Equal(Player.Count(), 1u, "Player.Count() is not one, possibly because it failed silently.", "Player count is one for single player game."); } [APITestCase(TestType.EditMode)] public static void PositionTest() { Player p = new Player(PlayerType.Local); if (!Assert.Errorless(() => { p.Teleport(0, 0, 0, relative: false); }, "Player.Teleport(origin) errored: ", "Player teleported to origin successfully.")) return; if (!Assert.CloseTo(p.Position, float3.zero, "Player is not close to origin despite being teleported there.", "Player.Position is at origin.")) return; if (!Assert.Errorless(() => { p.Position = float3.zero + 1; }, "Player.Position = origin+1 errored: ", "Player moved to origin+1.")) return; Assert.CloseTo(p.Position, float3.zero + 1, "Player is not close to origin+1 despite being teleported there.", "Player.Position is at origin+1."); } [APITestCase(TestType.Game)] public static void SeatEventTestBuild() { Player.LocalPlayer.SeatEntered += Assert.CallsBack("SeatEntered"); Player.LocalPlayer.SeatExited += Assert.CallsBack("SeatExited"); Block.PlaceNew(BlockIDs.DriverSeat, -1f); } [APITestCase(TestType.SimulationMode)] public static IEnumerator SeatEventTestSim() { yield return new WaitForSecondsEnumerator(1).Continue(); Assert.Equal(Player.LocalPlayer.SpawnMachine(), true, "Failed to spawn the player's machine.", "Successfully spawned the player's machine."); yield return new WaitForSecondsEnumerator(1).Continue(); var seats = Game.CurrentGame().GetBlocksInGame(BlockIDs.DriverSeat); int c = 0; while (seats.Length == 0 && c < 10) { Logging.MetaLog("Waiting for a seat to be spawned..."); yield return new WaitForSecondsEnumerator(1).Continue(); Console.WriteLine("Spawn machine: " + Player.LocalPlayer.SpawnMachine()); seats = Game.CurrentGame().GetBlocksInGame(BlockIDs.DriverSeat); c++; } if (seats.Length == 0) { Assert.Fail("No driver seat found!"); yield break; } if (seats[0] is Seat seat) Assert.Errorless(() => Player.LocalPlayer.EnterSeat(seat), "Failed to enter seat.", "Entered seat successfully."); else Assert.Fail("Found a seat that is not a seat!"); yield return new WaitForSecondsEnumerator(1).Continue(); Assert.Errorless(() => Player.LocalPlayer.ExitSeat(), "Failed to exit seat.", "Exited seat successfully."); } [APITestCase(TestType.Menu)] public static void InvalidStateTest() { if (!Assert.Errorless(() => { Player.Count(); }, "Player.Count() errored in menu.", "Player.Count() succeeded in menu.")) return; Assert.Equal(Player.Count(), 0u, "Player.Count() is not zero in menu.", "Player count is zero in menu as expected."); } } #endif }