ketura

stat.v.2.0

Aug 23rd, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.49 KB | None | 0 0
  1.  
  2. public abstract class Stat<T>
  3. {
  4.     public string Name { get; }
  5.     public string Description { get; }
  6.     public string Notes { get; }
  7.  
  8.     public virtual Type StatType { get { return typeof(Value); } }
  9.  
  10.     public abstract T Value { get; protected set; }
  11.  
  12.     public virtual T Use() { return Value; }
  13.     public virtual T Use(T amount) { return Value; }
  14.  
  15.     public abstract T Modify(T t);
  16.     public virtual T ModifyAndUse(T t, int count=1)
  17.     {
  18.         Modify(t);
  19.         return Use(count);
  20.     }
  21. }
  22.  
  23. public abstract class SimpleStat<T> : Stat<T>
  24. {
  25.     public override T Value { get; protected set; }
  26.  
  27.     public override T Modify(T t)
  28.     {
  29.         Value = t;
  30.         return t;
  31.     }
  32. }
  33.  
  34. public class StringStat : SimpleStat<string> { }
  35. public class BoolStat : SimpleStat<bool> { }
  36.  
  37. public abstract class NumericStat<T> : Stat<T>
  38.     where T : struct
  39. {
  40.     public virtual T MaxValue { get; }
  41.     public virtual T MinValue { get; }
  42.  
  43.     public virtual T Value { get; protected set; }
  44.  
  45.     public override T Modify(T amount)
  46.     {
  47.         Value = Value + amount;
  48.         ClampValue();
  49.         return Value;
  50.     }
  51.  
  52.     protected virtual void ClampValue()
  53.     {
  54.         Value = (Value < MinValue) ? MinValue : ((Value > MaxValue) ? MaxValue : Value);
  55.     }
  56.  
  57.     public virtual T Add(T amount)
  58.     {
  59.         return Modify(amount);
  60.     }
  61.  
  62.     public virtual T Subtract(T amount)
  63.     {
  64.         return Add(-amount);
  65.     }
  66.  
  67.     public virtual T Set(T amount)
  68.     {
  69.         return Add(amount - Value);
  70.     }
  71. }
  72.  
  73. public class DecimalStat : NumericStat<decimal> { }
  74. public class DoubleStat : NumericStat<double> { }
  75. public class FloatStat : NumericStat<float> { }
  76. public class ByteStat : NumericStat<byte> { }
  77. public class SByteStat : NumericStat<sbyte> { }
  78. public class ShortStat : NumericStat<short> { }
  79. public class UShortStat : NumericStat<ushort> { }
  80. public class IntStat : NumericStat<int> { }
  81. public class UIntStat : NumericStat<uint> { }
  82. public class LongStat : NumericStat<long> { }
  83. public class ULongStat : NumericStat<ulong> { }
  84.  
  85. public abstract class HybridStat<T> : NumericStat<T>
  86. {
  87.     public byte Decimals { get; }
  88.  
  89.     public virtual T RawValue { get; protected set; }
  90.  
  91.     //these might need cached since Decimals never changes
  92.     public override T MaxValue { get { return (T)(Ceiling / DecimalFactor); } }
  93.     public override T MinValue { get { return (T)(Floor / DecimalFactor); } }
  94.     public virtual double DecimalFactor { get { return Math.Pow(10, Decimals); } }
  95.  
  96.     public virtual double DecimalPart { get { return (RawValue % DecimalFactor) / DecimalFactor; } }
  97.  
  98.     public override T Value
  99.     {    
  100.         get { return (T)(RawValue / DecimalFactor); }
  101.         protected set
  102.         {
  103.  
  104.             RawValue = (T)(value / DecimalFactor);
  105.             ClampValue();
  106.         }
  107.     }
  108.  
  109.     protected override void ClampValue()
  110.     {
  111.         RawValue = (RawValue < MinValue) ? MinValue : ((RawValue > MaxValue) ? MaxValue : RawValue);
  112.     }
  113. }
  114.  
  115. public class UnsignedHybridStat : HybridStat<ulong>
  116. {
  117.     public static readonly byte MaxDecimals = 19;
  118.  
  119.     public virtual ulong Modify(long amount)
  120.     {
  121.         Value = Value + amount;
  122.         return Value;
  123.     }
  124. }
  125.  
  126. public class SignedHybridStat : HybridStat<long>
  127. {
  128.     public static readonly byte MaxDecimals = 20;
  129. }
  130.  
  131. //------------------
  132. //These will go in a mod
  133. //------------------
  134.  
  135. public class ResourceStat<T,U> : FloatStat
  136.     where T : UnsignedHybridStat
  137.     where U : UnsignedHybridStat
  138. {
  139.     protected T BaseStat { get; set; }
  140.     public U CurrentStat { get; protected set; }
  141.  
  142.     public override ulong Value
  143.     {
  144.         get { return BaseStat.Value; }
  145.         protected set { BaseStat.Set(value);}
  146.     }
  147.  
  148.     public ulong Current
  149.     {
  150.         get { return CurrentStat.Value; }
  151.         protected set { CurrentStat.Set(value); }
  152.     }
  153.  
  154.     public override ulong Use()
  155.     {
  156.         return BaseStat.Use();
  157.     }
  158.  
  159.     public override ulong Use(ulong amount)
  160.     {
  161.         return BaseStat.Use(u);
  162.     }
  163.  
  164.     public override ulong Modify(ulong u)
  165.     {
  166.         return BaseStat.Modify(u)
  167.     }
  168.  
  169.     public virtual ulong Add(long amount)
  170.     {
  171.         CurrentStat.Modify(amount);
  172.         BaseStat.Use(amount);
  173.     }
  174.  
  175.     public virtual ulong Subtract(long amount)
  176.     {
  177.         Add(-amount);
  178.     }
  179.  
  180.     public virtual ulong Set(ulong amount)
  181.     {
  182.         Add(amount - CurrentStat.Value);
  183.     }
  184. }
  185.  
  186. //------------------
  187. //These will go in a Renegade-specific mod
  188. //------------------
  189.  
  190. public class EVStat : UnsignedHybridStat
  191. {
  192.     public float ExcerciseAmount { get; }
  193.     public ulong UsageThreshold { get; }
  194.  
  195.     public override ulong Use()
  196.     {
  197.         return Use(1L);
  198.     }
  199.  
  200.     public override ulong Use(ulong u)
  201.     {
  202.         ulong oldValue = Value;
  203.         int count = Math.Max(u / UsageThreshold, 1);
  204.         Value = oldValue + (ExcerciseAmount * count);
  205.         return oldValue;
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment