NumericComponent.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Collections.Generic;
  2. using MongoDB.Bson.Serialization.Attributes;
  3. using MongoDB.Bson.Serialization.Options;
  4. namespace Model
  5. {
  6. public class NumericComponent : ComponentDB
  7. {
  8. [BsonDictionaryOptions(DictionaryRepresentation.ArrayOfArrays)]
  9. public readonly Dictionary<NumericType, int> NumericDic = new Dictionary<NumericType, int>();
  10. public float GetAsFloat(NumericType numericType)
  11. {
  12. return (float)GetByKey(numericType) / 10000;
  13. }
  14. public int GetAsInt(NumericType numericType)
  15. {
  16. return this[numericType];
  17. }
  18. public void Set(NumericType nt, float value)
  19. {
  20. this[nt] = (int)(value * 10000);
  21. }
  22. public void Set(NumericType nt, int value)
  23. {
  24. this[nt] = value;
  25. }
  26. public int this[NumericType numericType]
  27. {
  28. get
  29. {
  30. return this.GetByKey(numericType);
  31. }
  32. set
  33. {
  34. int v = this.GetByKey(numericType);
  35. if (v == value)
  36. {
  37. return;
  38. }
  39. NumericDic[numericType] = value;
  40. Game.Scene.GetComponent<EventComponent>().Run(EventIdType.NumbericChange, this.Entity.Id, numericType, value);
  41. }
  42. }
  43. private int GetByKey(NumericType key)
  44. {
  45. int value;
  46. this.NumericDic.TryGetValue(key, out value);
  47. return value;
  48. }
  49. }
  50. }