VoxeLibre/mods/ITEMS/mcl_throwing/init.lua

80 lines
2.2 KiB
Lua
Raw Normal View History

2017-02-15 01:18:23 +03:00
mcl_throwing = {}
local modpath = minetest.get_modpath(minetest.get_current_modname())
2021-04-16 00:41:34 +03:00
--
-- Snowballs and other throwable items
--
local GRAVITY = tonumber(minetest.settings:get("movement_gravity"))
local entity_mapping = {}
local velocities = {}
function mcl_throwing.register_throwable_object(name, entity, velocity)
entity_mapping[name] = entity
velocities[name] = velocity
end
function mcl_throwing.throw(throw_item, pos, dir, velocity, thrower)
if velocity == nil then
velocity = velocities[throw_item]
end
if velocity == nil then
velocity = 22
end
2020-12-07 02:02:32 +03:00
minetest.sound_play("mcl_throwing_throw", {pos=pos, gain=0.4, max_hear_distance=16}, true)
local itemstring = ItemStack(throw_item):get_name()
local obj = minetest.add_entity(pos, entity_mapping[itemstring])
2019-03-06 06:38:57 +03:00
obj:set_velocity({x=dir.x*velocity, y=dir.y*velocity, z=dir.z*velocity})
obj:set_acceleration({x=dir.x*-3, y=-GRAVITY, z=dir.z*-3})
2019-12-09 13:03:48 +03:00
if thrower then
obj:get_luaentity()._thrower = thrower
end
return obj
end
-- Throw item
function mcl_throwing.get_player_throw_function(entity_name, velocity)
local func = function(item, player, pointed_thing)
local playerpos = player:get_pos()
local dir = player:get_look_dir()
2021-04-16 00:41:34 +03:00
mcl_throwing.throw(item, {x=playerpos.x, y=playerpos.y+1.5, z=playerpos.z}, dir, velocity, player:get_player_name())
2020-07-10 17:08:40 +03:00
if not minetest.is_creative_enabled(player:get_player_name()) then
item:take_item()
end
return item
end
return func
end
function mcl_throwing.dispense_function(stack, dispenserpos, droppos, dropnode, dropdir)
-- Launch throwable item
local shootpos = vector.add(dispenserpos, vector.multiply(dropdir, 0.51))
mcl_throwing.throw(stack:get_name(), shootpos, dropdir)
end
-- Staticdata handling because objects may want to be reloaded
2021-04-02 15:27:35 +03:00
function mcl_throwing.get_staticdata(self)
2019-12-09 13:03:48 +03:00
local thrower
-- Only save thrower if it's a player name
if type(self._thrower) == "string" then
thrower = self._thrower
end
local data = {
_lastpos = self._lastpos,
2019-12-09 13:03:48 +03:00
_thrower = thrower,
}
return minetest.serialize(data)
end
2021-04-02 15:27:35 +03:00
function mcl_throwing.on_activate(self, staticdata, dtime_s)
local data = minetest.deserialize(staticdata)
if data then
self._lastpos = data._lastpos
self._thrower = data._thrower
end
end
dofile(modpath.."/register.lua")