using System.Numerics; using Robust.Shared.Serialization; namespace Content.Shared.Sprite; public abstract class SharedScaleVisualsSystem : EntitySystem { [Dependency] private readonly SharedAppearanceSystem _appearance = default!; public override void Initialize() { base.Initialize(); SubscribeLocalEvent(OnMapInit); SubscribeLocalEvent(OnComponentShutdown); } private void OnMapInit(Entity ent, ref MapInitEvent args) { SetSpriteScale(ent.Owner, ent.Comp.Scale); } private void OnComponentShutdown(Entity ent, ref ComponentShutdown args) { ResetScale(ent); } protected virtual void ResetScale(Entity ent) { var ev = new ScaleEntityEvent(ent.Owner, Vector2.One); RaiseLocalEvent(ent.Owner, ref ev); } /// /// Used to set the datafield to a certain value from the server. /// public void SetSpriteScale(EntityUid uid, Vector2 scale) { var comp = EnsureComp(uid); comp.Scale = scale; Dirty(uid, comp); var appearanceComponent = EnsureComp(uid); _appearance.SetData(uid, ScaleVisuals.Scale, scale, appearanceComponent); // Raise an event for content use. var ev = new ScaleEntityEvent(uid, scale); RaiseLocalEvent(uid, ref ev); } /// /// Gets the current scale set by . /// This does not include any direct changes made to the SpriteComponent. /// public Vector2 GetSpriteScale(EntityUid uid) { if (!TryComp(uid, out var appearanceComponent)) return Vector2.One; if (!_appearance.TryGetData(uid, ScaleVisuals.Scale, out var scale, appearanceComponent)) scale = Vector2.One; return scale; } } /// /// Raised when a sprite scale is changed. /// [ByRefEvent] public readonly record struct ScaleEntityEvent(EntityUid Uid, Vector2 Scale); [Serializable, NetSerializable] public enum ScaleVisuals : byte { Scale, }