CraftSystem: first working version
#31
@@ -99,46 +99,71 @@ function WorkStation:new(name, def)
|
||||
return instance
|
||||
end
|
||||
|
||||
---@param player Player
|
||||
---@param crafts CraftSystem.ImmersiveCraft[]
|
||||
---@param pos Position
|
||||
---Show the player the initial workstation interface for choosing a craft.
|
||||
---@param player Player
|
||||
---@param crafts CraftSystem.ImmersiveCraft[]
|
||||
---@param pos Position
|
||||
function WorkStation:showSelectionCraftForm(player, crafts, pos)
|
||||
-- Create a table in the format craft result - craft name.
|
||||
local outputs = {}
|
||||
|
||||
for _, craft in ipairs(crafts) do
|
||||
outputs[craft.output] = craft.name
|
||||
end
|
||||
|
||||
-- We get the name of the player who wants to start crafting.
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
-- We set the context for transferring data about the workstation coordinates and the list of crafts.
|
||||
WorkStationUI:setContex(playerName, 'pos', pos)
|
||||
WorkStationUI:setContex(playerName, 'outputs', outputs)
|
||||
|
||||
core.show_formspec(playerName, 'work_station:selection_craft', WorkStationUI:selectionCraftForm(playerName, false))
|
||||
-- Showing the desktop interface to the player.
|
||||
WorkStationUI:showForm(playerName,false)
|
||||
end
|
||||
|
||||
-- Registering a callback for player choice of craft (this does not start crafting).
|
||||
WorkStationUI:registerOnFormFieldActionMatch('%S+::item_button',function(player, fieldName, fieldValue)
|
||||
---@type ItemStackString?
|
||||
-- We get the name of the craft from the name of the button the player pressed.
|
||||
local craftName = string.match(fieldName, '(%S+)::item_button')
|
||||
assert(craftName ~= nil, 'Craft name cannot be empty')
|
||||
|
||||
if craftName == nil then
|
||||
return
|
||||
end
|
||||
|
||||
-- We get a craft object.
|
||||
local craft = ImmersiveCraftHandler:getCraftByName(craftName)
|
||||
-- We get the name of the player who wants to start crafting.
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
-- We enter into the context the craft that the player has chosen.
|
||||
WorkStationUI:setContex(playerName, 'craft', craft)
|
||||
core.show_formspec(playerName, 'work_station:selection_craft', WorkStationUI:selectionCraftForm(playerName, true))
|
||||
-- We show the player a new interface with a crafting description tab.
|
||||
WorkStationUI:showForm(playerName, true)
|
||||
end)
|
||||
|
||||
-- We register a callback when the player starts crafting.
|
||||
WorkStationUI:registerOnFormFieldAction('work_station:start_craft', function(player, fieldName, fieldValue)
|
||||
-- We get the name of the player who wants to start crafting.
|
||||
local playerName = player:get_player_name()
|
||||
local pos = WorkStationUI:getContex(playerName, 'pos')
|
||||
|
||||
-- We get the workstation position and craft from the context.
|
||||
local pos = WorkStationUI:getContex(playerName, 'pos')
|
||||
---@type CraftSystem.ImmersiveCraft
|
||||
local craft = WorkStationUI:getContex(playerName, 'craft')
|
||||
|
||||
craft:startCraft(pos, player)
|
||||
core.close_formspec(playerName, 'work_station:selection_craft')
|
||||
|
||||
-- We process the amount of crafting.
|
||||
local isCraftStart = false
|
||||
if fieldValue == 'x1' then
|
||||
-- Single craft
|
||||
isCraftStart = craft:startCraft(pos, player, 1)
|
||||
elseif fieldValue == 'x10' then
|
||||
-- Ten crafts at a time.
|
||||
isCraftStart = craft:startCraft(pos, player, 10)
|
||||
end
|
||||
|
||||
-- If the craft was started...
|
||||
if isCraftStart then
|
||||
-- ...we are closing the formspec.
|
||||
WorkStationUI:closeForm(playerName)
|
||||
end
|
||||
end)
|
||||
|
||||
-- --- DEBUG ---
|
||||
|
||||
Reference in New Issue
Block a user