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

@@ -15,6 +15,9 @@ namespace Content.Client
factory.RegisterIgnore("Inventory");
factory.RegisterIgnore("Item");
factory.RegisterIgnore("Interactable");
factory.RegisterIgnore("Damageable");
factory.RegisterIgnore("Destructible");
factory.RegisterIgnore("Temperature");
factory.Register<HandsComponent>();
factory.RegisterReference<HandsComponent, IHandsComponent>();

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"/>
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@@ -39,14 +39,14 @@
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="System"/>
<Reference Include="System.Core"/>
<Reference Include="System.Xml.Linq"/>
<Reference Include="System.Data.DataSetExtensions"/>
<Reference Include="Microsoft.CSharp"/>
<Reference Include="System.Data"/>
<Reference Include="System.Net.Http"/>
<Reference Include="System.Xml"/>
<Reference Include="YamlDotNet">
<HintPath>$(SolutionDir)packages\YamlDotNet.4.2.1\lib\net35\YamlDotNet.dll</HintPath>
</Reference>
@@ -55,16 +55,23 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="EntryPoint.cs" />
<Compile Include="EntryPoint.cs"/>
<Compile Include="GameObjects\Components\Interactable\InteractableComponent.cs" />
<Compile Include="Interfaces\GameObjects\Components\Interactable\IInteractableComponent.cs" />
<Compile Include="Interfaces\GameObjects\Components\Items\IHandsComponent.cs" />
<Compile Include="Interfaces\GameObjects\Components\Items\IInventoryComponent.cs" />
<Compile Include="Interfaces\GameObjects\Components\Items\IItemComponent.cs" />
<Compile Include="GameObjects\Components\Items\ServerHandsComponent.cs" />
<Compile Include="GameObjects\Components\Items\InventoryComponent.cs" />
<Compile Include="GameObjects\Components\Items\ItemComponent.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Interfaces\GameObjects\Components\Items\IHandsComponent.cs"/>
<Compile Include="Interfaces\GameObjects\Components\Items\IInventoryComponent.cs"/>
<Compile Include="Interfaces\GameObjects\Components\Items\IItemComponent.cs"/>
<Compile Include="GameObjects\Components\Items\ServerHandsComponent.cs"/>
<Compile Include="GameObjects\Components\Items\InventoryComponent.cs"/>
<Compile Include="GameObjects\Components\Items\ItemComponent.cs"/>
<Compile Include="GameObjects\Components\Damage\DamageableComponent.cs"/>
<Compile Include="GameObjects\Components\Damage\DestructibleComponent.cs"/>
<Compile Include="GameObjects\Components\Damage\ResistanceSet.cs"/>
<Compile Include="GameObjects\Components\Temperature\TemperatureComponent.cs"/>
<Compile Include="Interfaces\GameObjects\IOnDamageBehavior.cs"/>
<Compile Include="Properties\AssemblyInfo.cs"/>
<Compile Include="Interfaces\GameObjects\Components\Temperature\ITemperatureComponent.cs" />
<Compile Include="Interfaces\GameObjects\Components\Damage\IDamageableComponent.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Content.Shared\Content.Shared.csproj">
@@ -84,13 +91,13 @@
<Name>SS14.Shared</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\SS14.Content.targets" />
<Target Name="AfterBuild" DependsOnTargets="CopyContentAssemblies" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>
<Import Project="..\SS14.Content.targets"/>
<Target Name="AfterBuild" DependsOnTargets="CopyContentAssemblies"/>
<ItemGroup>
<ContentAssemblies Include="$(OutputPath)Content.Server.dll" />
<ContentAssemblies Include="$(OutputPath)Content.Shared.dll" />
<ContentAssemblies Include="$(OutputPath)Content.Server.pdb" Condition="'$(Configuration)' == 'Debug'" />
<ContentAssemblies Include="$(OutputPath)Content.Shared.pdb" Condition="'$(Configuration)' == 'Debug'" />
<ContentAssemblies Include="$(OutputPath)Content.Server.dll"/>
<ContentAssemblies Include="$(OutputPath)Content.Shared.dll"/>
<ContentAssemblies Include="$(OutputPath)Content.Server.pdb" Condition="'$(Configuration)' == 'Debug'"/>
<ContentAssemblies Include="$(OutputPath)Content.Shared.pdb" Condition="'$(Configuration)' == 'Debug'"/>
</ItemGroup>
</Project>

View File

@@ -23,6 +23,10 @@ namespace Content.Server
factory.Register<InteractableComponent>();
factory.RegisterReference<InteractableComponent, IInteractableComponent>();
factory.Register<DamageableComponent>();
factory.Register<DestructibleComponent>();
factory.Register<TemperatureComponent>();
}
}
}

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;
}
}
}
}

View File

@@ -0,0 +1,65 @@
using Content.Server.Interfaces.GameObjects;
using Content.Shared.Maths;
using System;
using SS14.Shared.GameObjects;
using SS14.Shared.Utility;
using YamlDotNet.RepresentationModel;
using Content.Shared.GameObjects;
namespace Content.Server.GameObjects
{
/// <summary>
/// Handles changing temperature,
/// informing others of the current temperature,
/// and taking fire damage from high temperature.
/// </summary>
public class TemperatureComponent : Component, ITemperatureComponent
{
/// <inheritdoc />
public override string Name => "Temperature";
/// <inheritdoc />
public override uint? NetID => ContentNetIDs.TEMPERATURE;
//TODO: should be programmatic instead of how it currently is
public float CurrentTemperature { get; private set; } = PhysicalConstants.ZERO_CELCIUS;
float _fireDamageThreshold = 0;
float _fireDamageCoefficient = 1;
float _secondsSinceLastDamageUpdate = 0;
/// <inheritdoc />
public override void LoadParameters(YamlMappingNode mapping)
{
YamlNode node;
if (mapping.TryGetNode("firedamagethreshold", out node))
{
_fireDamageThreshold = node.AsFloat();
}
if (mapping.TryGetNode("firedamagecoefficient", out node))
{
_fireDamageCoefficient = node.AsFloat();
}
}
/// <inheritdoc />
public override void Update(float frameTime)
{
base.Update(frameTime);
int fireDamage = (int)Math.Floor(Math.Max(0, CurrentTemperature - _fireDamageThreshold) / _fireDamageCoefficient);
_secondsSinceLastDamageUpdate += frameTime;
Owner.TryGetComponent<DamageableComponent>(out DamageableComponent component);
while (_secondsSinceLastDamageUpdate >= 1)
{
component?.TakeDamage(DamageType.Heat, fireDamage);
_secondsSinceLastDamageUpdate -= 1;
}
}
}
}

View File

@@ -0,0 +1,30 @@
using Content.Server.GameObjects;
using SS14.Shared.Interfaces.GameObjects;
using System;
namespace Content.Server.Interfaces.GameObjects
{
public interface IDamageableComponent : IComponent
{
event EventHandler<DamageThresholdPassedEventArgs> DamageThresholdPassed;
ResistanceSet Resistances { get; }
/// <summary>
/// The function that handles receiving damage.
/// Converts damage via the resistance set then applies it
/// and informs components of thresholds passed as necessary.
/// </summary>
/// <param name="damageType">Type of damage being received.</param>
/// <param name="amount">Amount of damage being received.</param>
void TakeDamage(DamageType damageType, int amount);
/// <summary>
/// Handles receiving healing.
/// Converts healing via the resistance set then applies it
/// and informs components of thresholds passed as necessary.
/// </summary>
/// <param name="damageType">Type of damage being received.</param>
/// <param name="amount">Amount of damage being received.</param>
void TakeHealing(DamageType damageType, int amount);
}
}

View File

@@ -0,0 +1,9 @@
using SS14.Shared.Interfaces.GameObjects;
namespace Content.Server.Interfaces.GameObjects
{
public interface ITemperatureComponent : IComponent
{
float CurrentTemperature { get; }
}
}

View File

@@ -0,0 +1,27 @@
using System.Collections.Generic;
using Content.Server.GameObjects;
namespace Content.Server.Interfaces
{
/// <summary>
/// Any component/entity that has behaviour linked to taking damage should implement this interface.
/// TODO: Don't know how to work around this currently, but due to how events work
/// you need to hook it up to the DamageableComponent via Initialize().
/// See DestructibleComponent.Initialize() for an example.
/// </summary>
interface IOnDamageBehavior
{
/// <summary>
/// Gets a list of all DamageThresholds this component/entity are interested in.
/// </summary>
/// <returns>List of DamageThresholds to be added to DamageableComponent for watching.</returns>
List<DamageThreshold> GetAllDamageThresholds();
/// <summary>
/// Damage threshold passed event hookup.
/// </summary>
/// <param name="obj">Damageable component.</param>
/// <param name="e">Damage threshold and whether it's passed in one way or another.</param>
void OnDamageThresholdPassed(object obj, DamageThresholdPassedEventArgs e);
}
}

View File

@@ -55,9 +55,11 @@
</ItemGroup>
<ItemGroup>
<Compile Include="EntryPoint.cs" />
<Compile Include="GameObjects\ContentNetIDs.cs" />
<Compile Include="GameObjects\PhysicalConstants.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="GameObjects\Components\Items\SharedHandsComponent.cs" />
<Compile Include="GameObjects\Components\NetIDs.cs" />
<Compile Include="Maths\PhysicalConstants.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\engine\Lidgren.Network\Lidgren.Network.csproj">

View File

@@ -1,7 +0,0 @@
namespace Content.Shared.GameObjects
{
public static class ContentNetIDs
{
public const uint HANDS = 1000;
}
}

View File

@@ -0,0 +1,11 @@
namespace Content.Shared.GameObjects
{
// Starting from 1000 to avoid crossover with engine.
public static class ContentNetIDs
{
public const uint DAMAGEABLE = 1000;
public const uint DESTRUCTIBLE = 1001;
public const uint TEMPERATURE = 1002;
public const uint HANDS = 1003;
}
}

View File

@@ -0,0 +1,10 @@
namespace Content.Shared.GameObjects
{
/// <summary>
/// Contains physical constants used in calculations.
/// </summary>
class PhysicalConstants
{
public const float ZERO_CELCIUS = 273.15f;
}
}

View File

@@ -0,0 +1,10 @@
namespace Content.Shared.Maths
{
/// <summary>
/// Contains physical constants used in calculations.
/// </summary>
public static class PhysicalConstants
{
public const float ZERO_CELCIUS = 273.15f;
}
}

View File

@@ -0,0 +1,23 @@
- type: entity
id: thing_that_heats_up_on_its_own_and_dies
name: Thing that heats up on its own and dies
components:
- type: Transform
- type: Clickable
- type: Sprite
sprites:
- shoes
- type: Icon
icon: shoes
- type: Damageable
resistanceset: Standard
- type: Destructible
thresholdtype: Total
thresholdvalue: 100
- type: Temperature
firedamagethreshold: 200
firedamagecoefficient: 20