Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace GrimoireEngine.Framework.Collections
- {
- public enum AugmentationType
- {
- Increase,
- Decrease
- }
- public class AugmentationTable<T> where T : struct, IConvertible, IComparable, IFormattable
- {
- private Dictionary<T, int> _augmentationTable;
- private static T[] _augmentationKeys;
- public AugmentationType TableType { get; set; }
- public string Name { get; set; }
- public T[] Keys
- {
- get
- {
- if (_augmentationKeys == null)
- {
- _augmentationKeys = (T[])Enum.GetValues(typeof(T));
- }
- return _augmentationKeys;
- }
- }
- public int this[T type]
- {
- get
- {
- return GetAugmentation(type);
- }
- set
- {
- SetAugmentation(type, value);
- }
- }
- public const int DefaultAugmentation = MinimumAugmentation;
- public const int MinimumAugmentation = 0;
- public const int MaximumAugmentation = 100;
- public AugmentationTable(
- AugmentationType tableType = AugmentationType.Increase,
- int defaultValue = DefaultAugmentation)
- {
- TableType = tableType;
- Initialize(defaultValue);
- }
- public void Initialize(int defaultValue = DefaultAugmentation)
- {
- /**
- * We check to see if the internal dictionary has yet to be
- * created. If true, we create it and then add the keys to it using
- * the default value. We do it this way so that we can avoid
- * multiple heap allocations down the road.
- */
- if (this._augmentationTable == null)
- {
- this._augmentationTable = new Dictionary<T, int>();
- for (int i = 0; i < Keys.Length; i++)
- {
- this._augmentationTable.Add(Keys[i], defaultValue);
- }
- }
- else
- {
- /**
- * The internal dictionary is already created, so
- * we can just set the values directly instead of recreating
- * the dictionary. Doing it this way avoids extra
- * heap allocation(s).
- */
- for (int i = 0; i < Keys.Length; i++)
- {
- SetAugmentation(Keys[i], defaultValue);
- }
- }
- }
- public void IncreaseAugmentation(T type, int value)
- {
- SetAugmentation(type, GetAugmentation(type) + value);
- }
- public void DecreaseAugmentation(T type, int value)
- {
- SetAugmentation(type, GetAugmentation(type) - value);
- }
- public void ClearAugmentations()
- {
- Initialize();
- }
- public void ClearAugmentation(T type)
- {
- SetAugmentation(type, DefaultAugmentation);
- }
- public int GetAugmentation(T type)
- {
- return _augmentationTable[type];
- }
- public void SetAugmentation(T type, int value)
- {
- this._augmentationTable[type] = Clamp(value);
- }
- public int ComputeAugmentation(T type, int value)
- {
- return ComputeAugmentation(this.TableType, type, value);
- }
- public int ComputeAugmentation(AugmentationType augmentationType, T type, int value)
- {
- switch (augmentationType)
- {
- case AugmentationType.Increase:
- return (int)(value * (1.0f + (GetAugmentation(type) / 100f)));
- case AugmentationType.Decrease:
- return (int)(value - ((value * (GetAugmentation(type) / 100f))));
- default:
- throw new ArgumentOutOfRangeException(nameof(augmentationType), augmentationType, null);
- }
- }
- private int Clamp(int value)
- {
- return (value < MinimumAugmentation) ? MinimumAugmentation : (value > MaximumAugmentation) ? MaximumAugmentation : value;
- }
- public AugmentationTable<T> Clone()
- {
- AugmentationTable<T> table = new AugmentationTable<T>();
- for (int i = 0; i < Keys.Length; i++)
- {
- table.SetAugmentation(Keys[i], GetAugmentation(Keys[i]));
- }
- table.TableType = TableType;
- return table;
- }
- public void Copy(AugmentationTable<T> table)
- {
- TableType = table.TableType;
- for (int i = 0; i < Keys.Length; i++)
- {
- this.SetAugmentation(Keys[i], table.GetAugmentation(Keys[i]));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement