1
0

ExplosionOverlaySystem.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using Content.Shared.Explosion;
  2. using Content.Shared.Explosion.Components;
  3. using Robust.Client.Graphics;
  4. using Robust.Client.ResourceManagement;
  5. using Robust.Shared.GameStates;
  6. using Robust.Shared.Graphics.RSI;
  7. using Robust.Shared.Map;
  8. using Robust.Shared.Prototypes;
  9. namespace Content.Client.Explosion;
  10. /// <summary>
  11. /// This system is responsible for showing the client-side explosion effects (light source & fire-overlay). The
  12. /// fire overlay code is just a bastardized version of the atmos plasma fire overlay and uses the same texture.
  13. /// </summary>
  14. public sealed class ExplosionOverlaySystem : EntitySystem
  15. {
  16. [Dependency] private readonly IPrototypeManager _protoMan = default!;
  17. [Dependency] private readonly IResourceCache _resCache = default!;
  18. [Dependency] private readonly IOverlayManager _overlayMan = default!;
  19. [Dependency] private readonly SharedPointLightSystem _lights = default!;
  20. [Dependency] private readonly IMapManager _mapMan = default!;
  21. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  22. public override void Initialize()
  23. {
  24. base.Initialize();
  25. SubscribeLocalEvent<ExplosionVisualsComponent, ComponentInit>(OnExplosionInit);
  26. SubscribeLocalEvent<ExplosionVisualsComponent, ComponentRemove>(OnCompRemove);
  27. SubscribeLocalEvent<ExplosionVisualsComponent, ComponentHandleState>(OnExplosionHandleState);
  28. _overlayMan.AddOverlay(new ExplosionOverlay(_appearance));
  29. }
  30. private void OnExplosionHandleState(EntityUid uid, ExplosionVisualsComponent component, ref ComponentHandleState args)
  31. {
  32. if (args.Current is not ExplosionVisualsState state)
  33. return;
  34. component.Epicenter = state.Epicenter;
  35. component.SpaceTiles = state.SpaceTiles;
  36. component.Tiles.Clear();
  37. foreach (var (nent, data) in state.Tiles)
  38. {
  39. component.Tiles[GetEntity(nent)] = data;
  40. }
  41. component.Intensity = state.Intensity;
  42. component.ExplosionType = state.ExplosionType;
  43. component.SpaceMatrix = state.SpaceMatrix;
  44. component.SpaceTileSize = state.SpaceTileSize;
  45. }
  46. private void OnCompRemove(EntityUid uid, ExplosionVisualsComponent component, ComponentRemove args)
  47. {
  48. if (TryComp(uid, out ExplosionVisualsTexturesComponent? textures) && !Deleted(textures.LightEntity))
  49. QueueDel(textures.LightEntity);
  50. }
  51. private void OnExplosionInit(EntityUid uid, ExplosionVisualsComponent component, ComponentInit args)
  52. {
  53. EnsureComp<ExplosionVisualsTexturesComponent>(uid);
  54. if (!_protoMan.TryIndex(component.ExplosionType, out ExplosionPrototype? type) ||
  55. !TryComp(uid, out ExplosionVisualsTexturesComponent? textures))
  56. {
  57. return;
  58. }
  59. // Map may have been deleted.
  60. if (_mapMan.MapExists(component.Epicenter.MapId))
  61. {
  62. // spawn in a client-side light source at the epicenter
  63. var lightEntity = Spawn("ExplosionLight", component.Epicenter);
  64. var light = _lights.EnsureLight(lightEntity);
  65. _lights.SetRadius(lightEntity, component.Intensity.Count, light);
  66. _lights.SetEnergy(lightEntity, component.Intensity.Count, light);
  67. _lights.SetColor(lightEntity, type.LightColor, light);
  68. textures.LightEntity = lightEntity;
  69. }
  70. textures.FireColor = type.FireColor;
  71. textures.IntensityPerState = type.IntensityPerState;
  72. var fireRsi = _resCache.GetResource<RSIResource>(type.TexturePath).RSI;
  73. foreach (var state in fireRsi)
  74. {
  75. textures.FireFrames.Add(state.GetFrames(RsiDirection.South));
  76. if (textures.FireFrames.Count == type.FireStates)
  77. break;
  78. }
  79. }
  80. public override void Shutdown()
  81. {
  82. base.Shutdown();
  83. _overlayMan.RemoveOverlay<ExplosionOverlay>();
  84. }
  85. }