像原神给角色升级吃经验的时候,要省吃俭用的样子
local function GetUseNextMaterialCount(materialList, selectedMap, nextIndex, need, parentIsEnough, GetItemEnough,GetItemCount)
if nextIndex == 0 then
return false
end
local materialBean = materialList[nextIndex]
local templateId = materialBean.id
local pExp = materialBean.exp
local count = need // pExp
if count > 0 then
if not GetItemEnough(templateId, count) then
local bagCount = bagMgr:GetItemCount(templateId)
count = bagCount
end
end
if count > 0 then
need = need - count * pExp
end
if need <= 0 then
selectedMap:Add(templateId, count)
return true
end
local myIsEnough = GetItemEnough(templateId, count + 1)
nextIndex = nextIndex - 1
local childIsEnough = self:GetUseNextMaterialCount(materialList,
selectedMap,
nextIndex, need, myIsEnough or parentIsEnough,
GetItemEnough,GetItemCount
)
if childIsEnough then
if count > 0 then
selectedMap:Add(templateId, count)
end
return true
elseif myIsEnough then
count = count + 1
selectedMap:Add(templateId, count)
return true
elseif not parentIsEnough then
if count > 0 then
selectedMap:Add(templateId, count)
end
end
return false
end