Saves.Item.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using Microsoft.Xna.Framework;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. namespace OpenVIII
  6. {
  7. public static partial class Saves
  8. {
  9. #region Structs
  10. [StructLayout(LayoutKind.Explicit, Pack = 1, Size = 2)]
  11. public struct Item
  12. {
  13. #region Fields
  14. [FieldOffset(0)]
  15. public byte ID;
  16. [FieldOffset(1)]
  17. public byte QTY;
  18. #endregion Fields
  19. #region Constructors
  20. public Item(byte ID, byte QTY) { this.ID = ID; this.QTY = QTY; }
  21. public Item(KeyValuePair<byte, byte> e) : this(e.Key, e.Value)
  22. { }
  23. #endregion Constructors
  24. #region Properties
  25. public Item_In_Menu? DATA => Memory.MItems?.Items[ID];
  26. public override string ToString() => DATA?.Name;
  27. public Item UsedOne()
  28. {
  29. if(QTY <= 1)
  30. {
  31. ID = 0;
  32. }
  33. QTY--;
  34. return this;
  35. }
  36. public Item Remove()
  37. {
  38. QTY = 0;
  39. ID = 0;
  40. return this;
  41. }
  42. public Item Add(byte qty, byte? id = null)
  43. {
  44. ID = id ?? ID;
  45. if (ID > 0)
  46. {
  47. byte Q = (byte)MathHelper.Clamp(qty + QTY, 0, 100);
  48. if (Q > QTY)
  49. {
  50. QTY = Q;
  51. }
  52. }
  53. return this;
  54. }
  55. public Item Clone()
  56. {
  57. //shadowcopy
  58. Item d = new Item(ID, QTY);
  59. //deepcopy anything that needs it here.
  60. return d;
  61. }
  62. public Item Add(sbyte qty)
  63. {
  64. QTY = (byte)(QTY + qty);
  65. return this;
  66. }
  67. #endregion Properties
  68. };
  69. #endregion Structs
  70. }
  71. }