Entity Reagent Reactions v2 (#3714)
* Refactors reactions to be more POWERFUL and DATA-ORIENTED
This commit is contained in:
committed by
GitHub
parent
6739d6a6a9
commit
a6f04e22e4
56
Content.Shared/Chemistry/ReagentEntityReaction.cs
Normal file
56
Content.Shared/Chemistry/ReagentEntityReaction.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using Content.Shared.Interfaces.GameObjects.Components;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Shared.Chemistry
|
||||
{
|
||||
public enum ReactionMethod
|
||||
{
|
||||
Touch,
|
||||
Injection,
|
||||
Ingestion,
|
||||
}
|
||||
|
||||
[DataDefinition]
|
||||
public abstract class ReagentEntityReaction
|
||||
{
|
||||
[ViewVariables]
|
||||
[field: DataField("touch")]
|
||||
public bool Touch { get; } = false;
|
||||
|
||||
[ViewVariables]
|
||||
[field: DataField("injection")]
|
||||
public bool Injection { get; } = false;
|
||||
|
||||
[ViewVariables]
|
||||
[field: DataField("ingestion")]
|
||||
public bool Ingestion { get; } = false;
|
||||
|
||||
public void React(ReactionMethod method, IEntity entity, ReagentPrototype reagent, ReagentUnit volume, Solution? source)
|
||||
{
|
||||
switch (method)
|
||||
{
|
||||
case ReactionMethod.Touch:
|
||||
if (!Touch)
|
||||
return;
|
||||
break;
|
||||
case ReactionMethod.Injection:
|
||||
if(!Injection)
|
||||
return;
|
||||
break;
|
||||
case ReactionMethod.Ingestion:
|
||||
if(!Ingestion)
|
||||
return;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(method), method, null);
|
||||
}
|
||||
|
||||
React(entity, reagent, volume, source);
|
||||
}
|
||||
|
||||
protected abstract void React(IEntity entity, ReagentPrototype reagent, ReagentUnit volume, Solution? source);
|
||||
}
|
||||
}
|
||||
@@ -82,38 +82,6 @@ namespace Content.Shared.Chemistry
|
||||
return SubstanceColor;
|
||||
}
|
||||
|
||||
public ReagentUnit ReactionEntity(IEntity? entity, ReactionMethod method, ReagentUnit reactVolume)
|
||||
{
|
||||
var removed = ReagentUnit.Zero;
|
||||
|
||||
if (entity == null || entity.Deleted)
|
||||
return removed;
|
||||
|
||||
foreach (var react in entity.GetAllComponents<IReagentReaction>())
|
||||
{
|
||||
switch (method)
|
||||
{
|
||||
case ReactionMethod.Touch:
|
||||
removed += react.ReagentReactTouch(this, reactVolume);
|
||||
break;
|
||||
case ReactionMethod.Ingestion:
|
||||
removed += react.ReagentReactIngestion(this, reactVolume);
|
||||
break;
|
||||
case ReactionMethod.Injection:
|
||||
removed += react.ReagentReactInjection(this, reactVolume);
|
||||
break;
|
||||
}
|
||||
|
||||
if (removed > reactVolume)
|
||||
throw new Exception("Removed more than we have!");
|
||||
|
||||
if (removed == reactVolume)
|
||||
break;
|
||||
}
|
||||
|
||||
return removed;
|
||||
}
|
||||
|
||||
public ReagentUnit ReactionTile(TileRef tile, ReagentUnit reactVolume)
|
||||
{
|
||||
var removed = ReagentUnit.Zero;
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using Content.Shared.GameObjects.EntitySystems;
|
||||
using Content.Shared.Interfaces.GameObjects.Components;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
@@ -314,15 +315,11 @@ namespace Content.Shared.Chemistry
|
||||
|
||||
public void DoEntityReaction(IEntity entity, ReactionMethod method)
|
||||
{
|
||||
var proto = IoCManager.Resolve<IPrototypeManager>();
|
||||
var chemistry = EntitySystem.Get<ChemistrySystem>();
|
||||
|
||||
foreach (var (reagentId, quantity) in _contents.ToArray())
|
||||
{
|
||||
if (!proto.TryIndex(reagentId, out ReagentPrototype? reagent))
|
||||
continue;
|
||||
|
||||
var removedAmount = reagent.ReactionEntity(entity, method, quantity);
|
||||
RemoveReagent(reagentId, removedAmount);
|
||||
chemistry.ReactionEntity(entity, method, reagentId, quantity, this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using Content.Shared.Chemistry;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Shared.GameObjects.Components.Chemistry
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class ReactiveComponent : Component
|
||||
{
|
||||
public override string Name => "Reactive";
|
||||
|
||||
[field: DataField("reactions", true, serverOnly:true)]
|
||||
public ReagentEntityReaction[] Reactions { get; } = Array.Empty<ReagentEntityReaction>();
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,13 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Content.Shared.Chemistry;
|
||||
using Content.Shared.GameObjects.Components.Chemistry;
|
||||
using Content.Shared.Interfaces.GameObjects.Components;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Shared.GameObjects.EntitySystems
|
||||
{
|
||||
@@ -25,6 +30,8 @@ namespace Content.Shared.GameObjects.EntitySystems
|
||||
[UsedImplicitly]
|
||||
public class ChemistrySystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
|
||||
public void HandleSolutionChange(IEntity owner)
|
||||
{
|
||||
var eventArgs = new SolutionChangeEventArgs
|
||||
@@ -41,5 +48,27 @@ namespace Content.Shared.GameObjects.EntitySystems
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void ReactionEntity(IEntity? entity, ReactionMethod method, string reagentId, ReagentUnit reactVolume, Solution? source)
|
||||
{
|
||||
// We throw if the reagent specified doesn't exist.
|
||||
ReactionEntity(entity, method, _prototypeManager.Index<ReagentPrototype>(reagentId), reactVolume, source);
|
||||
}
|
||||
|
||||
public void ReactionEntity(IEntity? entity, ReactionMethod method, ReagentPrototype reagent, ReagentUnit reactVolume, Solution? source)
|
||||
{
|
||||
if (entity == null || entity.Deleted || !entity.TryGetComponent(out ReactiveComponent? reactive))
|
||||
return;
|
||||
|
||||
foreach (var reaction in reactive.Reactions)
|
||||
{
|
||||
// If we have a source solution, use the reagent quantity we have left. Otherwise, use the reaction volume specified.
|
||||
reaction.React(method, entity, reagent, source?.GetReagentQuantity(reagent.ID) ?? reactVolume, source);
|
||||
|
||||
// Make sure we still have enough reagent to go...
|
||||
if (source != null && !source.ContainsReagent(reagent.ID))
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#nullable enable
|
||||
using System.Linq;
|
||||
using Content.Shared.GameObjects.Components.Movement;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
@@ -11,7 +12,7 @@ namespace Content.Shared.GameObjects.EntitySystems
|
||||
/// <inheritdoc />
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
foreach (var slipperyComp in ComponentManager.EntityQuery<SlipperyComponent>(false))
|
||||
foreach (var slipperyComp in ComponentManager.EntityQuery<SlipperyComponent>().ToArray())
|
||||
{
|
||||
slipperyComp.Update();
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
#nullable enable
|
||||
using Content.Shared.Chemistry;
|
||||
using Robust.Shared.Analyzers;
|
||||
|
||||
namespace Content.Shared.Interfaces.GameObjects.Components
|
||||
{
|
||||
public enum ReactionMethod
|
||||
{
|
||||
Touch,
|
||||
Injection,
|
||||
Ingestion,
|
||||
}
|
||||
|
||||
[RequiresExplicitImplementation]
|
||||
public interface IReagentReaction
|
||||
{
|
||||
ReagentUnit ReagentReactTouch(ReagentPrototype reagent, ReagentUnit volume) => ReagentUnit.Zero;
|
||||
ReagentUnit ReagentReactInjection(ReagentPrototype reagent, ReagentUnit volume) => ReagentUnit.Zero;
|
||||
ReagentUnit ReagentReactIngestion(ReagentPrototype reagent, ReagentUnit volume) => ReagentUnit.Zero;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user