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);
}
}
}

View File

@@ -11,14 +11,28 @@ namespace Content.Server.GameObjects.Components.Power
{
public override string Name => "Battery";
/// <summary>
/// Maximum charge of the battery in joules (ie. watt seconds)
/// </summary>
[ViewVariables(VVAccess.ReadWrite)] public int MaxCharge { get => _maxCharge; set => SetMaxCharge(value); }
private int _maxCharge;
/// <summary>
/// Current charge of the battery in joules (ie. watt seconds)
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public float CurrentCharge { get => _currentCharge; set => SetCurrentCharge(value); }
private float _currentCharge;
/// <summary>
/// True if the battery is fully charged.
/// </summary>
[ViewVariables] public bool IsFullyCharged => MathHelper.CloseTo(CurrentCharge, MaxCharge);
[ViewVariables(VVAccess.ReadWrite)] public bool AutoRecharge { get; set; }
[ViewVariables(VVAccess.ReadWrite)] public float AutoRechargeRate { get; set; }
[ViewVariables] public BatteryState BatteryState { get; private set; }
public override void ExposeData(ObjectSerializer serializer)
@@ -26,6 +40,8 @@ namespace Content.Server.GameObjects.Components.Power
base.ExposeData(serializer);
serializer.DataField(ref _maxCharge, "maxCharge", 1000);
serializer.DataField(ref _currentCharge, "startingCharge", 500);
serializer.DataField(this, x => x.AutoRecharge, "autoRecharge", false);
serializer.DataField(this, x => x.AutoRechargeRate, "autoRechargeRate", 0);
}
public override void Initialize()
@@ -75,7 +91,7 @@ namespace Content.Server.GameObjects.Components.Power
private void UpdateStorageState()
{
if (CurrentCharge == MaxCharge)
if (IsFullyCharged)
{
BatteryState = BatteryState.Full;
}
@@ -103,6 +119,13 @@ namespace Content.Server.GameObjects.Components.Power
UpdateStorageState();
OnChargeChanged();
}
public void OnUpdate(float frameTime)
{
if (!AutoRecharge) return;
if (IsFullyCharged) return;
CurrentCharge += AutoRechargeRate * frameTime;
}
}
public enum BatteryState

View File

@@ -1,18 +1,33 @@
using Content.Shared.GameObjects.Components.Power;
using Content.Shared.GameObjects.EntitySystems;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Power
{
/// <summary>
/// Batteries that have update an <see cref="AppearanceComponent"/> based on their charge percent.
/// Batteries that can update an <see cref="AppearanceComponent"/> based on their charge percent
/// and fit into a <see cref="PowerCellSlotComponent"/> of the appropriate size.
/// </summary>
[RegisterComponent]
[ComponentReference(typeof(BatteryComponent))]
public class PowerCellComponent : BatteryComponent
public class PowerCellComponent : BatteryComponent, IExamine
{
public override string Name => "PowerCell";
[ViewVariables] public PowerCellSize CellSize => _cellSize;
private PowerCellSize _cellSize = PowerCellSize.Small;
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _cellSize, "cellSize", PowerCellSize.Small);
}
public override void Initialize()
{
base.Initialize();
@@ -33,5 +48,20 @@ namespace Content.Server.GameObjects.Components.Power
appearance.SetData(PowerCellVisuals.ChargeLevel, CurrentCharge / MaxCharge);
}
}
void IExamine.Examine(FormattedMessage message, bool inDetailsRange)
{
if(inDetailsRange)
{
message.AddMarkup(Loc.GetString($"The charge indicator reads {CurrentCharge / MaxCharge * 100:F0} %."));
}
}
}
public enum PowerCellSize
{
Small,
Medium,
Large
}
}

View File

@@ -0,0 +1,254 @@
#nullable enable
using System;
using Content.Server.GameObjects.Components.GUI;
using Content.Server.GameObjects.Components.Items.Storage;
using Content.Shared.Audio;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.GameObjects.Verbs;
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.Power
{
/// <summary>
/// Provides a "battery compartment" that can contain a <see cref="PowerCellComponent"/> of the matching
/// <see cref="PowerCellSize"/>. Intended to supplement other components, not very useful by itself.
/// </summary>
[RegisterComponent]
public class PowerCellSlotComponent : Component, IExamine, IMapInit
{
public override string Name => "PowerCellSlot";
/// <summary>
/// What size of cell fits into this component.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public PowerCellSize SlotSize { get; set; } = PowerCellSize.Small;
/// <summary>
/// Can the cell be removed ?
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public bool CanRemoveCell { get; set; } = true;
/// <summary>
/// Should the "Remove cell" verb be displayed on this component?
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public bool ShowVerb { get; set; } = true;
/// <summary>
/// String passed to <see><cref>String.Format</cref></see> when showing the description text for this item.
/// String.Format is given a single parameter which is the size letter (S/M/L) of the cells this component uses.
/// Use null to show no text.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public string? DescFormatString { get; set; }
/// <summary>
/// File path to a sound file that should be played when the cell is removed.
/// </summary>
/// <example>"/Audio/Items/pistol_magout.ogg"</example>
[ViewVariables(VVAccess.ReadWrite)]
public string? CellRemoveSound { get; set; }
/// <summary>
/// File path to a sound file that should be played when a cell is inserted.
/// </summary>
/// <example>"/Audio/Items/pistol_magin.ogg"</example>
[ViewVariables(VVAccess.ReadWrite)]
public string? CellInsertSound { get; set; }
[ViewVariables] private ContainerSlot _cellContainer = default!;
[ViewVariables]
public PowerCellComponent? Cell
{
get
{
if (_cellContainer.ContainedEntity == null) return null;
return _cellContainer.ContainedEntity.TryGetComponent(out PowerCellComponent? cell) ? cell : null;
}
}
[ViewVariables] public bool HasCell => Cell != null;
/// <summary>
/// True if we don't want a cell inserted during map init.
/// </summary>
private bool _startEmpty = false;
/// <summary>
/// If not null, this cell type will be inserted at MapInit instead of the default Standard cell.
/// </summary>
private string? _startingCellType = null;
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(this, x => x.SlotSize, "slotSize", PowerCellSize.Small);
serializer.DataField(this, x => x.CanRemoveCell, "canRemoveCell", true);
serializer.DataField(this, x => x.ShowVerb, "showVerb", true);
serializer.DataField(ref _startEmpty, "startEmpty", false);
serializer.DataField(ref _startingCellType, "startingCellType", null);
serializer.DataField(this, x => x.CellRemoveSound, "cellRemoveSound", "/Audio/Items/pistol_magin.ogg");
serializer.DataField(this, x => x.CellInsertSound, "cellInsertSound", "/Audio/Items/pistol_magout.ogg");
serializer.DataField(this, x => x.DescFormatString, "descFormatString", "It uses size {0} power cells.");
}
public override void Initialize()
{
base.Initialize();
_cellContainer = ContainerManagerComponent.Ensure<ContainerSlot>("cellslot_cell_container", Owner, out _);
}
void IExamine.Examine(FormattedMessage message, bool inDetailsRange)
{
if (!inDetailsRange) return;
string sizeLetter = SlotSize switch
{
PowerCellSize.Small => Loc.GetString("S"),
PowerCellSize.Medium => Loc.GetString("M"),
PowerCellSize.Large => Loc.GetString("L"),
_ => "???"
};
if (DescFormatString != null) message.AddMarkup(string.Format(DescFormatString, sizeLetter));
}
/// <summary>
/// Remove the cell from this component. If a user is specified, the cell will be put in their hands
/// or failing that, at their feet. If no user is specified the cell will be put at the location of
/// the parent of this component.
/// </summary>
/// <param name="user">(optional) the user to give the removed cell to.</param>
/// <param name="playSound">Should <see cref="CellRemoveSound"/> be played upon removal?</param>
/// <returns>The cell component of the entity that was removed, or null if removal failed.</returns>
public PowerCellComponent? EjectCell(IEntity? user, bool playSound = true)
{
var cell = Cell;
if (cell == null || !CanRemoveCell) return null;
if (!_cellContainer.Remove(cell.Owner)) return null;
//Dirty();
if (user != null)
{
if (!user.TryGetComponent(out HandsComponent? hands) || !hands.PutInHand(cell.Owner.GetComponent<ItemComponent>()))
{
cell.Owner.Transform.Coordinates = user.Transform.Coordinates;
}
}
else
{
cell.Owner.Transform.Coordinates = Owner.Transform.Coordinates;
}
if (playSound && CellRemoveSound != null)
{
EntitySystem.Get<AudioSystem>().PlayFromEntity(CellRemoveSound, Owner, AudioHelpers.WithVariation(0.125f));
}
SendMessage(new PowerCellChangedMessage(true));
return cell;
}
/// <summary>
/// Tries to insert the given cell into this component. The cell will be put into the container of this component.
/// </summary>
/// <param name="cell">The cell to insert.</param>
/// <param name="playSound">Should <see cref="CellInsertSound"/> be played upon insertion?</param>
/// <returns>True if insertion succeeded; false otherwise.</returns>
public bool InsertCell(IEntity cell, bool playSound = true)
{
if (Cell != null) return false;
if (!cell.TryGetComponent<ItemComponent>(out var _)) return false;
if (!cell.TryGetComponent<PowerCellComponent>(out var cellComponent)) return false;
if (cellComponent.CellSize != SlotSize) return false;
if (!_cellContainer.Insert(cell)) return false;
//Dirty();
if (playSound && CellInsertSound != null)
{
EntitySystem.Get<AudioSystem>().PlayFromEntity(CellInsertSound, Owner, AudioHelpers.WithVariation(0.125f));
}
SendMessage(new PowerCellChangedMessage(false));
return true;
}
[Verb]
public sealed class EjectCellVerb : Verb<PowerCellSlotComponent>
{
protected override void GetData(IEntity user, PowerCellSlotComponent component, VerbData data)
{
if (!component.ShowVerb || !ActionBlockerSystem.CanInteract(user))
{
data.Visibility = VerbVisibility.Invisible;
return;
}
if (component.Cell == null)
{
data.Text = Loc.GetString("Eject cell (cell missing)");
}
else
{
data.Text = Loc.GetString("Eject cell");
}
if (component.Cell == null || !component.CanRemoveCell)
{
data.Visibility = VerbVisibility.Disabled;
}
}
protected override void Activate(IEntity user, PowerCellSlotComponent component)
{
component.EjectCell(user);
}
}
void IMapInit.MapInit()
{
if (_startEmpty || _cellContainer.ContainedEntity != null)
{
return;
}
string type;
if (_startingCellType != null)
{
type = _startingCellType;
}
else
{
type = SlotSize switch
{
PowerCellSize.Small => "PowerCellSmallStandard",
PowerCellSize.Medium => "PowerCellMediumStandard",
PowerCellSize.Large => "PowerCellLargeStandard",
_ => throw new ArgumentOutOfRangeException()
};
}
var cell = Owner.EntityManager.SpawnEntity(type, Owner.Transform.Coordinates);
_cellContainer.Insert(cell);
}
}
public class PowerCellChangedMessage : ComponentMessage
{
/// <summary>
/// If true, the cell was ejected; if false, it was inserted.
/// </summary>
public bool Ejected { get; }
public PowerCellChangedMessage(bool ejected)
{
Ejected = ejected;
}
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
#nullable enable
using System.Collections.Generic;
using System.Threading.Tasks;
using Content.Server.GameObjects.Components.GUI;
using Content.Server.GameObjects.Components.Items.Storage;
@@ -28,7 +29,7 @@ using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Weapon.Melee
{
[RegisterComponent]
public class StunbatonComponent : MeleeWeaponComponent, IUse, IExamine, IMapInit, IInteractUsing, IThrowCollide
public class StunbatonComponent : MeleeWeaponComponent, IUse, IExamine, IInteractUsing, IThrowCollide
{
[Dependency] private readonly IRobustRandom _robustRandom = default!;
@@ -36,7 +37,8 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee
private bool _activated = false;
[ViewVariables] private ContainerSlot _cellContainer;
[ViewVariables] private PowerCellSlotComponent _cellSlot = default!;
private PowerCellComponent? Cell => _cellSlot.Cell;
[ViewVariables(VVAccess.ReadWrite)]
private float _paralyzeChanceNoSlowdown = 0.35f;
@@ -55,23 +57,10 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee
[ViewVariables]
public bool Activated => _activated;
[ViewVariables]
private BatteryComponent Cell
{
get
{
if (_cellContainer.ContainedEntity == null) return null;
_cellContainer.ContainedEntity.TryGetComponent(out BatteryComponent cell);
return cell;
}
}
public override void Initialize()
{
base.Initialize();
_cellContainer =
ContainerManagerComponent.Ensure<ContainerSlot>("stunbaton_cell_container", Owner, out var existed);
_cellSlot = Owner.EnsureComponent<PowerCellSlotComponent>();
}
public override void ExposeData(ObjectSerializer serializer)
@@ -84,6 +73,23 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee
serializer.DataField(ref _slowdownTime, "slowdownTime", 5f);
}
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
switch (message)
{
case PowerCellChangedMessage m:
if (component is PowerCellSlotComponent slotComponent && slotComponent == _cellSlot)
{
if (m.Ejected)
{
TurnOff();
}
}
break;
}
}
protected override bool OnHitEntities(IReadOnlyList<IEntity> entities, AttackEventArgs eventArgs)
{
if (!Activated || entities.Count == 0 || Cell == null)
@@ -96,7 +102,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee
foreach (var entity in entities)
{
if (!entity.TryGetComponent(out StunnableComponent stunnable)) continue;
if (!entity.TryGetComponent(out StunnableComponent? stunnable)) continue;
if(!stunnable.SlowedDown)
if(_robustRandom.Prob(_paralyzeChanceNoSlowdown))
@@ -120,6 +126,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee
private bool ToggleStatus(IEntity user)
{
if (!ActionBlockerSystem.CanUse(user)) return false;
if (Activated)
{
TurnOff();
@@ -158,9 +165,8 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee
var sprite = Owner.GetComponent<SpriteComponent>();
var item = Owner.GetComponent<ItemComponent>();
var cell = Cell;
if (cell == null)
if (Cell == null)
{
EntitySystem.Get<AudioSystem>().PlayAtCoords("/Audio/Machines/button.ogg", Owner.Transform.Coordinates, AudioHelpers.WithVariation(0.25f));
@@ -168,7 +174,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee
return;
}
if (cell.CurrentCharge < EnergyPerUse)
if (Cell.CurrentCharge < EnergyPerUse)
{
EntitySystem.Get<AudioSystem>().PlayAtCoords("/Audio/Machines/button.ogg", Owner.Transform.Coordinates, AudioHelpers.WithVariation(0.25f));
Owner.PopupMessage(user, Loc.GetString("Dead cell..."));
@@ -191,51 +197,12 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee
public async Task<bool> 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;
}
private void EjectCell(IEntity user)
{
if (Cell == null)
{
return;
}
var cell = Cell;
if (!_cellContainer.Remove(cell.Owner))
{
return;
}
if (!user.TryGetComponent(out HandsComponent hands))
{
return;
}
if (!hands.PutInHand(cell.Owner.GetComponent<ItemComponent>()))
{
cell.Owner.Transform.Coordinates = user.Transform.Coordinates;
}
EntitySystem.Get<AudioSystem>().PlayAtCoords("/Audio/Items/pistol_magout.ogg", Owner.Transform.Coordinates, AudioHelpers.WithVariation(0.25f));
}
public void Examine(FormattedMessage message, bool inDetailsRange)
{
if (Activated)
@@ -244,48 +211,9 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee
}
}
public void MapInit()
{
if (_cellContainer.ContainedEntity != null)
{
return;
}
var cell = Owner.EntityManager.SpawnEntity("PowerCellSmallHyper", Owner.Transform.Coordinates);
_cellContainer.Insert(cell);
}
[Verb]
public sealed class EjectCellVerb : Verb<StunbatonComponent>
{
protected override void GetData(IEntity user, StunbatonComponent 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, StunbatonComponent component)
{
component.EjectCell(user);
}
}
public void DoHit(ThrowCollideEventArgs eventArgs)
{
if (!Activated || Cell == null || !Cell.TryUseCharge(EnergyPerUse) || !eventArgs.Target.TryGetComponent(out StunnableComponent stunnable))
if (!Activated || Cell == null || !Cell.TryUseCharge(EnergyPerUse) || !eventArgs.Target.TryGetComponent(out StunnableComponent? stunnable))
return;
EntitySystem.Get<AudioSystem>().PlayAtCoords("/Audio/Weapons/egloves.ogg", Owner.Transform.Coordinates, AudioHelpers.WithVariation(0.25f));