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

function init_level()
   -----------------------------------
   -- Create portal
   local spawn_point=get_trigger_point(65280)
   local p=get_trigger_point_world_pos(spawn_point)
   local portal=instance_game_sprite("portal","portal",p)
   set_hostile(portal,false)
   set_shadow(portal,false)
   -----------------------------------
   -- Spawn Nullsph
   local spawn_points=get_trigger_points(128)
   for i,v in ipairs(spawn_points) do
      local p=get_trigger_point_world_pos(v)
      local orb=instance_game_sprite("nullsph","nullsph",p)
      bind_script_to_game_sprite(orb,"scripts/nullsph.lua")
      set_sprite_anim_speed(orb,20)
   end
   -----------------------------------
   -- Spawn old man
   local player=get_player()
   set_frame(player,frame_north)
   enable_input(player,false)
   enable_anim(player,false)
   enable_movement(player,false)
   -- Particle system
   local spawn_point=get_trigger_point(1)
   local p=add_vec3(get_trigger_point_world_pos(spawn_point),{ 1.5,0,4.5 })
   instance_particle_system("puff_white_smoke",p)

   local light=instance_light(add_vec3(p,{0,2,-1}),10)

   -- Old man
   wait_time(1000)
   local old_man=instance_game_sprite("old_man","old_man",p)
   set_frame(old_man,frame_south)
   wait_time(2000)
   -----------------------------------
   -- Speech part
   local text_id
   text_id=say(old_man,"Ah, finally!",2000,{ 1,1,0,1 }); wait_text(text_id)
   text_id=say(old_man,"Welcome to the lair of the Nullsphs...",4000,{ 1,1,0,1 }); wait_text(text_id)
   text_id=say(old_man,"The Nullsphs are creatures\nof pure energy...",4000,{ 1,1,0,1 }); wait_text(text_id)
   text_id=say(old_man,"As such, they're very quick, so...",4000,{ 1,1,0,1 }); wait_text(text_id)
   text_id=say(old_man,"...it's dangerous to go alone...",4000,{ 1,1,0,1 }); wait_text(text_id)
   text_id=say(player,"Oh, surprise!",4000,{ 0.2,1.0,0.1,1 }); wait_text(text_id)
   text_id=say(old_man,"But I have a treat for you!",4000,{ 1,1,0,1 }); wait_text(text_id)
   text_id=say(old_man,"The Hourglass of Bending Time!",3000,{ 1,1,0,1 }); wait_text(text_id)
   text_id=say(old_man,"It allows you to slow down\ntime around you!",3000,{ 1,1,0,1 }); wait_text(text_id)
   text_id=say(old_man,"It has a limited charge,\nbut it will recharge over time...",4000,{ 1,1,0,1 }); wait_text(text_id)
   text_id=say(old_man,"Good luck!",2000,{ 1,1,0,1 }); wait_text(text_id)
   wait_time(1000)
   -----------------------------------
   -- Disappear old man
   -- Particle system
   instance_particle_system("puff_white_smoke",p)
   -- Old man
   wait_time(1000)
   kill_game_sprite(old_man)
   -----------------------------------
   -- Give object to player
   give_object(player,"scripts/hourglass.lua")
   -----------------------------------
   -- Renable player
   enable_input(player,true)
   enable_anim(player,true)
   enable_movement(player,true)
end
