2017-01-16 23:11:04 +01:00
|
|
|
--
|
2017-01-25 11:45:17 +01:00
|
|
|
-- Snowballs and other throwable items
|
2017-01-16 23:11:04 +01:00
|
|
|
--
|
|
|
|
|
2017-01-25 11:45:17 +01:00
|
|
|
local GRAVITY = tonumber(minetest.setting_get("movement_gravity"))
|
2017-01-16 23:11:04 +01:00
|
|
|
|
2017-02-14 23:41:19 +01:00
|
|
|
local entity_mapping = {
|
|
|
|
["mcl_throwing:snowball"] = "mcl_throwing:snowball_entity",
|
|
|
|
["mcl_throwing:egg"] = "mcl_throwing:egg_entity",
|
|
|
|
["mcl_throwing:ender_pearl"] = "mcl_throwing:ender_pearl_entity",
|
|
|
|
}
|
|
|
|
|
2017-02-14 23:18:23 +01:00
|
|
|
local velocities = {
|
|
|
|
["mcl_throwing:snowball_entity"] = 22,
|
|
|
|
["mcl_throwing:egg_entity"] = 22,
|
|
|
|
["mcl_throwing:ender_pearl_entity"] = 22,
|
|
|
|
}
|
|
|
|
|
2017-02-14 23:41:19 +01:00
|
|
|
mcl_throwing.throw = function(throw_item, pos, dir, velocity)
|
2017-02-14 23:18:23 +01:00
|
|
|
if velocity == nil then
|
|
|
|
velocity = velocities[entity_name]
|
|
|
|
end
|
|
|
|
if velocity == nil then
|
|
|
|
velocity = 22
|
|
|
|
end
|
2017-02-14 23:41:19 +01:00
|
|
|
|
|
|
|
local itemstring = throw_item:get_name()
|
|
|
|
local obj = minetest.add_entity(pos, entity_mapping[itemstring])
|
|
|
|
obj:setvelocity({x=dir.x*velocity, y=dir.y*velocity, z=dir.z*velocity})
|
|
|
|
obj:setacceleration({x=dir.x*-3, y=-GRAVITY, z=dir.z*-3})
|
|
|
|
return obj
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Throw item
|
|
|
|
local throw_function = function(entity_name, velocity)
|
2017-01-16 23:11:04 +01:00
|
|
|
local func = function(item, player, pointed_thing)
|
2017-02-14 23:41:19 +01:00
|
|
|
local playerpos = player:getpos()
|
|
|
|
local dir = player:get_look_dir()
|
|
|
|
local obj = mcl_throwing.throw(item, {x=playerpos.x, y=playerpos.y+1.5, z=playerpos.z}, dir, velocity)
|
2017-01-25 11:45:17 +01:00
|
|
|
obj:get_luaentity()._thrower = player:get_player_name()
|
2017-01-26 18:50:06 +01:00
|
|
|
if not minetest.setting_getbool("creative_mode") then
|
|
|
|
item:take_item()
|
|
|
|
end
|
2017-01-16 23:11:04 +01:00
|
|
|
return item
|
|
|
|
end
|
|
|
|
return func
|
|
|
|
end
|
|
|
|
|
2017-01-26 18:59:57 +01:00
|
|
|
-- Staticdata handling because objects may want to be reloaded
|
|
|
|
local get_staticdata = function(self)
|
|
|
|
local data = {
|
|
|
|
_lastpos = self._lastpos,
|
|
|
|
_thrower = self._thrower,
|
|
|
|
}
|
|
|
|
return minetest.serialize(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
local on_activate = function(self, staticdata, dtime_s)
|
|
|
|
local data = minetest.deserialize(staticdata)
|
|
|
|
if data then
|
|
|
|
self._lastpos = data._lastpos
|
|
|
|
self._thrower = data._thrower
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-16 23:11:04 +01:00
|
|
|
-- The snowball entity
|
|
|
|
local snowball_ENTITY={
|
|
|
|
physical = false,
|
|
|
|
timer=0,
|
|
|
|
textures = {"mcl_throwing_snowball.png"},
|
2017-02-14 23:43:43 +01:00
|
|
|
visual_size = {x=0.5, y=0.5},
|
2017-01-16 23:11:04 +01:00
|
|
|
collisionbox = {0,0,0,0,0,0},
|
2017-01-26 18:59:57 +01:00
|
|
|
|
|
|
|
get_staticdata = get_staticdata,
|
|
|
|
on_activate = on_activate,
|
|
|
|
|
|
|
|
_lastpos={},
|
2017-01-16 23:11:04 +01:00
|
|
|
}
|
|
|
|
local egg_ENTITY={
|
|
|
|
physical = false,
|
|
|
|
timer=0,
|
|
|
|
textures = {"mcl_throwing_egg.png"},
|
2017-02-14 23:43:43 +01:00
|
|
|
visual_size = {x=0.45, y=0.45},
|
2017-01-16 23:11:04 +01:00
|
|
|
collisionbox = {0,0,0,0,0,0},
|
2017-01-26 18:59:57 +01:00
|
|
|
|
|
|
|
get_staticdata = get_staticdata,
|
|
|
|
on_activate = on_activate,
|
|
|
|
|
|
|
|
_lastpos={},
|
2017-01-16 23:11:04 +01:00
|
|
|
}
|
2017-01-25 11:45:17 +01:00
|
|
|
-- Ender pearl entity
|
|
|
|
local pearl_ENTITY={
|
|
|
|
physical = false,
|
|
|
|
timer=0,
|
|
|
|
textures = {"mcl_throwing_ender_pearl.png"},
|
2017-02-14 23:43:43 +01:00
|
|
|
visual_size = {x=0.9, y=0.9},
|
2017-01-25 11:45:17 +01:00
|
|
|
collisionbox = {0,0,0,0,0,0},
|
2017-01-26 18:59:57 +01:00
|
|
|
|
|
|
|
get_staticdata = get_staticdata,
|
|
|
|
on_activate = on_activate,
|
|
|
|
|
|
|
|
_lastpos={},
|
2017-01-25 11:45:17 +01:00
|
|
|
_thrower = nil, -- Player ObjectRef of the player who threw the ender pearl
|
|
|
|
}
|
2017-01-16 23:11:04 +01:00
|
|
|
|
2017-01-25 11:45:17 +01:00
|
|
|
-- Snowball and egg entity on_step()--> called when snowball is moving.
|
2017-01-16 23:11:04 +01:00
|
|
|
local on_step = function(self, dtime)
|
|
|
|
self.timer=self.timer+dtime
|
|
|
|
local pos = self.object:getpos()
|
|
|
|
local node = minetest.get_node(pos)
|
2017-01-26 19:05:25 +01:00
|
|
|
local def = minetest.registered_nodes[node.name]
|
2017-01-16 23:11:04 +01:00
|
|
|
|
2017-01-26 19:05:25 +01:00
|
|
|
-- Destroy when hitting a solid node
|
2017-01-25 11:45:17 +01:00
|
|
|
if self._lastpos.x~=nil then
|
2017-01-26 19:05:25 +01:00
|
|
|
if (def and def.walkable) or not def then
|
2017-01-25 11:45:17 +01:00
|
|
|
self.object:remove()
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self._lastpos={x=pos.x, y=pos.y, z=pos.z} -- Set _lastpos-->Node will be added at last pos outside the node
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Movement function of ender pearl
|
|
|
|
local pearl_on_step = function(self, dtime)
|
|
|
|
self.timer=self.timer+dtime
|
|
|
|
local pos = self.object:getpos()
|
|
|
|
local node = minetest.get_node(pos)
|
2017-01-26 19:05:25 +01:00
|
|
|
local def = minetest.registered_nodes[node.name]
|
2017-01-25 11:45:17 +01:00
|
|
|
|
2017-01-26 19:05:25 +01:00
|
|
|
-- Destroy when hitting a solid node
|
2017-01-25 11:45:17 +01:00
|
|
|
if self._lastpos.x~=nil then
|
2017-01-26 19:05:25 +01:00
|
|
|
if (def and def.walkable) or not def then
|
2017-01-25 11:45:17 +01:00
|
|
|
local player = minetest.get_player_by_name(self._thrower)
|
|
|
|
if player then
|
|
|
|
-- Teleport and hurt player
|
|
|
|
player:setpos(pos)
|
|
|
|
player:set_hp(player:get_hp() - 5)
|
|
|
|
end
|
2017-01-16 23:11:04 +01:00
|
|
|
self.object:remove()
|
2017-01-25 11:45:17 +01:00
|
|
|
return
|
2017-01-16 23:11:04 +01:00
|
|
|
end
|
|
|
|
end
|
2017-01-25 11:45:17 +01:00
|
|
|
self._lastpos={x=pos.x, y=pos.y, z=pos.z} -- Set lastpos-->Node will be added at last pos outside the node
|
2017-01-16 23:11:04 +01:00
|
|
|
end
|
|
|
|
|
2017-01-25 11:45:17 +01:00
|
|
|
snowball_ENTITY.on_step = on_step
|
|
|
|
egg_ENTITY.on_step = on_step
|
|
|
|
pearl_ENTITY.on_step = pearl_on_step
|
|
|
|
|
2017-01-16 23:11:04 +01:00
|
|
|
minetest.register_entity("mcl_throwing:snowball_entity", snowball_ENTITY)
|
|
|
|
minetest.register_entity("mcl_throwing:egg_entity", egg_ENTITY)
|
2017-01-25 11:45:17 +01:00
|
|
|
minetest.register_entity("mcl_throwing:ender_pearl_entity", pearl_ENTITY)
|
2017-01-16 23:11:04 +01:00
|
|
|
|
|
|
|
-- Snowball
|
|
|
|
minetest.register_craftitem("mcl_throwing:snowball", {
|
|
|
|
description = "Snowball",
|
|
|
|
inventory_image = "mcl_throwing_snowball.png",
|
|
|
|
stack_max = 16,
|
2017-02-14 23:18:23 +01:00
|
|
|
on_use = throw_function("mcl_throwing:snowball_entity"),
|
2017-01-16 23:11:04 +01:00
|
|
|
on_construct = function(pos)
|
|
|
|
pos.y = pos.y - 1
|
|
|
|
if minetest.get_node(pos).name == "default:dirt_with_grass" then
|
|
|
|
minetest.set_node(pos, {name="default:dirt_with_snow"})
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2017-01-25 11:45:17 +01:00
|
|
|
-- Egg
|
2017-01-16 23:11:04 +01:00
|
|
|
minetest.register_craftitem("mcl_throwing:egg", {
|
|
|
|
description = "Egg",
|
|
|
|
inventory_image = "mcl_throwing_egg.png",
|
|
|
|
stack_max = 16,
|
2017-02-14 23:18:23 +01:00
|
|
|
on_use = throw_function("mcl_throwing:egg_entity"),
|
2017-01-20 11:33:56 +01:00
|
|
|
groups = { craftitem = 1 },
|
2017-01-16 23:11:04 +01:00
|
|
|
})
|
2017-01-25 11:45:17 +01:00
|
|
|
|
|
|
|
-- Ender Pearl
|
|
|
|
minetest.register_craftitem("mcl_throwing:ender_pearl", {
|
|
|
|
description = "Ender Pearl",
|
|
|
|
wield_image = "mcl_throwing_ender_pearl.png",
|
|
|
|
inventory_image = "mcl_throwing_ender_pearl.png",
|
|
|
|
stack_max = 16,
|
2017-02-14 23:18:23 +01:00
|
|
|
on_use = throw_function("mcl_throwing:ender_pearl_entity"),
|
2017-01-25 11:45:17 +01:00
|
|
|
})
|
|
|
|
|