2021-02-01 10:19:43 -06:00
|
|
|
using System;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Chemistry.Components;
|
|
|
|
|
using Content.Server.Explosion;
|
2021-07-04 18:11:52 +02:00
|
|
|
using Content.Server.Power.Components;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Chemistry;
|
|
|
|
|
using Content.Shared.Examine;
|
|
|
|
|
using Content.Shared.PowerCell;
|
|
|
|
|
using Content.Shared.Rounding;
|
2019-04-15 21:11:38 -06:00
|
|
|
using Robust.Server.GameObjects;
|
2019-07-31 15:02:36 +02:00
|
|
|
using Robust.Shared.GameObjects;
|
2020-10-29 20:17:03 +02:00
|
|
|
using Robust.Shared.Localization;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2020-10-29 20:17:03 +02:00
|
|
|
using Robust.Shared.Utility;
|
|
|
|
|
using Robust.Shared.ViewVariables;
|
2018-09-21 08:21:40 +02:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.PowerCell.Components
|
2018-09-21 08:21:40 +02:00
|
|
|
{
|
2020-06-28 09:23:26 -06:00
|
|
|
/// <summary>
|
2020-10-29 20:17:03 +02:00
|
|
|
/// Batteries that can update an <see cref="AppearanceComponent"/> based on their charge percent
|
|
|
|
|
/// and fit into a <see cref="PowerCellSlotComponent"/> of the appropriate size.
|
2020-06-28 09:23:26 -06:00
|
|
|
/// </summary>
|
2019-07-31 15:02:36 +02:00
|
|
|
[RegisterComponent]
|
2020-06-28 09:23:26 -06:00
|
|
|
[ComponentReference(typeof(BatteryComponent))]
|
2021-09-06 15:49:44 +02:00
|
|
|
public class PowerCellComponent : BatteryComponent, IExamine
|
2018-09-21 08:21:40 +02:00
|
|
|
{
|
|
|
|
|
public override string Name => "PowerCell";
|
2021-09-06 15:49:44 +02:00
|
|
|
public const string SolutionName = "powerCell";
|
2018-09-21 08:21:40 +02:00
|
|
|
|
2020-10-29 20:17:03 +02:00
|
|
|
[ViewVariables] public PowerCellSize CellSize => _cellSize;
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("cellSize")]
|
2020-10-29 20:17:03 +02:00
|
|
|
private PowerCellSize _cellSize = PowerCellSize.Small;
|
|
|
|
|
|
2021-09-06 15:49:44 +02:00
|
|
|
[ViewVariables] public bool IsRigged { get; set; }
|
2021-01-24 16:21:18 +01:00
|
|
|
|
2021-06-19 19:41:26 -07:00
|
|
|
protected override void Initialize()
|
2018-09-21 08:21:40 +02:00
|
|
|
{
|
|
|
|
|
base.Initialize();
|
2020-06-28 09:23:26 -06:00
|
|
|
CurrentCharge = MaxCharge;
|
|
|
|
|
UpdateVisuals();
|
2018-09-21 08:21:40 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-28 09:23:26 -06:00
|
|
|
protected override void OnChargeChanged()
|
2018-09-21 08:21:40 +02:00
|
|
|
{
|
2020-06-28 09:23:26 -06:00
|
|
|
base.OnChargeChanged();
|
|
|
|
|
UpdateVisuals();
|
2018-09-21 08:21:40 +02:00
|
|
|
}
|
|
|
|
|
|
2021-01-24 16:21:18 +01:00
|
|
|
public override bool TryUseCharge(float chargeToUse)
|
|
|
|
|
{
|
|
|
|
|
if (IsRigged)
|
|
|
|
|
{
|
|
|
|
|
Explode();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return base.TryUseCharge(chargeToUse);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override float UseCharge(float toDeduct)
|
|
|
|
|
{
|
|
|
|
|
if (IsRigged)
|
|
|
|
|
{
|
|
|
|
|
Explode();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return base.UseCharge(toDeduct);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Explode()
|
|
|
|
|
{
|
|
|
|
|
var heavy = (int) Math.Ceiling(Math.Sqrt(CurrentCharge) / 60);
|
|
|
|
|
var light = (int) Math.Ceiling(Math.Sqrt(CurrentCharge) / 30);
|
|
|
|
|
|
|
|
|
|
CurrentCharge = 0;
|
|
|
|
|
Owner.SpawnExplosion(0, heavy, light, light*2);
|
|
|
|
|
Owner.Delete();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-28 09:23:26 -06:00
|
|
|
private void UpdateVisuals()
|
2018-09-21 08:21:40 +02:00
|
|
|
{
|
2021-01-24 16:21:18 +01:00
|
|
|
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
|
2020-08-22 22:29:20 +02:00
|
|
|
{
|
2020-11-01 07:56:46 +11:00
|
|
|
appearance.SetData(PowerCellVisuals.ChargeLevel, GetLevel(CurrentCharge / MaxCharge));
|
2020-08-22 22:29:20 +02:00
|
|
|
}
|
2018-09-21 08:21:40 +02:00
|
|
|
}
|
2020-10-29 20:17:03 +02:00
|
|
|
|
2020-11-01 07:56:46 +11:00
|
|
|
private byte GetLevel(float fraction)
|
|
|
|
|
{
|
|
|
|
|
return (byte) ContentHelpers.RoundToNearestLevels(fraction, 1, SharedPowerCell.PowerCellVisualsLevels);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-29 20:17:03 +02:00
|
|
|
void IExamine.Examine(FormattedMessage message, bool inDetailsRange)
|
|
|
|
|
{
|
2021-01-24 16:21:18 +01:00
|
|
|
if (inDetailsRange)
|
2020-10-29 20:17:03 +02:00
|
|
|
{
|
2021-08-08 21:43:12 +00:00
|
|
|
message.AddMarkup(Loc.GetString("power-cell-component-examine-details", ("currentCharge", $"{CurrentCharge / MaxCharge * 100:F0}")));
|
2020-10-29 20:17:03 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum PowerCellSize
|
|
|
|
|
{
|
|
|
|
|
Small,
|
|
|
|
|
Medium,
|
|
|
|
|
Large
|
2018-09-21 08:21:40 +02:00
|
|
|
}
|
|
|
|
|
}
|