Refactor reaction effects to use reagent effects (#5426)

* reaction effect refactor

* works now
This commit is contained in:
mirrorcult
2021-11-21 00:35:02 -07:00
committed by GitHub
parent 9b39838db7
commit 1ab7170adb
36 changed files with 83 additions and 82 deletions

View File

@@ -53,7 +53,7 @@ namespace Content.Shared.Chemistry
if (!effect.ShouldApply(args, _robustRandom))
continue;
effect.Metabolize(args);
effect.Effect(args);
}
}
}

View File

@@ -1,13 +0,0 @@
using Content.Shared.Chemistry.Components;
using Robust.Shared.GameObjects;
namespace Content.Shared.Chemistry.Reaction
{
/// <summary>
/// Chemical reaction effect on the world such as an explosion, EMP, or fire.
/// </summary>
public interface IReactionEffect
{
void React(Solution solution, EntityUid solutionEntity, double intensity, IEntityManager entityManager);
}
}

View File

@@ -14,10 +14,6 @@ namespace Content.Shared.Chemistry.Reaction
[Prototype("reaction")]
public class ReactionPrototype : IPrototype
{
[DataField("reactants")] private Dictionary<string, ReactantPrototype> _reactants = new();
[DataField("products")] private Dictionary<string, FixedPoint2> _products = new();
[DataField("effects", serverOnly: true)] private List<IReactionEffect> _effects = new();
[ViewVariables]
[DataField("id", required: true)]
public string ID { get; } = default!;
@@ -28,15 +24,17 @@ namespace Content.Shared.Chemistry.Reaction
/// <summary>
/// Reactants required for the reaction to occur.
/// </summary>
public IReadOnlyDictionary<string, ReactantPrototype> Reactants => _reactants;
[DataField("reactants")] public Dictionary<string, ReactantPrototype> Reactants = new();
/// <summary>
/// Reagents created when the reaction occurs.
/// </summary>
public IReadOnlyDictionary<string, FixedPoint2> Products => _products;
[DataField("products")] public Dictionary<string, FixedPoint2> Products = new();
/// <summary>
/// Effects to be triggered when the reaction occurs.
/// </summary>
public IReadOnlyList<IReactionEffect> Effects => _effects;
[DataField("effects", serverOnly: true)] public List<ReagentEffect> Effects = new();
// TODO SERV3: Empty on the client, (de)serialize on the server with module manager is server module
[DataField("sound", serverOnly: true)] public SoundSpecifier Sound { get; private set; } = new SoundPathSpecifier("/Audio/Effects/Chemistry/bubbles.ogg");

View File

@@ -6,6 +6,7 @@ using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Shared.Chemistry.Reaction
{
@@ -16,6 +17,7 @@ namespace Content.Shared.Chemistry.Reaction
private const int MaxReactionIterations = 20;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
public override void Initialize()
{
@@ -58,6 +60,9 @@ namespace Content.Shared.Chemistry.Reaction
/// </summary>
private Solution PerformReaction(Solution solution, EntityUid ownerUid, ReactionPrototype reaction, FixedPoint2 unitReactions)
{
// We do this so that ReagentEffect can have something to work with, even if it's
// a little meaningless.
var randomReagent = _prototypeManager.Index<ReagentPrototype>(_random.Pick(reaction.Reactants).Key);
//Remove reactants
foreach (var reactant in reaction.Reactants)
{
@@ -76,16 +81,23 @@ namespace Content.Shared.Chemistry.Reaction
}
// Trigger reaction effects
OnReaction(solution, reaction, ownerUid, unitReactions);
OnReaction(solution, reaction, randomReagent, ownerUid, unitReactions);
return products;
}
protected virtual void OnReaction(Solution solution, ReactionPrototype reaction, EntityUid ownerUid, FixedPoint2 unitReactions)
protected virtual void OnReaction(Solution solution, ReactionPrototype reaction, ReagentPrototype randomReagent, EntityUid ownerUid, FixedPoint2 unitReactions)
{
var args = new ReagentEffectArgs(ownerUid, null, solution,
randomReagent,
unitReactions, EntityManager, null);
foreach (var effect in reaction.Effects)
{
effect.React(solution, ownerUid, unitReactions.Double(), EntityManager);
if (!effect.ShouldApply(args))
continue;
effect.Effect(args);
}
}

View File

@@ -30,7 +30,7 @@ namespace Content.Shared.Chemistry.Reagent
[DataField("probability")]
public float Probability = 1.0f;
public abstract void Metabolize(ReagentEffectArgs args);
public abstract void Effect(ReagentEffectArgs args);
}
public static class ReagentEffectExt
@@ -41,11 +41,6 @@ namespace Content.Shared.Chemistry.Reagent
if (random == null)
random = IoCManager.Resolve<IRobustRandom>();
// Make sure we still have enough reagent to go...
if (args.Source != null && !args.Source.ContainsReagent(args.Reagent.ID))
return false;
if (effect.Probability < 1.0f && !random.Prob(effect.Probability))
return false;
@@ -74,7 +69,7 @@ namespace Content.Shared.Chemistry.Reagent
EntityUid? OrganEntity,
Solution? Source,
ReagentPrototype Reagent,
FixedPoint2 Metabolizing,
FixedPoint2 Quantity,
IEntityManager EntityManager,
ReactionMethod? Method
);

View File

@@ -120,7 +120,7 @@ namespace Content.Shared.Chemistry.Reagent
if (!plantMetabolizable.ShouldApply(args, random))
continue;
plantMetabolizable.Metabolize(args);
plantMetabolizable.Effect(args);
}
}
}