Files
tbd-station-14/Content.Shared/Chemistry/ReagentPrototype.cs
Vera Aguilera Puerto a6f04e22e4 Entity Reagent Reactions v2 (#3714)
* Refactors reactions to be more POWERFUL and DATA-ORIENTED
2021-03-26 12:02:41 +01:00

118 lines
3.9 KiB
C#

#nullable enable
using System;
using System.Collections.Generic;
using Content.Shared.Interfaces.Chemistry;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Shared.Chemistry
{
[Prototype("reagent")]
[DataDefinition]
public class ReagentPrototype : IPrototype
{
[DataField("metabolism", serverOnly: true)]
private readonly List<IMetabolizable> _metabolism = new() {new DefaultMetabolizable()};
[DataField("tileReactions", serverOnly: true)]
private readonly List<ITileReaction> _tileReactions = new(0);
[DataField("plantMetabolism", serverOnly: true)]
private readonly List<IPlantMetabolizable> _plantMetabolism = new(0);
[DataField("customPlantMetabolism")]
private readonly float _customPlantMetabolism = 1f;
[ViewVariables]
[field: DataField("id", required: true)]
public string ID { get; } = default!;
[field: DataField("name")]
public string Name { get; } = string.Empty;
[field: DataField("desc")]
public string Description { get; } = string.Empty;
[field: DataField("physicalDesc")]
public string PhysicalDescription { get; } = string.Empty;
[field: DataField("color")]
public Color SubstanceColor { get; } = Color.White;
[field: DataField("toxin")]
public bool Toxin { get; }
[field: DataField("boozePower")]
public int BoozePower { get; }
[field: DataField("boilingPoint")]
public float? BoilingPoint { get; }
[field: DataField("meltingPoint")]
public float? MeltingPoint { get; }
[field: DataField("spritePath")]
public string SpriteReplacementPath { get; } = string.Empty;
//List of metabolism effects this reagent has, should really only be used server-side.
public IReadOnlyList<IMetabolizable> Metabolism => _metabolism;
public IReadOnlyList<ITileReaction> TileReactions => _tileReactions;
public IReadOnlyList<IPlantMetabolizable> PlantMetabolism => _plantMetabolism;
/// <summary>
/// If the substance color is too dark we user a lighter version to make the text color readable when the user examines a solution.
/// </summary>
public Color GetSubstanceTextColor()
{
var highestValue = MathF.Max(SubstanceColor.R, MathF.Max(SubstanceColor.G, SubstanceColor.B));
var difference = 0.5f - highestValue;
if (difference > 0f)
{
return new Color(SubstanceColor.R + difference,
SubstanceColor.G + difference,
SubstanceColor.B + difference);
}
return SubstanceColor;
}
public ReagentUnit ReactionTile(TileRef tile, ReagentUnit reactVolume)
{
var removed = ReagentUnit.Zero;
if (tile.Tile.IsEmpty)
return removed;
foreach (var reaction in _tileReactions)
{
removed += reaction.TileReact(tile, this, reactVolume - removed);
if (removed > reactVolume)
throw new Exception("Removed more than we have!");
if (removed == reactVolume)
break;
}
return removed;
}
public void ReactionPlant(IEntity? plantHolder)
{
if (plantHolder == null || plantHolder.Deleted)
return;
foreach (var plantMetabolizable in _plantMetabolism)
{
plantMetabolizable.Metabolize(plantHolder, _customPlantMetabolism);
}
}
}
}