|
| 1 | +using System.Numerics; |
| 2 | +using Robust.Shared.Serialization; |
| 3 | + |
| 4 | +namespace Content.Shared.Sprite; |
| 5 | + |
| 6 | +public abstract class SharedScaleVisualsSystem : EntitySystem |
| 7 | +{ |
| 8 | + [Dependency] private readonly SharedAppearanceSystem _appearance = default!; |
| 9 | + |
| 10 | + public override void Initialize() |
| 11 | + { |
| 12 | + base.Initialize(); |
| 13 | + |
| 14 | + SubscribeLocalEvent<ScaleVisualsComponent, MapInitEvent>(OnMapInit); |
| 15 | + SubscribeLocalEvent<ScaleVisualsComponent, ComponentShutdown>(OnComponentShutdown); |
| 16 | + } |
| 17 | + |
| 18 | + private void OnMapInit(Entity<ScaleVisualsComponent> ent, ref MapInitEvent args) |
| 19 | + { |
| 20 | + SetSpriteScale(ent.Owner, ent.Comp.Scale); |
| 21 | + } |
| 22 | + |
| 23 | + private void OnComponentShutdown(Entity<ScaleVisualsComponent> ent, ref ComponentShutdown args) |
| 24 | + { |
| 25 | + ResetScale(ent); |
| 26 | + } |
| 27 | + |
| 28 | + protected virtual void ResetScale(Entity<ScaleVisualsComponent> ent) |
| 29 | + { |
| 30 | + var ev = new ScaleEntityEvent(ent.Owner, Vector2.One); |
| 31 | + RaiseLocalEvent(ent.Owner, ref ev); |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Used to set the <see cref="Robust.Client.GameObjects.SpriteComponent.Scale"/> datafield to a certain value from the server. |
| 36 | + /// </summary> |
| 37 | + public void SetSpriteScale(EntityUid uid, Vector2 scale) |
| 38 | + { |
| 39 | + var comp = EnsureComp<ScaleVisualsComponent>(uid); |
| 40 | + comp.Scale = scale; |
| 41 | + Dirty(uid, comp); |
| 42 | + |
| 43 | + var appearanceComponent = EnsureComp<AppearanceComponent>(uid); |
| 44 | + _appearance.SetData(uid, ScaleVisuals.Scale, scale, appearanceComponent); |
| 45 | + |
| 46 | + // Raise an event for content use. |
| 47 | + var ev = new ScaleEntityEvent(uid, scale); |
| 48 | + RaiseLocalEvent(uid, ref ev); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Gets the current scale set by <see cref="SetSpriteScale"/>. |
| 53 | + /// This does not include any direct changes made to the SpriteComponent. |
| 54 | + /// </summary> |
| 55 | + public Vector2 GetSpriteScale(EntityUid uid) |
| 56 | + { |
| 57 | + if (!TryComp<AppearanceComponent>(uid, out var appearanceComponent)) |
| 58 | + return Vector2.One; |
| 59 | + |
| 60 | + if (!_appearance.TryGetData<Vector2>(uid, ScaleVisuals.Scale, out var scale, appearanceComponent)) |
| 61 | + scale = Vector2.One; |
| 62 | + |
| 63 | + return scale; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/// <summary> |
| 68 | +/// Raised when a sprite scale is changed. |
| 69 | +/// </summary> |
| 70 | +[ByRefEvent] |
| 71 | +public readonly record struct ScaleEntityEvent(EntityUid Uid, Vector2 Scale); |
| 72 | + |
| 73 | +[Serializable, NetSerializable] |
| 74 | +public enum ScaleVisuals : byte |
| 75 | +{ |
| 76 | + Scale, |
| 77 | +} |
0 commit comments