2021-01-14 23:40:10 -06:00
#nullable enable
2021-01-23 19:36:48 +01:00
using System ;
2021-01-07 00:31:43 -06:00
using System.Collections.Generic ;
2020-08-13 14:40:27 +02:00
using Content.Server.Interfaces.Chemistry ;
2021-01-07 00:31:43 -06:00
using Content.Shared.Interfaces ;
using Robust.Shared.IoC ;
2019-10-11 16:57:16 -04:00
using Robust.Shared.Prototypes ;
using Robust.Shared.Serialization ;
using YamlDotNet.RepresentationModel ;
2021-01-07 00:31:43 -06:00
namespace Content.Shared.Chemistry
2019-10-11 16:57:16 -04:00
{
/// <summary>
/// Prototype for chemical reaction definitions
/// </summary>
[Prototype("reaction")]
public class ReactionPrototype : IPrototype , IIndexedPrototype
{
2021-01-14 23:40:10 -06:00
private string _id = default ! ;
private string _name = default ! ;
private Dictionary < string , ReactantPrototype > _reactants = default ! ;
private Dictionary < string , ReagentUnit > _products = default ! ;
2021-01-23 19:36:48 +01:00
private IReactionEffect [ ] _effects = default ! ;
2019-10-11 16:57:16 -04:00
public string ID = > _id ;
public string Name = > _name ;
/// <summary>
/// Reactants required for the reaction to occur.
/// </summary>
public IReadOnlyDictionary < string , ReactantPrototype > Reactants = > _reactants ;
/// <summary>
/// Reagents created when the reaction occurs.
/// </summary>
2020-04-05 11:36:12 +02:00
public IReadOnlyDictionary < string , ReagentUnit > Products = > _products ;
2019-10-11 16:57:16 -04:00
/// <summary>
/// Effects to be triggered when the reaction occurs.
/// </summary>
public IReadOnlyList < IReactionEffect > Effects = > _effects ;
2021-01-14 23:40:10 -06:00
public string? Sound { get ; private set ; }
2021-01-14 01:06:23 -06:00
2021-01-07 00:31:43 -06:00
[Dependency] private readonly IModuleManager _moduleManager = default ! ;
2019-10-11 16:57:16 -04:00
public void LoadFrom ( YamlMappingNode mapping )
{
var serializer = YamlObjectSerializer . NewReader ( mapping ) ;
serializer . DataField ( ref _id , "id" , string . Empty ) ;
serializer . DataField ( ref _name , "name" , string . Empty ) ;
serializer . DataField ( ref _reactants , "reactants" , new Dictionary < string , ReactantPrototype > ( ) ) ;
2020-04-05 11:36:12 +02:00
serializer . DataField ( ref _products , "products" , new Dictionary < string , ReagentUnit > ( ) ) ;
2021-01-14 01:06:23 -06:00
serializer . DataField ( this , x = > x . Sound , "sound" , "/Audio/Effects/Chemistry/bubbles.ogg" ) ;
2021-01-07 00:31:43 -06:00
if ( _moduleManager . IsServerModule )
{
//TODO: Don't have a check for if this is the server
//Some implementations of IReactionEffect can't currently be moved to shared, so this is here to prevent the client from breaking when reading server-only IReactionEffects.
2021-01-23 19:36:48 +01:00
serializer . DataField ( ref _effects , "effects" , Array . Empty < IReactionEffect > ( ) ) ;
2021-01-07 00:31:43 -06:00
}
2021-01-14 23:40:10 -06:00
else
{
2021-01-23 19:36:48 +01:00
_effects = Array . Empty < IReactionEffect > ( ) ; //To ensure _effects isn't null since it is only serializable on the server right snow
2021-01-14 23:40:10 -06:00
}
2019-10-11 16:57:16 -04:00
}
}
/// <summary>
/// Prototype for chemical reaction reactants.
/// </summary>
public class ReactantPrototype : IExposeData
{
2020-04-05 11:36:12 +02:00
private ReagentUnit _amount ;
2019-10-11 16:57:16 -04:00
private bool _catalyst ;
/// <summary>
/// Minimum amount of the reactant needed for the reaction to occur.
/// </summary>
2020-04-05 11:36:12 +02:00
public ReagentUnit Amount = > _amount ;
2019-10-11 16:57:16 -04:00
/// <summary>
/// Whether or not the reactant is a catalyst. Catalysts aren't removed when a reaction occurs.
/// </summary>
public bool Catalyst = > _catalyst ;
2021-02-04 17:44:49 +01:00
void IExposeData . ExposeData ( ObjectSerializer serializer )
2019-10-11 16:57:16 -04:00
{
2020-04-05 11:36:12 +02:00
serializer . DataField ( ref _amount , "amount" , ReagentUnit . New ( 1 ) ) ;
2019-10-11 16:57:16 -04:00
serializer . DataField ( ref _catalyst , "catalyst" , false ) ;
}
}
}