Files
simple-makes-complex/mods/CORE/BaseProcessingStation/src/BaseProcessingStation.lua

101 lines
3.2 KiB
Lua

local TICKRATE = 1
local assert = assert
local coreGetMeta, coreGetTimer, coreSwapNode = core.get_meta, core.get_node_timer, core.swap_node
local CraftSystem = Smc.requireCraftSystem()
local PlayerInventoryUI = Smc.requirePlayerInventoryUI()
---An abstract class representing nodes that implement crafting of the `processing` type.
---@class BaseProcessingStation : Class, NodeSuperClass
---@field craftType CraftSystem.ProcessingCraftType
---@field handler CraftSystem.ProcessingCraftHandler
---@field tab PlayerInventoryUI.getTabFunc
---@field nodeName string?
---@field activeNodeName string?
local BaseProcessingStation = Class:extend({})
---Definition table for the `BaseProcessingStation`.
---
---**Only for EmmyLua.**
---@class BaseProcessingStationDef
---@field craftType CraftSystem.ProcessingCraftType
---@field tab PlayerInventoryUI.getTabFunc
---@alias BaseProcessingStation.NodesDefinition {settings:Node.Settings, activeSettings:Node.Settings?, callbacks:Node.Callbacks?}
---@param def BaseProcessingStationDef
---@param nodesDef BaseProcessingStation.NodesDefinition?
function BaseProcessingStation:new(def, nodesDef)
local handler = CraftSystem:getProcessingCraftHandler(def.craftType)
---@type BaseProcessingStation
local instance = self:getInstance({})
instance.craftType = def.craftType
instance.handler = handler
instance.tab = def.tab
return instance
end
---A wrapper around `core.get_meta()` for convenience
---and returning the correct `MetaDataRef` type.
---@static
---@param pos MapVector
---@return MetaDataRef
function BaseProcessingStation:getMeta(pos)
return coreGetMeta(pos)
end
---Opens the player's inventory, adding the node tab described in `self.tab`.
---@param pos MapVector
---@param player Player
function BaseProcessingStation:showForm(pos, player)
local playerName = player:get_player_name()
PlayerInventoryUI:addActiveNodeForm(pos, playerName)
PlayerInventoryUI:setContex(playerName, 'left_tab_name', 'node_tab')
PlayerInventoryUI:setContex(playerName, 'node_tab_pos', pos)
PlayerInventoryUI:setContex(playerName, 'node_tab', self.tab)
PlayerInventoryUI:showForm(playerName)
end
---Starts the station timer only if it is not running.
---
---Returns `true` if the timer has been started.
---@static
---@param pos MapVector
---@param timeout number? If is `nil`, the global `TICKRATE` setting will be used.
---@return boolean
function BaseProcessingStation:startTimer(pos, timeout)
local timer = coreGetTimer(pos)
if not timer:is_started() then
timer:start(timeout or TICKRATE)
return true
end
return false
end
---Replaces the node with active or inactive depending on the status of the process.
---
---If an active node is not specified for a station,
---then no replacement will occur regardless of the status of the process.
---@param isProcessing boolean The stations process status. If `true`, the process is running.
---@param pos MapVector
function BaseProcessingStation:swapNode(isProcessing, pos)
assert(self.nodeName ~= nil)
if self.activeNodeName ~= nil then
if isProcessing then
coreSwapNode(pos, {name = self.activeNodeName})
else
coreSwapNode(pos, {name = self.nodeName})
end
end
end
return BaseProcessingStation