Replaced static Rounders with an impleneted interface

This commit is contained in:
PrPleGoo
2020-03-14 14:04:08 +01:00
parent f05fdfb5fc
commit dc66621804
11 changed files with 91 additions and 70 deletions

View File

@@ -1,8 +1,7 @@
using System;
using Content.Shared.Interfaces.Chemistry;
using Content.Shared.Maths;
using Content.Shared.Interfaces.Chemistry;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Serialization;
using Robust.Shared.IoC;
using Robust.Shared.Serialization;
namespace Content.Shared.Chemistry
@@ -10,6 +9,9 @@ namespace Content.Shared.Chemistry
//Default metabolism for reagents. Metabolizes the reagent with no effects
class DefaultMetabolizable : IMetabolizable
{
#pragma warning disable 649
[Dependency] private readonly IRounderForReagents _rounder;
#pragma warning restore 649
//Rate of metabolism in units / second
private decimal _metabolismRate = 1;
public decimal MetabolismRate => _metabolismRate;
@@ -21,7 +23,7 @@ namespace Content.Shared.Chemistry
decimal IMetabolizable.Metabolize(IEntity solutionEntity, string reagentId, float tickTime)
{
var metabolismAmount = (MetabolismRate * (decimal)tickTime).RoundForReagents();
var metabolismAmount = _rounder.Round(MetabolismRate * (decimal)tickTime);
return metabolismAmount;
}
}

View File

@@ -0,0 +1,14 @@
using Content.Shared.Interfaces.Chemistry;
using System;
namespace Content.Shared.Chemistry
{
public class RounderForReagents : IRounderForReagents
{
public decimal Round(decimal value)
{
return Math.Round(value, 2);
}
}
}

View File

@@ -1,12 +1,13 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Content.Shared.Maths;
using Content.Shared.Interfaces.Chemistry;
using Robust.Shared.Interfaces.Serialization;
using Robust.Shared.IoC;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace Content.Shared.Chemistry
{
@@ -15,6 +16,9 @@ namespace Content.Shared.Chemistry
/// </summary>
public class Solution : IExposeData, IEnumerable<Solution.ReagentQuantity>
{
#pragma warning disable 649
[Dependency] private readonly IRounderForReagents _rounder;
#pragma warning restore 649
// Most objects on the station hold only 1 or 2 reagents
[ViewVariables]
private List<ReagentQuantity> _contents = new List<ReagentQuantity>(2);
@@ -63,7 +67,7 @@ namespace Content.Shared.Chemistry
/// <param name="quantity">The quantity in milli-units.</param>
public void AddReagent(string reagentId, decimal quantity)
{
quantity = quantity.RoundForReagents();
quantity = _rounder.Round(quantity);
if (quantity <= 0)
return;
@@ -111,7 +115,7 @@ namespace Content.Shared.Chemistry
var curQuantity = reagent.Quantity;
var newQuantity = (curQuantity - quantity).RoundForReagents();
var newQuantity = _rounder.Round(curQuantity - quantity);
if (newQuantity <= 0)
{
_contents.RemoveSwap(i);
@@ -136,7 +140,7 @@ namespace Content.Shared.Chemistry
if(quantity <= 0)
return;
var ratio = (TotalVolume - quantity).RoundForReagents() / TotalVolume;
var ratio = _rounder.Round(TotalVolume - quantity) / TotalVolume;
if (ratio <= 0)
{
@@ -151,12 +155,12 @@ namespace Content.Shared.Chemistry
// quantity taken is always a little greedy, so fractional quantities get rounded up to the nearest
// whole unit. This should prevent little bits of chemical remaining because of float rounding errors.
var newQuantity = (oldQuantity * ratio).RoundForReagents();
var newQuantity = _rounder.Round(oldQuantity * ratio);
_contents[i] = new ReagentQuantity(reagent.ReagentId, newQuantity);
}
TotalVolume = (TotalVolume * ratio).RoundForReagents();
TotalVolume = _rounder.Round(TotalVolume * ratio);
}
public void RemoveAllSolution()

View File

@@ -1,12 +1,12 @@
using System;
using System.Collections.Generic;
using Content.Shared.Chemistry;
using Content.Shared.Chemistry;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
using System;
using System.Collections.Generic;
namespace Content.Shared.GameObjects.Components.Chemistry
{
@@ -17,8 +17,8 @@ namespace Content.Shared.GameObjects.Components.Chemistry
#pragma warning restore 649
[ViewVariables]
protected Solution _containedSolution = new Solution();
protected decimal _maxVolume;
protected Solution ContainedSolution = new Solution();
private decimal _maxVolume;
private SolutionCaps _capabilities;
/// <summary>
@@ -40,7 +40,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
/// The total volume of all the of the reagents in the container.
/// </summary>
[ViewVariables]
public decimal CurrentVolume => _containedSolution.TotalVolume;
public decimal CurrentVolume => ContainedSolution.TotalVolume;
/// <summary>
/// The volume without reagents remaining in the container.
@@ -64,7 +64,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
set => _capabilities = value;
}
public IReadOnlyList<Solution.ReagentQuantity> ReagentList => _containedSolution.Contents;
public IReadOnlyList<Solution.ReagentQuantity> ReagentList => ContainedSolution.Contents;
/// <summary>
/// Shortcut for Capabilities PourIn flag to avoid binary operators.
@@ -94,8 +94,8 @@ namespace Content.Shared.GameObjects.Components.Chemistry
{
base.ExposeData(serializer);
serializer.DataField(ref _maxVolume, "maxVol", 0);
serializer.DataField(ref _containedSolution, "contents", _containedSolution);
serializer.DataField(ref _maxVolume, "maxVol", 0M);
serializer.DataField(ref ContainedSolution, "contents", ContainedSolution);
serializer.DataField(ref _capabilities, "caps", SolutionCaps.None);
}
@@ -112,13 +112,13 @@ namespace Content.Shared.GameObjects.Components.Chemistry
{
base.Shutdown();
_containedSolution.RemoveAllSolution();
_containedSolution = new Solution();
ContainedSolution.RemoveAllSolution();
ContainedSolution = new Solution();
}
public void RemoveAllSolution()
{
_containedSolution.RemoveAllSolution();
ContainedSolution.RemoveAllSolution();
OnSolutionChanged();
}
@@ -126,7 +126,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
{
if (!ContainsReagent(reagentId, out var currentQuantity)) return false;
_containedSolution.RemoveReagent(reagentId, quantity);
ContainedSolution.RemoveReagent(reagentId, quantity);
OnSolutionChanged();
return true;
}
@@ -141,27 +141,27 @@ namespace Content.Shared.GameObjects.Components.Chemistry
if (CurrentVolume == 0)
return false;
_containedSolution.RemoveSolution(quantity);
ContainedSolution.RemoveSolution(quantity);
OnSolutionChanged();
return true;
}
public Solution SplitSolution(decimal quantity)
{
var solutionSplit = _containedSolution.SplitSolution(quantity);
var solutionSplit = ContainedSolution.SplitSolution(quantity);
OnSolutionChanged();
return solutionSplit;
}
protected void RecalculateColor()
{
if(_containedSolution.TotalVolume == 0)
if(ContainedSolution.TotalVolume == 0)
SubstanceColor = Color.White;
Color mixColor = default;
var runningTotalQuantity = 0M;
foreach (var reagent in _containedSolution)
foreach (var reagent in ContainedSolution)
{
runningTotalQuantity += reagent.Quantity;
@@ -218,7 +218,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
/// <returns>Return true if the solution contains the reagent.</returns>
public bool ContainsReagent(string reagentId, out decimal quantity)
{
foreach (var reagent in _containedSolution.Contents)
foreach (var reagent in ContainedSolution.Contents)
{
if (reagent.ReagentId == reagentId)
{

View File

@@ -0,0 +1,7 @@
namespace Content.Shared.Interfaces.Chemistry
{
public interface IRounderForReagents
{
decimal Round(decimal value);
}
}

View File

@@ -1,12 +0,0 @@
using System;
namespace Content.Shared.Maths
{
public static class Rounders
{
public static decimal RoundForReagents(this decimal me)
{
return Math.Round(me, 2);
}
}
}