3bdf94f80cc7c4e432a1b8a5268e57b2c305372c.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, Container, _crd;
  4. _export("Container", void 0);
  5. return {
  6. setters: [function (_cc) {
  7. _cclegacy = _cc.cclegacy;
  8. }],
  9. execute: function () {
  10. _crd = true;
  11. _cclegacy._RF.push({}, "7b367Ns5DhICaZLmc2yobWd", "Container", undefined);
  12. _export("Container", Container = class Container {
  13. constructor() {
  14. this._itemList = null;
  15. this._itemList = new Array();
  16. }
  17. addCount(type, count) {
  18. return this.addItem({
  19. type: type,
  20. count: count
  21. });
  22. }
  23. useCount(type, count) {
  24. for (var i = 0; i < this._itemList.length; i++) {
  25. if (this._itemList[i].type == type) {
  26. if (this._itemList[i].count >= count) {
  27. this._itemList[i].count -= count;
  28. return this._itemList[i];
  29. }
  30. }
  31. }
  32. return null;
  33. }
  34. /**
  35. * 添加物品
  36. * @param item
  37. * @returns
  38. */
  39. addItem(item) {
  40. for (var i = 0; i < this._itemList.length; i++) {
  41. if (this._itemList[i].type == item.type) {
  42. this._itemList[i].count += item.count;
  43. return this._itemList[i];
  44. }
  45. }
  46. this._itemList.push(item);
  47. return item;
  48. }
  49. getCount(type) {
  50. for (var i = 0; i < this._itemList.length; i++) {
  51. if (this._itemList[i].type == type) {
  52. return this._itemList[i].count;
  53. }
  54. }
  55. return 0;
  56. }
  57. get itemList() {
  58. return this._itemList;
  59. }
  60. set itemList(arr) {
  61. this._itemList = arr;
  62. }
  63. /**
  64. * 序列化(数据库存储)
  65. */
  66. serialize() {
  67. var list = [];
  68. for (var i = 0; i < this._itemList.length; i++) {
  69. var item = this._itemList[i];
  70. list.push({
  71. type: item.type,
  72. count: item.count
  73. });
  74. }
  75. var data = {
  76. "list": list
  77. };
  78. return data;
  79. }
  80. /**
  81. * 反序列化(数据库读取)
  82. * @param data
  83. */
  84. unserialize(data) {
  85. if (!data) return;
  86. var list = data.list;
  87. this._itemList = [];
  88. for (var i = 0; i < list.length; i++) {
  89. this._itemList.push({
  90. type: list[i].type,
  91. count: list[i].count
  92. });
  93. }
  94. }
  95. });
  96. _cclegacy._RF.pop();
  97. _crd = false;
  98. }
  99. };
  100. });
  101. //# sourceMappingURL=3bdf94f80cc7c4e432a1b8a5268e57b2c305372c.js.map