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 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)
   -- 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, glad you decided to help!",4000,{ 1,1,0,1 }); wait_text(text_id)
   text_id=say(player,"But... but...",4000,{ 0.2,1.0,0.1,1 }); wait_text(text_id)
   text_id=say(old_man,"No buts, young man!\nI know, you consider it an honor...",4000,{ 1,1,0,1 }); wait_text(text_id)
   text_id=say(old_man,"Anyway, you have to reach the portal at\nthe end of this dungeon...",4000,{ 1,1,0,1 }); wait_text(text_id)
   text_id=say(old_man,"But...",1000,{ 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, that means you're coming with me?",4000,{ 0.2,1.0,0.1,1 }); wait_text(text_id)
   text_id=say(old_man,"!",2000,{ 1,1,0,1 }); wait_text(text_id)
   text_id=say(old_man,"Don't be silly!",4000,{ 1,1,0,1 }); wait_text(text_id)
   text_id=say(old_man,"I'm old, I'd just get in the way!",4000,{ 1,1,0,1 }); wait_text(text_id)
   text_id=say(old_man,"Just take this Unsummoning Wand...",4000,{ 1,1,0,1 }); wait_text(text_id)
   text_id=say(old_man,"It will unsummon the Unholy Rabbits\nthat are all over this dungeon...",4000,{ 1,1,0,1 }); wait_text(text_id)
   text_id=say(old_man,"Watch out, it only has 5 charges,\nso use them wisely!",4000,{ 1,1,0,1 }); wait_text(text_id)
   text_id=say(old_man,"Press <Space Bar> to use items...",4000,{ 1,1,0,1 }); wait_text(text_id)
   text_id=say(old_man,"And if the music creeps you\nout, press 's' to turn it off!",4000,{ 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)
   -----------------------------------
   -- Spawn imps
   local spawn_points=get_trigger_points(128)
   for i,v in ipairs(spawn_points) do
      local p=get_trigger_point_world_pos(v)
      instance_particle_system("puff_fire",p)
   end
   wait_time(600)
   for i,v in ipairs(spawn_points) do
      local p=get_trigger_point_world_pos(v)
      local imp=instance_game_sprite("imp","imp",p)
      bind_script_to_game_sprite(imp,"scripts/imp.lua")
   end
   -----------------------------------
   -- Give object to player
   give_object(player,"scripts/unsummon_wand.lua")
   -----------------------------------
   -- Renable player
   enable_input(player,true)
   enable_anim(player,true)
   enable_movement(player,true)
end
