Code tidy and particle effect added

This commit is contained in:
TenPlus1 2015-09-08 16:56:22 +01:00
parent 4ed639de55
commit 5d74e552f9
2 changed files with 81 additions and 48 deletions

129
init.lua
View file

@ -1,19 +1,20 @@
--= Teleport Potion mod 0.3 by TenPlus1 --= Teleport Potion mod 0.4 by TenPlus1
--= Create potion/pad, right-click to enter coords and walk into the blue light, -- Create teleport potion or pad, place then right-click to enter coords
--= Portal closes after 10 seconds, pad remains... SFX are license Free... -- and step onto pad or walk into the blue portal light, portal closes after
-- 10 seconds, pad remains... SFX are license Free...
teleport = {} teleport = {}
-- Teleport Portal recipe -- teleport portal recipe
minetest.register_craft({ minetest.register_craft({
output = 'teleport_potion:potion', output = 'teleport_potion:potion',
type = "shapeless", type = "shapeless",
recipe = {'vessels:glass_bottle', 'default:diamondblock'} recipe = {'vessels:glass_bottle', 'default:diamondblock'}
}) })
-- Teleport Pad recipe -- teleport pad recipe
minetest.register_craft({ minetest.register_craft({
output = 'teleport_potion:pad', output = 'teleport_potion:pad',
recipe = { recipe = {
@ -23,10 +24,10 @@ minetest.register_craft({
} }
}) })
-- Default coords -- default coords (from static spawnpoint or default values at end)
teleport.default = {x = 0, y = 0, z = 0} teleport.default = (minetest.setting_get_pos("static_spawnpoint") or {x = 0, y = 2, z = 0})
-- Portal -- teleport portal
minetest.register_node("teleport_potion:portal", { minetest.register_node("teleport_potion:portal", {
drawtype = "plantlike", drawtype = "plantlike",
tiles = { tiles = {
@ -46,14 +47,14 @@ minetest.register_node("teleport_potion:portal", {
buildable_to = true, buildable_to = true,
waving = 1, waving = 1,
sunlight_propagates = true, sunlight_propagates = true,
damage_per_second = 1, -- Walking into portal also hurts player damage_per_second = 1, -- walking into portal also hurts player
-- Start timer when portal appears -- start timer when portal appears
on_construct = function(pos) on_construct = function(pos)
minetest.get_node_timer(pos):start(10) minetest.get_node_timer(pos):start(10)
end, end,
-- Remove portal after 10 seconds -- remove portal after 10 seconds
on_timer = function(pos) on_timer = function(pos)
minetest.sound_play("portal_close", { minetest.sound_play("portal_close", {
pos = pos, pos = pos,
@ -64,7 +65,7 @@ minetest.register_node("teleport_potion:portal", {
end, end,
}) })
-- Potion -- teleport potion
minetest.register_node("teleport_potion:potion", { minetest.register_node("teleport_potion:potion", {
tile_images = {"pad.png"}, tile_images = {"pad.png"},
drawtype = "signlike", drawtype = "signlike",
@ -75,7 +76,6 @@ minetest.register_node("teleport_potion:potion", {
description="Teleport Potion (place and right-click to enchant location)", description="Teleport Potion (place and right-click to enchant location)",
inventory_image = "potion.png", inventory_image = "potion.png",
wield_image = "potion.png", wield_image = "potion.png",
metadata_name = "sign",
groups = {snappy = 3, dig_immediate = 3}, groups = {snappy = 3, dig_immediate = 3},
selection_box = {type = "wallmounted"}, selection_box = {type = "wallmounted"},
@ -83,41 +83,44 @@ minetest.register_node("teleport_potion:potion", {
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
-- Text entry formspec -- text entry formspec
meta:set_string("formspec", "field[text;;${text}]") meta:set_string("formspec", "field[text;;${text}]")
meta:set_string("infotext", "Enter teleport coords (e.g 200,20,-200)") meta:set_string("infotext", "Enter teleport coords (e.g 200,20,-200)")
meta:set_string("text", teleport.default.x..","..teleport.default.y..","..teleport.default.z) meta:set_string("text", teleport.default.x..","..teleport.default.y..","..teleport.default.z)
-- Load with default coords -- set default coords
meta:set_float("enabled", -1)
meta:set_float("x", teleport.default.x) meta:set_float("x", teleport.default.x)
meta:set_float("y", teleport.default.y) meta:set_float("y", teleport.default.y)
meta:set_float("z", teleport.default.z) meta:set_float("z", teleport.default.z)
end, end,
-- Right-click to enter new coords -- right-click to enter new coords
on_right_click = function(pos, placer) on_right_click = function(pos, placer)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
end, end,
-- Once entered, check coords and teleport, otherwise return potion -- check if coords ok then open portal, otherwise return potion
on_receive_fields = function(pos, formname, fields, sender) on_receive_fields = function(pos, formname, fields, sender)
local coords = teleport.coordinates(fields.text) local coords = teleport.coordinates(fields.text)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local name = sender:get_player_name() local name = sender:get_player_name()
if coords then if coords then
minetest.add_node(pos, {name = "teleport_potion:portal"}) minetest.add_node(pos, {name = "teleport_potion:portal"})
local newmeta = minetest.get_meta(pos) local newmeta = minetest.get_meta(pos)
-- set portal destination
newmeta:set_float("x", coords.x) newmeta:set_float("x", coords.x)
newmeta:set_float("y", coords.y) newmeta:set_float("y", coords.y)
newmeta:set_float("z", coords.z) newmeta:set_float("z", coords.z)
newmeta:set_string("text", fields.text) newmeta:set_string("text", fields.text)
-- portal open effect and sound
effect(pos)
minetest.sound_play("portal_open", { minetest.sound_play("portal_open", {
pos = pos, pos = pos,
gain = 1.0, gain = 1.0,
@ -132,7 +135,7 @@ minetest.register_node("teleport_potion:potion", {
end, end,
}) })
-- Pad -- teleport pad
minetest.register_node("teleport_potion:pad", { minetest.register_node("teleport_potion:pad", {
tile_images = {"padd.png"}, tile_images = {"padd.png"},
drawtype = 'nodebox', drawtype = 'nodebox',
@ -143,9 +146,8 @@ minetest.register_node("teleport_potion:pad", {
description="Teleport Pad (place and right-click to enchant location)", description="Teleport Pad (place and right-click to enchant location)",
inventory_image = "padd.png", inventory_image = "padd.png",
wield_image = "padd.png", wield_image = "padd.png",
metadata_name = "sign",
light_source = 5, light_source = 5,
groups = {snappy = 3, dig_immediate = 3}, groups = {snappy = 3},
node_box = { node_box = {
type = "wallmounted", type = "wallmounted",
wall_top = {-0.5, 0.4375, -0.5, 0.5, 0.5, 0.5}, wall_top = {-0.5, 0.4375, -0.5, 0.5, 0.5, 0.5},
@ -158,24 +160,23 @@ minetest.register_node("teleport_potion:pad", {
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
-- Text entry formspec -- text entry formspec
meta:set_string("formspec", "field[text;;${text}]") meta:set_string("formspec", "field[text;;${text}]")
meta:set_string("infotext", "Enter teleport coords (e.g 200,20,-200)") meta:set_string("infotext", "Enter teleport coords (e.g 200,20,-200)")
meta:set_string("text", teleport.default.x..","..teleport.default.y..","..teleport.default.z) meta:set_string("text", teleport.default.x..","..teleport.default.y..","..teleport.default.z)
-- Load with default coords -- set default coords
meta:set_float("enabled", -1)
meta:set_float("x", teleport.default.x) meta:set_float("x", teleport.default.x)
meta:set_float("y", teleport.default.y) meta:set_float("y", teleport.default.y)
meta:set_float("z", teleport.default.z) meta:set_float("z", teleport.default.z)
end, end,
-- Right-click to enter new coords -- right-click to enter new coords
on_right_click = function(pos, placer) on_right_click = function(pos, placer)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
end, end,
-- Once entered, check coords and teleport, otherwise return potion -- once entered, check coords, if ok then return potion
on_receive_fields = function(pos, formname, fields, sender) on_receive_fields = function(pos, formname, fields, sender)
local coords = teleport.coordinates(fields.text) local coords = teleport.coordinates(fields.text)
@ -187,7 +188,7 @@ minetest.register_node("teleport_potion:pad", {
return return
end end
if coords then if coords then
local newmeta = minetest.get_meta(pos) local newmeta = minetest.get_meta(pos)
@ -209,60 +210,92 @@ minetest.register_node("teleport_potion:pad", {
end, end,
}) })
-- Check coords
teleport.coordinates = function(str) teleport.coordinates = function(str)
if not str or str == "" then return nil end if not str or str == "" then return nil end
-- Get coords from string -- get coords from string
local x, y, z = string.match(str, "^(-?%d+),(-?%d+),(-?%d+)") local x, y, z = string.match(str, "^(-?%d+),(-?%d+),(-?%d+)")
-- Check coords -- check coords
if x == nil or string.len(x) > 6 if x == nil or string.len(x) > 6
or y == nil or string.len(y) > 6 or y == nil or string.len(y) > 6
or z == nil or string.len(z) > 6 then or z == nil or string.len(z) > 6 then
return nil return nil
end end
-- Convert string coords to numbers -- convert string coords to numbers
x = x + 0.0 x = tonumber(x)
y = y + 0.0 y = tonumber(y)
z = z + 0.0 z = tonumber(z)
-- Are coords in map range ? -- are coords in map range ?
if x > 30900 or x < -30900 if x > 30900 or x < -30900
or y > 30900 or y < -30900 or y > 30900 or y < -30900
or z > 30900 or z < -30900 then or z > 30900 or z < -30900 then
return nil return nil
end end
-- Return ok coords -- return ok coords
return {x = x, y = y, z = z} return {x = x, y = y, z = z}
end end
-- Has player walked inside portal -- particle effects
function effect(pos)
minetest.add_particlespawner({
amount = 20,
time = 0.25,
minpos = pos,
maxpos = pos,
minvel = {x = -2, y = -2, z = -2},
maxvel = {x = 2, y = 2, z = 2},
minacc = {x = -4, y = -4, z = -4},
maxacc = {x = 4, y = 4, z = 4},
minexptime = 0.1,
maxexptime = 1,
minsize = 0.5,
maxsize = 1,
texture = "particle.png",
})
end
-- check pad and teleport objects on top
minetest.register_abm({ minetest.register_abm({
nodenames = {"teleport_potion:portal", "teleport_potion:pad"}, nodenames = {"teleport_potion:portal", "teleport_potion:pad"},
interval = 1.0, interval = 1,
chance = 1, chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider) action = function(pos, node, active_object_count, active_object_count_wider)
-- check objects inside pad/portal
local objs = minetest.get_objects_inside_radius(pos, 1) local objs = minetest.get_objects_inside_radius(pos, 1)
local meta, target_coords if #objs == 0 then return end
-- get coords from pad/portal
local meta = minetest.get_meta(pos)
local target_coords = {
x = meta:get_float("x"),
y = meta:get_float("y"),
z = meta:get_float("z")
}
for k, player in pairs(objs) do for k, player in pairs(objs) do
if player:get_player_name() then if player:get_player_name() then
meta = minetest.get_meta(pos)
target_coords={ -- play sound on portal end
x=meta:get_float("x"),
y=meta:get_float("y"),
z=meta:get_float("z")
}
minetest.sound_play("portal_close", { minetest.sound_play("portal_close", {
pos = pos, pos = pos,
gain = 1.0, gain = 1.0,
max_hear_distance = 5 max_hear_distance = 5
}) })
-- move player/object
player:moveto(target_coords, false) player:moveto(target_coords, false)
-- paricle effects on arrival
effect(target_coords)
-- play sound on destination end
minetest.sound_play("portal_close", { minetest.sound_play("portal_close", {
pos = target_coords, pos = target_coords,
gain = 1.0, gain = 1.0,
@ -270,5 +303,5 @@ minetest.register_abm({
}) })
end end
end end
end end
}) })

BIN
textures/particle.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 173 B