DoorSystem.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Content.Shared.Doors.Components;
  2. using Content.Shared.Doors.Systems;
  3. using Robust.Client.Animations;
  4. using Robust.Client.GameObjects;
  5. using Robust.Client.ResourceManagement;
  6. using Robust.Shared.Serialization.TypeSerializers.Implementations;
  7. namespace Content.Client.Doors;
  8. public sealed class DoorSystem : SharedDoorSystem
  9. {
  10. [Dependency] private readonly AnimationPlayerSystem _animationSystem = default!;
  11. [Dependency] private readonly IResourceCache _resourceCache = default!;
  12. public override void Initialize()
  13. {
  14. base.Initialize();
  15. SubscribeLocalEvent<DoorComponent, AppearanceChangeEvent>(OnAppearanceChange);
  16. }
  17. protected override void OnComponentInit(Entity<DoorComponent> ent, ref ComponentInit args)
  18. {
  19. var comp = ent.Comp;
  20. comp.OpenSpriteStates = new List<(DoorVisualLayers, string)>(2);
  21. comp.ClosedSpriteStates = new List<(DoorVisualLayers, string)>(2);
  22. comp.OpenSpriteStates.Add((DoorVisualLayers.Base, comp.OpenSpriteState));
  23. comp.ClosedSpriteStates.Add((DoorVisualLayers.Base, comp.ClosedSpriteState));
  24. comp.OpeningAnimation = new Animation
  25. {
  26. Length = TimeSpan.FromSeconds(comp.OpeningAnimationTime),
  27. AnimationTracks =
  28. {
  29. new AnimationTrackSpriteFlick
  30. {
  31. LayerKey = DoorVisualLayers.Base,
  32. KeyFrames =
  33. {
  34. new AnimationTrackSpriteFlick.KeyFrame(comp.OpeningSpriteState, 0f),
  35. },
  36. },
  37. },
  38. };
  39. comp.ClosingAnimation = new Animation
  40. {
  41. Length = TimeSpan.FromSeconds(comp.ClosingAnimationTime),
  42. AnimationTracks =
  43. {
  44. new AnimationTrackSpriteFlick
  45. {
  46. LayerKey = DoorVisualLayers.Base,
  47. KeyFrames =
  48. {
  49. new AnimationTrackSpriteFlick.KeyFrame(comp.ClosingSpriteState, 0f),
  50. },
  51. },
  52. },
  53. };
  54. comp.EmaggingAnimation = new Animation
  55. {
  56. Length = TimeSpan.FromSeconds(comp.EmaggingAnimationTime),
  57. AnimationTracks =
  58. {
  59. new AnimationTrackSpriteFlick
  60. {
  61. LayerKey = DoorVisualLayers.BaseUnlit,
  62. KeyFrames =
  63. {
  64. new AnimationTrackSpriteFlick.KeyFrame(comp.EmaggingSpriteState, 0f),
  65. },
  66. },
  67. },
  68. };
  69. }
  70. private void OnAppearanceChange(Entity<DoorComponent> entity, ref AppearanceChangeEvent args)
  71. {
  72. if (args.Sprite == null)
  73. return;
  74. if (!AppearanceSystem.TryGetData<DoorState>(entity, DoorVisuals.State, out var state, args.Component))
  75. state = DoorState.Closed;
  76. if (AppearanceSystem.TryGetData<string>(entity, DoorVisuals.BaseRSI, out var baseRsi, args.Component))
  77. UpdateSpriteLayers(args.Sprite, baseRsi);
  78. if (_animationSystem.HasRunningAnimation(entity, DoorComponent.AnimationKey))
  79. _animationSystem.Stop(entity.Owner, DoorComponent.AnimationKey);
  80. UpdateAppearanceForDoorState(entity, args.Sprite, state);
  81. }
  82. private void UpdateAppearanceForDoorState(Entity<DoorComponent> entity, SpriteComponent sprite, DoorState state)
  83. {
  84. sprite.DrawDepth = state is DoorState.Open ? entity.Comp.OpenDrawDepth : entity.Comp.ClosedDrawDepth;
  85. switch (state)
  86. {
  87. case DoorState.Open:
  88. foreach (var (layer, layerState) in entity.Comp.OpenSpriteStates)
  89. {
  90. sprite.LayerSetState(layer, layerState);
  91. }
  92. return;
  93. case DoorState.Closed:
  94. foreach (var (layer, layerState) in entity.Comp.ClosedSpriteStates)
  95. {
  96. sprite.LayerSetState(layer, layerState);
  97. }
  98. return;
  99. case DoorState.Opening:
  100. if (entity.Comp.OpeningAnimationTime == 0.0)
  101. return;
  102. _animationSystem.Play(entity, (Animation)entity.Comp.OpeningAnimation, DoorComponent.AnimationKey);
  103. return;
  104. case DoorState.Closing:
  105. if (entity.Comp.ClosingAnimationTime == 0.0 || entity.Comp.CurrentlyCrushing.Count != 0)
  106. return;
  107. _animationSystem.Play(entity, (Animation)entity.Comp.ClosingAnimation, DoorComponent.AnimationKey);
  108. return;
  109. case DoorState.Denying:
  110. _animationSystem.Play(entity, (Animation)entity.Comp.DenyingAnimation, DoorComponent.AnimationKey);
  111. return;
  112. case DoorState.Emagging:
  113. _animationSystem.Play(entity, (Animation)entity.Comp.EmaggingAnimation, DoorComponent.AnimationKey);
  114. return;
  115. }
  116. }
  117. private void UpdateSpriteLayers(SpriteComponent sprite, string baseRsi)
  118. {
  119. if (!_resourceCache.TryGetResource<RSIResource>(SpriteSpecifierSerializer.TextureRoot / baseRsi, out var res))
  120. {
  121. Log.Error("Unable to load RSI '{0}'. Trace:\n{1}", baseRsi, Environment.StackTrace);
  122. return;
  123. }
  124. sprite.BaseRSI = res.RSI;
  125. }
  126. }