ResistanceSet Yaml serialization (#1484)

* ResistanceSet serialization

* DefaultResistanceSet

* remove wall resistance

* ResistanceSet ExposeData fix

* Remove commented out code

Co-authored-by: py01 <pyronetics01@gmail.com>
This commit is contained in:
py01
2020-07-28 00:40:08 -06:00
committed by GitHub
parent bd7079278e
commit af1a824fcd
3 changed files with 32 additions and 51 deletions

View File

@@ -34,7 +34,8 @@ namespace Content.Server.GameObjects
public IReadOnlyDictionary<DamageType, int> CurrentDamage => _currentDamage;
private Dictionary<DamageType, int> _currentDamage = new Dictionary<DamageType, int>();
Dictionary<DamageType, List<DamageThreshold>> Thresholds = new Dictionary<DamageType, List<DamageThreshold>>();
[ViewVariables]
public Dictionary<DamageType, List<DamageThreshold>> Thresholds = new Dictionary<DamageType, List<DamageThreshold>>();
public event EventHandler<DamageThresholdPassedEventArgs> DamageThresholdPassed;
public event EventHandler<DamageEventArgs> Damaged;
@@ -47,12 +48,7 @@ namespace Content.Server.GameObjects
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
// TODO: Writing.
serializer.DataReadFunction("resistanceset", "honk", name =>
{
Resistances = ResistanceSet.GetResistanceSet(name);
});
serializer.DataField(this, x => Resistances, "resistances", ResistanceSet.DefaultResistanceSet);
}
public bool IsDead()

View File

@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using Content.Shared.GameObjects;
using Robust.Shared.Interfaces.Serialization;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects
{
@@ -8,49 +11,31 @@ namespace Content.Server.GameObjects
/// Resistance set used by damageable objects.
/// For each damage type, has a coefficient, damage reduction and "included in total" value.
/// </summary>
public class ResistanceSet
public class ResistanceSet : IExposeData
{
Dictionary<DamageType, ResistanceSetSettings> _resistances = new Dictionary<DamageType, ResistanceSetSettings>();
static Dictionary<string, ResistanceSet> _resistanceSets = new Dictionary<string, ResistanceSet>();
public static ResistanceSet DefaultResistanceSet = new ResistanceSet();
[ViewVariables]
private readonly Dictionary<DamageType, ResistanceSetSettings> _resistances = new Dictionary<DamageType, ResistanceSetSettings>();
//TODO: make it load from YAML instead of hardcoded like this
public ResistanceSet()
{
_resistances.Add(DamageType.Total, new ResistanceSetSettings(1f, 0, true));
_resistances.Add(DamageType.Acid, new ResistanceSetSettings(1f, 0, true));
_resistances.Add(DamageType.Brute, new ResistanceSetSettings(1f, 0, true));
_resistances.Add(DamageType.Heat, new ResistanceSetSettings(1f, 0, true));
_resistances.Add(DamageType.Cold, new ResistanceSetSettings(1f, 0, true));
_resistances.Add(DamageType.Toxic, new ResistanceSetSettings(1f, 0, true));
_resistances.Add(DamageType.Electric, new ResistanceSetSettings(1f, 0, true));
}
/// <summary>
/// Loads a resistance set with the given name.
/// </summary>
/// <param name="setName">Name of the resistance set.</param>
/// <returns>Resistance set by given name</returns>
public static ResistanceSet GetResistanceSet(string setName)
{
ResistanceSet resistanceSet = null;
if (!_resistanceSets.TryGetValue(setName, out resistanceSet))
foreach (DamageType damageType in Enum.GetValues(typeof(DamageType)))
{
resistanceSet = Load(setName);
_resistances[damageType] = new ResistanceSetSettings();
}
return resistanceSet;
}
static ResistanceSet Load(string setName)
public void ExposeData(ObjectSerializer serializer)
{
//TODO: only creates a standard set RN, should be YAMLed
ResistanceSet resistanceSet = new ResistanceSet();
_resistanceSets.Add(setName, resistanceSet);
return resistanceSet;
foreach (DamageType damageType in Enum.GetValues(typeof(DamageType)))
{
var resistanceName = damageType.ToString().ToLower();
serializer.DataReadFunction(resistanceName, new ResistanceSetSettings(), resistanceSetting =>
{
_resistances[damageType] = resistanceSetting;
});
}
}
/// <summary>
@@ -78,23 +63,23 @@ namespace Content.Server.GameObjects
{
//Damage that goes straight to total (for whatever reason) never applies twice
return damageType == DamageType.Total ? false : _resistances[damageType].AppliesToTotal;
return damageType != DamageType.Total && _resistances[damageType].AppliesToTotal;
}
/// <summary>
/// Settings for a specific damage type in a resistance set.
/// </summary>
struct ResistanceSetSettings
public class ResistanceSetSettings : IExposeData
{
public float Coefficient { get; private set; }
public int DamageReduction { get; private set; }
public bool AppliesToTotal { get; private set; }
public float Coefficient { get; private set; } = 1;
public int DamageReduction { get; private set; } = 0;
public bool AppliesToTotal { get; private set; } = true;
public ResistanceSetSettings(float coefficient, int damageReduction, bool appliesInTotal)
public void ExposeData(ObjectSerializer serializer)
{
Coefficient = coefficient;
DamageReduction = damageReduction;
AppliesToTotal = appliesInTotal;
serializer.DataField(this, x => Coefficient, "coefficient", 1);
serializer.DataField(this, x => DamageReduction, "damageReduction", 0);
serializer.DataField(this, x => AppliesToTotal, "appliesToTotal", true);
}
}
}

View File

@@ -1,4 +1,4 @@
- type: entity
- type: entity
id: base_wall
name: basewall
description: Keeps the air in and the greytide out.