1
0

DestructibleDestructionTest.cs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System.Linq;
  2. using Content.Server.Destructible.Thresholds;
  3. using Content.Server.Destructible.Thresholds.Behaviors;
  4. using Content.Shared.Damage;
  5. using Content.Shared.Damage.Prototypes;
  6. using Content.Shared.Destructible.Thresholds;
  7. using Robust.Shared.GameObjects;
  8. using Robust.Shared.Prototypes;
  9. using static Content.IntegrationTests.Tests.Destructible.DestructibleTestPrototypes;
  10. namespace Content.IntegrationTests.Tests.Destructible
  11. {
  12. public sealed class DestructibleDestructionTest
  13. {
  14. [Test]
  15. public async Task Test()
  16. {
  17. await using var pair = await PoolManager.GetServerClient();
  18. var server = pair.Server;
  19. var testMap = await pair.CreateTestMap();
  20. var sEntityManager = server.ResolveDependency<IEntityManager>();
  21. var sPrototypeManager = server.ResolveDependency<IPrototypeManager>();
  22. var sEntitySystemManager = server.ResolveDependency<IEntitySystemManager>();
  23. EntityUid sDestructibleEntity = default;
  24. TestDestructibleListenerSystem sTestThresholdListenerSystem = null;
  25. await server.WaitPost(() =>
  26. {
  27. var coordinates = testMap.GridCoords;
  28. sDestructibleEntity = sEntityManager.SpawnEntity(DestructibleDestructionEntityId, coordinates);
  29. sTestThresholdListenerSystem = sEntitySystemManager.GetEntitySystem<TestDestructibleListenerSystem>();
  30. sTestThresholdListenerSystem.ThresholdsReached.Clear();
  31. });
  32. await server.WaitAssertion(() =>
  33. {
  34. var coordinates = sEntityManager.GetComponent<TransformComponent>(sDestructibleEntity).Coordinates;
  35. var bruteDamageGroup = sPrototypeManager.Index<DamageGroupPrototype>("TestBrute");
  36. DamageSpecifier bruteDamage = new(bruteDamageGroup, 50);
  37. #pragma warning disable NUnit2045 // Interdependent assertions.
  38. Assert.DoesNotThrow(() =>
  39. {
  40. sEntityManager.System<DamageableSystem>().TryChangeDamage(sDestructibleEntity, bruteDamage, true);
  41. });
  42. Assert.That(sTestThresholdListenerSystem.ThresholdsReached, Has.Count.EqualTo(1));
  43. #pragma warning restore NUnit2045
  44. var threshold = sTestThresholdListenerSystem.ThresholdsReached[0].Threshold;
  45. Assert.Multiple(() =>
  46. {
  47. Assert.That(threshold.Triggered, Is.True);
  48. Assert.That(threshold.Behaviors, Has.Count.EqualTo(3));
  49. });
  50. var spawnEntitiesBehavior = (SpawnEntitiesBehavior) threshold.Behaviors.Single(b => b is SpawnEntitiesBehavior);
  51. Assert.Multiple(() =>
  52. {
  53. Assert.That(spawnEntitiesBehavior.Spawn, Has.Count.EqualTo(1));
  54. Assert.That(spawnEntitiesBehavior.Spawn.Keys.Single(), Is.EqualTo(SpawnedEntityId));
  55. Assert.That(spawnEntitiesBehavior.Spawn.Values.Single(), Is.EqualTo(new MinMax { Min = 1, Max = 1 }));
  56. });
  57. var entitiesInRange = sEntityManager.System<EntityLookupSystem>().GetEntitiesInRange(coordinates, 3, LookupFlags.All | LookupFlags.Approximate);
  58. var found = false;
  59. foreach (var entity in entitiesInRange)
  60. {
  61. if (sEntityManager.GetComponent<MetaDataComponent>(entity).EntityPrototype == null)
  62. {
  63. continue;
  64. }
  65. if (sEntityManager.GetComponent<MetaDataComponent>(entity).EntityPrototype?.Name != SpawnedEntityId)
  66. {
  67. continue;
  68. }
  69. found = true;
  70. break;
  71. }
  72. Assert.That(found, Is.True, $"Unable to find {SpawnedEntityId} nearby for destructible test; found {entitiesInRange.Count} entities.");
  73. });
  74. await pair.CleanReturnAsync();
  75. }
  76. }
  77. }