From caf05770c5e0331b065f4fe9c4004b755f550de3 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Wed, 25 Jan 2017 11:45:17 +0100 Subject: [PATCH] Make ender pearl throwable and teleport player --- mods/mcl_ender_pearl/init.lua | 7 -- mods/mcl_ender_pearl/mod.conf | 1 - .../textures/mcl_throwing_ender_pearl.png} | Bin mods/mcl_throwing/throwable.lua | 68 +++++++++++++++--- 4 files changed, 59 insertions(+), 17 deletions(-) delete mode 100644 mods/mcl_ender_pearl/init.lua delete mode 100644 mods/mcl_ender_pearl/mod.conf rename mods/{mcl_ender_pearl/textures/mcl_ender_pearl_ender_pearl.png => mcl_throwing/textures/mcl_throwing_ender_pearl.png} (100%) diff --git a/mods/mcl_ender_pearl/init.lua b/mods/mcl_ender_pearl/init.lua deleted file mode 100644 index 98aa27b52e..0000000000 --- a/mods/mcl_ender_pearl/init.lua +++ /dev/null @@ -1,7 +0,0 @@ -minetest.register_craftitem("mcl_ender_pearl:ender_pearl", { - description = "Ender Pearl", - wield_image = "mcl_ender_pearl_ender_pearl.png", - inventory_image = "mcl_ender_pearl_ender_pearl.png", - stack_max = 16, -}) - diff --git a/mods/mcl_ender_pearl/mod.conf b/mods/mcl_ender_pearl/mod.conf deleted file mode 100644 index 6f190736b2..0000000000 --- a/mods/mcl_ender_pearl/mod.conf +++ /dev/null @@ -1 +0,0 @@ -name = mcl_ender_pearl diff --git a/mods/mcl_ender_pearl/textures/mcl_ender_pearl_ender_pearl.png b/mods/mcl_throwing/textures/mcl_throwing_ender_pearl.png similarity index 100% rename from mods/mcl_ender_pearl/textures/mcl_ender_pearl_ender_pearl.png rename to mods/mcl_throwing/textures/mcl_throwing_ender_pearl.png diff --git a/mods/mcl_throwing/throwable.lua b/mods/mcl_throwing/throwable.lua index c85e508981..47760061b3 100644 --- a/mods/mcl_throwing/throwable.lua +++ b/mods/mcl_throwing/throwable.lua @@ -1,12 +1,13 @@ -- --- Snowballs +-- Snowballs and other throwable items -- -local GRAVITY = 9.81 +local GRAVITY = tonumber(minetest.setting_get("movement_gravity")) local snowball_VELOCITY=19 local egg_VELOCITY=19 +local pearl_VELOCITY=19 ---Shoot snowball. +--Shoot item local throw_function = function (entity_name, velocity) local func = function(item, player, pointed_thing) local playerpos=player:getpos() @@ -14,6 +15,7 @@ local throw_function = function (entity_name, velocity) local dir=player:get_look_dir() 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}) + obj:get_luaentity()._thrower = player:get_player_name() item:take_item() return item end @@ -25,34 +27,71 @@ local snowball_ENTITY={ physical = false, timer=0, textures = {"mcl_throwing_snowball.png"}, - lastpos={}, + _lastpos={}, collisionbox = {0,0,0,0,0,0}, } local egg_ENTITY={ physical = false, timer=0, textures = {"mcl_throwing_egg.png"}, - lastpos={}, + _lastpos={}, collisionbox = {0,0,0,0,0,0}, } +-- Ender pearl entity +local pearl_ENTITY={ + physical = false, + timer=0, + textures = {"mcl_throwing_ender_pearl.png"}, + _lastpos={}, + collisionbox = {0,0,0,0,0,0}, + _thrower = nil, -- Player ObjectRef of the player who threw the ender pearl +} --- Snowball_entity.on_step()--> called when snowball is moving. +-- Snowball and egg entity on_step()--> called when snowball is moving. local on_step = function(self, dtime) self.timer=self.timer+dtime local pos = self.object:getpos() local node = minetest.get_node(pos) - --Become item when hitting a node. - if self.lastpos.x~=nil then --If there is no lastpos for some reason. + -- Remove when hitting a node. + if self._lastpos.x~=nil then if node.name ~= "air" then 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 + 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) + + --Become item when hitting a node. + if self._lastpos.x~=nil then + if node.name ~= "air" then + 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 + 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 + +snowball_ENTITY.on_step = on_step +egg_ENTITY.on_step = on_step +pearl_ENTITY.on_step = pearl_on_step + minetest.register_entity("mcl_throwing:snowball_entity", snowball_ENTITY) minetest.register_entity("mcl_throwing:egg_entity", egg_ENTITY) +minetest.register_entity("mcl_throwing:ender_pearl_entity", pearl_ENTITY) -- Snowball minetest.register_craftitem("mcl_throwing:snowball", { @@ -68,6 +107,7 @@ minetest.register_craftitem("mcl_throwing:snowball", { end, }) +-- Egg minetest.register_craftitem("mcl_throwing:egg", { description = "Egg", inventory_image = "mcl_throwing_egg.png", @@ -75,3 +115,13 @@ minetest.register_craftitem("mcl_throwing:egg", { on_use = throw_function("mcl_throwing:egg_entity", egg_VELOCITY), groups = { craftitem = 1 }, }) + +-- 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, + on_use = throw_function("mcl_throwing:ender_pearl_entity", pearl_VELOCITY), +}) +