use moveresult, add switcharoo effect

This commit is contained in:
tenplus1 2025-06-11 10:03:37 +01:00
parent 355be56a2c
commit 37a84c6f71
2 changed files with 42 additions and 57 deletions

View file

@ -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)