Advertisement
Guest User

AugmentationTable

a guest
Jan 3rd, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace GrimoireEngine.Framework.Collections
  5. {
  6.  
  7.     public enum AugmentationType
  8.     {
  9.         Increase,
  10.         Decrease
  11.     }
  12.  
  13.     public class AugmentationTable<T> where T : struct, IConvertible, IComparable, IFormattable
  14.     {
  15.         private Dictionary<T, int> _augmentationTable;
  16.         private static T[] _augmentationKeys;
  17.         public AugmentationType TableType { get; set; }
  18.         public string Name { get; set; }
  19.  
  20.         public T[] Keys
  21.         {
  22.             get
  23.             {
  24.                 if (_augmentationKeys == null)
  25.                 {
  26.                     _augmentationKeys = (T[])Enum.GetValues(typeof(T));
  27.                 }
  28.                 return _augmentationKeys;
  29.             }
  30.         }
  31.  
  32.         public int this[T type]
  33.         {
  34.             get
  35.             {
  36.                 return GetAugmentation(type);
  37.             }
  38.             set
  39.             {
  40.                 SetAugmentation(type, value);
  41.             }
  42.         }
  43.  
  44.         public const int DefaultAugmentation = MinimumAugmentation;
  45.         public const int MinimumAugmentation = 0;
  46.         public const int MaximumAugmentation = 100;
  47.  
  48.         public AugmentationTable(
  49.             AugmentationType tableType = AugmentationType.Increase,
  50.             int defaultValue = DefaultAugmentation)
  51.         {
  52.             TableType = tableType;
  53.             Initialize(defaultValue);
  54.         }
  55.  
  56.         public void Initialize(int defaultValue = DefaultAugmentation)
  57.         {
  58.             /**
  59.              * We check to see if the internal dictionary has yet to be
  60.              * created. If true, we create it and then add the keys to it using
  61.              * the default value. We do it this way so that we can avoid
  62.              * multiple heap allocations down the road.
  63.              */
  64.             if (this._augmentationTable == null)
  65.             {
  66.                 this._augmentationTable = new Dictionary<T, int>();
  67.                 for (int i = 0; i < Keys.Length; i++)
  68.                 {
  69.                     this._augmentationTable.Add(Keys[i], defaultValue);
  70.                 }
  71.             }
  72.             else
  73.             {
  74.                 /**
  75.                  * The internal dictionary is already created, so
  76.                  * we can just set the values directly instead of recreating
  77.                  * the dictionary. Doing it this way avoids extra
  78.                  * heap allocation(s).
  79.                  */
  80.                 for (int i = 0; i < Keys.Length; i++)
  81.                 {
  82.                     SetAugmentation(Keys[i], defaultValue);
  83.                 }
  84.             }
  85.         }
  86.  
  87.         public void IncreaseAugmentation(T type, int value)
  88.         {
  89.             SetAugmentation(type, GetAugmentation(type) + value);
  90.         }
  91.  
  92.         public void DecreaseAugmentation(T type, int value)
  93.         {
  94.             SetAugmentation(type, GetAugmentation(type) - value);
  95.         }
  96.  
  97.         public void ClearAugmentations()
  98.         {
  99.             Initialize();
  100.         }
  101.  
  102.         public void ClearAugmentation(T type)
  103.         {
  104.             SetAugmentation(type, DefaultAugmentation);
  105.         }
  106.  
  107.         public int GetAugmentation(T type)
  108.         {
  109.             return _augmentationTable[type];
  110.         }
  111.  
  112.         public void SetAugmentation(T type, int value)
  113.         {
  114.             this._augmentationTable[type] = Clamp(value);
  115.         }
  116.  
  117.         public int ComputeAugmentation(T type, int value)
  118.         {
  119.             return ComputeAugmentation(this.TableType, type, value);
  120.         }
  121.  
  122.         public int ComputeAugmentation(AugmentationType augmentationType, T type, int value)
  123.         {
  124.             switch (augmentationType)
  125.             {
  126.                 case AugmentationType.Increase:
  127.                     return (int)(value * (1.0f + (GetAugmentation(type) / 100f)));
  128.                 case AugmentationType.Decrease:
  129.                     return (int)(value - ((value * (GetAugmentation(type) / 100f))));
  130.                 default:
  131.                     throw new ArgumentOutOfRangeException(nameof(augmentationType), augmentationType, null);
  132.             }
  133.         }
  134.  
  135.         private int Clamp(int value)
  136.         {
  137.             return (value < MinimumAugmentation) ? MinimumAugmentation : (value > MaximumAugmentation) ? MaximumAugmentation : value;
  138.         }
  139.  
  140.         public AugmentationTable<T> Clone()
  141.         {
  142.             AugmentationTable<T> table = new AugmentationTable<T>();
  143.             for (int i = 0; i < Keys.Length; i++)
  144.             {
  145.                 table.SetAugmentation(Keys[i], GetAugmentation(Keys[i]));
  146.             }
  147.             table.TableType = TableType;
  148.             return table;
  149.         }
  150.  
  151.         public void Copy(AugmentationTable<T> table)
  152.         {
  153.             TableType = table.TableType;
  154.             for (int i = 0; i < Keys.Length; i++)
  155.             {
  156.                 this.SetAugmentation(Keys[i], table.GetAugmentation(Keys[i]));
  157.             }
  158.         }
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement