CraftSystem: end of work?
This commit is contained in:
@@ -5,6 +5,8 @@ local require = modInfo.require
|
|||||||
|
|
||||||
---@type CraftSystem.BaseCraft
|
---@type CraftSystem.BaseCraft
|
||||||
local BaseCraft = require('CraftSystem.BaseCraft')
|
local BaseCraft = require('CraftSystem.BaseCraft')
|
||||||
|
---@type CraftSystem.ImmersiveCraftCounter
|
||||||
|
local ImmersiveCraftCounter = require('CraftSystem.ImmersiveCraftCounter')
|
||||||
|
|
||||||
---Types of immersive stages.
|
---Types of immersive stages.
|
||||||
---@class CraftSystem.StagesTypes : table<CraftSystem.ImmersiveCraftStage.Type, CraftSystem.ImmersiveCraftStage>
|
---@class CraftSystem.StagesTypes : table<CraftSystem.ImmersiveCraftStage.Type, CraftSystem.ImmersiveCraftStage>
|
||||||
@@ -95,10 +97,44 @@ function ImmersiveCraft:new(name, def)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---Initialize the start of the immersive crafting process.
|
---Initialize the start of the immersive crafting process.
|
||||||
|
---
|
||||||
|
---Returns `true` if the crafting process was running.
|
||||||
---@param pos MapPosition
|
---@param pos MapPosition
|
||||||
---@param Player Player
|
---@param player Player
|
||||||
function ImmersiveCraft:startCraft(pos, Player)
|
---@param amount integer
|
||||||
-- TODO: забирать из инвентаря игрока материалы для крафта
|
---@return boolean
|
||||||
|
function ImmersiveCraft:startCraft(pos, player, amount)
|
||||||
|
-- Adding craft to the counter.
|
||||||
|
-- If crafting has not been added...
|
||||||
|
if not ImmersiveCraftCounter:addCraft(pos, player) then
|
||||||
|
-- ...then we don't let the player start a new craft.
|
||||||
|
core.chat_send_player(player:get_player_name(),
|
||||||
|
'The limit of simultaneously active crafts has been reached:'..
|
||||||
|
'complete or cancel active crafts to start a new one')
|
||||||
|
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
-- We get the material as an ItemStack.
|
||||||
|
---@type ItemStack
|
||||||
|
local stack = ItemStack(self.triggerItem)
|
||||||
|
-- We edit the amount of materials in accordance with the number of crafts being performed.
|
||||||
|
stack:set_count(stack:get_count() * amount)
|
||||||
|
|
||||||
|
-- We get the player's inventory, from where we must take the materials for crafting.
|
||||||
|
local playerInventory = player:get_inventory()
|
||||||
|
|
||||||
|
-- If the player does not have the necessary materials for crafting...
|
||||||
|
if not playerInventory:contains_item('main', stack) then
|
||||||
|
-- ...we inform him about this.
|
||||||
|
core.chat_send_player(player:get_player_name(),
|
||||||
|
'Not enough materials for crafting')
|
||||||
|
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
-- If we have the materials, we take them and start initializing the craft.
|
||||||
|
playerInventory:remove_item('main', stack)
|
||||||
|
|
||||||
-- We get the first stage of crafting.
|
-- We get the first stage of crafting.
|
||||||
local stage = self.stagesByName[self.startStageName]
|
local stage = self.stagesByName[self.startStageName]
|
||||||
@@ -108,24 +144,94 @@ function ImmersiveCraft:startCraft(pos, Player)
|
|||||||
local craftData = {
|
local craftData = {
|
||||||
craftName = self.name,
|
craftName = self.name,
|
||||||
stageName = stage.name,
|
stageName = stage.name,
|
||||||
|
amount = amount,
|
||||||
}
|
}
|
||||||
-- We save it to the workstation metadata.
|
-- We save it to the workstation metadata.
|
||||||
Smc.setCraftDataByPos(pos, craftData)
|
Smc.setCraftDataByPos(pos, craftData)
|
||||||
|
|
||||||
-- We are starting the first stage of crafting.
|
-- We are starting the first stage of crafting.
|
||||||
stage:startHandler(pos, Player)
|
stage:startHandler(pos, player)
|
||||||
|
|
||||||
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
---Complete the immersive crafting process.
|
---Complete the immersive crafting process.
|
||||||
---@param pos MapPosition
|
---@param pos MapPosition
|
||||||
---@param Player Player
|
---@param Player Player
|
||||||
function ImmersiveCraft:endCraft(pos, Player)
|
function ImmersiveCraft:endCraft(pos, Player)
|
||||||
|
-- We get current data on crafting.
|
||||||
|
---@type ImmersiveCraftData?
|
||||||
|
local craftData = Smc.getCraftDataByPos(pos)
|
||||||
|
assert(craftData ~= nil, 'Immersive crafting data is empty.')
|
||||||
|
|
||||||
|
-- We drop the result of crafting.
|
||||||
|
local dropPos = vector.new(pos) + vector.new(0, 0.9,0)
|
||||||
|
for i=1, craftData.amount do
|
||||||
|
core.item_drop(ItemStack(self.output), nil, dropPos)
|
||||||
|
end
|
||||||
|
|
||||||
-- Clearing the temporary data table for crafting, freeing up the work station for new crafts.
|
-- Clearing the temporary data table for crafting, freeing up the work station for new crafts.
|
||||||
Smc.setCraftDataByPos(pos)
|
Smc.setCraftDataByPos(pos)
|
||||||
|
|
||||||
-- We drop away the result of crafting.
|
-- Remove craft from the counter.
|
||||||
-- TODO: переписать определение этого метода в хелпере: itemstack не может быть строкой, а dropper опционален
|
ImmersiveCraftCounter:removeCraft(pos, Player)
|
||||||
core.item_drop(ItemStack(self.output), nil, vector.new(pos)+vector.new(0, 0.9,0))
|
end
|
||||||
|
|
||||||
|
---Stop the crafting process and free up the workbench.
|
||||||
|
---@param pos MapPosition
|
||||||
|
---@param Player Player
|
||||||
|
function ImmersiveCraft:abortCraft(pos, Player)
|
||||||
|
-- We get current data on crafting.
|
||||||
|
---@type ImmersiveCraftData?
|
||||||
|
local craftData = Smc.getCraftDataByPos(pos)
|
||||||
|
assert(craftData ~= nil, 'Immersive crafting data is empty.')
|
||||||
|
|
||||||
|
-- We get the current stage and start the abort process.
|
||||||
|
local stage = self.stagesByName[craftData.stageName]
|
||||||
|
stage:abortHandler(pos, Player)
|
||||||
|
|
||||||
|
-- Clearing the temporary data table for crafting, freeing up the work station for new crafts.
|
||||||
|
Smc.setCraftDataByPos(pos)
|
||||||
|
|
||||||
|
-- Remove craft from the counter.
|
||||||
|
ImmersiveCraftCounter:removeCraft(pos, Player)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Returns a human-readable description of the crafting process.
|
||||||
|
---@return string
|
||||||
|
function ImmersiveCraft:toString()
|
||||||
|
-- We get the material as an ItemStack.
|
||||||
|
---@type ItemStack
|
||||||
|
local stack = ItemStack(self.triggerItem)
|
||||||
|
-- We obtain a table of item parameters.
|
||||||
|
---@type ItemDefinition
|
||||||
|
local itemDef = stack:get_definition()
|
||||||
|
|
||||||
|
-- Let's start writing a description of the craft.
|
||||||
|
local craftDesc = ('Material: %s x%s\n\n'):format(itemDef._title, stack:get_count())..
|
||||||
|
'Crafting process:\n'
|
||||||
|
|
||||||
|
-- Let's start with the first crafting stage.
|
||||||
|
---@type string?
|
||||||
|
local stageName = self.startStageName
|
||||||
|
local stages = self.stagesByName
|
||||||
|
|
||||||
|
-- While we have a stage name...
|
||||||
|
local count = 1
|
||||||
|
while stageName ~= nil do
|
||||||
|
-- We get a crafting stage...
|
||||||
|
local stage = stages[stageName]
|
||||||
|
|
||||||
|
-- ...and we add its description to the list of actions for crafting.
|
||||||
|
craftDesc = craftDesc..tostring(count)..'. '..stage:toString()..'\n'
|
||||||
|
|
||||||
|
-- We also get the name of the next stage.
|
||||||
|
stageName = stage.nextStage
|
||||||
|
end
|
||||||
|
|
||||||
|
-- If the cycle is interrupted, it means we have processed all the stages.
|
||||||
|
-- Return the craft description.
|
||||||
|
return craftDesc
|
||||||
end
|
end
|
||||||
|
|
||||||
return ImmersiveCraft
|
return ImmersiveCraft
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ local ImmersiveCraftStage = require('CraftSystem.ImmersiveCraftStage')
|
|||||||
---@field startHandler fun(self:CraftSystem.ImmersiveCraftStage.TappingStage, pos:MapPosition, Player:Player)
|
---@field startHandler fun(self:CraftSystem.ImmersiveCraftStage.TappingStage, pos:MapPosition, Player:Player)
|
||||||
---@field processHandler fun(self:CraftSystem.ImmersiveCraftStage.TappingStage, pos:MapPosition, Player:Player)
|
---@field processHandler fun(self:CraftSystem.ImmersiveCraftStage.TappingStage, pos:MapPosition, Player:Player)
|
||||||
---@field endHandler fun(self:CraftSystem.ImmersiveCraftStage.TappingStage, pos:MapPosition, Player:Player)
|
---@field endHandler fun(self:CraftSystem.ImmersiveCraftStage.TappingStage, pos:MapPosition, Player:Player)
|
||||||
|
---@field toString fun(self:CraftSystem.ImmersiveCraftStage.TappingStage):string
|
||||||
local TappingStage = extend(ImmersiveCraftStage, {})
|
local TappingStage = extend(ImmersiveCraftStage, {})
|
||||||
|
|
||||||
---Definition table for the `CraftSystem.ImmersiveCraftStage.TappingStage`.
|
---Definition table for the `CraftSystem.ImmersiveCraftStage.TappingStage`.
|
||||||
@@ -42,7 +43,7 @@ function TappingStage:new(def)
|
|||||||
-- Registering the crafting stage start handler.
|
-- Registering the crafting stage start handler.
|
||||||
instance.startHandler = function(tappingStage , pos, Player)
|
instance.startHandler = function(tappingStage , pos, Player)
|
||||||
-- We determine the coordinate where we will create the crafting node (entity).
|
-- We determine the coordinate where we will create the crafting node (entity).
|
||||||
local entityPos = vector.new(pos) + vector.new(0,0.9,0)
|
local entityPos = vector.new(pos) + vector.new(0,0.8,0)
|
||||||
|
|
||||||
-- We create a table of initial parameters that we will pass to the entity.
|
-- We create a table of initial parameters that we will pass to the entity.
|
||||||
---@class CraftSystem.ImmersiveCraftStage.TappingNode.InitParams
|
---@class CraftSystem.ImmersiveCraftStage.TappingNode.InitParams
|
||||||
@@ -70,7 +71,6 @@ function TappingStage:new(def)
|
|||||||
|
|
||||||
-- Saving temporary data in work station metadata.
|
-- Saving temporary data in work station metadata.
|
||||||
setStageDataByPos(pos, stageData)
|
setStageDataByPos(pos, stageData)
|
||||||
print(pos, dump(Smc.getCraftDataByPos(pos)), 'ONE')
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Registering the crafting stage process handler.
|
-- Registering the crafting stage process handler.
|
||||||
@@ -79,7 +79,6 @@ function TappingStage:new(def)
|
|||||||
-- Obtaining stage time data from work station metadata.
|
-- Obtaining stage time data from work station metadata.
|
||||||
---@type ImmersiveStageData.TappingStageData?
|
---@type ImmersiveStageData.TappingStageData?
|
||||||
local stageData = getStageDataByPos(pos)
|
local stageData = getStageDataByPos(pos)
|
||||||
print(dump(pos), dump(Smc.getCraftDataByPos(pos)), 'TWO')
|
|
||||||
assert(stageData ~= nil, 'Immersive stage data is empty.')
|
assert(stageData ~= nil, 'Immersive stage data is empty.')
|
||||||
|
|
||||||
-- Increasing the click counter
|
-- Increasing the click counter
|
||||||
@@ -115,9 +114,64 @@ function TappingStage:new(def)
|
|||||||
Events.call('craft_system:next_immersive_stage:'..tappingStage.craftName, tappingStage, pos, Player)
|
Events.call('craft_system:next_immersive_stage:'..tappingStage.craftName, tappingStage, pos, Player)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Registering the aborting the crafting process handler.
|
||||||
|
---@param tappingStage CraftSystem.ImmersiveCraftStage.TappingStage
|
||||||
|
instance.abortHandler = function(tappingStage, pos, Player)
|
||||||
|
-- We receive current data on crafting.
|
||||||
|
---@type ImmersiveCraftData?
|
||||||
|
local craftData = Smc.getCraftDataByPos(pos)
|
||||||
|
assert(craftData ~= nil, 'Immersive crafting data is empty.')
|
||||||
|
|
||||||
|
-- Obtaining stage time data from work station metadata.
|
||||||
|
---@type ImmersiveStageData.TappingStageData?
|
||||||
|
local stageData = craftData.stageData
|
||||||
|
assert(stageData ~= nil, 'Immersive stage data is empty.')
|
||||||
|
|
||||||
|
-- We get the entity object.
|
||||||
|
local entity = Smc.getObjectByGUID(stageData.entityGUID)
|
||||||
|
assert(entity ~= nil, 'The entity for immersive crafting is nil.')
|
||||||
|
|
||||||
|
-- Removing an entity from the world.
|
||||||
|
entity:remove()
|
||||||
|
|
||||||
|
-- We drop the item as it was before processing.
|
||||||
|
local dropPos = vector.new(pos)+vector.new(0, 0.9,0)
|
||||||
|
for i=1, craftData.amount do
|
||||||
|
core.item_drop(ItemStack(tappingStage.targetItem), nil, dropPos)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Returns a human-readable description of the crafting stage.
|
||||||
|
instance.toString = function(tappingStage)
|
||||||
|
local stageDesc = ('Tapping x%s'):format(tappingStage.clicksNeed)
|
||||||
|
|
||||||
|
return stageDesc
|
||||||
|
end
|
||||||
|
|
||||||
-- Subscribe to an event that means the player hit the entity.
|
-- Subscribe to an event that means the player hit the entity.
|
||||||
Events.subscribe(('craft_system:punch_tapping_node:%s:%s'):format(instance.craftName, instance.name),
|
Events.subscribe(('craft_system:punch_tapping_node:%s:%s'):format(instance.craftName, instance.name),
|
||||||
function(eventName, pos, Player)
|
---@param craftNode LuaEntity
|
||||||
|
function(eventName, pos, Player, craftNode)
|
||||||
|
-- DRAFT START:
|
||||||
|
-- Creating particles
|
||||||
|
for i=0, 10 do
|
||||||
|
core.add_particle({
|
||||||
|
pos = vector.new(pos)+vector.new(0,0.5,0),
|
||||||
|
velocity = {x=math.random(-1,1), y=4, z=math.random(-1,1)},
|
||||||
|
acceleration = {x=0, y=-9.8, z=0},
|
||||||
|
expirationtime = 2,
|
||||||
|
collision_removal = true,
|
||||||
|
node = {name=instance.targetItem}
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Making the entity shake (silly method :P).
|
||||||
|
craftNode.object:set_rotation(vector.new(0.01,0.1,0.01))
|
||||||
|
core.after(0.1, function() craftNode.object:set_rotation(vector.new(-0.01,-0.1,-0.01))end)
|
||||||
|
core.after(0.2, function() craftNode.object:set_rotation(vector.new(0,0,0))end)
|
||||||
|
-- DRAFT END
|
||||||
|
|
||||||
|
-- Calling the crafting stage process handler.
|
||||||
instance:processHandler(pos, Player)
|
instance:processHandler(pos, Player)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user