Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APC & SMES appearances. #78

Merged
merged 10 commits into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Content.Client/Content.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,7 @@
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
<Compile Include="GameObjects\Components\Power\SmesVisualizer2D.cs" />
<Compile Include="GameObjects\Components\Power\ApcVisualizer2D.cs" />
</ItemGroup>
</Project>
</Project>
67 changes: 67 additions & 0 deletions Content.Client/GameObjects/Components/Power/ApcVisualizer2D.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Content.Shared.GameObjects.Components.Power;
using SS14.Client.GameObjects;
using SS14.Client.Interfaces.GameObjects.Components;
using SS14.Shared.Interfaces.GameObjects;

namespace Content.Client.GameObjects.Components.Power
{
public class ApcVisualizer2D : AppearanceVisualizer
{
public override void InitializeEntity(IEntity entity)
{
base.InitializeEntity(entity);

var sprite = entity.GetComponent<ISpriteComponent>();

sprite.LayerMapSet(Layers.ChargeState, sprite.AddLayerState("apco3-0"));
sprite.LayerSetShader(Layers.ChargeState, "unshaded");

sprite.LayerMapSet(Layers.Lock, sprite.AddLayerState("apcox-0"));
sprite.LayerSetShader(Layers.Lock, "unshaded");

sprite.LayerMapSet(Layers.Equipment, sprite.AddLayerState("apco0-3"));
sprite.LayerSetShader(Layers.Equipment, "unshaded");

sprite.LayerMapSet(Layers.Lighting, sprite.AddLayerState("apco1-3"));
sprite.LayerSetShader(Layers.Lighting, "unshaded");

sprite.LayerMapSet(Layers.Environment, sprite.AddLayerState("apco2-3"));
sprite.LayerSetShader(Layers.Environment, "unshaded");
}

public override void OnChangeData(AppearanceComponent component)
{
base.OnChangeData(component);

var sprite = component.Owner.GetComponent<ISpriteComponent>();
if (component.TryGetData<ApcChargeState>(ApcVisuals.ChargeState, out var state))
{
switch (state)
{
case ApcChargeState.Lack:
sprite.LayerSetState(Layers.ChargeState, "apco3-0");
break;
case ApcChargeState.Charging:
sprite.LayerSetState(Layers.ChargeState, "apco3-1");
break;
case ApcChargeState.Full:
sprite.LayerSetState(Layers.ChargeState, "apco3-2");
break;
}
}
else
{
sprite.LayerSetState(Layers.ChargeState, "apco3-0");
}
}

enum Layers
{
ChargeState,
Lock,
Equipment,
Lighting,
Environment,
}
}
}
15 changes: 10 additions & 5 deletions Content.Client/GameObjects/Components/Power/PowerDebugTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,25 @@ namespace Content.Client.GameObjects.Components.Power
{
public class PowerDebugTool : SharedPowerDebugTool
{
SS14Window LastWindow;
public override void HandleMessage(ComponentMessage message, INetChannel netChannel = null, IComponent component = null)
{
base.HandleMessage(message, netChannel, component);

switch (message)
{
case OpenDataWindowMsg msg:
var window = new SS14Window
if (LastWindow != null && !LastWindow.Disposed)
{
Title = "Power Debug Tool"
LastWindow.Dispose();
}
LastWindow = new SS14Window
{
Title = "Power Debug Tool",
};
window.Contents.AddChild(new Label() { Text = msg.Data });
window.AddToScreen();
window.Open();
LastWindow.Contents.AddChild(new Label() { Text = msg.Data });
LastWindow.AddToScreen();
LastWindow.Open();
break;
}
}
Expand Down
77 changes: 77 additions & 0 deletions Content.Client/GameObjects/Components/Power/SMESVisualizer2D.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using Content.Shared.GameObjects.Components.Power;
using SS14.Client.GameObjects;
using SS14.Client.Interfaces.GameObjects.Components;
using SS14.Shared.Interfaces.GameObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Content.Client.GameObjects.Components.Power
{
public class SmesVisualizer2D : AppearanceVisualizer
{
public override void InitializeEntity(IEntity entity)
{
base.InitializeEntity(entity);

var sprite = entity.GetComponent<ISpriteComponent>();

sprite.LayerMapSet(Layers.Input, sprite.AddLayerState("smes-oc0"));
sprite.LayerSetShader(Layers.Input, "unshaded");
sprite.LayerMapSet(Layers.Charge, sprite.AddLayerState("smes-og1"));
sprite.LayerSetShader(Layers.Charge, "unshaded");
sprite.LayerSetVisible(Layers.Charge, false);
sprite.LayerMapSet(Layers.Output, sprite.AddLayerState("smes-op0"));
sprite.LayerSetShader(Layers.Output, "unshaded");
}

public override void OnChangeData(AppearanceComponent component)
{
base.OnChangeData(component);

var sprite = component.Owner.GetComponent<ISpriteComponent>();
if (!component.TryGetData<int>(SmesVisuals.LastChargeLevel, out var level) || level == 0)
{
sprite.LayerSetVisible(Layers.Charge, false);
}
else
{
sprite.LayerSetVisible(Layers.Charge, true);
sprite.LayerSetState(Layers.Charge, $"smes-og{level}");
}

if (component.TryGetData<ChargeState>(SmesVisuals.LastChargeState, out var state))
{
switch (state)
{
case ChargeState.Still:
sprite.LayerSetState(Layers.Input, "smes-oc0");
sprite.LayerSetState(Layers.Output, "smes-op1");
break;
case ChargeState.Charging:
sprite.LayerSetState(Layers.Input, "smes-oc1");
sprite.LayerSetState(Layers.Output, "smes-op1");
break;
case ChargeState.Discharging:
sprite.LayerSetState(Layers.Input, "smes-oc0");
sprite.LayerSetState(Layers.Output, "smes-op2");
break;
}
}
else
{
sprite.LayerSetState(Layers.Input, "smes-oc0");
sprite.LayerSetState(Layers.Output, "smes-op1");
}
}

enum Layers
{
Input,
Charge,
Output,
}
}
}
4 changes: 3 additions & 1 deletion Content.Server/Content.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,7 @@
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
<Compile Include="GameObjects\Components\Power\SmesComponent.cs" />
<Compile Include="GameObjects\Components\Power\ApcComponent.cs" />
</ItemGroup>
</Project>
</Project>
2 changes: 2 additions & 0 deletions Content.Server/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public override void Init()

factory.Register<PowerDebugTool>();
factory.Register<PoweredLightComponent>();
factory.Register<SmesComponent>();
factory.Register<ApcComponent>();
}

/// <inheritdoc />
Expand Down
50 changes: 50 additions & 0 deletions Content.Server/GameObjects/Components/Power/ApcComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Content.Shared.GameObjects.Components.Power;
using SS14.Server.GameObjects;
using SS14.Shared.GameObjects;

namespace Content.Server.GameObjects.Components.Power
{
public class ApcComponent : Component
{
public override string Name => "Apc";

PowerStorageComponent Storage;
AppearanceComponent Appearance;

ApcChargeState LastChargeState;

public override void Initialize()
{
base.Initialize();
Storage = Owner.GetComponent<PowerStorageComponent>();
Appearance = Owner.GetComponent<AppearanceComponent>();
}

public override void Update(float frameTime)
{
var newState = CalcChargeState();
if (newState != LastChargeState)
{
LastChargeState = newState;
Appearance.SetData(ApcVisuals.ChargeState, newState);
}
}

ApcChargeState CalcChargeState()
{
var storageCharge = Storage.GetChargeState();
if (storageCharge == ChargeState.Discharging)
{
return ApcChargeState.Lack;
}

if (storageCharge == ChargeState.Charging)
{
return ApcChargeState.Charging;
}

// Still.
return Storage.Full ? ApcChargeState.Full : ApcChargeState.Lack;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Text;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.GameObjects.Components.Power;
Expand Down Expand Up @@ -61,9 +62,11 @@ void IAfterAttack.Afterattack(IEntity user, GridLocalCoordinates clicklocation,

if (attacked.TryGetComponent<PowerStorageComponent>(out var storage))
{
var stateSeconds = (DateTime.Now - storage.LastChargeStateChange).TotalSeconds;
builder.AppendFormat(@"Power Storage:
Capacity: {0}, Charge: {1}, ChargeRate: {2}, DistributionRate: {3}, ChargePowernet: {4}
", storage.Capacity, storage.Charge, storage.ChargeRate, storage.DistributionRate, storage.ChargePowernet);
LastChargeState: {5} ({6}), LastChargeStateChange: {7:0.00} seconds ago.
", storage.Capacity, storage.Charge, storage.ChargeRate, storage.DistributionRate, storage.ChargePowernet, storage.LastChargeState, storage.GetChargeState(), stateSeconds);
}

if (attacked.TryGetComponent<PowerTransferComponent>(out var transfer))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SS14.Shared.GameObjects;
using Content.Shared.GameObjects.Components.Power;
using SS14.Shared.GameObjects;
using SS14.Shared.Interfaces.GameObjects;
using SS14.Shared.IoC;
using SS14.Shared.Utility;
Expand All @@ -14,6 +15,9 @@ public class PowerStorageComponent : Component
{
public override string Name => "PowerStorage";

public ChargeState LastChargeState { get; private set; } = ChargeState.Still;
public DateTime LastChargeStateChange { get; private set; }

/// <summary>
/// Maximum amount of energy the internal battery can store.
/// In Joules.
Expand All @@ -38,6 +42,8 @@ public class PowerStorageComponent : Component
/// </summary>
public float DistributionRate { get; private set; } = 1000;

public bool Full => Charge >= Capacity;

private bool _chargepowernet = false;

/// <summary>
Expand Down Expand Up @@ -129,31 +135,53 @@ public bool CanDeductCharge(float todeduct)
public void DeductCharge(float todeduct)
{
Charge = Math.Max(0, Charge - todeduct);
LastChargeState = ChargeState.Discharging;
LastChargeStateChange = DateTime.Now;
}

public void AddCharge(float charge)
{
Charge = Math.Min(Capacity, Charge + charge);
LastChargeState = ChargeState.Charging;
LastChargeStateChange = DateTime.Now;
}

/// <summary>
/// Returns the charge available from the energy storage
/// Returns the amount of energy that can be taken in by this storage in the specified amount of time.
/// </summary>
public float RequestCharge(float frameTime)
{
return Math.Min(ChargeRate * frameTime, Capacity - Charge);
}

/// <summary>
/// Returns the charge available from the energy storage
/// Returns the amount of energy available for discharge in the specified amount of time.
/// </summary>
public float AvailableCharge(float frameTime)
{
return Math.Min(DistributionRate * frameTime, Charge);
}

public ChargeState GetChargeState()
{
return GetChargeState(TimeSpan.FromSeconds(1));
}

public ChargeState GetChargeState(TimeSpan timeout)
{
if (LastChargeStateChange + timeout > DateTime.Now)
{
return LastChargeState;
}
return ChargeState.Still;
}

public void ChargePowerTick(float frameTime)
{
if (Full)
{
return;
}
AddCharge(RequestCharge(frameTime));
}

Expand Down
8 changes: 8 additions & 0 deletions Content.Server/GameObjects/Components/Power/Powernet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ public void Update(float frameTime)
break;
}
var demand = consumer.RequestCharge(frameTime);
if (demand == 0)
{
continue;
}
var taken = Math.Min(demand, totalRemaining);
totalRemaining -= taken;
consumer.AddCharge(taken);
Expand All @@ -155,6 +159,10 @@ public void Update(float frameTime)
foreach (var supplier in PowerStorageSupplierList)
{
var load = supplier.AvailableCharge(frameTime);
if (load == 0)
{
continue;
}
var added = Math.Min(load, supplierUsed);
supplierUsed -= added;
supplier.DeductCharge(added);
Expand Down
Loading