From eba5d2fa6168d388a0d2ecddb3fd95af547c9654 Mon Sep 17 00:00:00 2001 From: FN Date: Mon, 20 May 2024 14:46:14 +0700 Subject: [PATCH 1/2] Enhanced translator discharge --- .../EntitySystems/LanguageTranslatorSystem.cs | 28 +++++++++++++++++-- .../Components/LanguageTranslatorComponent.cs | 2 +- .../Entities/Objects/Devices/translator.yml | 2 +- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Content.Server/Corvax/Language/EntitySystems/LanguageTranslatorSystem.cs b/Content.Server/Corvax/Language/EntitySystems/LanguageTranslatorSystem.cs index 39d7c1d9abb..4f1ad604c4b 100644 --- a/Content.Server/Corvax/Language/EntitySystems/LanguageTranslatorSystem.cs +++ b/Content.Server/Corvax/Language/EntitySystems/LanguageTranslatorSystem.cs @@ -1,4 +1,5 @@ using System.Diagnostics.CodeAnalysis; +using Content.Server.Power.EntitySystems; using Content.Server.PowerCell; using Content.Shared.Actions; using Content.Shared.Corvax.Language.Components; @@ -15,6 +16,7 @@ public sealed class LanguageTranslatorSystem : EntitySystem { [Dependency] private readonly InventorySystem _inventory = default!; [Dependency] private readonly PowerCellSystem _power = default!; + [Dependency] private readonly BatterySystem _battery = default!; [Dependency] private readonly AppearanceSystem _appearance = default!; public override void Initialize() @@ -66,12 +68,29 @@ private void Toggle(EntityUid entity, LanguageTranslatorComponent component) { component.Activated = !component.Activated; - _appearance.SetData(entity, LanguageTranslatorVisuals.Activated, component.Activated); + UpdateVisuals(new(entity, component)); } public bool TryUseTranslator(EntityUid entity, string message) { - return TryGetTranslator(_inventory.GetHandOrInventoryEntities(entity), out var translator) && translator.Value.Comp.Activated && _power.TryUseCharge(translator.Value, 0.2f * message.Length); + if (!TryGetTranslator(_inventory.GetHandOrInventoryEntities(entity), out var translator) || !translator.Value.Comp.Activated) + return false; + + if (_power.TryUseCharge(translator.Value, 0.2f * message.Length)) + { + UpdateVisuals(translator.Value); + + return true; + } + + if (_power.TryGetBatteryFromSlot(entity, out var battery, out var batteryComponent)) + { + _battery.SetCharge(battery.Value, 0, batteryComponent); + + UpdateVisuals(translator.Value); + } + + return false; } private bool TryGetTranslator(IEnumerable entities, [NotNullWhen(true)] out Entity? translator) @@ -91,4 +110,9 @@ private bool TryGetTranslator(IEnumerable entities, [NotNullWhen(true translator = null; return false; } + + public void UpdateVisuals(Entity entity) + { + _appearance.SetData(entity, LanguageTranslatorVisuals.Enabled, entity.Comp.Activated && _power.TryGetBatteryFromSlot(entity, out var battery) && battery.CurrentCharge > 0); + } } diff --git a/Content.Shared/Corvax/Language/Components/LanguageTranslatorComponent.cs b/Content.Shared/Corvax/Language/Components/LanguageTranslatorComponent.cs index de34d41eb07..db959b46b68 100644 --- a/Content.Shared/Corvax/Language/Components/LanguageTranslatorComponent.cs +++ b/Content.Shared/Corvax/Language/Components/LanguageTranslatorComponent.cs @@ -14,5 +14,5 @@ public sealed partial class LanguageTranslatorComponent : Component public enum LanguageTranslatorVisuals { Layer, - Activated + Enabled } diff --git a/Resources/Prototypes/Corvax/Entities/Objects/Devices/translator.yml b/Resources/Prototypes/Corvax/Entities/Objects/Devices/translator.yml index 4ecb7a3ceeb..a3ce836021b 100644 --- a/Resources/Prototypes/Corvax/Entities/Objects/Devices/translator.yml +++ b/Resources/Prototypes/Corvax/Entities/Objects/Devices/translator.yml @@ -12,7 +12,7 @@ - type: Appearance - type: GenericVisualizer visuals: - enum.LanguageTranslatorVisuals.Activated: + enum.LanguageTranslatorVisuals.Enabled: enum.LanguageTranslatorVisuals.Layer: True: { state: icon } False: { state: icon_off } From 1a1e8b038734b25041286e7d154248a7b9bdbcd8 Mon Sep 17 00:00:00 2001 From: FN Date: Wed, 22 May 2024 00:25:22 +0700 Subject: [PATCH 2/2] Fixed discharge --- .../EntitySystems/LanguageTranslatorSystem.cs | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/Content.Server/Corvax/Language/EntitySystems/LanguageTranslatorSystem.cs b/Content.Server/Corvax/Language/EntitySystems/LanguageTranslatorSystem.cs index 4f1ad604c4b..a3904b6edb5 100644 --- a/Content.Server/Corvax/Language/EntitySystems/LanguageTranslatorSystem.cs +++ b/Content.Server/Corvax/Language/EntitySystems/LanguageTranslatorSystem.cs @@ -1,4 +1,5 @@ using System.Diagnostics.CodeAnalysis; +using Content.Server.Power.Components; using Content.Server.Power.EntitySystems; using Content.Server.PowerCell; using Content.Shared.Actions; @@ -9,6 +10,7 @@ using Content.Shared.Toggleable; using Content.Shared.Verbs; using Robust.Server.GameObjects; +using Robust.Shared.Containers; namespace Content.Server.Corvax.Language.EntitySystems; @@ -27,6 +29,10 @@ public override void Initialize() SubscribeLocalEvent(OnToggleAction); SubscribeLocalEvent>(OnGetVerbs); + + SubscribeLocalEvent(OnChargeChanged); + SubscribeLocalEvent(OnInsert); + SubscribeLocalEvent(OnRemove); } private void OnActivateInWorld(EntityUid entity, LanguageTranslatorComponent component, ActivateInWorldEvent e) @@ -71,25 +77,38 @@ private void Toggle(EntityUid entity, LanguageTranslatorComponent component) UpdateVisuals(new(entity, component)); } + private void OnChargeChanged(EntityUid entity, BatteryComponent component, ChargeChangedEvent e) + { + TryUpdateVisuals(Transform(entity).ParentUid); + } + + private void OnInsert(EntityUid entity, BatteryComponent component, EntGotInsertedIntoContainerMessage e) + { + TryUpdateVisuals(e.Container.Owner); + } + + private void OnRemove(EntityUid entity, BatteryComponent component, EntGotRemovedFromContainerMessage e) + { + TryUpdateVisuals(e.Container.Owner); + } + + private void TryUpdateVisuals(EntityUid entity) + { + if (TryComp(entity, out var translatorComponent)) + UpdateVisuals(new(entity, translatorComponent)); + } + public bool TryUseTranslator(EntityUid entity, string message) { if (!TryGetTranslator(_inventory.GetHandOrInventoryEntities(entity), out var translator) || !translator.Value.Comp.Activated) return false; if (_power.TryUseCharge(translator.Value, 0.2f * message.Length)) - { - UpdateVisuals(translator.Value); - return true; - } - if (_power.TryGetBatteryFromSlot(entity, out var battery, out var batteryComponent)) - { + if (_power.TryGetBatteryFromSlot(translator.Value, out var battery, out var batteryComponent)) _battery.SetCharge(battery.Value, 0, batteryComponent); - UpdateVisuals(translator.Value); - } - return false; }