epic/blocks/set_gravity.lua

80 lines
1.9 KiB
Lua
Raw Normal View History

2019-09-30 15:44:17 +02:00
local use_player_monoids = minetest.global_exists("player_monoids")
2019-10-01 07:56:47 +02:00
local update_formspec = function(meta)
2019-09-30 15:44:17 +02:00
local gravity = meta:get_string("gravity")
meta:set_string("infotext", "Set-gravity block: '" .. gravity .. "'")
meta:set_string("formspec", "size[8,2;]" ..
-- col 1
2019-12-26 20:07:36 +01:00
"field[0.2,0.5;8,1;gravity;Gravity-multiplier;" .. gravity .. "]" ..
2019-09-30 15:44:17 +02:00
-- col 2
"button_exit[0.1,1.5;8,1;save;Save]" ..
"")
end
2019-09-30 20:53:50 +02:00
minetest.register_node("epic:set_gravity", {
2019-10-02 17:46:58 +02:00
description = "Epic set gravity block",
2019-09-30 15:44:17 +02:00
tiles = {
"epic_node_bg.png",
"epic_node_bg.png",
"epic_node_bg.png",
"epic_node_bg.png",
"epic_node_bg.png",
2019-10-22 19:51:35 +02:00
"epic_node_bg.png^epic_gravity.png",
2019-09-30 15:44:17 +02:00
},
paramtype2 = "facedir",
groups = {cracky=3,oddly_breakable_by_hand=3,epic=1},
on_rotate = screwdriver.rotate_simple,
2019-10-01 07:56:47 +02:00
after_place_node = function(pos)
2019-09-30 15:44:17 +02:00
local meta = minetest.get_meta(pos)
meta:set_string("gravity", "9.81")
update_formspec(meta, pos)
end,
2019-10-01 07:56:47 +02:00
on_receive_fields = function(pos, _, fields, sender)
2019-09-30 15:44:17 +02:00
local meta = minetest.get_meta(pos);
if not sender or minetest.is_protected(pos, sender:get_player_name()) then
-- not allowed
return
end
if fields.save then
meta:set_string("gravity", fields.gravity or "9.81")
update_formspec(meta, pos)
end
end,
epic = {
2019-10-01 07:56:47 +02:00
on_enter = function(_, meta, player, ctx)
2019-12-26 20:07:36 +01:00
local gravity = tonumber(meta:get_string("gravity")) or 1
2019-09-30 15:44:17 +02:00
if use_player_monoids then
player_monoids.gravity:add_change(player, gravity, "epic:set_gravity")
else
player:set_physics_override({ gravity = gravity })
end
ctx.next()
end
}
})
2019-11-08 10:50:17 +01:00
2020-02-12 21:11:00 +01:00
local function cleanup(playername)
local player = minetest.get_player_by_name(playername)
if player then
if use_player_monoids then
player_monoids.gravity:del_change(player, "epic:set_gravity")
else
player:set_physics_override({ gravity = 1 })
2019-11-08 10:50:17 +01:00
end
end
2020-02-12 21:11:00 +01:00
end
epic.register_hook({
on_epic_exit = cleanup,
on_epic_abort = cleanup
2019-11-08 10:50:17 +01:00
})