BaseProcessingStation: create class

This commit is contained in:
Van
2026-05-22 22:19:49 +03:00
parent dc0d5dc2c1
commit 44952915e7
3 changed files with 95 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
local modInfo = Mod.getInfo('smc__core__base_processing_station')
local require = modInfo.require
return require('BaseProcessingStation')

View File

@@ -0,0 +1,2 @@
name = smc__core__base_processing_station
depends = smc__core__craft_system, smc__core__node

View File

@@ -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