using Content.Shared.Examine; using Content.Shared.FixedPoint; using Content.Shared.Interaction; using Content.Shared.Store.Components; using Content.Shared.Whitelist; using Robust.Shared.Audio.Systems; using Robust.Shared.Prototypes; using Robust.Shared.Timing; namespace Content.Shared.Store; /// /// This handles currency generation and collection for . /// public abstract partial class SharedStoreSystem { [Dependency] private IGameTiming _timing = default!; [Dependency] private EntityWhitelistSystem _whitelist = default!; [Dependency] private SharedAudioSystem _audio = default!; [SubscribeLocalEvent] private void OnGeneratorInitialize(Entity entity, ref MapInitEvent args) { entity.Comp.NextGenerationTime = _timing.CurTime + entity.Comp.GenerationDelay; DirtyField(entity, entity.Comp, nameof(StoreCurrencyGeneratorComponent.NextGenerationTime)); } [SubscribeLocalEvent] private void OnInteractedUsingHand(Entity entity, ref InteractHandEvent args) { AttemptCollection(entity, args.User, args.User); } [SubscribeLocalEvent] private void OnInteractedUsingStore(Entity entity, ref InteractUsingEvent args) { AttemptCollection(entity, args.Used, args.User); } [SubscribeLocalEvent] private void OnExamined(Entity entity, ref ExaminedEvent args) { if (!args.IsInDetailsRange) return; // If we only wanna show the examine message to valid stores. if (entity.Comp.StoreExaminable) { if (!HasComp(args.Examiner)) return; if (!_whitelist.CheckBoth(args.Examiner, entity.Comp.Blacklist, entity.Comp.Whitelist)) return; } if (!ProtoMan.TryIndex(entity.Comp.Currency, out var proto)) return; args.PushMarkup(Loc.GetString("store-generator-examine", ("amount", entity.Comp.Amount), ("currency", Loc.GetString(proto.DisplayName)), ("entity", entity))); } private void UpdateGenerator(float frameTime) { var query = EntityQueryEnumerator(); var curTime = _timing.CurTime; while (query.MoveNext(out var uid, out var generator)) { if (generator.NextGenerationTime > curTime) return; generator.NextGenerationTime += generator.GenerationDelay; DirtyField(uid, generator, nameof(StoreCurrencyGeneratorComponent.NextGenerationTime)); if (!generator.Enabled) return; if (generator.Amount >= generator.MaxAmount) return; generator.Amount += generator.GeneratedAmount; DirtyField(uid, generator, nameof(StoreCurrencyGeneratorComponent.Amount)); } } private void CollectGenerator(Entity generator, Entity collector, EntityUid user, CurrencyPrototype proto) { Dictionary, FixedPoint2> currency = new(); currency.Add(proto.ID, generator.Comp.Amount); if (TryAddCurrency(currency, collector, collector.Comp)) { Popup.PopupEntity(Loc.GetString(generator.Comp.CollectPopup, ("amount", generator.Comp.Amount), ("currency", Loc.GetString(proto.DisplayName)), ("entity", generator)), generator, user); generator.Comp.Amount = 0; DirtyField(generator, generator.Comp, nameof(StoreCurrencyGeneratorComponent.Amount)); _audio.PlayPredicted(generator.Comp.CollectSound, generator, user); } } private void AttemptCollection(Entity entity, EntityUid collector, EntityUid user) { if (!TryComp(collector, out var storeComp)) return; if (!_whitelist.CheckBoth(collector, entity.Comp.Blacklist, entity.Comp.Whitelist)) return; if (!ProtoMan.TryIndex(entity.Comp.Currency, out var proto)) return; if (entity.Comp.Amount == 0) { Popup.PopupEntity(Loc.GetString(entity.Comp.EmptyPopup), entity, user); return; } CollectGenerator(entity, (collector, storeComp), collector, proto); } }