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.

89 lines
3.4KB

  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 = new Player(PlayerType.Local);
  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. Player.LocalPlayer.SeatEntered += Assert.CallsBack<PlayerSeatEventArgs>("SeatEntered");
  38. Player.LocalPlayer.SeatExited += Assert.CallsBack<PlayerSeatEventArgs>("SeatExited");
  39. Block.PlaceNew(BlockIDs.DriverSeat, -1f);
  40. }
  41. [APITestCase(TestType.SimulationMode)]
  42. public static IEnumerator<TaskContract> SeatEventTestSim()
  43. {
  44. yield return new WaitForSecondsEnumerator(1).Continue();
  45. Assert.Equal(Player.LocalPlayer.SpawnMachine(), true, "Failed to spawn the player's machine.", "Successfully spawned the player's machine.");
  46. yield return new WaitForSecondsEnumerator(1).Continue();
  47. var seats = Game.CurrentGame().GetBlocksInGame(BlockIDs.DriverSeat);
  48. int c = 0;
  49. while (seats.Length == 0 && c < 10)
  50. {
  51. Logging.MetaLog("Waiting for a seat to be spawned...");
  52. yield return new WaitForSecondsEnumerator(1).Continue();
  53. Console.WriteLine("Spawn machine: " + Player.LocalPlayer.SpawnMachine());
  54. seats = Game.CurrentGame().GetBlocksInGame(BlockIDs.DriverSeat);
  55. c++;
  56. }
  57. if (seats.Length == 0)
  58. {
  59. Assert.Fail("No driver seat found!");
  60. yield break;
  61. }
  62. if (seats[0] is Seat seat)
  63. Assert.Errorless(() => Player.LocalPlayer.EnterSeat(seat), "Failed to enter seat.",
  64. "Entered seat successfully.");
  65. else
  66. Assert.Fail("Found a seat that is not a seat!");
  67. yield return new WaitForSecondsEnumerator(1).Continue();
  68. Assert.Errorless(() => Player.LocalPlayer.ExitSeat(), "Failed to exit seat.",
  69. "Exited seat successfully.");
  70. }
  71. [APITestCase(TestType.Menu)]
  72. public static void InvalidStateTest()
  73. {
  74. if (!Assert.Errorless(() => { Player.Count(); }, "Player.Count() errored in menu.", "Player.Count() succeeded in menu.")) return;
  75. Assert.Equal(Player.Count(), 0u, "Player.Count() is not zero in menu.", "Player count is zero in menu as expected.");
  76. }
  77. }
  78. #endif
  79. }