108 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
		
		
			
		
	
	
			108 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
|  | -- 禁用不必要的if诊断警告 | ||
|  | ---@diagnostic disable: unnecessary-if | ||
|  | 
 | ||
|  | -- 导入基类模块 | ||
|  | local PWClass = require("Core.PWClass") | ||
|  | 
 | ||
|  | --- @class StorageClass | ||
|  | --- @field max_capacity number 最大容量(格子总数) | ||
|  | --- @field cur_capacity number 当前已用容量 | ||
|  | --- @field grids_list table[] 存储格子列表 | ||
|  | local StorageClass = PWClass.derive("StorageClass") | ||
|  | 
 | ||
|  | --- 创建新物品格子 | ||
|  | --- @param item_id any 物品唯一标识 | ||
|  | --- @return table 新创建的物品格子 | ||
|  | local function CreateGrid(item_id) | ||
|  |     return { | ||
|  |         item_id = item_id,  -- 物品ID | ||
|  |         cur_cnt = 0,        -- 当前数量 | ||
|  |         max_cnt = 1         -- 最大堆叠数(可扩展为配置项) | ||
|  |     } | ||
|  | end | ||
|  | 
 | ||
|  | --- 查找或创建可用物品格子 | ||
|  | --- @param storage StorageClass 存储实例 | ||
|  | --- @param item_id any 目标物品ID | ||
|  | --- @return table 可用格子(找不到时创建新格子) | ||
|  | local function FindOrCreateAvailableGrid(storage, item_id) | ||
|  |     -- 优先查找同类型且未满的格子 | ||
|  |     for _, grid in ipairs(storage.grids_list) do | ||
|  |         if grid.item_id == item_id and grid.cur_cnt < grid.max_cnt then | ||
|  |             return grid | ||
|  |         end | ||
|  |     end | ||
|  | 
 | ||
|  |     -- 无可用格子时创建新的物品类型格子 | ||
|  |     local new_grid = CreateGrid(item_id) | ||
|  |     table.insert(storage.grids_list, new_grid) | ||
|  |     return new_grid | ||
|  | end | ||
|  | 
 | ||
|  | --- 构造函数 | ||
|  | function StorageClass:ctor() | ||
|  |     self.max_capacity = 10     -- 默认最大容量 | ||
|  |     self.cur_capacity = 0      -- 当前使用容量 | ||
|  |     self.grids_list = {}       -- 格子容器 | ||
|  | end | ||
|  | 
 | ||
|  | --- 设置存储容量上限 | ||
|  | --- @param capacity number 新的最大容量 | ||
|  | function StorageClass:SetMaxCapacity(capacity) | ||
|  |     self.max_capacity = capacity | ||
|  | end | ||
|  | 
 | ||
|  | --- 存储物品 | ||
|  | --- @param item_id any 要存储的物品ID | ||
|  | function StorageClass:Store(item_id) | ||
|  |     -- 容量检查 | ||
|  |     if self.cur_capacity >= self.max_capacity then | ||
|  |         return false  -- 建议返回操作结果 | ||
|  |     end | ||
|  | 
 | ||
|  |     local grid = FindOrCreateAvailableGrid(self, item_id) | ||
|  |     grid.cur_cnt = grid.cur_cnt + 1 | ||
|  |     self.cur_capacity = self.cur_capacity + 1 | ||
|  |     return true  -- 建议添加返回值 | ||
|  | end | ||
|  | 
 | ||
|  | --- 取出物品 | ||
|  | --- @param item_id any 目标物品ID | ||
|  | --- @return boolean 是否成功取出 | ||
|  | function StorageClass:Withdraw(item_id) | ||
|  |     -- 逆序遍历提高取出效率(通常新物品在末尾) | ||
|  |     for i = #self.grids_list, 1, -1 do | ||
|  |         local grid = self.grids_list[i] | ||
|  |         if grid ~= nil and grid.item_id == item_id and grid.cur_cnt > 0 then | ||
|  |             grid.cur_cnt = grid.cur_cnt - 1 | ||
|  |             self.cur_capacity = self.cur_capacity - 1 | ||
|  | 
 | ||
|  |             -- 清空空格子 | ||
|  |             if grid.cur_cnt == 0 then | ||
|  |                 table.remove(self.grids_list, i) | ||
|  |             end | ||
|  |             return true | ||
|  |         end | ||
|  |     end | ||
|  |     return false | ||
|  | end | ||
|  | 
 | ||
|  | function StorageClass:Visit(vistor) | ||
|  |     for _, grid in ipairs(self.grids_list) do vistor(_, grid) end | ||
|  | end | ||
|  | 
 | ||
|  | --- 查询物品(满足条件的物品及数量) | ||
|  | --- @param query_function fun(item_id:any):boolean 物品过滤函数 | ||
|  | --- @return table 物品ID到数量的映射表 | ||
|  | function StorageClass:QueryItem(query_function) | ||
|  |     local items = {} | ||
|  |     for _, grid in ipairs(self.grids_list) do | ||
|  |         -- 仅统计有物品且满足查询条件的格子 | ||
|  |         if grid.cur_cnt > 0 and query_function(grid.item_id) then | ||
|  |             items[grid.item_id] = (items[grid.item_id] or 0) + grid.cur_cnt | ||
|  |         end | ||
|  |     end | ||
|  |     return items | ||
|  | end | ||
|  | 
 | ||
|  | return StorageClass |