springboard.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. local function springboard_formspec(x, y, z)
  2. local formspec =
  3. 'formspec_version[3]'..
  4. 'size[8,6]'..
  5. 'textarea[.5,.5;7,2;;;Set the springiness here. A player will be boosted when the spring releases. (1 second after punching the node.) Directions are world based, and do not correlate to the direction the node is positioned or player orientation. Play around with the values to find something you like.]'..
  6. 'field[1,3;2,.5;x;X: (-10 - 10);'..x..']'..
  7. 'field[1,4;2,.5;y;Y: (7 - 25);'..y..']'..
  8. 'field[1,5;2,.5;z;Z: (-10 - 10);'..z..']'..
  9. 'button_exit[5,5;2,.5;save;Submit]'
  10. return formspec
  11. end
  12. local function math_clamp(val, lower, upper)
  13. if not lobby.is_integer(val) then
  14. val = 0
  15. end
  16. return math.max(lower, math.min(upper, val))
  17. end
  18. minetest.register_on_player_receive_fields(function(player, formname, fields)
  19. local name = player:get_player_name()
  20. if formname == 'platformer:springboard' then
  21. if fields.save then
  22. local pos = tasks.player_config[name]
  23. local meta = minetest.get_meta(pos)
  24. local vel_x = math_clamp(fields.x, -10,10)
  25. local vel_y = math_clamp(fields.y, 7,25)
  26. local vel_z = math_clamp(fields.z, -10,10)
  27. meta:set_string('x', vel_x)
  28. meta:set_string('y', vel_y)
  29. meta:set_string('z', vel_z)
  30. end
  31. end
  32. end)
  33. minetest.register_node('platformer:springboard_sprung', {
  34. description = 'Springboard',
  35. drawtype = 'mesh',
  36. mesh = 'platformer_spring_sprung.obj',
  37. tiles = {'platformer_spring_sprung.png'},
  38. paramtype = 'light',
  39. paramtype2 = 'facedir',
  40. groups = {breakable=1},
  41. on_construct = function(pos)
  42. local meta = minetest.get_meta(pos)
  43. meta:set_string('x', 0)
  44. meta:set_string('y', 7)
  45. meta:set_string('z', 0)
  46. end,
  47. on_punch = function(pos, node)
  48. local timer = minetest.get_node_timer(pos)
  49. minetest.swap_node(pos, {name = 'platformer:springboard_loaded', param2 = node.param2})
  50. timer:start(1)
  51. end,
  52. on_rightclick = function(pos, node, clicker, itemstack)
  53. local item = itemstack:get_name()
  54. local name = clicker:get_player_name()
  55. if item == 'tasks:configurator' then
  56. if not minetest.is_protected(pos, name) or minetest.check_player_privs(name, {server = true}) then
  57. tasks.player_config[name] = pos
  58. local meta = minetest.get_meta(pos)
  59. local x_vel = meta:get_string('x')
  60. local y_vel = meta:get_string('y')
  61. local z_vel = meta:get_string('z')
  62. minetest.show_formspec(name, 'platformer:springboard', springboard_formspec(x_vel, y_vel, z_vel))
  63. end
  64. end
  65. end,
  66. })
  67. minetest.register_node('platformer:springboard_loaded', {
  68. description = 'Springboard',
  69. drawtype = 'mesh',
  70. mesh = 'platformer_spring_loaded.obj',
  71. tiles = {'platformer_spring_loaded.png'},
  72. paramtype = 'light',
  73. paramtype2 = 'facedir',
  74. selection_box = {type = 'fixed',
  75. fixed = {
  76. {-.5, -.5, -.5, .5, 0, .5},}},
  77. collision_box = {type = 'fixed',
  78. fixed = {
  79. {-.5, -.5, -.5, .5, 0, .5},}},
  80. groups = {breakable=1, not_in_creative_inventory=1},
  81. drop = 'platformer:springboard_sprung',
  82. on_timer = function(pos)
  83. local objs = minetest.get_objects_inside_radius(pos, .8)
  84. for _, obj in pairs(objs) do
  85. if obj:is_player() then
  86. local meta = minetest.get_meta(pos)
  87. local x_vel = tonumber(meta:get_string('x')) or 0
  88. local y_vel = tonumber(meta:get_string('y')) or 0
  89. local z_vel = tonumber(meta:get_string('z')) or 0
  90. obj:add_velocity({x=x_vel, y=y_vel, z=z_vel})
  91. end
  92. end
  93. local node = minetest.get_node(pos)
  94. minetest.swap_node(pos, {name = 'platformer:springboard_sprung', param2 = node.param2})
  95. minetest.sound_play('platformer_spring_launch', {pos = pos, gain = 2, max_hear_distance = 14})
  96. end
  97. })