Files
BusyRabbit/Content/Lua/GamePlay/CookSystem/CookManager.lua
2025-08-08 17:22:47 +08:00

111 lines
4.0 KiB
Lua

local CookManager = {}
local Utils = require("GamePlay.Utils")
local Emitter = require("Utils.Emitter")
local CookMaterialUtils = require("Utils.Cook.CookMaterial")
local ECookingHeat = import("ECookingHeat")
--- @enum ECookCheckStatus
local ECookCheckStatus = {
Normal = 0, -- 不用做任何操作
NextState = 1, -- 可以进入下一个阶段
Error = 2, -- 出现了错误
}
local function TemperatureToDoneness(temperature)
return math.floor(temperature / 70)
end
function CookManager:Reset()
self.current_temperature = 0 -- 当前的温度
self.temperature_thresholds = {} -- 温度临界值,缓存表格内容
self.heating_record_table = {} -- 食材受热记录表
end
function CookManager:Tick(temperature, delta_time)
local need_remove_record = {} -- 记录已经到火候,或其他原因,需要移除的食材
self.current_temperature = temperature
-- 遍历,为所有的食材加热
for cook_material_id, record_table in pairs(self.heating_record_table) do
local doneness = CookMaterialUtils.TemperatureToDoneness(temperature) -- 获取此时的火候
local record_value = record_table[doneness] or 0
record_table[doneness] = record_value + (delta_time * 1000)
local check_status = self:CheckHeatingStatus(cook_material_id)
if check_status == ECookCheckStatus.NextState then
local next_cook_material = self:GetCookMaterialNextState(cook_material_id, doneness)
if next_cook_material == "None" then -- 炸锅了
-- TODO 抛出事件
self:Reset()
return
end
self:AddCookMaterial(next_cook_material, true)
need_remove_record[cook_material_id] = true
end
print(cook_material_id, doneness, record_table[doneness])
end
-- 移除已经加热到下一阶段的食材
for cook_material_id, need_remove in pairs(need_remove_record) do
if need_remove then
self.heating_record_table[cook_material_id] = nil
end
end
end
function CookManager:GetCookMaterialNextState(cook_material_id, doneness)
local threshold = self.temperature_thresholds[cook_material_id]
if not threshold then return "None" end
local state_config = threshold.CookConfig:Get(doneness)
if not state_config then return "None" end
return state_config.NextStateID
end
function CookManager:CheckHeatingStatus(cook_material_id)
local threshold = self.temperature_thresholds[cook_material_id]
local record_state = self.heating_record_table[cook_material_id]
if not threshold or not record_state then return ECookCheckStatus.Error end
local state_configs = threshold.CookConfig
for doneness, cook_time in pairs(record_state) do
local config = state_configs:Get(doneness)
if config and cook_time >= config.CookingDuration then
return ECookCheckStatus.NextState
end
end
return ECookCheckStatus.Normal
end
function CookManager:CheckFinishStatus()
-- 检测当前菜品是否完成
end
function CookManager:AddCookMaterial(cook_material_id, is_auto_push)
local config = Utils.GetDataTableConfig(
"CookMaterialStateConfig", cook_material_id
)
if not config then
print("ERROR: can't find ", cook_material_id, " in CookMaterialStateConfig")
return
end
self.heating_record_table[cook_material_id] = {}
self.temperature_thresholds[cook_material_id] = config
print("call", cook_material_id, is_auto_push)
Emitter.EmitEvent("cook_material_change", cook_material_id, is_auto_push)
end
function CookManager:UseCookware(cookward_id)
for cook_materiad_id, _ in pairs(self.heating_record_table) do
local next_state = CookMaterialUtils.GetCookwareOpNextState(
cookward_id, cook_materiad_id, self.current_temperature
)
if next_state ~= "None" then
self:AddCookMaterial(next_state, true)
end
end
Emitter.EmitEvent("use_cookward", self.cookware_id)
end
return CookManager