Power cells

This commit is contained in:
Pieter-Jan Briers
2018-09-21 08:21:40 +02:00
parent 952c58c226
commit c33daddda2
12 changed files with 349 additions and 100 deletions

View File

@@ -72,6 +72,7 @@
<Compile Include="GameObjects\Components\Inventory\ClientInventoryComponent.cs" />
<Compile Include="GameObjects\Components\Items\ItemComponent.cs" />
<Compile Include="GameObjects\Components\Power\ApcBoundUserInterface.cs" />
<Compile Include="GameObjects\Components\Power\PowerCellVisualizer2D.cs" />
<Compile Include="GameObjects\Components\Storage\ClientStorageComponent.cs" />
<Compile Include="Input\ContentContexts.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View File

@@ -0,0 +1,48 @@
using Content.Shared.GameObjects.Components.Power;
using Content.Shared.Utility;
using SS14.Client.GameObjects;
using SS14.Client.Interfaces.GameObjects.Components;
using SS14.Shared.Interfaces.GameObjects;
using SS14.Shared.Utility;
using YamlDotNet.RepresentationModel;
namespace Content.Client.GameObjects.Components.Power
{
public class PowerCellVisualizer2D : AppearanceVisualizer
{
private string _prefix;
public override void LoadData(YamlMappingNode node)
{
base.LoadData(node);
_prefix = node.GetNode("prefix").AsString();
}
public override void InitializeEntity(IEntity entity)
{
base.InitializeEntity(entity);
var sprite = entity.GetComponent<ISpriteComponent>();
sprite.LayerMapSet(Layers.Charge, sprite.AddLayerState($"{_prefix}_100"));
sprite.LayerSetShader(Layers.Charge, "unshaded");
}
public override void OnChangeData(AppearanceComponent component)
{
base.OnChangeData(component);
var sprite = component.Owner.GetComponent<ISpriteComponent>();
if (component.TryGetData(PowerCellVisuals.ChargeLevel, out float fraction))
{
sprite.LayerSetState(Layers.Charge, $"{_prefix}_{ContentHelpers.RoundToLevels(fraction, 1, 5) * 25}");
}
}
private enum Layers
{
Charge
}
}
}

View File

@@ -80,12 +80,14 @@
<Compile Include="GameObjects\Components\Items\Storage\ServerStorageComponent.cs" />
<Compile Include="GameObjects\Components\Items\Storage\ItemComponent.cs" />
<Compile Include="GameObjects\Components\Mobs\MindComponent.cs" />
<Compile Include="GameObjects\Components\Power\PowerCellComponent.cs" />
<Compile Include="GameObjects\Components\Power\PowerStorageComponent.cs" />
<Compile Include="GameObjects\Components\Power\PowerGeneratorComponent.cs" />
<Compile Include="GameObjects\Components\Power\PowerDevice.cs" />
<Compile Include="GameObjects\Components\Power\Powernet.cs" />
<Compile Include="GameObjects\Components\Power\PowerNodeComponent.cs" />
<Compile Include="GameObjects\Components\Power\PowerProviderComponent.cs" />
<Compile Include="GameObjects\Components\Power\PowerStorageNetComponent.cs" />
<Compile Include="GameObjects\Components\Power\PowerTransferComponent.cs" />
<Compile Include="GameObjects\Components\Projectiles\ProjectileComponent.cs" />
<Compile Include="GameObjects\Components\Weapon\Melee\MeleeWeaponComponent.cs" />

View File

@@ -86,7 +86,10 @@ namespace Content.Server
factory.Register<PowerProviderComponent>();
factory.RegisterReference<PowerProviderComponent, PowerDeviceComponent>();
factory.Register<PowerNodeComponent>();
factory.Register<PowerStorageComponent>();
factory.Register<PowerStorageNetComponent>();
factory.RegisterReference<PowerStorageNetComponent, PowerStorageComponent>();
factory.Register<PowerCellComponent>();
factory.RegisterReference<PowerCellComponent, PowerStorageComponent>();
factory.Register<PowerDeviceComponent>();
factory.Register<PowerGeneratorComponent>();

View File

@@ -0,0 +1,48 @@
using Content.Shared.GameObjects.Components.Power;
using SS14.Server.GameObjects;
namespace Content.Server.GameObjects.Components.Power
{
public class PowerCellComponent : PowerStorageComponent
{
public override string Name => "PowerCell";
private AppearanceComponent _appearance;
public override float Charge
{
get => base.Charge;
set
{
base.Charge = value;
_updateAppearance();
}
}
public override void Initialize()
{
base.Initialize();
Owner.TryGetComponent(out _appearance);
}
public override void DeductCharge(float toDeduct)
{
base.DeductCharge(toDeduct);
_updateAppearance();
}
public override void AddCharge(float charge)
{
base.AddCharge(charge);
_updateAppearance();
}
private void _updateAppearance()
{
_appearance?.SetData(PowerCellVisuals.ChargeLevel, Charge / Capacity);
}
}
}

View File

@@ -75,7 +75,7 @@ namespace Content.Server.GameObjects.Components.Power
}
}
if (attacked.TryGetComponent<PowerStorageComponent>(out var storage))
if (attacked.TryGetComponent<PowerStorageNetComponent>(out var storage))
{
var stateSeconds = (DateTime.Now - storage.LastChargeStateChange).TotalSeconds;
builder.AppendFormat(@"Power Storage:

View File

@@ -11,12 +11,11 @@ using YamlDotNet.RepresentationModel;
namespace Content.Server.GameObjects.Components.Power
{
/// <summary>
/// Feeds energy from the powernet and may have the ability to supply back into it
/// Stores electrical energy. Used by power cells and SMESes.
/// </summary>
public class PowerStorageComponent : Component
public abstract class PowerStorageComponent : Component
{
public override string Name => "PowerStorage";
[ViewVariables]
public ChargeState LastChargeState { get; private set; } = ChargeState.Still;
public DateTime LastChargeStateChange { get; private set; }
@@ -31,9 +30,15 @@ namespace Content.Server.GameObjects.Components.Power
/// <summary>
/// Energy the battery is currently storing.
/// In Joules.
/// In most cases you should use <see cref="DeductCharge"/> and <see cref="AddCharge"/> to modify this.
/// </summary>
[ViewVariables]
public float Charge => _charge;
[ViewVariables(VVAccess.ReadWrite)]
public virtual float Charge
{
get => _charge;
set => _charge = value;
}
private float _charge = 0;
/// <summary>
@@ -55,26 +60,6 @@ namespace Content.Server.GameObjects.Components.Power
[ViewVariables]
public bool Full => Charge >= Capacity;
private bool _chargepowernet = false;
/// <summary>
/// Do we distribute power into the powernet from our stores if the powernet requires it?
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public bool ChargePowernet
{
get => _chargepowernet;
set
{
_chargepowernet = value;
if (Owner.TryGetComponent(out PowerNodeComponent node))
{
if (node.Parent != null)
node.Parent.UpdateStorageType(this);
}
}
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
@@ -83,46 +68,14 @@ namespace Content.Server.GameObjects.Components.Power
serializer.DataField(ref _charge, "charge", 0);
serializer.DataField(ref _chargeRate, "chargerate", 1000);
serializer.DataField(ref _distributionRate, "distributionrate", 1000);
serializer.DataField(ref _chargepowernet, "chargepowernet", false);
}
public override void OnAdd()
{
base.OnAdd();
if (!Owner.TryGetComponent(out PowerNodeComponent node))
{
Owner.AddComponent<PowerNodeComponent>();
node = Owner.GetComponent<PowerNodeComponent>();
}
node.OnPowernetConnect += PowernetConnect;
node.OnPowernetDisconnect += PowernetDisconnect;
node.OnPowernetRegenerate += PowernetRegenerate;
}
public override void OnRemove()
{
if (Owner.TryGetComponent(out PowerNodeComponent node))
{
if (node.Parent != null)
{
node.Parent.RemovePowerStorage(this);
}
node.OnPowernetConnect -= PowernetConnect;
node.OnPowernetDisconnect -= PowernetDisconnect;
node.OnPowernetRegenerate -= PowernetRegenerate;
}
base.OnRemove();
}
/// <summary>
/// Checks if the storage can supply the amount of charge directly requested
/// </summary>
public bool CanDeductCharge(float todeduct)
public bool CanDeductCharge(float toDeduct)
{
if (Charge > todeduct)
if (Charge > toDeduct)
return true;
return false;
}
@@ -130,14 +83,14 @@ namespace Content.Server.GameObjects.Components.Power
/// <summary>
/// Deducts the requested charge from the energy storage
/// </summary>
public void DeductCharge(float todeduct)
public virtual void DeductCharge(float toDeduct)
{
_charge = Math.Max(0, Charge - todeduct);
_charge = Math.Max(0, Charge - toDeduct);
LastChargeState = ChargeState.Discharging;
LastChargeStateChange = DateTime.Now;
}
public void AddCharge(float charge)
public virtual void AddCharge(float charge)
{
_charge = Math.Min(Capacity, Charge + charge);
LastChargeState = ChargeState.Charging;
@@ -160,6 +113,24 @@ namespace Content.Server.GameObjects.Components.Power
return Math.Min(DistributionRate * frameTime, Charge);
}
/// <summary>
/// Tries to deduct a wattage over a certain amount of time.
/// </summary>
/// <param name="wattage">The wattage of the power drain.</param>
/// <param name="frameTime">The amount of time in this "frame".</param>
/// <returns>True if the amount of energy was deducted, false.</returns>
public bool TryDeductWattage(float wattage, float frameTime)
{
var avail = AvailableCharge(frameTime);
if (avail < wattage * frameTime)
{
return false;
}
DeductCharge(wattage * frameTime);
return true;
}
public ChargeState GetChargeState()
{
return GetChargeState(TimeSpan.FromSeconds(1));
@@ -182,35 +153,5 @@ namespace Content.Server.GameObjects.Components.Power
}
AddCharge(RequestCharge(frameTime));
}
/// <summary>
/// Node has become anchored to a powernet
/// </summary>
/// <param name="sender"></param>
/// <param name="eventarg"></param>
private void PowernetConnect(object sender, PowernetEventArgs eventarg)
{
eventarg.Powernet.AddPowerStorage(this);
}
/// <summary>
/// Node has had its powernet regenerated
/// </summary>
/// <param name="sender"></param>
/// <param name="eventarg"></param>
private void PowernetRegenerate(object sender, PowernetEventArgs eventarg)
{
eventarg.Powernet.AddPowerStorage(this);
}
/// <summary>
/// Node has become unanchored from a powernet
/// </summary>
/// <param name="sender"></param>
/// <param name="eventarg"></param>
private void PowernetDisconnect(object sender, PowernetEventArgs eventarg)
{
eventarg.Powernet.RemovePowerStorage(this);
}
}
}

View File

@@ -0,0 +1,100 @@
using SS14.Shared.Serialization;
using SS14.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Power
{
/// <summary>
/// Feeds energy from the powernet and may have the ability to supply back into it
/// </summary>
public class PowerStorageNetComponent : PowerStorageComponent
{
public override string Name => "PowerStorage";
private bool _chargePowernet = false;
/// <summary>
/// Do we distribute power into the powernet from our stores if the powernet requires it?
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public bool ChargePowernet
{
get => _chargePowernet;
set
{
_chargePowernet = value;
if (Owner.TryGetComponent(out PowerNodeComponent node))
{
node.Parent?.UpdateStorageType(this);
}
}
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _chargePowernet, "chargepowernet", false);
}
public override void OnAdd()
{
base.OnAdd();
if (!Owner.TryGetComponent(out PowerNodeComponent node))
{
Owner.AddComponent<PowerNodeComponent>();
node = Owner.GetComponent<PowerNodeComponent>();
}
node.OnPowernetConnect += PowernetConnect;
node.OnPowernetDisconnect += PowernetDisconnect;
node.OnPowernetRegenerate += PowernetRegenerate;
}
public override void OnRemove()
{
if (Owner.TryGetComponent(out PowerNodeComponent node))
{
if (node.Parent != null)
{
node.Parent.RemovePowerStorage(this);
}
node.OnPowernetConnect -= PowernetConnect;
node.OnPowernetDisconnect -= PowernetDisconnect;
node.OnPowernetRegenerate -= PowernetRegenerate;
}
base.OnRemove();
}
/// <summary>
/// Node has become anchored to a powernet
/// </summary>
/// <param name="sender"></param>
/// <param name="eventarg"></param>
private void PowernetConnect(object sender, PowernetEventArgs eventarg)
{
eventarg.Powernet.AddPowerStorage(this);
}
/// <summary>
/// Node has had its powernet regenerated
/// </summary>
/// <param name="sender"></param>
/// <param name="eventarg"></param>
private void PowernetRegenerate(object sender, PowernetEventArgs eventarg)
{
eventarg.Powernet.AddPowerStorage(this);
}
/// <summary>
/// Node has become unanchored from a powernet
/// </summary>
/// <param name="sender"></param>
/// <param name="eventarg"></param>
private void PowernetDisconnect(object sender, PowernetEventArgs eventarg)
{
eventarg.Powernet.RemovePowerStorage(this);
}
}
}

View File

@@ -63,7 +63,7 @@ namespace Content.Server.GameObjects.Components.Power
/// <summary>
/// A list of the energy storage components that will feed the powernet if necessary, and if there is enough power feed itself
/// </summary>
private readonly List<PowerStorageComponent> PowerStorageSupplierList = new List<PowerStorageComponent>();
private readonly List<PowerStorageNetComponent> PowerStorageSupplierList = new List<PowerStorageNetComponent>();
[ViewVariables]
public int PowerStorageSupplierCount => PowerStorageSupplierList.Count;
@@ -71,7 +71,7 @@ namespace Content.Server.GameObjects.Components.Power
/// <summary>
/// A list of energy storage components that will never feed the powernet, will try to draw energy to feed themselves if possible
/// </summary>
private readonly List<PowerStorageComponent> PowerStorageConsumerList = new List<PowerStorageComponent>();
private readonly List<PowerStorageNetComponent> PowerStorageConsumerList = new List<PowerStorageNetComponent>();
[ViewVariables]
public int PowerStorageConsumerCount => PowerStorageConsumerList.Count;
@@ -473,7 +473,7 @@ namespace Content.Server.GameObjects.Components.Power
/// <summary>
/// Register a power supply from a generator connected to the powernet
/// </summary>
public void AddPowerStorage(PowerStorageComponent storage)
public void AddPowerStorage(PowerStorageNetComponent storage)
{
if (storage.ChargePowernet)
PowerStorageSupplierList.Add(storage);
@@ -482,7 +482,7 @@ namespace Content.Server.GameObjects.Components.Power
}
//How do I even call this? TODO: fix
public void UpdateStorageType(PowerStorageComponent storage)
public void UpdateStorageType(PowerStorageNetComponent storage)
{
//If our chargepowernet settings change we need to tell the powernet of this new setting and remove traces of our old setting
if (PowerStorageSupplierList.Contains(storage))
@@ -500,7 +500,7 @@ namespace Content.Server.GameObjects.Components.Power
/// <summary>
/// Remove a power supply from a generator connected to the powernet
/// </summary>
public void RemovePowerStorage(PowerStorageComponent storage)
public void RemovePowerStorage(PowerStorageNetComponent storage)
{
if (PowerStorageSupplierList.Contains(storage))
{

View File

@@ -67,6 +67,7 @@
<Compile Include="GameObjects\Components\Inventory\InventoryTemplates.cs" />
<Compile Include="GameObjects\Components\Inventory\SharedInventoryComponent.cs" />
<Compile Include="GameObjects\Components\Power\PowerShared.cs" />
<Compile Include="GameObjects\Components\Power\SharedPowerCellComponent.cs" />
<Compile Include="GameObjects\Components\Storage\SharedStorageComponent.cs" />
<Compile Include="GameObjects\ContentNetIDs.cs" />
<Compile Include="GameObjects\PhysicalConstants.cs" />

View File

@@ -0,0 +1,11 @@
using System;
using SS14.Shared.Serialization;
namespace Content.Shared.GameObjects.Components.Power
{
[Serializable, NetSerializable]
public enum PowerCellVisuals
{
ChargeLevel
}
}

View File

@@ -0,0 +1,94 @@
- type: entity
id: PowerCellSmallBase
abstract: true
parent: BaseItem
components:
- type: BoundingBox
aabb: -0.15,-0.3,0.2,0.3
- type: PowerCell
- type: Appearance
- type: Sprite
netsync: false
- type: entity
name: Small Standard Power Cell
id: PowerCellSmallStandard
parent: PowerCellSmallBase
components:
- type: Sprite
sprite: Objects/PowerCells/power_cell_small_st.rsi
layers:
- state: s_st
- type: Icon
sprite: Objects/PowerCells/power_cell_small_st.rsi
state: s_st
- type: PowerCell
capacity: 1500
charge: 1500
- type: Appearance
visuals:
- type: PowerCellVisualizer2D
prefix: s_st
- type: entity
name: Small High-Capacity Power Cell
id: PowerCellSmallHigh
parent: PowerCellSmallBase
components:
- type: Sprite
sprite: Objects/PowerCells/power_cell_small_hi.rsi
layers:
- state: s_hi
- type: Icon
sprite: Objects/PowerCells/power_cell_small_hi.rsi
state: s_hi
- type: PowerCell
capacity: 3000
charge: 3000
- type: Appearance
visuals:
- type: PowerCellVisualizer2D
prefix: s_hi
- type: entity
name: Small Super-Capacity Power Cell
id: PowerCellSmallSuper
parent: PowerCellSmallBase
components:
- type: Sprite
sprite: Objects/PowerCells/power_cell_small_sup.rsi
layers:
- state: s_sup
- type: Icon
sprite: Objects/PowerCells/power_cell_small_sup.rsi
state: s_sup
- type: PowerCell
capacity: 6000
charge: 6000
- type: Appearance
visuals:
- type: PowerCellVisualizer2D
prefix: s_sup
- type: entity
name: Small Hyper-Capacity Power Cell
id: PowerCellSmallHyper
parent: PowerCellSmallBase
components:
- type: Sprite
sprite: Objects/PowerCells/power_cell_small_hy.rsi
layers:
- state: s_hy
- type: Icon
sprite: Objects/PowerCells/power_cell_small_hy.rsi
state: s_hy
- type: PowerCell
capacity: 10000
charge: 10000
- type: Appearance
visuals:
- type: PowerCellVisualizer2D
prefix: s_hy