diff --git a/README.md b/README.md index b1aae44..4916741 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ https://forum.minetest.net/viewtopic.php?f=9&t=9234 Change log: +- 1.5 - Use moveresult when throwing teleport potion, has switcharoo effect - 1.4 - Change Pad texture so it's not as contrasting, check for attached player before tp. - 1.3 - Added some formspec checks and switch to enable old teleport pad texture (thanks mazes 80) - 1.2 - New teleport pad texture, code tweaks to work with minetest 5.x diff --git a/init.lua b/init.lua index 617eaaf..5f4ceef 100644 --- a/init.lua +++ b/init.lua @@ -40,16 +40,14 @@ local function check_coordinates(str) -- check coords if x == nil or string.len(x) > 6 or y == nil or string.len(y) > 6 - or z == nil or string.len(z) > 6 then - return nil - end + or z == nil or string.len(z) > 6 then return end -- convert string coords to numbers x = tonumber(x) ; y = tonumber(y) ; z = tonumber(z) -- are coords in map range ? if x > dist or x < -dist or y > dist or y < -dist or z > dist or z < -dist then - return nil + return end -- return ok coords @@ -141,12 +139,14 @@ core.register_node("teleport_potion:portal", { local function throw_potion(itemstack, player) - local playerpos = player:get_pos() + local pos = player:get_pos() + local prop = player:get_properties() + local y_off = prop.eye_height or 1.5 local obj = core.add_entity({ - x = playerpos.x, - y = playerpos.y + 1.5, - z = playerpos.z + x = pos.x, + y = pos.y + y_off, + z = pos.z }, "teleport_potion:potion_entity") local dir = player:get_look_dir() @@ -163,66 +163,50 @@ end local potion_entity = { initial_properties = { - physical = true, + physical = true, static_save = false, pointable = false, visual = "sprite", visual_size = {x = 1.0, y = 1.0}, textures = {"teleport_potion_potion.png"}, - collisionbox = {-0.1,-0.1,-0.1,0.1,0.1,0.1}, + collisionbox = {-0.2,-0.2,-0.2,0.2,0.2,0.2}, }, - lastpos = {}, player = "" } -potion_entity.on_step = function(self, dtime) +potion_entity.on_step = function(self, dtime, moveresult) - if not self.player then + if not self.player or self.player == "" then + self.object:remove() ; return + end + + if moveresult.collides then + + -- round up coords to fix glitching through doors + local pos = vector.round(self.object:get_pos()) + local oldpos = self.player:get_pos() + + -- if we hit a player or mob then switch positions + local def = moveresult.collisions and moveresult.collisions[1] + + if def.object then + def.object:set_pos(oldpos) + end + + -- play sound and disappearing particle effect at current position + core.sound_play("portal_close", { + pos = oldpos, gain = 1.0, max_hear_distance = 7}, true) + + effect(oldpos, 25, "teleport_potion_particle.png", 2, 2, 1, -10, 15) + + -- teleport to new position, play sound and show appear effect + self.player:set_pos(pos) + + core.sound_play("portal_close", { + pos = pos, gain = 1.0, max_hear_distance = 7}, true) + + effect(pos, 20, "teleport_potion_particle.png", 2, 2, 1, 10, 15) self.object:remove() - - return end - - local pos = self.object:get_pos() - - if self.lastpos.x ~= nil then - - local vel = self.object:get_velocity() - - -- only when potion hits something physical - if vel.x == 0 or vel.y == 0 or vel.z == 0 then - - if self.player ~= "" then - - -- round up coords to fix glitching through doors - self.lastpos = vector.round(self.lastpos) - - local oldpos = self.player:get_pos() - - -- play sound and disappearing particle effect at current position - core.sound_play("portal_close", { - pos = oldpos, gain = 1.0, max_hear_distance = 5}, true) - - oldpos.y = oldpos.y + 1 - - effect(oldpos, 25, "teleport_potion_particle.png", 2, 2, 1, -10, 15) - - -- teleport to new position, play sound and show appear effect - self.player:set_pos(self.lastpos) - - core.sound_play("portal_close", { - pos = self.lastpos, gain = 1.0, max_hear_distance = 5}, true) - - effect(self.lastpos, 20, "teleport_potion_particle.png", 2, 2, 1, 10, 15) - end - - self.object:remove() - - return - - end - end - - self.lastpos = pos end core.register_entity("teleport_potion:potion_entity", potion_entity)