automatic snowball throwing in creative mdoe

This commit is contained in:
HybridDog 2015-06-04 13:09:04 +02:00
parent 09a422a0e7
commit 777d5b1407
1 changed files with 53 additions and 41 deletions

View File

@ -13,63 +13,66 @@ local function get_gravity()
return grav*snow.snowball_gravity return grav*snow.snowball_gravity
end end
--[[local someone_digging local someone_throwing
local timer = 0]] local timer = 0
--Shoot snowball --Shoot snowball
local function snow_shoot_snowball(item, player) local function snow_shoot_snowball(item, player)
local pos = player:getpos() local addp = {y = 1.625} -- + (math.random()-0.5)/5}
pos.y = pos.y+1.625
local obj = minetest.add_entity(pos, "snow:snowball_entity")
local dir = player:get_look_dir() local dir = player:get_look_dir()
local dif = 2*math.sqrt(dir.z*dir.z+dir.x*dir.x)
addp.x = dir.z/dif -- + (math.random()-0.5)/5
addp.z = -dir.x/dif -- + (math.random()-0.5)/5
local pos = vector.add(player:getpos(), addp)
local obj = minetest.add_entity(pos, "snow:snowball_entity")
obj:setvelocity(vector.multiply(dir, snow.snowball_velocity)) obj:setvelocity(vector.multiply(dir, snow.snowball_velocity))
obj:setacceleration({x=dir.x*-3, y=-get_gravity(), z=dir.z*-3}) obj:setacceleration({x=dir.x*-3, y=-get_gravity(), z=dir.z*-3})
if creative_mode then if creative_mode then
if not someone_throwing then
someone_throwing = true
timer = -0.5
end
return return
end end
item:take_item() item:take_item()
return item return item
end end
--[[ simulate the players digging with it using a globalstep if creative_mode then
minetest.register_globalstep(function(dtime) local function update_step(dtime)
-- abort if noone uses a drill timer = timer+dtime
if not someone_digging then if timer < 0.006 then
return return
end end
timer = 0
-- abort that it doesn't dig too fast local active
timer = timer+dtime for _,player in pairs(minetest.get_connected_players()) do
if timer < speed then if player:get_player_control().LMB then
return local item = player:get_wielded_item()
end local itemname = item:get_name()
timer = 0 if itemname == "default:snow" then
snow_shoot_snowball(nil, player)
local active
for _,player in pairs(minetest.get_connected_players()) do
if player:get_player_control().LMB then
local item = player:get_wielded_item()
local itemname = item:get_name()
for name,func in pairs(drills) do
if name == itemname then
-- player has a mk3 drill as wielditem and holds left mouse button
local pt = get_pointed_thing(player, ranges[itemname])
if pt then
-- simulate the function
player:set_wielded_item(func(item, player, pt))
end
active = true active = true
break break
end end
end end
end end
-- disable the function if noone currently throws them
if not active then
someone_throwing = false
end
end end
-- disable the function if noone currently uses a mk3 drill to reduce lag -- do automatic throwing using a globalstep
if not active then minetest.register_globalstep(function(dtime)
someone_digging = false -- only if one holds left click
end if someone_throwing then
end)--]] update_step(dtime)
end
end)
end
--The snowball Entity --The snowball Entity
local snow_snowball_ENTITY = { local snow_snowball_ENTITY = {
@ -80,15 +83,24 @@ local snow_snowball_ENTITY = {
} }
function snow_snowball_ENTITY.on_activate(self) function snow_snowball_ENTITY.on_activate(self)
self.object:setacceleration({x=0, y=-get_gravity(), z=0})
self.lastpos = self.object:getpos() self.lastpos = self.object:getpos()
minetest.after(0, function(obj) minetest.after(0.1, function(obj)
if not obj if not obj then
or obj:getvelocity().y ~= 0 then return
end
local vel = obj:getvelocity()
if vel
and vel.y ~= 0 then
return return
end end
minetest.after(0, function(obj) minetest.after(0, function(obj)
if obj if not obj then
and obj:getvelocity().y == 0 then return
end
local vel = obj:getvelocity()
if not vel
or vel.y == 0 then
obj:remove() obj:remove()
end end
end, obj) end, obj)