std.include("scripts/common.lua")

local this=nil
local target_gate=nil
local disable_time=0

function init(t)
   this=t
   
   local pos=get_object_world_pos(this)

   while (true) do
      if (disable_time>0) then
         wait_time(disable_time)
         disable_time=0
      end
      local player=get_player()
      local player_pos=get_object_world_pos(player)
      local d=vec3_sqr_dist(player_pos,pos)
      if (d<4) then
         local t0=get_time()
         while (d<4) do
            player=get_player()
            player_pos=get_object_world_pos(player)
            d=vec3_sqr_dist(player_pos,pos)
            coroutine.yield()
            if ((get_time()-t0)>500) then
               set_object_world_pos(player,get_object_world_pos(target_gate))
               send_event_to_sprite(target_gate,"temp_disable_gate",nil)
               temp_disable_gate()
               play_sound("teleport",1)
               d=100
            end
         end
      end
      coroutine.yield()
   end
end

function set_target_gate(v)
   target_gate=v
end

function temp_disable_gate()
   disable_time=2000
end