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

local this=nil
local ps=nil

function init(t)
   this=t

   local player=get_player()
   set_frame(player,frame_north)
   enable_input(player,false)
   enable_anim(player,false)
   enable_movement(player,false)

   wait_time(1000)

   local text_id
   text_id=say(this,"Buh...",2000,{ 1,1,0,1 }); wait_text(text_id)
   text_id=say(player,"AH!",1000,{ 0.2,1.0,0.1,1 }); wait_text(text_id)
   text_id=say(this,"Ehehehe...",1000,{ 1,1,0,1 }); wait_text(text_id)
   text_id=say(this,"Use the cursor keys to\nmove to a portal...",2000,{ 1,1,0,1 }); wait_text(text_id)

   enable_input(player,true)
   enable_anim(player,true)
   enable_movement(player,true)

   local new_game_portal=get_trigger_point(65280)
   local load_game_portal=get_trigger_point(65281)
   local exit_game_portal=get_trigger_point(65282)

   -- Do AI
   local current_portal=0
   local d=0
   local p={ }
   local player_pos={ }
   while (current_portal~=4) do
      coroutine.yield()

      player_pos=get_object_world_pos(player)
      p=get_trigger_point_world_pos(new_game_portal)

      d=vec3_sqr_dist(player_pos,p)
      if ((d<36) and (current_portal~=1)) then
         instance_particle_system("puff_white_smoke",get_object_world_pos(this))
         -- Old man
         wait_time(500)
         local new_pos=add_vec3(p,{ 2,0,0 })
         instance_particle_system("puff_white_smoke",new_pos)
         set_object_world_pos(this,new_pos)

         text_id=say(this,"Enter this portal to\nstart a new game!",4000,{ 1,1,0,1 });

         current_portal=1

      end
      if (d<4) then
         next_level("new_game")
         current_portal=4
      end

      p=get_trigger_point_world_pos(load_game_portal)
      d=vec3_sqr_dist(player_pos,p)

      if ((d<36) and (current_portal~=2)) then
         instance_particle_system("puff_white_smoke",get_object_world_pos(this))
         -- Old man
         wait_time(500)
         local new_pos=add_vec3(p,{ 2,0,2 })
         instance_particle_system("puff_white_smoke",new_pos)
         set_object_world_pos(this,new_pos)

         text_id=say(this,"Enter this portal to start\nfrom the last level visited!",4000,{ 1,1,0,1 });

         current_portal=2
      end
      if (d<4) then
         next_level("load_game")
         current_portal=4
      end

      p=get_trigger_point_world_pos(exit_game_portal)
      d=vec3_sqr_dist(player_pos,p)
      if ((d<36) and (current_portal~=3)) then
         instance_particle_system("puff_white_smoke",get_object_world_pos(this))
         -- Old man
         wait_time(500)
         local new_pos=add_vec3(p,{ 2,0,0 })
         instance_particle_system("puff_white_smoke",new_pos)
         set_object_world_pos(this,new_pos)

         text_id=say(this,"Enter this portal to leave\nthis awesome adventure!",3000,{ 1,1,0,1 });
         wait_text(text_id)
         text_id=say(this,"...you coward...",1000,{ 1,1,0,1 });

         current_portal=3
      end
      if (d<4) then
         next_level("exit_game")
         current_portal=4
      end
   end
end

