platforms.lua 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. minetest.register_entity('platformer:platform', {
  2. initial_properties = {
  3. visual = 'mesh',
  4. mesh = 'platformer_platform_x1.obj',
  5. visual_size = {x = 10, y = 10},
  6. collisionbox = {-1, .25, -.5, 1, .5, .5},
  7. physical = true,
  8. textures = {'platformer_platform_xz.png'}
  9. },
  10. init = function(self, texture)
  11. self.data.texture = texture
  12. self.object:set_properties({textures = texture})
  13. end,
  14. on_activate = function(self, staticdata)
  15. local rot = self.object:get_rotation()
  16. if rot.y == 0 then
  17. self.object:set_rotation({y= 3.1459, x= 0, z= 0})
  18. self.object:set_velocity({x= -1.5, y= 0, z= 0})
  19. elseif rot.y <= 3.146 and rot.y >= 3.145 then
  20. self.object:set_rotation({y= 0, x= 0, z= 0})
  21. self.object:set_velocity({x= 1.5, y= 0, z= 0})
  22. elseif rot.y <= 1.573 and rot.y >= 1.572 then
  23. self.object:set_rotation({y= -1.57295, x= 0, z= 0})
  24. self.object:set_velocity({x= 0, y= 0, z= -1.5})
  25. elseif rot.y <= -1.572 and rot.y >= -1.573 then
  26. self.object:set_rotation({y= 1.57295, x= 0, z= 0})
  27. self.object:set_velocity({x= 0, y= 0, z= 1.5})
  28. end
  29. end,
  30. on_punch = function(self, puncher)
  31. local rot = self.object:get_rotation()
  32. if rot.y == 0 then
  33. self.object:set_velocity({x= 1.5, y= 0, z= 0})
  34. elseif rot.y <= 3.146 and rot.y >= 3.145 then
  35. self.object:set_velocity({x= -1.5, y= 0, z= 0})
  36. elseif rot.y <= 1.573 and rot.y >= 1.572 then
  37. self.object:set_velocity({x= 0, y= 0, z= 1.5})
  38. elseif rot.y <= -1.572 and rot.y >= -1.573 then
  39. self.object:set_velocity({x= 0, y= 0, z= -1.5})
  40. end
  41. end,
  42. on_rightclick = function(self, clicker)
  43. local tool = clicker:get_wielded_item():to_string()
  44. if tool == 'creative:tool_breaking' then
  45. self.object:remove()
  46. end
  47. end,
  48. on_step = function(self, dtime, moveresult)
  49. --[[local pos = self.object:get_pos() -- Code doesn't reliably move player with the platform.
  50. print (dump(self.object:get_velocity()))
  51. local vel = vector.divide(self.object:get_velocity(), 2)
  52. print (dump(vel))
  53. for _, player in pairs(minetest.get_connected_players()) do
  54. local player_pos = player:get_pos()
  55. local p2p_distance = vector.distance(player_pos, pos)
  56. if p2p_distance < 2 and player_pos.y > pos.y then
  57. print 'moving player'
  58. player:add_velocity(vel)
  59. --player:set_attach(self.object)
  60. end
  61. end
  62. --]]
  63. if moveresult.collides then
  64. local rot = self.object:get_rotation()
  65. if rot.y == 0 then
  66. self.object:set_rotation({y= 3.1459, x= 0, z= 0})
  67. self.object:set_velocity({x= -1.5, y= 0, z= 0})
  68. elseif rot.y <= 3.146 and rot.y >= 3.145 then
  69. self.object:set_rotation({y= 0, x= 0, z= 0})
  70. self.object:set_velocity({x= 1.5, y= 0, z= 0})
  71. elseif rot.y <= 1.573 and rot.y >= 1.572 then
  72. self.object:set_rotation({y= -1.57295, x= 0, z= 0})
  73. self.object:set_velocity({x= 0, y= 0, z= -1.5})
  74. elseif rot.y <= -1.572 and rot.y >= -1.573 then
  75. self.object:set_rotation({y= 1.57295, x= 0, z= 0})
  76. self.object:set_velocity({x= 0, y= 0, z= 1.5})
  77. end
  78. end
  79. end,
  80. })
  81. minetest.register_craftitem('platformer:platform_placer', {
  82. description = 'Places moving platforms.',
  83. inventory_image = 'platformer_placer_xz.png',
  84. on_place = function(itemstack, placer, pointed_thing)
  85. local pos = pointed_thing.above
  86. if not minetest.is_protected(pos, placer:get_player_name()) then
  87. local dir = minetest.dir_to_facedir(placer:get_look_dir())
  88. if dir == 0 then
  89. local platform = minetest.add_entity(pos, 'platformer:platform')
  90. platform:set_rotation({y= 1.57295, x= 0, z= 0})
  91. platform:set_velocity({x= 0, y= 0, z= 1})
  92. platform:set_properties({collisionbox = {-.5, .25, -1, .5, .5, 1}})
  93. elseif dir == 1 then
  94. local platform = minetest.add_entity(pos, 'platformer:platform')
  95. platform:set_rotation({y= 0, x= 0, z= 0})
  96. platform:set_velocity({x= 1, y= 0, z= 0})
  97. elseif dir == 2 then
  98. local platform = minetest.add_entity(pos, 'platformer:platform')
  99. platform:set_rotation({y= -1.57295, x= 0, z= 0})
  100. platform:set_velocity({x= 0, y= 0, z= -1})
  101. platform:set_properties({collisionbox = {-.5, .25, -1, .5, .5, 1}})
  102. elseif dir == 3 then
  103. local platform = minetest.add_entity(pos, 'platformer:platform')
  104. platform:set_rotation({y= 3.1459, x= 0, z= 0})
  105. platform:set_velocity({x= -1, y= 0, z= 0})
  106. end
  107. end
  108. end,
  109. })
  110. --All this code should be removed, once I figure out how to port the old items over to the new.
  111. local x_box = {
  112. type = 'fixed',
  113. fixed = {
  114. {-1, .25, -.5625, 1, .5, .5625},},}
  115. local y_box = {
  116. type = 'fixed',
  117. fixed = {
  118. {-.5625, .25, -.5625, .5625, .5, .5625},},}
  119. local z_box = {
  120. type = 'fixed',
  121. fixed = {
  122. {-.5625, .25, -1, .5625, .5, 1},},}
  123. minetest.register_node('platformer:x1', {
  124. description = 'X axis (positive)',
  125. drawtype = 'mesh',
  126. mesh = 'platformer_platform_x1.obj',
  127. tiles = {'platformer_platform_xz.png'},
  128. light_source = 2,
  129. selection_box = x_box,
  130. collision_box = x_box,
  131. groups = {breakable = 1, not_in_creative_inventory=1},
  132. on_construct = function(pos)
  133. local timer = minetest.get_node_timer(pos)
  134. timer:start(1.5)
  135. end,
  136. on_timer = function(pos)
  137. local new_pos = {x=pos.x+1, y=pos.y, z=pos.z}
  138. local new_node = minetest.get_node(new_pos).name
  139. if new_node == 'air' then
  140. minetest.set_node(new_pos, {name='platformer:x1'})
  141. minetest.set_node(pos, {name='air'})
  142. else
  143. minetest.set_node(pos, {name='platformer:x2'})
  144. end
  145. end
  146. })
  147. minetest.register_node('platformer:x2', {
  148. description = 'X axis (negative)',
  149. drawtype = 'mesh',
  150. mesh = 'platformer_platform_x2.obj',
  151. tiles = {'platformer_platform_xz.png'},
  152. light_source = 2,
  153. selection_box = x_box,
  154. collision_box = x_box,
  155. groups = {breakable = 1, not_in_creative_inventory=1},
  156. drops = 'platformer:x1',
  157. on_construct = function(pos)
  158. local timer = minetest.get_node_timer(pos)
  159. timer:start(1.5)
  160. end,
  161. on_timer = function(pos)
  162. local new_pos = {x=pos.x-1, y=pos.y, z=pos.z}
  163. local new_node = minetest.get_node(new_pos).name
  164. if new_node == 'air' then
  165. minetest.set_node(new_pos, {name='platformer:x2'})
  166. minetest.set_node(pos, {name='air'})
  167. else
  168. minetest.set_node(pos, {name='platformer:x1'})
  169. end
  170. end
  171. })
  172. minetest.register_node('platformer:y1', {
  173. description = 'y axis (positive)',
  174. drawtype = 'mesh',
  175. mesh = 'platformer_platform_y.obj',
  176. tiles = {'platformer_platform_y.png'},
  177. climbable = true,
  178. walkable = false,
  179. light_source = 2,
  180. selection_box = y_box,
  181. collision_box = y_box,
  182. groups = {breakable = 1, not_in_creative_inventory=1},
  183. on_construct = function(pos)
  184. local timer = minetest.get_node_timer(pos)
  185. timer:start(1.5)
  186. end,
  187. on_timer = function(pos)
  188. local new_pos = {x=pos.x, y=pos.y+1, z=pos.z}
  189. local new_node = minetest.get_node(new_pos).name
  190. if new_node == 'air' then
  191. minetest.set_node(new_pos, {name='platformer:y1'})
  192. minetest.set_node(pos, {name='air'})
  193. else
  194. minetest.set_node(pos, {name='platformer:y2'})
  195. end
  196. end
  197. })
  198. minetest.register_node('platformer:y2', {
  199. description = 'y axis (negative)',
  200. drawtype = 'mesh',
  201. mesh = 'platformer_platform_y.obj',
  202. tiles = {'platformer_platform_y.png'},
  203. climbable = true,
  204. walkable = false,
  205. light_source = 2,
  206. selection_box = y_box,
  207. collision_box = y_box,
  208. groups = {breakable = 1, not_in_creative_inventory=1},
  209. drops = 'platformer:y1',
  210. on_construct = function(pos)
  211. local timer = minetest.get_node_timer(pos)
  212. timer:start(1.5)
  213. end,
  214. on_timer = function(pos)
  215. local new_pos = {x=pos.x, y=pos.y-1, z=pos.z}
  216. local new_node = minetest.get_node(new_pos).name
  217. if new_node == 'air' then
  218. minetest.set_node(new_pos, {name='platformer:y2'})
  219. minetest.set_node(pos, {name='air'})
  220. else
  221. minetest.set_node(pos, {name='platformer:y1'})
  222. end
  223. end
  224. })
  225. minetest.register_node('platformer:z1', {
  226. description = 'z axis (positive)',
  227. drawtype = 'mesh',
  228. mesh = 'platformer_platform_z1.obj',
  229. tiles = {'platformer_platform_xz.png'},
  230. light_source = 2,
  231. selection_box = z_box,
  232. collision_box = z_box,
  233. groups = {breakable = 1, not_in_creative_inventory=1},
  234. on_construct = function(pos)
  235. local timer = minetest.get_node_timer(pos)
  236. timer:start(1.5)
  237. end,
  238. on_timer = function(pos)
  239. local new_pos = {x=pos.x, y=pos.y, z=pos.z+1}
  240. local new_node = minetest.get_node(new_pos).name
  241. if new_node == 'air' then
  242. minetest.set_node(new_pos, {name='platformer:z1'})
  243. minetest.set_node(pos, {name='air'})
  244. else
  245. minetest.set_node(pos, {name='platformer:z2'})
  246. end
  247. end
  248. })
  249. minetest.register_node('platformer:z2', {
  250. description = 'z axis (negative)',
  251. drawtype = 'mesh',
  252. mesh = 'platformer_platform_z2.obj',
  253. tiles = {'platformer_platform_xz.png'},
  254. light_source = 2,
  255. selection_box = z_box,
  256. collision_box = z_box,
  257. groups = {breakable = 1, not_in_creative_inventory=1},
  258. drops = 'platformer:z1',
  259. on_construct = function(pos)
  260. local timer = minetest.get_node_timer(pos)
  261. timer:start(1.5)
  262. end,
  263. on_timer = function(pos)
  264. local new_pos = {x=pos.x, y=pos.y, z=pos.z-1}
  265. local new_node = minetest.get_node(new_pos).name
  266. if new_node == 'air' then
  267. minetest.set_node(new_pos, {name='platformer:z2'})
  268. minetest.set_node(pos, {name='air'})
  269. else
  270. minetest.set_node(pos, {name='platformer:z1'})
  271. end
  272. end
  273. })