Power Cell Refactor (#5943)

Co-authored-by: ShadowCommander <10494922+ShadowCommander@users.noreply.github.com>
This commit is contained in:
Leon Friedrich
2022-01-05 17:20:25 +13:00
committed by GitHub
parent 4eddefdda1
commit 0aa4f9efbe
37 changed files with 673 additions and 987 deletions

View File

@@ -1,13 +1,11 @@
using Content.Server.Power.Components;
using Content.Server.Weapon.Ranged.Barrels.Components;
using Content.Shared.ActionBlocker;
using Content.Shared.Popups;
using Content.Shared.PowerCell.Components;
using Content.Shared.Verbs;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using System;
namespace Content.Server.Weapon.Ranged.Barrels
{
@@ -18,11 +16,9 @@ namespace Content.Server.Weapon.Ranged.Barrels
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<RevolverBarrelComponent, GetAlternativeVerbsEvent>(AddSpinVerb);
SubscribeLocalEvent<ServerBatteryBarrelComponent, EntInsertedIntoContainerMessage>(OnCellSlotUpdated);
SubscribeLocalEvent<ServerBatteryBarrelComponent, EntRemovedFromContainerMessage>(OnCellSlotUpdated);
SubscribeLocalEvent<ServerBatteryBarrelComponent, PowerCellChangedEvent>(OnCellSlotUpdated);
SubscribeLocalEvent<BoltActionBarrelComponent, GetInteractionVerbsEvent>(AddToggleBoltVerb);
@@ -30,10 +26,9 @@ namespace Content.Server.Weapon.Ranged.Barrels
SubscribeLocalEvent<ServerMagazineBarrelComponent, GetAlternativeVerbsEvent>(AddEjectMagazineVerb);
}
private void OnCellSlotUpdated(EntityUid uid, ServerBatteryBarrelComponent component, ContainerModifiedMessage args)
private void OnCellSlotUpdated(EntityUid uid, ServerBatteryBarrelComponent component, PowerCellChangedEvent args)
{
if (args.Container.ID == component.CellSlot.ID)
component.UpdateAppearance();
component.UpdateAppearance();
}
private void AddSpinVerb(EntityUid uid, RevolverBarrelComponent component, GetAlternativeVerbsEvent args)

View File

@@ -1,15 +1,12 @@
using System;
using Content.Server.Power.Components;
using Content.Server.PowerCell;
using Content.Server.Projectiles.Components;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.Weapons.Ranged.Barrels.Components;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Player;
using Robust.Shared.Players;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
@@ -25,9 +22,6 @@ namespace Content.Server.Weapon.Ranged.Barrels.Components
public override string Name => "BatteryBarrel";
[DataField("cellSlot", required: true)]
public ItemSlot CellSlot = new();
// The minimum change we need before we can fire
[DataField("lowerChargeLimit")]
[ViewVariables] private float _lowerChargeLimit = 10;
@@ -37,19 +31,19 @@ namespace Content.Server.Weapon.Ranged.Barrels.Components
[DataField("ammoPrototype", customTypeSerializer:typeof(PrototypeIdSerializer<EntityPrototype>))]
[ViewVariables] private string? _ammoPrototype;
public BatteryComponent? PowerCell => _entities.GetComponentOrNull<BatteryComponent>(CellSlot.Item);
private ContainerSlot _ammoContainer = default!;
public override int ShotsLeft
{
get
{
if (CellSlot.Item is not {} powerCell)
if (!EntitySystem.Get<PowerCellSystem>().TryGetBatteryFromSlot(Owner, out var battery))
{
return 0;
}
return (int) Math.Ceiling(_entities.GetComponent<BatteryComponent>(powerCell).CurrentCharge / _baseFireCost);
return (int) Math.Ceiling(battery.CurrentCharge / _baseFireCost);
}
}
@@ -57,12 +51,12 @@ namespace Content.Server.Weapon.Ranged.Barrels.Components
{
get
{
if (CellSlot.Item is not {} powerCell)
if (!EntitySystem.Get<PowerCellSystem>().TryGetBatteryFromSlot(Owner, out var battery))
{
return 0;
}
return (int) Math.Ceiling(_entities.GetComponent<BatteryComponent>(powerCell).MaxCharge / _baseFireCost);
return (int) Math.Ceiling(battery.MaxCharge / _baseFireCost);
}
}
@@ -81,8 +75,6 @@ namespace Content.Server.Weapon.Ranged.Barrels.Components
{
base.Initialize();
EntitySystem.Get<ItemSlotsSystem>().AddItemSlot(Owner, $"{Name}-powercell-container", CellSlot);
if (_ammoPrototype != null)
{
_ammoContainer = Owner.EnsureContainer<ContainerSlot>($"{Name}-ammo-container");
@@ -95,12 +87,6 @@ namespace Content.Server.Weapon.Ranged.Barrels.Components
Dirty();
}
protected override void OnRemove()
{
base.OnRemove();
EntitySystem.Get<ItemSlotsSystem>().RemoveItemSlot(Owner, CellSlot);
}
protected override void Startup()
{
base.Startup();
@@ -109,7 +95,7 @@ namespace Content.Server.Weapon.Ranged.Barrels.Components
public void UpdateAppearance()
{
_appearanceComponent?.SetData(MagazineBarrelVisuals.MagLoaded, CellSlot.HasItem);
_appearanceComponent?.SetData(MagazineBarrelVisuals.MagLoaded, EntitySystem.Get<PowerCellSystem>().TryGetBatteryFromSlot(Owner, out _));
_appearanceComponent?.SetData(AmmoVisuals.AmmoCount, ShotsLeft);
_appearanceComponent?.SetData(AmmoVisuals.AmmoMax, Capacity);
Dirty();
@@ -131,14 +117,9 @@ namespace Content.Server.Weapon.Ranged.Barrels.Components
public override EntityUid? TakeProjectile(EntityCoordinates spawnAt)
{
var powerCellEntity = CellSlot.Item;
if (powerCellEntity == null)
{
if (!EntitySystem.Get<PowerCellSystem>().TryGetBatteryFromSlot(Owner, out var capacitor))
return null;
}
var capacitor = _entities.GetComponent<BatteryComponent>(powerCellEntity.Value);
if (capacitor.CurrentCharge < _lowerChargeLimit)
{
return null;

View File

@@ -1,49 +0,0 @@
using Content.Server.Power.Components;
using Content.Server.PowerCell.Components;
using Content.Server.Weapon.Ranged.Barrels.Components;
using Content.Shared.Interaction;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
namespace Content.Server.Weapon
{
/// <summary>
/// Recharges the battery in a <see cref="ServerBatteryBarrelComponent"/>.
/// </summary>
[RegisterComponent]
[ComponentReference(typeof(IActivate))]
[ComponentReference(typeof(BaseCharger))]
public sealed class WeaponCapacitorChargerComponent : BaseCharger
{
[Dependency] private readonly IEntityManager _entMan = default!;
public override string Name => "WeaponCapacitorCharger";
public override bool IsEntityCompatible(EntityUid entity)
{
return _entMan.TryGetComponent(entity, out ServerBatteryBarrelComponent? battery) && battery.PowerCell != null ||
_entMan.TryGetComponent(entity, out PowerCellSlotComponent? slot) && slot.HasCell;
}
protected override BatteryComponent? GetBatteryFrom(EntityUid entity)
{
if (_entMan.TryGetComponent(entity, out PowerCellSlotComponent? slot))
{
if (slot.Cell != null)
{
return slot.Cell;
}
}
if (_entMan.TryGetComponent(entity, out ServerBatteryBarrelComponent? battery))
{
if (battery.PowerCell != null)
{
return battery.PowerCell;
}
}
return null;
}
}
}