A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.7KB

  1. using System;
  2. using System.Collections.Generic;
  3. using Svelto.Tasks;
  4. using Svelto.Tasks.Enumerators;
  5. using Unity.Mathematics;
  6. using TechbloxModdingAPI.App;
  7. using TechbloxModdingAPI.Blocks;
  8. using TechbloxModdingAPI.Tests;
  9. using TechbloxModdingAPI.Utility;
  10. namespace TechbloxModdingAPI.Players
  11. {
  12. #if TEST
  13. /// <summary>
  14. /// Player test cases. Not accessible in release versions.
  15. /// </summary>
  16. [APITestClass]
  17. public static class PlayerTests
  18. {
  19. [APITestCase(TestType.EditMode)]
  20. public static void ExistsTest()
  21. {
  22. if (!Assert.Equal(Player.Exists(PlayerType.Local), true, "Local player does not exist.", "Local player detected.")) return;
  23. Assert.Equal(Player.Count(), 1u, "Player.Count() is not one, possibly because it failed silently.", "Player count is one for single player game.");
  24. }
  25. [APITestCase(TestType.EditMode)]
  26. public static void PositionTest()
  27. {
  28. Player p = Player.LocalPlayer;
  29. if (!Assert.Errorless(() => { p.Teleport(0, 0, 0, relative: false); }, "Player.Teleport(origin) errored: ", "Player teleported to origin successfully.")) return;
  30. if (!Assert.CloseTo(p.Position, float3.zero, "Player is not close to origin despite being teleported there.", "Player.Position is at origin.")) return;
  31. if (!Assert.Errorless(() => { p.Position = float3.zero + 1; }, "Player.Position = origin+1 errored: ", "Player moved to origin+1.")) return;
  32. Assert.CloseTo(p.Position, float3.zero + 1, "Player is not close to origin+1 despite being teleported there.", "Player.Position is at origin+1.");
  33. }
  34. [APITestCase(TestType.Game)]
  35. public static void SeatEventTestBuild()
  36. {
  37. Block.PlaceNew(BlockIDs.DriverSeat, Player.LocalPlayer.Position);
  38. }
  39. [APITestCase(TestType.SimulationMode)]
  40. public static IEnumerator<TaskContract> SeatEventTestSim()
  41. {
  42. Player.LocalPlayer.SeatEntered += Assert.CallsBack<PlayerSeatEventArgs>("SeatEntered");
  43. Player.LocalPlayer.SeatExited += Assert.CallsBack<PlayerSeatEventArgs>("SeatExited");
  44. Assert.Equal(Player.LocalPlayer.SpawnMachine(), true, "Failed to spawn the player's machine.", "Successfully spawned the player's machine.");
  45. yield return new WaitForSecondsEnumerator(1).Continue();
  46. var seats = Game.CurrentGame().GetBlocksInGame(BlockIDs.DriverSeat);
  47. int c = 0;
  48. while (seats.Length == 0 && c < 10)
  49. {
  50. Logging.MetaLog("Waiting for a seat to be spawned...");
  51. yield return new WaitForSecondsEnumerator(1).Continue();
  52. Logging.MetaLog("Spawn machine: " + Player.LocalPlayer.SpawnMachine());
  53. seats = Game.CurrentGame().GetBlocksInGame(BlockIDs.DriverSeat);
  54. c++;
  55. }
  56. if (seats.Length == 0)
  57. {
  58. Assert.Fail("No driver seat found!");
  59. yield break;
  60. }
  61. if (seats[0] is Seat seat)
  62. {
  63. Assert.Errorless(() => Player.LocalPlayer.EnterSeat(seat), "Failed to enter seat.",
  64. "Entered seat successfully.");
  65. while (Player.LocalPlayer.State != PlayerState.InSeat)
  66. {
  67. bool cont = false;
  68. Client.Instance.PromptUser(new SingleChoicePrompt("Testing", $"Enter the seat at {seat.Position} pls", "OK", () => cont = true));
  69. while (!cont)
  70. yield return Yield.It;
  71. yield return new WaitForSecondsEnumerator(5f).Continue();
  72. }
  73. }
  74. else
  75. Assert.Fail("Found a seat that is not a seat!");
  76. yield return new WaitForSecondsEnumerator(5).Continue();
  77. Assert.Errorless(() => Player.LocalPlayer.ExitSeat(), "Failed to exit seat.",
  78. "Exited seat successfully.");
  79. }
  80. [APITestCase(TestType.Menu)]
  81. public static void InvalidStateTest()
  82. {
  83. if (!Assert.Errorless(() => { Player.Count(); }, "Player.Count() errored in menu.", "Player.Count() succeeded in menu.")) return;
  84. Assert.Equal(Player.Count(), 0u, "Player.Count() is not zero in menu.", "Player count is zero in menu as expected.");
  85. }
  86. }
  87. #endif
  88. }