Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Enhance translator discharge #199

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Server.PowerCell;
using Content.Shared.Actions;
using Content.Shared.Corvax.Language.Components;
Expand All @@ -8,13 +10,15 @@
using Content.Shared.Toggleable;
using Content.Shared.Verbs;
using Robust.Server.GameObjects;
using Robust.Shared.Containers;

namespace Content.Server.Corvax.Language.EntitySystems;

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()
Expand All @@ -25,6 +29,10 @@ public override void Initialize()
SubscribeLocalEvent<LanguageTranslatorComponent, ToggleActionEvent>(OnToggleAction);

SubscribeLocalEvent<LanguageTranslatorComponent, GetVerbsEvent<ActivationVerb>>(OnGetVerbs);

SubscribeLocalEvent<BatteryComponent, ChargeChangedEvent>(OnChargeChanged);
SubscribeLocalEvent<BatteryComponent, EntGotInsertedIntoContainerMessage>(OnInsert);
SubscribeLocalEvent<BatteryComponent, EntGotRemovedFromContainerMessage>(OnRemove);
}

private void OnActivateInWorld(EntityUid entity, LanguageTranslatorComponent component, ActivateInWorldEvent e)
Expand Down Expand Up @@ -66,12 +74,42 @@ private void Toggle(EntityUid entity, LanguageTranslatorComponent component)
{
component.Activated = !component.Activated;

_appearance.SetData(entity, LanguageTranslatorVisuals.Activated, component.Activated);
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<LanguageTranslatorComponent>(entity, out var translatorComponent))
UpdateVisuals(new(entity, translatorComponent));
}

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))
return true;

if (_power.TryGetBatteryFromSlot(translator.Value, out var battery, out var batteryComponent))
_battery.SetCharge(battery.Value, 0, batteryComponent);

return false;
}

private bool TryGetTranslator(IEnumerable<EntityUid> entities, [NotNullWhen(true)] out Entity<LanguageTranslatorComponent>? translator)
Expand All @@ -91,4 +129,9 @@ private bool TryGetTranslator(IEnumerable<EntityUid> entities, [NotNullWhen(true
translator = null;
return false;
}

public void UpdateVisuals(Entity<LanguageTranslatorComponent> entity)
{
_appearance.SetData(entity, LanguageTranslatorVisuals.Enabled, entity.Comp.Activated && _power.TryGetBatteryFromSlot(entity, out var battery) && battery.CurrentCharge > 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ public sealed partial class LanguageTranslatorComponent : Component
public enum LanguageTranslatorVisuals
{
Layer,
Activated
Enabled
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Loading