ketura

Stat

Aug 23rd, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.69 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 abstract T Value { get; protected set; }
  9.  
  10.     public virtual T Use() { return Value; }
  11.     public virtual T Use(T amount) { return Value; }
  12.  
  13.     public abstract T Modify(T t);
  14. }
  15.  
  16. public abstract class SimpleStat<T> : Stat<T>
  17. {
  18.     public override T Value { get; protected set; }
  19.  
  20.     public override T Modify(T t)
  21.     {
  22.         Value = t;
  23.         return t;
  24.     }
  25. }
  26.  
  27. public class StringStat : SimpleStat<string> { }
  28.  
  29. public class BoolStat : SimpleStat<bool> { }
  30.  
  31. public abstract class NumericStat<T> : Stat<T>
  32.     where T : struct
  33. {
  34.     //readonly properties, set during constructor and nowhere else
  35.     public T Ceiling { get; }
  36.     public T Floor { get; }
  37.    
  38.     //these might need cached since Decimals never changes
  39.     public virtual T MaxValue { get { return Ceiling == 0 ? T.MaxValue : Ceiling; } }
  40.     public virtual T MinValue { get { return Floor == 0 ? T.MinValue : Floor; } }
  41.  
  42.     public virtual T Value { get; protected set; }
  43.  
  44.     public override T Modify(T amount)
  45.     {
  46.         Value = Value + amount;
  47.         ClampValue();
  48.         return Value;
  49.     }
  50.  
  51.     protected virtual void ClampValue()
  52.     {
  53.         Value = (Value < MinValue) ? MinValue : ((Value > MaxValue) ? MaxValue : Value);
  54.     }
  55. }
  56.  
  57. public abstract class IntegerStat<T> : NumericStat<T>
  58. {
  59.     public byte Decimals { get; }
  60.  
  61.     public virtual T RawValue { get; protected set; }
  62.  
  63.     //these might need cached since Decimals never changes
  64.     public override T MaxValue { get { return (T)(Ceiling / DecimalFactor); } }
  65.     public override T MinValue { get { return (T)(Floor / DecimalFactor); } }
  66.     public virtual double DecimalFactor { get { return Math.Pow(10, Decimals); } }
  67.  
  68.     public virtual double DecimalPart { get { return (RawValue % DecimalFactor) / DecimalFactor; } }
  69.  
  70.     public override T Value
  71.     {    
  72.         get { return (T)(RawValue / DecimalFactor); }
  73.         protected set
  74.         {
  75.  
  76.             RawValue = (T)(value / DecimalFactor);
  77.             ClampValue();
  78.         }
  79.     }
  80.  
  81.     protected override void ClampValue()
  82.     {
  83.         RawValue = (RawValue < MinValue) ? MinValue : ((RawValue > MaxValue) ? MaxValue : RawValue);
  84.     }
  85. }
  86.  
  87. public class DecimalStat : NumericStat<decimal> { }
  88.  
  89. public class ULongStat : IntegerStat<ulong>
  90. {
  91.     public static readonly byte MaxDecimals = 19;
  92.  
  93.     public virtual ulong Modify(long amount)
  94.     {
  95.         Value = Value + amount;
  96.         return Value;
  97.     }
  98. }
  99.  
  100. public class LongStat : IntegerStat<long>
  101. {
  102.     public static readonly byte MaxDecimals = 20;
  103. }
  104.  
  105. public class EVStat : ULongStat
  106. {
  107.     public float ExcerciseAmount { get; }
  108.     public ulong UsageThreshold { get; }
  109.  
  110.     public override ulong Use()
  111.     {
  112.         return Use(1L);
  113.     }
  114.  
  115.     public override ulong Use(ulong u)
  116.     {
  117.         ulong oldValue = Value;
  118.         int count = Math.Max(u / UsageThreshold, 1);
  119.         Value = oldValue + (ExcerciseAmount * count);
  120.         return oldValue;
  121.     }
  122. }
  123.  
  124. public class ResourceStat : Stat<ulong>
  125. {
  126.     protected EVStat BaseStat { get; set; }
  127.     public ULongStat CurrentStat { get; protected set; }
  128.  
  129.     public override ulong Value
  130.     {
  131.         get { return BaseStat.Value; }
  132.         protected set { BaseStat.Value = value;}
  133.     }
  134.  
  135.     public ulong Current
  136.     {
  137.         get { return CurrentStat.Value; }
  138.         protected set { CurrentStat.Value = value; }
  139.     }
  140.  
  141.     public override ulong Use()
  142.     {
  143.         return BaseStat.Use();
  144.     }
  145.  
  146.     public override ulong Use(ulong amount)
  147.     {
  148.         return BaseStat.Use(u);
  149.     }
  150.  
  151.     public override ulong Modify(ulong u)
  152.     {
  153.         return BaseStat.Modify(u)
  154.     }
  155.  
  156.     public virtual ulong Add(long amount)
  157.     {
  158.         CurrentStat.Modify(amount);
  159.         BaseStat.Use(amount);
  160.     }
  161.  
  162.     public virtual ulong Subtract(long amount)
  163.     {
  164.         Add(-amount);
  165.     }
  166.  
  167.     public virtual ulong Set(ulong amount)
  168.     {
  169.         Add(amount - CurrentStat.Value);
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment