NumericComponent.cs 1.4 KB

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