|
- 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
- /// <summary>
- /// Player test cases. Not accessible in release versions.
- /// </summary>
- [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 = Player.LocalPlayer;
- 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<PlayerSeatEventArgs>("SeatEntered");
- Player.LocalPlayer.SeatExited += Assert.CallsBack<PlayerSeatEventArgs>("SeatExited");
- Block.PlaceNew(BlockIDs.DriverSeat, Player.LocalPlayer.Position);
- }
-
- [APITestCase(TestType.SimulationMode)]
- public static IEnumerator<TaskContract> 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)
- { //TODO: Actually, the problem is likely that the player ID is different in build and sim
- Assert.Errorless(() => Player.LocalPlayer.EnterSeat(seat), "Failed to enter seat.",
- "Entered seat successfully.");
- while (Player.LocalPlayer.State != PlayerState.InSeat)
- {
- bool cont = false;
- Client.Instance.PromptUser(new SingleChoicePrompt("Testing", $"Enter the seat at {seat.Position} pls", "OK", () => cont = true));
- while (!cont)
- yield return Yield.It;
- yield return new WaitForSecondsEnumerator(5f).Continue();
- }
- }
- else
- Assert.Fail("Found a seat that is not a seat!");
- yield return new WaitForSecondsEnumerator(5).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
- }
|