Adds sized (S, M, L) power cells and a generic component for battery powered items (#2352)

* Refactor battery/powercell assets and add new ones.

* committing before I fuck things up

* slot component doned I think

* dictionary update

* Fixes

* Moving flashlight to powerslotcomponent

* har har i am using the message tubes

* Better documentation comment

* Reverting this overengineered garbage.

* Off with ye I said

* Examine texts.

* Some minor fixes to IDE complaints

* slot size from yaml

* Ignored component + removing a useless typo entry

* Making stunbatons use this

* Handle the message and remove some unnecessary dirtiness

* actionblocker checks

* remove unused file

* remove updatevisual

* make these nullable

* make these nullable too

* Unrename sprite folder

* check itemcomponent on insertion

* Use SendMessage over Owner.SendMessage

* Add support for auto-recharging batteries, an auto-recharging cell, and make flashlight status update correctly if one is inserted in it.

* get rid of public fields which are Bad

* add a description for the stun baton while i'm in here

* one more public field

* Add the blinky animation to the atomic cell

* Fix the charge indicator being STUPID

* better comments

* this is a better function

* add pause for flashlight, remove unnecessary imports from battery

* potato battery copyright link

* WHO DID THAT

* mr clean has come

* Random pitch

* pausing

* round to nearest levels
This commit is contained in:
Peter Wedder
2020-10-29 20:17:03 +02:00
committed by GitHub
parent 60bee860cb
commit fca556a1c1
81 changed files with 1328 additions and 284 deletions

View File

@@ -1,52 +1,33 @@
#nullable enable
using System.Threading.Tasks;
using Content.Server.GameObjects.Components.GUI;
using Content.Server.GameObjects.Components.Items.Clothing;
using Content.Server.GameObjects.Components.Items.Storage;
using Content.Server.GameObjects.Components.Power;
using Content.Server.Interfaces.GameObjects.Components.Items;
using Content.Shared.GameObjects.Components;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.GameObjects.Verbs;
using Content.Shared.Interfaces;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.Components.Container;
using Robust.Server.GameObjects.EntitySystems;
using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Interactable
{
/// <summary>
/// Component that represents a handheld lightsource which can be toggled on and off.
/// Component that represents a powered handheld light source which can be toggled on and off.
/// </summary>
[RegisterComponent]
internal sealed class HandheldLightComponent : SharedHandheldLightComponent, IUse, IExamine, IInteractUsing,
IMapInit
internal sealed class HandheldLightComponent : SharedHandheldLightComponent, IUse, IExamine, IInteractUsing
{
[ViewVariables(VVAccess.ReadWrite)] public float Wattage { get; set; } = 10;
[ViewVariables] private ContainerSlot _cellContainer = default!;
[ViewVariables]
private BatteryComponent? Cell
{
get
{
if (_cellContainer.ContainedEntity == null) return null;
if (_cellContainer.ContainedEntity.TryGetComponent(out BatteryComponent? cell))
{
return cell;
}
return null;
}
}
[ViewVariables(VVAccess.ReadWrite)] public float Wattage { get; set; } = 10f;
[ViewVariables] private PowerCellSlotComponent _cellSlot = default!;
private PowerCellComponent? Cell => _cellSlot.Cell;
/// <summary>
/// Status of light, whether or not it is emitting light.
@@ -54,26 +35,36 @@ namespace Content.Server.GameObjects.Components.Interactable
[ViewVariables]
public bool Activated { get; private set; }
[ViewVariables] protected override bool HasCell => Cell != null;
[ViewVariables] protected override bool HasCell => _cellSlot.HasCell;
[ViewVariables(VVAccess.ReadWrite)] public string? TurnOnSound;
[ViewVariables(VVAccess.ReadWrite)] public string? TurnOnFailSound;
[ViewVariables(VVAccess.ReadWrite)] public string? TurnOffSound;
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(this, x => x.Wattage, "wattage", 10f);
serializer.DataField(ref TurnOnSound, "turnOnSound", "/Audio/Items/flashlight_toggle.ogg");
serializer.DataField(ref TurnOnFailSound, "turnOnFailSound", "/Audio/Machines/button.ogg");
serializer.DataField(ref TurnOffSound, "turnOffSound", "/Audio/Items/flashlight_toggle.ogg");
}
public override void Initialize()
{
base.Initialize();
Owner.EnsureComponent<PointLightComponent>();
_cellSlot = Owner.EnsureComponent<PowerCellSlotComponent>();
Dirty();
}
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
{
if (!eventArgs.Using.HasComponent<BatteryComponent>()) return false;
if (Cell != null) return false;
var handsComponent = eventArgs.User.GetComponent<IHandsComponent>();
if (!handsComponent.Drop(eventArgs.Using, _cellContainer))
{
return false;
}
EntitySystem.Get<AudioSystem>().PlayFromEntity("/Audio/Items/pistol_magin.ogg", Owner);
if (!ActionBlockerSystem.CanInteract(eventArgs.User)) return false;
if (!_cellSlot.InsertCell(eventArgs.Using)) return false;
Dirty();
return true;
}
@@ -83,6 +74,10 @@ namespace Content.Server.GameObjects.Components.Interactable
{
message.AddMarkup(Loc.GetString("The light is currently [color=darkgreen]on[/color]."));
}
else
{
message.AddMarkup(Loc.GetString("The light is currently [color=darkred]off[/color]."));
}
}
bool IUse.UseEntity(UseEntityEventArgs eventArgs)
@@ -90,45 +85,20 @@ namespace Content.Server.GameObjects.Components.Interactable
return ToggleStatus(eventArgs.User);
}
public override void Initialize()
{
base.Initialize();
Owner.EnsureComponent<PointLightComponent>();
_cellContainer =
ContainerManagerComponent.Ensure<ContainerSlot>("flashlight_cell_container", Owner, out _);
Dirty();
}
/// <summary>
/// Illuminates the light if it is not active, extinguishes it if it is active.
/// </summary>
/// <returns>True if the light's status was toggled, false otherwise.</returns>
private bool ToggleStatus(IEntity user)
{
var item = Owner.GetComponent<ItemComponent>();
// Update sprite and light states to match the activation.
if (Activated)
{
TurnOff();
item.EquippedPrefix = "off";
}
else
{
TurnOn(user);
item.EquippedPrefix = "on";
}
// Toggle always succeeds.
return true;
return Activated ? TurnOff() : TurnOn(user);
}
private void TurnOff(bool makeNoise = true)
private bool TurnOff(bool makeNoise = true)
{
if (!Activated)
{
return;
return false;
}
SetState(false);
@@ -136,40 +106,41 @@ namespace Content.Server.GameObjects.Components.Interactable
if (makeNoise)
{
EntitySystem.Get<AudioSystem>().PlayFromEntity("/Audio/Items/flashlight_toggle.ogg", Owner);
if (TurnOffSound != null) EntitySystem.Get<AudioSystem>().PlayFromEntity(TurnOffSound, Owner);
}
return true;
}
private void TurnOn(IEntity user)
private bool TurnOn(IEntity user)
{
if (Activated)
{
return;
return false;
}
var cell = Cell;
if (cell == null)
if (Cell == null)
{
EntitySystem.Get<AudioSystem>().PlayFromEntity("/Audio/Machines/button.ogg", Owner);
if (TurnOnFailSound != null) EntitySystem.Get<AudioSystem>().PlayFromEntity(TurnOnFailSound, Owner);
Owner.PopupMessage(user, Loc.GetString("Cell missing..."));
return;
return false;
}
// To prevent having to worry about frame time in here.
// Let's just say you need a whole second of charge before you can turn it on.
// Simple enough.
if (Wattage > cell.CurrentCharge)
if (Wattage > Cell.CurrentCharge)
{
EntitySystem.Get<AudioSystem>().PlayFromEntity("/Audio/Machines/button.ogg", Owner);
if (TurnOnFailSound != null) EntitySystem.Get<AudioSystem>().PlayFromEntity(TurnOnFailSound, Owner);
Owner.PopupMessage(user, Loc.GetString("Dead cell..."));
return;
return false;
}
Activated = true;
SetState(true);
EntitySystem.Get<AudioSystem>().PlayFromEntity("/Audio/Items/flashlight_toggle.ogg", Owner);
if (TurnOnSound != null) EntitySystem.Get<AudioSystem>().PlayFromEntity(TurnOnSound, Owner);
return true;
}
private void SetState(bool on)
@@ -188,11 +159,20 @@ namespace Content.Server.GameObjects.Components.Interactable
{
clothing.ClothingEquippedPrefix = on ? "On" : "Off";
}
if (Owner.TryGetComponent(out ItemComponent? item))
{
item.EquippedPrefix = on ? "on" : "off";
}
}
public void OnUpdate(float frameTime)
{
if (!Activated || Cell == null) return;
if (Cell == null)
{
TurnOff(false);
return;
}
var appearanceComponent = Owner.GetComponent<AppearanceComponent>();
@@ -209,43 +189,10 @@ namespace Content.Server.GameObjects.Components.Interactable
appearanceComponent.SetData(HandheldLightVisuals.Power, HandheldLightPowerStates.Dying);
}
if (Cell == null || !Cell.TryUseCharge(Wattage * frameTime)) TurnOff();
if (Activated && !Cell.TryUseCharge(Wattage * frameTime)) TurnOff(false);
Dirty();
}
private void EjectCell(IEntity user)
{
if (Cell == null)
{
return;
}
var cell = Cell;
if (!_cellContainer.Remove(cell.Owner))
{
return;
}
Dirty();
if (!user.TryGetComponent(out HandsComponent? hands))
{
return;
}
if (!hands.PutInHand(cell.Owner.GetComponent<ItemComponent>()))
{
cell.Owner.Transform.Coordinates = user.Transform.Coordinates;
}
// Assuming the battery has just been taken out of the flashlight, make sure it's getting disabled
TurnOff(false);
EntitySystem.Get<AudioSystem>().PlayFromEntity("/Audio/Items/pistol_magout.ogg", Owner);
}
public override ComponentState GetComponentState()
{
if (Cell == null)
@@ -262,44 +209,5 @@ namespace Content.Server.GameObjects.Components.Interactable
return new HandheldLightComponentState(Cell.CurrentCharge / Cell.MaxCharge, HasCell);
}
[Verb]
public sealed class EjectCellVerb : Verb<HandheldLightComponent>
{
protected override void GetData(IEntity user, HandheldLightComponent component, VerbData data)
{
if (!ActionBlockerSystem.CanInteract(user))
{
data.Visibility = VerbVisibility.Invisible;
return;
}
if (component.Cell == null)
{
data.Text = Loc.GetString("Eject cell (cell missing)");
data.Visibility = VerbVisibility.Disabled;
}
else
{
data.Text = Loc.GetString("Eject cell");
}
}
protected override void Activate(IEntity user, HandheldLightComponent component)
{
component.EjectCell(user);
}
}
void IMapInit.MapInit()
{
if (_cellContainer.ContainedEntity != null)
{
return;
}
var cell = Owner.EntityManager.SpawnEntity("PowerCellSmallStandard", Owner.Transform.Coordinates);
_cellContainer.Insert(cell);
}
}
}