Refactor Mining Laser

This commit is contained in:
mrkubax10 2025-04-19 20:18:02 +02:00
parent 000cff6940
commit 6e1d0b5229
2 changed files with 91 additions and 68 deletions

View File

@ -46,6 +46,13 @@ end
function industrialtest.ElectricTool.onUse(self,itemstack,user,pointed)
self:prepare(itemstack)
if user:is_player() then
local controls=user:get_player_control()
if controls.sneak and self:changeMode(itemstack) then
return true
end
end
local meta=itemstack:get_meta()
local opPower=self:getOpPower(itemstack)
if meta:get_int("industrialtest.powerAmount")<opPower then
@ -58,6 +65,11 @@ function industrialtest.ElectricTool.onUse(self,itemstack,user,pointed)
return true
end
function industrialtest.ElectricTool.changeMode(self,itemstack)
-- dummy function
return false
end
function industrialtest.ElectricTool.getOpPower(self,itemstack)
-- dummy function
return 0

View File

@ -15,10 +15,9 @@
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
local S=minetest.get_translator("industrialtest")
industrialtest.MiningLaserBeam={}
local beam={}
beam.onActivate=function(self,staticdata)
function industrialtest.MiningLaserBeam.onActivate(self,staticdata)
local data=minetest.deserialize(staticdata)
self.direction=data.direction
self.energy=data.energy
@ -28,11 +27,11 @@ beam.onActivate=function(self,staticdata)
self.user=data.user
end
beam.onPunch=function()
function industrialtest.MiningLaserBeam.onPunch()
return true
end
beam.onNodeBreak=function(pos,node,user)
function industrialtest.MiningLaserBeam.onNodeBreak(pos,node,user)
if industrialtest.mclAvailable then
local drops=minetest.get_node_drops(node.name,"")
for _, val in ipairs(drops) do
@ -45,7 +44,7 @@ beam.onNodeBreak=function(pos,node,user)
minetest.node_dig(pos,node,user)
end
beam.onStep=function(self,dtime,moveresult)
function industrialtest.MiningLaserBeam.onStep(self,dtime,moveresult)
local pos=self.object:get_pos()
if vector.distance(self.startPos,pos)>=self.distance or self.energy<=0 then
self.object:remove()
@ -67,7 +66,7 @@ beam.onStep=function(self,dtime,moveresult)
self.object:remove()
return
else
beam.onNodeBreak(val.node_pos,node,minetest.get_player_by_name(self.user))
industrialtest.MiningLaserBeam.onNodeBreak(val.node_pos,node,minetest.get_player_by_name(self.user))
self.energy=self.energy-1
end
elseif self.mode==4 then
@ -80,6 +79,7 @@ beam.onStep=function(self,dtime,moveresult)
end
end
-- Note: If there are more entities registered then replace manual registration with proper pseudo-OOP
minetest.register_entity("industrialtest:mining_laser_beam",{
initial_properties={
physical=true,
@ -103,18 +103,77 @@ minetest.register_entity("industrialtest:mining_laser_beam",{
static_save=false,
shaded=false,
},
on_activate=beam.onActivate,
on_punch=beam.onPunch,
on_step=beam.onStep
on_activate=industrialtest.MiningLaserBeam.onActivate,
on_punch=industrialtest.MiningLaserBeam.onPunch,
on_step=industrialtest.MiningLaserBeam.onStep
})
local miningLaser={}
miningLaser.mode1OpPower=1250
miningLaser.mode2OpPower=100
miningLaser.mode4OpPower=5000
miningLaser.modeCount=4
industrialtest.MiningLaser=table.copy(industrialtest.ElectricTool)
industrialtest.internal.unpackTableInto(industrialtest.MiningLaser,{
name="industrialtest:mining_laser",
description=S("Mining Laser (Mode 1)"),
inventoryImage="industrialtest_mining_laser.png",
capacity=300000,
flow=industrialtest.api.hvPowerFlow,
define={onUse=true},
_modeCount=4
})
miningLaser.getMode=function(itemstack)
function industrialtest.MiningLaser.hitUse(self,itemstack,user,pointed)
if not user:is_player() then
return false
end
local control=user:get_player_control()
if control.sneak then
end
local mode=self.getMode(itemstack)
local meta=itemstack:get_meta()
if mode==1 then
if not self:spawnBeam(itemstack,user,user:get_pos(),user:get_look_dir(),10,64) then
return false
end
elseif mode==2 then
if not self:spawnBeam(itemstack,user,user:get_pos(),user:get_look_dir(),3,2) then
return false
end
elseif mode==3 then
local yaw=user:get_look_horizontal()
local dir=vector.new(-math.sin(yaw),0,math.cos(yaw))
if not self:spawnBeam(itemstack,user,user:get_pos(),dir,10,64) then
return false
end
elseif mode==4 then
if not self:spawnBeam(itemstack,user,user:get_pos(),user:get_look_dir(),1,64) then
return false
end
end
return true
end
function industrialtest.MiningLaser.changeMode(self,itemstack)
local mode=self.getMode(itemstack)
mode=mode+1
if mode>=self._modeCount+1 then
mode=1
end
self.setMode(itemstack,mode)
return true
end
function industrialtest.MiningLaser.getOpPower(self,itemstack)
local mode=self.getMode(itemstack)
if mode==1 then
return 1250
elseif mode==2 or mode==3 then
return 100
elseif mode==4 then
return 5000
end
-- assert not reached
assert(false)
end
function industrialtest.MiningLaser.getMode(itemstack)
local meta=itemstack:get_meta()
if not meta:contains("mode") then
meta:set_int("mode",1)
@ -123,74 +182,26 @@ miningLaser.getMode=function(itemstack)
return meta:get_int("mode")
end
miningLaser.setMode=function(itemstack,mode)
function industrialtest.MiningLaser.setMode(itemstack,mode)
local meta=itemstack:get_meta()
meta:set_int("mode",mode)
meta:set_string("industrialtest.descriptionOverride",S("Mining Laser (Mode @1)",mode))
industrialtest.api.updateItemPowerText(itemstack)
end
miningLaser.spawnBeam=function(itemstack,user,pos,dir,opPower,energy,distance)
local meta=itemstack:get_meta()
if meta:get_int("industrialtest.powerAmount")<opPower then
return false
end
function industrialtest.MiningLaser.spawnBeam(self,itemstack,user,pos,dir,energy,distance)
minetest.add_entity(pos+vector.new(0,1.5,0),"industrialtest:mining_laser_beam",minetest.serialize({
direction=dir,
energy=energy,
distance=distance,
mode=miningLaser.getMode(itemstack),
mode=self.getMode(itemstack),
user=user:get_player_name()
}))
industrialtest.api.addPowerToItem(itemstack,-opPower)
return true
end
miningLaser.onUse=function(itemstack,user)
if not user:is_player() then
return nil
end
local mode=miningLaser.getMode(itemstack)
local control=user:get_player_control()
if control.sneak then
mode=mode+1
if mode>=miningLaser.modeCount+1 then
mode=1
end
miningLaser.setMode(itemstack,mode)
return itemstack
end
local meta=itemstack:get_meta()
if mode==1 then
if not miningLaser.spawnBeam(itemstack,user,user:get_pos(),user:get_look_dir(),miningLaser.mode1OpPower,10,64) then
return nil
end
elseif mode==2 then
if not miningLaser.spawnBeam(itemstack,user,user:get_pos(),user:get_look_dir(),miningLaser.mode2OpPower,3,2) then
return nil
end
elseif mode==3 then
local yaw=user:get_look_horizontal()
local dir=vector.new(-math.sin(yaw),0,math.cos(yaw))
if not miningLaser.spawnBeam(itemstack,user,user:get_pos(),dir,miningLaser.mode1OpPower,10,64) then
return nil
end
elseif mode==4 then
if not miningLaser.spawnBeam(itemstack,user,user:get_pos(),user:get_look_dir(),miningLaser.mode4OpPower,1,64) then
return nil
end
end
return itemstack
end
industrialtest.MiningLaser:register()
minetest.register_tool("industrialtest:mining_laser",{
description=S("Mining Laser (Mode 1)"),
inventory_image="industrialtest_mining_laser.png",
on_use=miningLaser.onUse,
_industrialtest_powerStorage=true,
_industrialtest_powerCapacity=300000,
_industrialtest_powerFlow=industrialtest.api.hvPowerFlow
})
minetest.register_craft({
type="shaped",
output="industrialtest:mining_laser",