2017-02-14 23:18:23 +01:00
|
|
|
mcl_throwing = {}
|
|
|
|
|
2021-03-26 22:31:15 +01:00
|
|
|
local modpath = minetest.get_modpath(minetest.get_current_modname())
|
2019-03-08 00:46:35 +01:00
|
|
|
|
2021-04-15 23:41:34 +02:00
|
|
|
--
|
2018-05-07 23:10:49 +02:00
|
|
|
-- Snowballs and other throwable items
|
|
|
|
--
|
|
|
|
|
|
|
|
local GRAVITY = tonumber(minetest.settings:get("movement_gravity"))
|
|
|
|
|
2021-03-26 22:31:15 +01:00
|
|
|
local entity_mapping = {}
|
|
|
|
local velocities = {}
|
2018-05-07 23:10:49 +02:00
|
|
|
|
2021-03-26 22:31:15 +01:00
|
|
|
function mcl_throwing.register_throwable_object(name, entity, velocity)
|
|
|
|
entity_mapping[name] = entity
|
|
|
|
velocities[name] = velocity
|
2024-09-16 02:12:21 +02:00
|
|
|
assert(minetest.registered_entities[entity], entity.." not registered")
|
|
|
|
assert(minetest.registered_entities[entity]._vl_projectile)
|
2021-03-26 22:31:15 +01:00
|
|
|
end
|
2018-05-07 23:10:49 +02:00
|
|
|
|
2021-03-26 22:31:15 +01:00
|
|
|
function mcl_throwing.throw(throw_item, pos, dir, velocity, thrower)
|
2024-05-22 21:42:36 +02:00
|
|
|
velocity = velocity or velocities[throw_item] or 22
|
2020-12-07 00:02:32 +01:00
|
|
|
minetest.sound_play("mcl_throwing_throw", {pos=pos, gain=0.4, max_hear_distance=16}, true)
|
2018-05-07 23:10:49 +02:00
|
|
|
|
|
|
|
local itemstring = ItemStack(throw_item):get_name()
|
2024-05-22 21:42:36 +02:00
|
|
|
local obj = vl_projectile.create(entity_mapping[itemstring], {
|
|
|
|
pos = pos,
|
|
|
|
owner = thrower,
|
|
|
|
dir = dir,
|
|
|
|
velocity = velocity,
|
|
|
|
drag = 3,
|
|
|
|
})
|
|
|
|
obj:get_luaentity()._thrower = thrower
|
2018-05-07 23:10:49 +02:00
|
|
|
return obj
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Throw item
|
2021-03-26 22:31:15 +01:00
|
|
|
function mcl_throwing.get_player_throw_function(entity_name, velocity)
|
2021-05-29 16:12:33 +02:00
|
|
|
local function func(item, player, pointed_thing)
|
2018-05-07 23:10:49 +02:00
|
|
|
local playerpos = player:get_pos()
|
|
|
|
local dir = player:get_look_dir()
|
2021-04-15 23:41:34 +02: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 16:08:40 +02:00
|
|
|
if not minetest.is_creative_enabled(player:get_player_name()) then
|
2018-05-07 23:10:49 +02:00
|
|
|
item:take_item()
|
|
|
|
end
|
|
|
|
return item
|
|
|
|
end
|
|
|
|
return func
|
|
|
|
end
|
|
|
|
|
2021-03-26 22:31:15 +01:00
|
|
|
function mcl_throwing.dispense_function(stack, dispenserpos, droppos, dropnode, dropdir)
|
2018-05-07 23:10:49 +02:00
|
|
|
-- 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 14:27:35 +02:00
|
|
|
function mcl_throwing.get_staticdata(self)
|
2019-12-09 11:03:48 +01:00
|
|
|
local thrower
|
|
|
|
-- Only save thrower if it's a player name
|
|
|
|
if type(self._thrower) == "string" then
|
|
|
|
thrower = self._thrower
|
|
|
|
end
|
2018-05-07 23:10:49 +02:00
|
|
|
local data = {
|
|
|
|
_lastpos = self._lastpos,
|
2019-12-09 11:03:48 +01:00
|
|
|
_thrower = thrower,
|
2018-05-07 23:10:49 +02:00
|
|
|
}
|
|
|
|
return minetest.serialize(data)
|
|
|
|
end
|
|
|
|
|
2021-04-02 14:27:35 +02:00
|
|
|
function mcl_throwing.on_activate(self, staticdata, dtime_s)
|
2018-05-07 23:10:49 +02:00
|
|
|
local data = minetest.deserialize(staticdata)
|
2024-05-22 21:42:36 +02:00
|
|
|
self._staticdata = data
|
2018-05-07 23:10:49 +02:00
|
|
|
if data then
|
|
|
|
self._lastpos = data._lastpos
|
|
|
|
self._thrower = data._thrower
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-05-22 21:42:36 +02:00
|
|
|
dofile(modpath.."/register.lua")
|