Urist's damage and health thing. (#10)

* Add prototype for temperature testing entity.

* Add Damageable, Destructible, Temperature. Add a prototype based on those.

* Works and cleaned up.

* Nerf
This commit is contained in:
Pieter-Jan Briers
2017-10-07 15:15:29 +02:00
committed by GitHub
parent 7597cd9172
commit 6f89d0672d
16 changed files with 618 additions and 34 deletions

View File

@@ -0,0 +1,197 @@
using Content.Server.Interfaces.GameObjects;
using System;
using System.Collections.Generic;
using OpenTK;
using SS14.Shared.GameObjects;
using SS14.Shared.Utility;
using YamlDotNet.RepresentationModel;
using Content.Server.Interfaces;
using Content.Shared.GameObjects;
namespace Content.Server.GameObjects
{
//TODO: add support for component add/remove
/// <summary>
/// A component that handles receiving damage and healing,
/// as well as informing other components of it.
/// </summary>
public class DamageableComponent : Component, IDamageableComponent
{
/// <inheritdoc />
public override string Name => "Damageable";
/// <inheritdoc />
public override uint? NetID => ContentNetIDs.DAMAGEABLE;
/// <summary>
/// The resistance set of this object.
/// Affects receiving damage of various types.
/// </summary>
public ResistanceSet Resistances { get; private set; }
Dictionary<DamageType, int> CurrentDamage = new Dictionary<DamageType, int>();
Dictionary<DamageType, List<int>> Thresholds = new Dictionary<DamageType, List<int>>();
public event EventHandler<DamageThresholdPassedEventArgs> DamageThresholdPassed;
/// <inheritdoc />
public override void LoadParameters(YamlMappingNode mapping)
{
if (mapping.TryGetNode("resistanceset", out YamlNode node))
{
Resistances = ResistanceSet.GetResistanceSet(node.AsString());
}
}
/// <inheritdoc />
public override void Initialize()
{
base.Initialize();
InitializeDamageType(DamageType.Total);
if (Owner is IOnDamageBehavior damageBehavior)
{
AddThresholdsFrom(damageBehavior);
}
RecalculateComponentThresholds();
}
/// <inheritdoc />
public void TakeDamage(DamageType damageType, int amount)
{
if (damageType == DamageType.Total)
{
throw new ArgumentException("Cannot take damage for DamageType.Total");
}
InitializeDamageType(damageType);
int oldValue = CurrentDamage[damageType];
int oldTotalValue = -1;
if (amount == 0)
{
return;
}
amount = Resistances.CalculateDamage(damageType, amount);
CurrentDamage[damageType] = Math.Max(0, CurrentDamage[damageType] + amount);
UpdateForDamageType(damageType, oldValue);
if (Resistances.AppliesToTotal(damageType))
{
oldTotalValue = CurrentDamage[DamageType.Total];
CurrentDamage[DamageType.Total] = Math.Max(0, CurrentDamage[DamageType.Total] + amount);
UpdateForDamageType(DamageType.Total, oldTotalValue);
}
}
/// <inheritdoc />
public void TakeHealing(DamageType damageType, int amount)
{
if (damageType == DamageType.Total)
{
throw new ArgumentException("Cannot heal for DamageType.Total");
}
TakeDamage(damageType, -amount);
}
void UpdateForDamageType(DamageType damageType, int oldValue)
{
int change = CurrentDamage[damageType] - oldValue;
if (change == 0)
{
return;
}
int changeSign = Math.Sign(change);
foreach (int value in Thresholds[damageType])
{
if (((value * changeSign) > (oldValue * changeSign)) && ((value * changeSign) <= (CurrentDamage[damageType] * changeSign)))
{
var args = new DamageThresholdPassedEventArgs(new DamageThreshold(damageType, value), (changeSign > 0));
DamageThresholdPassed?.Invoke(this, args);
}
}
}
void RecalculateComponentThresholds()
{
foreach (IOnDamageBehavior onDamageBehaviorComponent in Owner.GetComponents<IOnDamageBehavior>())
{
AddThresholdsFrom(onDamageBehaviorComponent);
}
}
void AddThresholdsFrom(IOnDamageBehavior onDamageBehavior)
{
if (onDamageBehavior == null)
{
throw new ArgumentNullException(nameof(onDamageBehavior));
}
List<DamageThreshold> thresholds = onDamageBehavior.GetAllDamageThresholds();
foreach (DamageThreshold threshold in thresholds)
{
if (!Thresholds[threshold.DamageType].Contains(threshold.Value))
{
Thresholds[threshold.DamageType].Add(threshold.Value);
}
}
}
void InitializeDamageType(DamageType damageType)
{
if (!CurrentDamage.ContainsKey(damageType))
{
CurrentDamage.Add(damageType, 0);
Thresholds.Add(damageType, new List<int>());
}
}
}
public struct DamageThreshold
{
public DamageType DamageType { get; }
public int Value { get; }
public DamageThreshold(DamageType damageType, int value)
{
DamageType = damageType;
Value = value;
}
public override bool Equals(Object obj)
{
return obj is DamageThreshold && this == (DamageThreshold)obj;
}
public override int GetHashCode()
{
return DamageType.GetHashCode() ^ Value.GetHashCode();
}
public static bool operator ==(DamageThreshold x, DamageThreshold y)
{
return x.DamageType == y.DamageType && x.Value == y.Value;
}
public static bool operator !=(DamageThreshold x, DamageThreshold y)
{
return !(x == y);
}
}
public class DamageThresholdPassedEventArgs : EventArgs
{
public DamageThreshold DamageThreshold { get; }
public bool Passed { get; }
public DamageThresholdPassedEventArgs(DamageThreshold threshold, bool passed)
{
DamageThreshold = threshold;
Passed = passed;
}
}
}

View File

@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using SS14.Shared.GameObjects;
using SS14.Shared.Log;
using SS14.Shared.Utility;
using YamlDotNet.RepresentationModel;
using Content.Server.Interfaces;
using Content.Shared.GameObjects;
namespace Content.Server.GameObjects
{
/// <summary>
/// Deletes the entity once a certain damage threshold has been reached.
/// </summary>
public class DestructibleComponent : Component, IOnDamageBehavior
{
/// <inheritdoc />
public override string Name => "Destructible";
/// <inheritdoc />
public override uint? NetID => ContentNetIDs.DESTRUCTIBLE;
/// <summary>
/// Damage threshold calculated from the values
/// given in the prototype declaration.
/// </summary>
public DamageThreshold Threshold { get; private set; }
/// <inheritdoc />
public override void LoadParameters(YamlMappingNode mapping)
{
//TODO currently only supports one threshold pair; gotta figure out YAML better
YamlNode node;
DamageType damageType = DamageType.Total;
int damageValue = 0;
if (mapping.TryGetNode("thresholdtype", out node))
{
damageType = node.AsEnum<DamageType>();
}
if (mapping.TryGetNode("thresholdvalue", out node))
{
damageValue = node.AsInt();
}
Threshold = new DamageThreshold(damageType, damageValue);
}
/// <inheritdoc />
public override void Initialize()
{
base.Initialize();
if (Owner.TryGetComponent<DamageableComponent>(out DamageableComponent damageable))
{
damageable.DamageThresholdPassed += OnDamageThresholdPassed;
}
}
/// <inheritdoc />
public List<DamageThreshold> GetAllDamageThresholds()
{
return new List<DamageThreshold>() { Threshold };
}
/// <inheritdoc />
public void OnDamageThresholdPassed(object obj, DamageThresholdPassedEventArgs e)
{
if (e.Passed && e.DamageThreshold == Threshold)
{
Owner.EntityManager.DeleteEntity(Owner);
}
}
}
}

View File

@@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
namespace Content.Server.GameObjects
{
/// <summary>
/// Damage types used in-game.
/// Total should never be used directly - it's a derived value.
/// </summary>
public enum DamageType
{
Total,
Brute,
Heat,
Cold,
Acid,
Toxic,
Electric
}
/// <summary>
/// Resistance set used by damageable objects.
/// For each damage type, has a coefficient, damage reduction and "included in total" value.
/// </summary>
public class ResistanceSet
{
Dictionary<DamageType, ResistanceSetSettings> _resistances = new Dictionary<DamageType, ResistanceSetSettings>();
static Dictionary<string, ResistanceSet> _resistanceSets = new Dictionary<string, ResistanceSet>();
//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))
{
resistanceSet = Load(setName);
}
return resistanceSet;
}
static ResistanceSet Load(string setName)
{
//TODO: only creates a standard set RN, should be YAMLed
ResistanceSet resistanceSet = new ResistanceSet();
_resistanceSets.Add(setName, resistanceSet);
return resistanceSet;
}
/// <summary>
/// Adjusts input damage with the resistance set values.
/// </summary>
/// <param name="damageType">Type of the damage.</param>
/// <param name="amount">Incoming amount of the damage.</param>
/// <returns>Damage adjusted by the resistance set.</returns>
public int CalculateDamage(DamageType damageType, int amount)
{
if (amount > 0) //if it's damage, reduction applies
{
amount -= _resistances[damageType].DamageReduction;
if (amount <= 0)
return 0;
}
amount = (int)Math.Floor(amount * _resistances[damageType].Coefficient);
return amount;
}
public bool AppliesToTotal(DamageType damageType)
{
//Damage that goes straight to total (for whatever reason) never applies twice
return damageType == DamageType.Total ? false : _resistances[damageType].AppliesToTotal;
}
/// <summary>
/// Settings for a specific damage type in a resistance set.
/// </summary>
struct ResistanceSetSettings
{
public float Coefficient { get; private set; }
public int DamageReduction { get; private set; }
public bool AppliesToTotal { get; private set; }
public ResistanceSetSettings(float coefficient, int damageReduction, bool appliesInTotal)
{
Coefficient = coefficient;
DamageReduction = damageReduction;
AppliesToTotal = appliesInTotal;
}
}
}
}