diff --git a/mods/CORE/BaseProcessingStation/init.lua b/mods/CORE/BaseProcessingStation/init.lua new file mode 100644 index 0000000..b8d2647 --- /dev/null +++ b/mods/CORE/BaseProcessingStation/init.lua @@ -0,0 +1,4 @@ +local modInfo = Mod.getInfo('smc__core__base_processing_station') +local require = modInfo.require + +return require('BaseProcessingStation') diff --git a/mods/CORE/BaseProcessingStation/mod.conf b/mods/CORE/BaseProcessingStation/mod.conf new file mode 100644 index 0000000..0ae7db0 --- /dev/null +++ b/mods/CORE/BaseProcessingStation/mod.conf @@ -0,0 +1,2 @@ +name = smc__core__base_processing_station +depends = smc__core__craft_system, smc__core__node diff --git a/mods/CORE/BaseProcessingStation/src/BaseProcessingStation.lua b/mods/CORE/BaseProcessingStation/src/BaseProcessingStation.lua new file mode 100644 index 0000000..01c1931 --- /dev/null +++ b/mods/CORE/BaseProcessingStation/src/BaseProcessingStation.lua @@ -0,0 +1,89 @@ +local CraftSystem = Smc.requireCraftSystem() + +local PlayerInventoryUI = Smc.requirePlayerInventoryUI() + +---@class BaseProcessingStation : Class +---@field name string +---@field timeout number +---@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 timeout number? +---@field craftType CraftSystem.ProcessingCraftType +---@field tab PlayerInventoryUI.getTabFunc + +---@alias BaseProcessingStation.NodesDef {settings:Node.Settings, activeSettings?:Node.Settings, callbacks:Node.Callbacks?} + +---@param name string +---@param def BaseProcessingStationDef +---@param nodesDef BaseProcessingStation.NodesDef +function BaseProcessingStation:new(name, def, nodesDef) + local handler = CraftSystem:getProcessingCraftHandler(def.craftType) + + ---@type BaseProcessingStation + local instance = self:getInstance({}) + instance.name = name + instance.timeout = def.timeout or 1 + instance.craftType = def.craftType + instance.handler = handler + instance.tab = def.tab + + return instance +end + +---Opens the player's inventory, adding the node tab described in `self.tab`. +---@param pos MapPosition +---@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. +--- +---The timer timeout is set to the timeout specified `self.timeout`. +--- +---Returns `true` if the timer has been started. +---@param pos MapPosition +---@return boolean +function BaseProcessingStation:startTimer(pos) + local timer = core.get_node_timer(pos) + if not timer:is_started() then + timer:start(self.timeout) + 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 MapPosition +function BaseProcessingStation:swapNode(isProcessing, pos) + if self.activeNodeName ~= nil then + if isProcessing then + core.swap_node(pos, {name = self.activeNodeName}) + else + core.swap_node(pos, {name = self.nodeName}) + end + end +end + +return BaseProcessingStation