ClickableTest.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Numerics;
  2. using Content.Client.Clickable;
  3. using Robust.Client.GameObjects;
  4. using Robust.Client.Graphics;
  5. using Robust.Shared.GameObjects;
  6. namespace Content.IntegrationTests.Tests
  7. {
  8. [TestFixture]
  9. public sealed class ClickableTest
  10. {
  11. private const double DirSouth = 0;
  12. private const double DirNorth = Math.PI;
  13. private const double DirEast = Math.PI / 2;
  14. private const double DirSouthEast = Math.PI / 4;
  15. private const double DirSouthEastJustShy = Math.PI / 4 - 0.1;
  16. [Test]
  17. [TestCase("ClickTestRotatingCornerVisible", 0.25f, 0.25f, DirSouth, 1, ExpectedResult = true)]
  18. [TestCase("ClickTestRotatingCornerVisible", 0.35f, 0.5f, DirSouth, 2, ExpectedResult = true)]
  19. [TestCase("ClickTestRotatingCornerVisible", -0.25f, -0.25f, DirSouth, 1, ExpectedResult = false)]
  20. [TestCase("ClickTestRotatingCornerVisible", 0.25f, 0.25f, DirNorth, 1, ExpectedResult = false)]
  21. [TestCase("ClickTestRotatingCornerVisible", -0.25f, -0.25f, DirNorth, 1, ExpectedResult = true)]
  22. [TestCase("ClickTestRotatingCornerVisible", -0.25f, 0.25f, DirEast, 1, ExpectedResult = true)]
  23. [TestCase("ClickTestRotatingCornerVisible", 0, 0.25f, DirSouthEast, 1, ExpectedResult = true)]
  24. [TestCase("ClickTestRotatingCornerVisibleNoRot", 0.25f, 0.25f, DirSouth, 1, ExpectedResult = true)]
  25. [TestCase("ClickTestRotatingCornerVisibleNoRot", -0.25f, -0.25f, DirSouth, 1, ExpectedResult = false)]
  26. [TestCase("ClickTestRotatingCornerVisibleNoRot", 0.25f, 0.25f, DirNorth, 1, ExpectedResult = false)]
  27. [TestCase("ClickTestRotatingCornerVisibleNoRot", -0.25f, -0.25f, DirNorth, 1, ExpectedResult = true)]
  28. [TestCase("ClickTestRotatingCornerVisibleNoRot", 0, 0.35f, DirSouthEastJustShy, 1, ExpectedResult = false)]
  29. [TestCase("ClickTestRotatingCornerVisibleNoRot", 0.25f, 0.25f, DirSouthEastJustShy, 1, ExpectedResult = true)]
  30. [TestCase("ClickTestRotatingCornerInvisible", 0.25f, 0.25f, DirSouth, 1, ExpectedResult = true)]
  31. [TestCase("ClickTestRotatingCornerInvisible", 0.35f, 0.5f, DirSouth, 2, ExpectedResult = true)]
  32. [TestCase("ClickTestRotatingCornerInvisible", -0.25f, -0.25f, DirSouth, 1, ExpectedResult = false)]
  33. [TestCase("ClickTestRotatingCornerInvisible", 0.25f, 0.25f, DirNorth, 1, ExpectedResult = false)]
  34. [TestCase("ClickTestRotatingCornerInvisible", -0.25f, -0.25f, DirNorth, 1, ExpectedResult = true)]
  35. [TestCase("ClickTestRotatingCornerInvisible", -0.25f, 0.25f, DirEast, 1, ExpectedResult = true)]
  36. [TestCase("ClickTestRotatingCornerInvisible", 0, 0.25f, DirSouthEast, 1, ExpectedResult = true)]
  37. [TestCase("ClickTestRotatingCornerInvisibleNoRot", 0.25f, 0.25f, DirSouth, 1, ExpectedResult = true)]
  38. [TestCase("ClickTestRotatingCornerInvisibleNoRot", -0.25f, -0.25f, DirSouth, 1, ExpectedResult = false)]
  39. [TestCase("ClickTestRotatingCornerInvisibleNoRot", 0.25f, 0.25f, DirNorth, 1, ExpectedResult = false)]
  40. [TestCase("ClickTestRotatingCornerInvisibleNoRot", -0.25f, -0.25f, DirNorth, 1, ExpectedResult = true)]
  41. [TestCase("ClickTestRotatingCornerInvisibleNoRot", 0, 0.35f, DirSouthEastJustShy, 1, ExpectedResult = false)]
  42. [TestCase("ClickTestRotatingCornerInvisibleNoRot", 0.25f, 0.25f, DirSouthEastJustShy, 1, ExpectedResult = true)]
  43. public async Task<bool> Test(string prototype, float clickPosX, float clickPosY, double angle, float scale)
  44. {
  45. await using var pair = await PoolManager.GetServerClient(new PoolSettings { Connected = true });
  46. var server = pair.Server;
  47. var client = pair.Client;
  48. var clientEntManager = client.ResolveDependency<IEntityManager>();
  49. var serverEntManager = server.ResolveDependency<IEntityManager>();
  50. var eyeManager = client.ResolveDependency<IEyeManager>();
  51. var spriteQuery = clientEntManager.GetEntityQuery<SpriteComponent>();
  52. var eye = client.ResolveDependency<IEyeManager>().CurrentEye;
  53. var testMap = await pair.CreateTestMap();
  54. EntityUid serverEnt = default;
  55. await server.WaitPost(() =>
  56. {
  57. serverEnt = serverEntManager.SpawnEntity(prototype, testMap.GridCoords);
  58. serverEntManager.System<SharedTransformSystem>().SetWorldRotation(serverEnt, angle);
  59. });
  60. // Let client sync up.
  61. await pair.RunTicksSync(5);
  62. var hit = false;
  63. var clientEnt = clientEntManager.GetEntity(serverEntManager.GetNetEntity(serverEnt));
  64. await client.WaitPost(() =>
  65. {
  66. var sprite = spriteQuery.GetComponent(clientEnt);
  67. sprite.Scale = new Vector2(scale, scale);
  68. // these tests currently all assume player eye is 0
  69. eyeManager.CurrentEye.Rotation = 0;
  70. var pos = clientEntManager.System<SharedTransformSystem>().GetWorldPosition(clientEnt);
  71. hit = clientEntManager.System<ClickableSystem>().CheckClick((clientEnt, null, sprite, null), new Vector2(clickPosX, clickPosY) + pos, eye, out _, out _, out _);
  72. });
  73. await server.WaitPost(() =>
  74. {
  75. serverEntManager.DeleteEntity(serverEnt);
  76. });
  77. await pair.CleanReturnAsync();
  78. return hit;
  79. }
  80. }
  81. }