ItemMgr.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { PREVIEW } from "cc/env"
  2. import { Global } from "../Global"
  3. import { CfgItem } from "../config/CfgItem"
  4. import { DtoItem } from "../dto/DtoItem"
  5. import { ItemType } from "../enum/ItemType"
  6. import { StorageUtil } from "../util/StorageUtil"
  7. export class ItemMgr {
  8. private static itemDic: { [key: number]: number } = {}
  9. public static get ItemDic() {
  10. return this.itemDic
  11. }
  12. public static init(): void {
  13. this.itemDic = StorageUtil.getObj('itemDic', Global.Default_Item)
  14. StorageUtil.setObj('itemDic', this.itemDic)
  15. }
  16. public static setItem(id: number, cnt: number): number {
  17. if (!this.itemDic[id]) this.itemDic[id] = 0
  18. this.itemDic[id] = cnt
  19. StorageUtil.setObj('itemDic', this.itemDic)
  20. return this.itemDic[id]
  21. }
  22. public static updateItem(id: number, cnt: number): number {
  23. if (!this.itemDic[id]) this.itemDic[id] = 0
  24. this.itemDic[id] += cnt
  25. StorageUtil.setObj('itemDic', this.itemDic)
  26. return this.itemDic[id]
  27. }
  28. public static getType(id: number): ItemType {
  29. const itemData: DtoItem = CfgItem[id]
  30. return itemData.type
  31. }
  32. public static getCnt(id: number): number {
  33. return this.itemDic[id]
  34. }
  35. }
  36. if (PREVIEW) {
  37. globalThis.ItemMgr = ItemMgr
  38. }