WorkStation: update
This commit is contained in:
@@ -9,15 +9,18 @@ local WorkStationUI = Mod.getInfo('smc__ui__work_station').require('WorkStationU
|
|||||||
---@type Node
|
---@type Node
|
||||||
local Node = Mod.getInfo('smc__core__node').require('Node')
|
local Node = Mod.getInfo('smc__core__node').require('Node')
|
||||||
|
|
||||||
|
-- Default settings that a work station node should have.
|
||||||
local WorkStationNode = Node:getExtended({
|
local WorkStationNode = Node:getExtended({
|
||||||
settings = {
|
settings = {
|
||||||
name = 'work_station',
|
name = 'work_station',
|
||||||
drawtype = 'normal',
|
drawtype = 'normal',
|
||||||
paramtype = 'light',
|
paramtype = 'light',
|
||||||
groups = {work_station = 1}
|
groups = {work_station = 1, dig_immediate = 2,}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
---A work station (node) that enables player interaction
|
||||||
|
---with the immersive crafting mechanics.
|
||||||
---@class WorkStation
|
---@class WorkStation
|
||||||
---@field name string
|
---@field name string
|
||||||
---@field group CraftSystem.CraftGroup
|
---@field group CraftSystem.CraftGroup
|
||||||
@@ -36,36 +39,61 @@ local WorkStation = {}
|
|||||||
function WorkStation:new(name, def)
|
function WorkStation:new(name, def)
|
||||||
local nodeDef = def.nodeDef
|
local nodeDef = def.nodeDef
|
||||||
if nodeDef.callbacks == nil then
|
if nodeDef.callbacks == nil then
|
||||||
|
---@type Node.Callbacks
|
||||||
nodeDef.callbacks = {}
|
nodeDef.callbacks = {}
|
||||||
end
|
end
|
||||||
local settings = nodeDef.settings
|
local settings = nodeDef.settings
|
||||||
local callbacks = nodeDef.callbacks
|
local callbacks = nodeDef.callbacks
|
||||||
|
|
||||||
|
-- Callback when the work station block is installed in the world
|
||||||
|
-- Ensures initialization of necessary parameters
|
||||||
callbacks.on_construct = function(pos)
|
callbacks.on_construct = function(pos)
|
||||||
|
-- Getting node metadata
|
||||||
local meta = core.get_meta(pos)
|
local meta = core.get_meta(pos)
|
||||||
|
|
||||||
--meta:set_string('work_station_name', name)
|
-- Initializing the table of temporary parameters for crafting.
|
||||||
--meta:set_string('craft_group', def.group)
|
Smc.setCraftDataByMeta(meta)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Callback for interaction with a work station (start of crafting)
|
||||||
callbacks.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
callbacks.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
||||||
Logger.warningLog('Punch')
|
-- If the workstation is occupied by another craft...
|
||||||
local crafts = ImmersiveCraftHandler:getCraftsByTriggerItem(itemstack:get_name())
|
if not Smc.workStationIsFree(pos) then
|
||||||
|
-- ...we inform the player about this...
|
||||||
if crafts == nil then
|
core.chat_send_player(clicker:get_player_name(), 'The workbench is occupied by another craft.')
|
||||||
|
-- ...and do nothing.
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
self:showSelectionCraftForm(clicker, crafts)
|
-- We get a list of crafts that can be produced using the item that the player was holding in his hand.
|
||||||
|
local crafts = ImmersiveCraftHandler:getCraftsByTriggerItem(itemstack:get_name())
|
||||||
|
|
||||||
|
-- If the list is empty...
|
||||||
|
if crafts == nil then
|
||||||
|
-- ..this means there are no crafts for this item,
|
||||||
|
-- so we don't do anything.
|
||||||
|
return
|
||||||
|
else
|
||||||
|
-- ...else we show the player the formspec for choosing a craft.
|
||||||
|
self:showSelectionCraftForm(clicker, crafts, pos)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local instance = getInstance(self, {
|
callbacks.on_dig = function(pos, node, digger)
|
||||||
name = name,
|
if Smc.workStationIsFree(pos) then
|
||||||
group = def.group,
|
return core.node_dig(pos, node, digger)
|
||||||
nodeDef = nodeDef
|
else
|
||||||
})
|
return false
|
||||||
print('NODEDEF: ',dump(nodeDef.callbacks))
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---@type WorkStation
|
||||||
|
local instance = getInstance(self, {})
|
||||||
|
instance.name = name
|
||||||
|
instance.group = def.group
|
||||||
|
instance.nodeDef = nodeDef
|
||||||
|
|
||||||
|
-- Registering a node for Luanti.
|
||||||
WorkStationNode:new(nodeDef)
|
WorkStationNode:new(nodeDef)
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
@@ -73,16 +101,48 @@ end
|
|||||||
|
|
||||||
---@param player Player
|
---@param player Player
|
||||||
---@param crafts CraftSystem.ImmersiveCraft[]
|
---@param crafts CraftSystem.ImmersiveCraft[]
|
||||||
function WorkStation:showSelectionCraftForm(player, crafts)
|
---@param pos Position
|
||||||
|
function WorkStation:showSelectionCraftForm(player, crafts, pos)
|
||||||
local outputs = {}
|
local outputs = {}
|
||||||
|
|
||||||
for i, craft in ipairs(crafts) do
|
for _, craft in ipairs(crafts) do
|
||||||
outputs[i] = craft.output
|
outputs[craft.output] = craft.name
|
||||||
end
|
end
|
||||||
|
|
||||||
core.show_formspec(player:get_player_name(), 'work_station:selection_craft', WorkStationUI:selectionCraftForm(outputs))
|
local playerName = player:get_player_name()
|
||||||
|
|
||||||
|
WorkStationUI:setContex(playerName, 'pos', pos)
|
||||||
|
WorkStationUI:setContex(playerName, 'outputs', outputs)
|
||||||
|
|
||||||
|
core.show_formspec(playerName, 'work_station:selection_craft', WorkStationUI:selectionCraftForm(playerName, false))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
WorkStationUI:registerOnFormFieldActionMatch('%S+::item_button',function(player, fieldName, fieldValue)
|
||||||
|
---@type ItemStackString?
|
||||||
|
local craftName = string.match(fieldName, '(%S+)::item_button')
|
||||||
|
|
||||||
|
if craftName == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local craft = ImmersiveCraftHandler:getCraftByName(craftName)
|
||||||
|
local playerName = player:get_player_name()
|
||||||
|
|
||||||
|
WorkStationUI:setContex(playerName, 'craft', craft)
|
||||||
|
core.show_formspec(playerName, 'work_station:selection_craft', WorkStationUI:selectionCraftForm(playerName, true))
|
||||||
|
end)
|
||||||
|
|
||||||
|
WorkStationUI:registerOnFormFieldAction('work_station:start_craft', function(player, fieldName, fieldValue)
|
||||||
|
local playerName = player:get_player_name()
|
||||||
|
local pos = WorkStationUI:getContex(playerName, 'pos')
|
||||||
|
local craft = WorkStationUI:getContex(playerName, 'craft')
|
||||||
|
|
||||||
|
craft:startCraft(pos, player)
|
||||||
|
core.close_formspec(playerName, 'work_station:selection_craft')
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- --- DEBUG ---
|
||||||
|
|
||||||
WorkStation:new('test_station', {
|
WorkStation:new('test_station', {
|
||||||
group = 'test',
|
group = 'test',
|
||||||
nodeDef = {
|
nodeDef = {
|
||||||
@@ -93,20 +153,52 @@ WorkStation:new('test_station', {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
ImmersiveCraftHandler:addCraft('bricks', {
|
ImmersiveCraftHandler:registerCraft('bricks', {
|
||||||
triggerItem = 'rocks:sylite',
|
triggerItem = 'rocks:sylite',
|
||||||
output = 'rocks:sylite_bricks',
|
output = 'rocks:sylite_bricks',
|
||||||
startStageName = 'ab',
|
startStageName = 'tapping',
|
||||||
group = 'test',
|
group = 'test',
|
||||||
stages = {},
|
stages = {
|
||||||
|
---@type CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||||
|
{
|
||||||
|
name = 'tapping',
|
||||||
|
targetItem = 'rocks:sylite',
|
||||||
|
type = 'tapping',
|
||||||
|
clicksNeed = 3,
|
||||||
|
}
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
ImmersiveCraftHandler:addCraft('block', {
|
ImmersiveCraftHandler:registerCraft('block', {
|
||||||
triggerItem = 'rocks:sylite',
|
triggerItem = 'rocks:sylite',
|
||||||
output = 'rocks:sylite_block',
|
output = 'rocks:sylite_block',
|
||||||
startStageName = 'ab',
|
startStageName = 'tapping',
|
||||||
group = 'test',
|
group = 'test',
|
||||||
stages = {},
|
stages = {
|
||||||
|
---@type CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||||
|
{
|
||||||
|
name = 'tapping',
|
||||||
|
targetItem = 'rocks:sylite',
|
||||||
|
type = 'tapping',
|
||||||
|
clicksNeed = 5,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
ImmersiveCraftHandler:registerCraft('marentine_planks', {
|
||||||
|
triggerItem = 'wood:marentine_log',
|
||||||
|
output = 'wood:marentine_planks',
|
||||||
|
startStageName = 'tapping',
|
||||||
|
group = 'test',
|
||||||
|
stages = {
|
||||||
|
---@type CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||||
|
{
|
||||||
|
name = 'tapping',
|
||||||
|
targetItem = 'wood:marentine_log',
|
||||||
|
type = 'tapping',
|
||||||
|
clicksNeed = 5,
|
||||||
|
}
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
return WorkStation
|
return WorkStation
|
||||||
|
|||||||
Reference in New Issue
Block a user