108 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
			
		
		
	
	
			108 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
local CookingBench = {}
 | 
						|
local Utils = require("GamePlay.Utils")
 | 
						|
local Emitter = require("Utils.Emitter")
 | 
						|
local CookManager = require("GamePlay.CookSystem.CookManager")
 | 
						|
local KismetSystemLibrary = import("KismetSystemLibrary")
 | 
						|
local ESlateVisibility = import("ESlateVisibility")
 | 
						|
 | 
						|
 | 
						|
--- 获取每次tick温度下降速度
 | 
						|
local function GetCoolingRate(wco)
 | 
						|
    return 0.5
 | 
						|
end
 | 
						|
 | 
						|
--- 获取每次点火的效率
 | 
						|
local function GetHeatingEfficiency(wco)
 | 
						|
    return 8
 | 
						|
end
 | 
						|
 | 
						|
local function GetMaxTemperature(wco)
 | 
						|
    return 210
 | 
						|
end
 | 
						|
 | 
						|
function CookingBench:ctor()
 | 
						|
    self.is_cooking = false
 | 
						|
    self.temperature = 0
 | 
						|
    self.max_temperature = 200
 | 
						|
 | 
						|
 | 
						|
    self.burn_timer = nil
 | 
						|
end
 | 
						|
 | 
						|
function CookingBench:OnInitialized()
 | 
						|
    self.WBP_Rabbit.OnClicked:Add(function() self:OnRabbitClicked()end)
 | 
						|
end
 | 
						|
 | 
						|
function CookingBench:Construct()
 | 
						|
    CookManager:Reset()
 | 
						|
    self:UpdateCookState()
 | 
						|
    self:UpdateFireState()
 | 
						|
end
 | 
						|
 | 
						|
 | 
						|
function CookingBench:Destruct()
 | 
						|
    self.is_cooking = false
 | 
						|
    self:UpdateFireState()
 | 
						|
end
 | 
						|
 | 
						|
 | 
						|
function CookingBench:OnRabbitClicked()
 | 
						|
    if self.is_cooking then
 | 
						|
        local max = GetMaxTemperature(self)
 | 
						|
        local once = GetHeatingEfficiency(self)
 | 
						|
        local new_value = self.temperature + once
 | 
						|
        if new_value > max then
 | 
						|
            self.temperature = max
 | 
						|
        else
 | 
						|
            self.temperature = new_value
 | 
						|
        end
 | 
						|
    else
 | 
						|
        self.is_cooking = true
 | 
						|
        self.temperature = 50
 | 
						|
 | 
						|
        self.burn_timer = KismetSystemLibrary.K2_SetTimerDelegate(
 | 
						|
            slua.createDelegate(function() self:CookTick() end),
 | 
						|
            1 / 30, true, true, 0, 0
 | 
						|
        )
 | 
						|
    end
 | 
						|
    self:UpdateCookState()
 | 
						|
    self:UpdateFireState()
 | 
						|
end
 | 
						|
 | 
						|
--- 每秒30次tick
 | 
						|
function CookingBench:CookTick()
 | 
						|
    local drop_value = GetCoolingRate(self)
 | 
						|
    local new_value = self.temperature - drop_value
 | 
						|
    if new_value > 0 then
 | 
						|
        self.temperature = new_value
 | 
						|
        CookManager:Tick(self.temperature, 1 / 30)
 | 
						|
    else
 | 
						|
        self.temperature = 0
 | 
						|
        self.is_cooking = false
 | 
						|
        KismetSystemLibrary.K2_ClearTimerHandle(self, self.burn_timer)
 | 
						|
        self.burn_timer = nil
 | 
						|
    end
 | 
						|
    self:UpdateCookState()
 | 
						|
    self:UpdateFireState()
 | 
						|
end
 | 
						|
 | 
						|
function CookingBench:UpdateCookState()
 | 
						|
    if self.temperature > 0 then
 | 
						|
        self.WBP_CookingProcess:BP_SetVisible(true)
 | 
						|
        local percent = self.temperature / self.max_temperature
 | 
						|
        self.WBP_CookingProcess.TemperatureProcess:SetPercent(percent)
 | 
						|
    else
 | 
						|
        self.WBP_CookingProcess:BP_SetVisible(false)
 | 
						|
    end
 | 
						|
end
 | 
						|
 | 
						|
function CookingBench:UpdateFireState()
 | 
						|
    if self.is_cooking == true then
 | 
						|
        self.CookingFireImg:SetVisibility(ESlateVisibility.SelfHitTestInvisible)
 | 
						|
    else
 | 
						|
        self.CookingFireImg:SetVisibility(ESlateVisibility.Collapsed)
 | 
						|
    end
 | 
						|
end
 | 
						|
 | 
						|
 | 
						|
return Class(nil, nil, CookingBench) |