minetest.register_entity('platformer:platform', {
   visual = 'mesh',
   mesh = 'platformer_platform_x1.obj',
   visual_size = {x = 10, y = 10},
   collisionbox = {-1, .25, -.5, 1, .5, .5},
   physical = true,
   textures = {'platformer_platform_xz.png'},

   init = function(self, texture)
      self.data.texture = texture
      self.object:set_properties({textures = texture})
   end,

   on_activate = function(self, staticdata)
      local rot = self.object:get_rotation()
      if rot.y == 0 then
         self.object:set_rotation({y= 3.1459, x= 0, z= 0})
         self.object:set_velocity({x= -1.5, y= 0, z= 0})
      elseif rot.y <= 3.146 and rot.y >= 3.145 then
         self.object:set_rotation({y= 0, x= 0, z= 0})
         self.object:set_velocity({x= 1.5, y= 0, z= 0})
      elseif rot.y <= 1.573 and rot.y >= 1.572 then
         self.object:set_rotation({y= -1.57295, x= 0, z= 0})
         self.object:set_velocity({x= 0, y= 0, z= -1.5})
      elseif rot.y <= -1.572 and rot.y >= -1.573 then
         self.object:set_rotation({y= 1.57295, x= 0, z= 0})
         self.object:set_velocity({x= 0, y= 0, z= 1.5})
      end
   end,

   on_punch = function(self, puncher)
      local rot = self.object:get_rotation()
      if rot.y == 0 then
         self.object:set_velocity({x= 1.5, y= 0, z= 0})
      elseif rot.y <= 3.146 and rot.y >= 3.145 then
         self.object:set_velocity({x= -1.5, y= 0, z= 0})
      elseif rot.y <= 1.573 and rot.y >= 1.572 then
         self.object:set_velocity({x= 0, y= 0, z= 1.5})
      elseif rot.y <= -1.572 and rot.y >= -1.573 then
         self.object:set_velocity({x= 0, y= 0, z= -1.5})
      end
   end,

   on_rightclick = function(self, clicker)
      local tool = clicker:get_wielded_item():to_string()
      if tool == 'creative:tool_breaking' then
         self.object:remove()
      end
   end,

   on_step = function(self, dtime, moveresult)
      --[[local pos = self.object:get_pos() -- Code doesn't reliably move player with the platform.
      print (dump(self.object:get_velocity()))
      local vel = vector.divide(self.object:get_velocity(), 2)
      print (dump(vel))
      for _, player in pairs(minetest.get_connected_players()) do
         local player_pos = player:get_pos()
         local p2p_distance = vector.distance(player_pos, pos)
         if p2p_distance < 2 and player_pos.y > pos.y then
            print 'moving player'
            player:add_velocity(vel)
            --player:set_attach(self.object)
         end
      end
--]]
      if moveresult.collides then
         local rot = self.object:get_rotation()
         if rot.y == 0 then
            self.object:set_rotation({y= 3.1459, x= 0, z= 0})
            self.object:set_velocity({x= -1.5, y= 0, z= 0})
         elseif rot.y <= 3.146 and rot.y >= 3.145 then
            self.object:set_rotation({y= 0, x= 0, z= 0})
            self.object:set_velocity({x= 1.5, y= 0, z= 0})
         elseif rot.y <= 1.573 and rot.y >= 1.572 then
            self.object:set_rotation({y= -1.57295, x= 0, z= 0})
            self.object:set_velocity({x= 0, y= 0, z= -1.5})
         elseif rot.y <= -1.572 and rot.y >= -1.573 then
            self.object:set_rotation({y= 1.57295, x= 0, z= 0})
            self.object:set_velocity({x= 0, y= 0, z= 1.5})
         end
      end
   end,
})

minetest.register_craftitem('platformer:platform_placer', {
   description = 'Places moving platforms.',
   inventory_image = 'platformer_placer_xz.png',
   on_place = function(itemstack, placer, pointed_thing)
      local pos = pointed_thing.above
      if not minetest.is_protected(pos, placer:get_player_name()) then
         local dir = minetest.dir_to_facedir(placer:get_look_dir())
         print (dir)
         if dir == 0 then
            local platform = minetest.add_entity(pos, 'platformer:platform')
            platform:set_rotation({y= 1.57295, x= 0, z= 0})
            platform:set_velocity({x= 0, y= 0, z= 1})
            platform:set_properties({collisionbox = {-.5, .25, -1, .5, .5, 1}})
         elseif dir == 1 then
            local platform = minetest.add_entity(pos, 'platformer:platform')
            platform:set_rotation({y= 0, x= 0, z= 0})
            platform:set_velocity({x= 1, y= 0, z= 0})
         elseif dir == 2 then
            local platform = minetest.add_entity(pos, 'platformer:platform')
            platform:set_rotation({y= -1.57295, x= 0, z= 0})
            platform:set_velocity({x= 0, y= 0, z= -1})
            platform:set_properties({collisionbox = {-.5, .25, -1, .5, .5, 1}})
         elseif dir == 3 then
            local platform = minetest.add_entity(pos, 'platformer:platform')
            platform:set_rotation({y= 3.1459, x= 0, z= 0})
            platform:set_velocity({x= -1, y= 0, z= 0})
         end
      end
   end,
})

--All this code should be removed, once I figure out how to port the old items over to the new.

local x_box = {
   type = 'fixed',
      fixed = {
         {-1, .25, -.5625, 1, .5, .5625},},}

local y_box = {
   type = 'fixed',
      fixed = {
         {-.5625, .25, -.5625, .5625, .5, .5625},},}

local z_box = {
   type = 'fixed',
      fixed = {
         {-.5625, .25, -1, .5625, .5, 1},},}

minetest.register_node('platformer:x1', {
   description = 'X axis (positive)',
   drawtype = 'mesh',
   mesh = 'platformer_platform_x1.obj',
   tiles = {'platformer_platform_xz.png'},
   light_source = 2,
   selection_box = x_box,
   collision_box = x_box,
   groups = {breakable = 1, not_in_creative_inventory=1},
   on_construct = function(pos)
      local timer = minetest.get_node_timer(pos)
      timer:start(1.5)
   end,
   on_timer = function(pos)
      local new_pos = {x=pos.x+1, y=pos.y, z=pos.z}
      local new_node = minetest.get_node(new_pos).name
      if new_node == 'air' then
         minetest.set_node(new_pos, {name='platformer:x1'})
         minetest.set_node(pos, {name='air'})
      else
         minetest.set_node(pos, {name='platformer:x2'})
      end
   end
})

minetest.register_node('platformer:x2', {
   description = 'X axis (negative)',
   drawtype = 'mesh',
   mesh = 'platformer_platform_x2.obj',
   tiles = {'platformer_platform_xz.png'},
   light_source = 2,
   selection_box = x_box,
   collision_box = x_box,
   groups = {breakable = 1, not_in_creative_inventory=1},
   drops = 'platformer:x1',
   on_construct = function(pos)
      local timer = minetest.get_node_timer(pos)
      timer:start(1.5)
   end,
   on_timer = function(pos)
      local new_pos = {x=pos.x-1, y=pos.y, z=pos.z}
      local new_node = minetest.get_node(new_pos).name
      if new_node == 'air' then
         minetest.set_node(new_pos, {name='platformer:x2'})
         minetest.set_node(pos, {name='air'})
      else
         minetest.set_node(pos, {name='platformer:x1'})
      end
   end
})

minetest.register_node('platformer:y1', {
   description = 'y axis (positive)',
   drawtype = 'mesh',
   mesh = 'platformer_platform_y.obj',
   tiles = {'platformer_platform_y.png'},
   climbable = true,
   walkable = false,
   light_source = 2,
   selection_box = y_box,
   collision_box = y_box,
   groups = {breakable = 1, not_in_creative_inventory=1},
   on_construct = function(pos)
      local timer = minetest.get_node_timer(pos)
      timer:start(1.5)
   end,
   on_timer = function(pos)
      local new_pos = {x=pos.x, y=pos.y+1, z=pos.z}
      local new_node = minetest.get_node(new_pos).name
      if new_node == 'air' then
         minetest.set_node(new_pos, {name='platformer:y1'})
         minetest.set_node(pos, {name='air'})
      else
         minetest.set_node(pos, {name='platformer:y2'})
      end
   end
})

minetest.register_node('platformer:y2', {
   description = 'y axis (negative)',
   drawtype = 'mesh',
   mesh = 'platformer_platform_y.obj',
   tiles = {'platformer_platform_y.png'},
   climbable = true,
   walkable = false,
   light_source = 2,
   selection_box = y_box,
   collision_box = y_box,
   groups = {breakable = 1, not_in_creative_inventory=1},
   drops = 'platformer:y1',
   on_construct = function(pos)
      local timer = minetest.get_node_timer(pos)
      timer:start(1.5)
   end,
   on_timer = function(pos)
      local new_pos = {x=pos.x, y=pos.y-1, z=pos.z}
      local new_node = minetest.get_node(new_pos).name
      if new_node == 'air' then
         minetest.set_node(new_pos, {name='platformer:y2'})
         minetest.set_node(pos, {name='air'})
      else
         minetest.set_node(pos, {name='platformer:y1'})
      end
   end
})

minetest.register_node('platformer:z1', {
   description = 'z axis (positive)',
   drawtype = 'mesh',
   mesh = 'platformer_platform_z1.obj',
   tiles = {'platformer_platform_xz.png'},
   light_source = 2,
   selection_box = z_box,
   collision_box = z_box,
   groups = {breakable = 1, not_in_creative_inventory=1},
   on_construct = function(pos)
      local timer = minetest.get_node_timer(pos)
      timer:start(1.5)
   end,
   on_timer = function(pos)
      local new_pos = {x=pos.x, y=pos.y, z=pos.z+1}
      local new_node = minetest.get_node(new_pos).name
      if new_node == 'air' then
         minetest.set_node(new_pos, {name='platformer:z1'})
         minetest.set_node(pos, {name='air'})
      else
         minetest.set_node(pos, {name='platformer:z2'})
      end
   end
})

minetest.register_node('platformer:z2', {
   description = 'z axis (negative)',
   drawtype = 'mesh',
   mesh = 'platformer_platform_z2.obj',
   tiles = {'platformer_platform_xz.png'},
   light_source = 2,
   selection_box = z_box,
   collision_box = z_box,
   groups = {breakable = 1, not_in_creative_inventory=1},
   drops = 'platformer:z1',
   on_construct = function(pos)
      local timer = minetest.get_node_timer(pos)
      timer:start(1.5)
   end,
   on_timer = function(pos)
      local new_pos = {x=pos.x, y=pos.y, z=pos.z-1}
      local new_node = minetest.get_node(new_pos).name
      if new_node == 'air' then
         minetest.set_node(new_pos, {name='platformer:z2'})
         minetest.set_node(pos, {name='air'})
      else
         minetest.set_node(pos, {name='platformer:z1'})
      end
   end
})
