
dofile( "../../Assets/Scripts/key_name_mappings.lua" )

local ASSETS_XML = "../../Assets/Xml/"

-- no real need for a table
local Aster     = Player:gAster()
local Lantern   = Player:gLantern()
local CONTROLLER = Global:ControllerConnected()

local menu = "entities/e_PopupText.xml"
local escYes = 0

menu = Global:CreateEntityAt( ASSETS_XML .. menu, 0, 60, 0, 0, 0, 0 )
menu:VIS_SetText( "" )

Global:AddToMaxVineLen( 400 )

local GrowPos = {}
local vine = { -- keyboard
               [ 1 ] = "A vine! With this, you can grow\nto the moon.",
               [ 2 ] = "Press L-ctrl to grow the vine\nand L-shift to ungrow.",
               [ 3 ] = "The vine will always grow toward\nthe lantern.",
               [ 4 ] = "Stand on the tip of the vine to\ndirect growth.",
               [ 5 ] = "If you're close to the tip, the\nvine will carry you!",
               [ 6 ] = "",
               [ "max" ] = 6 }
               
               -- controller
              vine[ 11 ] = vine[ 1 ]
              vine[ 12 ] = "Press (L-trigger) to grow the vine\nand (R-trigger) to ungrow."
              vine[ 13 ] = vine[ 3 ]
              vine[ 14 ] = vine[ 4 ]
              vine[ 15 ] = vine[ 5 ]
              vine[ 16 ] = vine[ 6 ]

local FLIES = "entities/e_FireflySwarm.xml"
local ffss = { -- keyboard
               [ 1 ] = "Oh! Fireflies!",
               [ 2 ] = "These guys will be attracted\ntoward your lantern.",
               [ 3 ] = "More light means faster growth!",
               [ 4 ] = "Every firefly you collect\nwill give your vine a small speed boost!",
               [ 5 ] = "",
               [ "max" ] = 5 }
               
               -- controller
               ffss[ 11 ] = ffss[ 1 ]
               ffss[ 12 ] = ffss[ 2 ]
               ffss[ 13 ] = ffss[ 3 ]
               ffss[ 14 ] = ffss[ 4 ]
               ffss[ 15 ] = ffss[ 5 ]
                
local bulb = { -- keyboard
               [ 1 ] = "The vine can grow for as\nlong as you like, but ...",
               [ 2 ] = "It can only grow a certain\ndistance from each island.",
               [ 3 ] = "To continue forward, grow\nthe vine into a red seed.",
               [ 4 ] = "This will sprout a new vine,",
               [ 5 ] = "And expand the area in which\nthe vine can grow.",
               [ 6 ] = "As you near the edge of\nyour vine's growth radius,",
               [ 7 ] = "The inner circle of light\nsurrounding your lantern will grow.",
               [ 8 ] = "",
               [ "max" ] = 8 }
               
               -- controller
               bulb[ 11 ] = bulb[ 1 ]
               bulb[ 12 ] = bulb[ 2 ]
               bulb[ 13 ] = bulb[ 3 ]
               bulb[ 14 ] = bulb[ 4 ]
               bulb[ 15 ] = bulb[ 5 ]
               bulb[ 16 ] = bulb[ 6 ]
               bulb[ 17 ] = bulb[ 7 ]
               bulb[ 18 ] = bulb[ 8 ]
               
local nBud = { -- keyboard
               [ 1 ] = "After a new bud is spawned,",
               [ 2 ] = "Pressing V will look toward the\nnewly spawned bud.",
               [ 3 ] = "",
               [ "max" ] = 3,
               [ "vID" ] = Global:gVineID() }
               
               -- controller
               nBud[ 11 ] = nBud[ 1 ]
               nBud[ 12 ] = "Pressing (Y) will look toward the\nnewly spawned bud."
               nBud[ 13 ] = nBud[ 3 ]

local timeIncr  = 0
local timeTotal = 0
local timeMax   = 100
local incrMin
if( not CONROLLER ) then
  incrMin = 10
else
  incrMin = 10
  
end
local vineStart = incrMin
local ffssStart = incrMin
local bulbStart = incrMin   
local nBudStart = incrMin
while( 1 )
do
  coroutine.yield()

  ------------------------------------------------------------------------------
  -- User skip
  if( Global:IsGameButtonTriggered( 0 ) )
  then
    if( vineStart > incrMin ) and ( vineStart < incrMin + vine.max ) then
      timeIncr = timeMax
      
    elseif( ffssStart > incrMin ) and ( ffssStart < incrMin + ffss.max ) then
      timeIncr = timeMax
    
    elseif( bulbStart > incrMin ) and ( bulbStart < incrMin + bulb.max ) then
      timeIncr = timeMax
    
    elseif( nBudStart > incrMin ) and ( nBudStart < incrMin + nBud.max ) then
      timeIncr = timeMax
    
    end
    
  end

  ------------------------------------------------------------------------------
  -- User skip all
  if( Global:IsTriggered( key_names_to_values( "Backspace" ) ) )
  then
    Global:MusicLayer( 1, 0, "../../Assets/Sounds/music_1_1.mp3" )
    vineStart = incrMin + vine.max
    ffssStart = incrMin + ffss.max
    bulbStart = incrMin + bulb.max
    nBudStart = incrMin + nBud.max
    
  end

  timeTotal = timeTotal + 1
  timeIncr  = timeIncr + 1

  ------------------------------------------------------------------------------
  -- 'Press Esc ...'
  if( escYes == 0 ) 
  then
    if( timeIncr > 120 )
    then
      if( CONTROLLER ) then
        menu:VIS_SetText( "Press (start) to access the menu." )
      
      else
        menu:VIS_SetText( "Press ESC to access the menu." )
        
      end
      menu:SPT_GiveNumber( "go", 1.0 )
      
      timeIncr = 0
      escYes = 1
      
    end
    
  elseif( escYes == 1 )
  then
    if( timeIncr > 150 )
    then
      if( CONTROLLER ) then
        menu:VIS_SetText( "Press (a) to play." )
      
      else
        menu:VIS_SetText( "Press SPACE to play." )
        
      end
      
      timeIncr = 0
      escYes = 2
      
    end
  
  elseif( escYes == 2 ) and ( Global:IsGameButtonTriggered( 0 ) ) 
  then
    menu:VIS_SetText( "" )
    
    timeIncr = 0
    escYes = 3
  
  end
  
  ------------------------------------------------------------------------------
  -- 'Press l-ctrl ...'
  if( vineStart == incrMin )
  then
    local posDif = vec3.magnitude( vec3.subtract( Global:gVineEndPoint(), Aster:SHR_gPosition() ) )
    if( posDif < 5 ) 
    then
      GrowPos = vec3.subtract( Aster:SHR_gPosition(), Global:gVineEndPoint() )
      
      local vinePos = Global:gVineEndPoint()
      Global:SetCameraTargetPositionVector( vinePos.x, vinePos.y, vinePos.z )
    
      vineStart = vineStart + 1
      timeIncr = 0
      
      menu:SPT_GiveNumber( "go", 1.0 )
      menu:VIS_SetText( vine[ vineStart ] )
      
      Global:MusicLayer( 1, 0, "../../Assets/Sounds/music_1_1.mp3" )
      
      Global:SetGameMode( false )
      Global:SetLetterboxDirection( 1.0 )
      
    end
  
  elseif( vineStart < incrMin + vine.max )
  then
    if( vineStart > incrMin + 3 ) and ( vineStart < incrMin + vine.max ) and ( Global:IsGameButtonPressed( 1 ) ) 
    then
      Global:sVineGrowSpeed( 1 )
      Global:GrowVine( GrowPos.x, GrowPos.y, GrowPos.z )
      
    end
    
    if( timeIncr > timeMax )
    then
      vineStart = vineStart + 1
      timeIncr = 0
      
      menu:SPT_GiveNumber( "go", 1.0 )
      menu:VIS_SetText( vine[ vineStart ] )
      
      if( vineStart == incrMin + vine.max ) 
      then
        Lantern:SPT_GiveNumber( "TakeCamera", 1 )
        Global:SetGameMode( true )
        Global:SetLetterboxDirection( -1.0 )
        
      end
      
    end
      
  end
  
  ------------------------------------------------------------------------------
  -- 'Fireflies will ...'
  if( ffssStart == incrMin ) 
  then 
    if( Global:gVineMaturity() > .25 )
    then
      local vPos = Global:gVineEndPoint()
      --Global:CreateEntityAt( ASSETS_XML .. FLIES, vPos.x - 15, vPos.y + 10, vPos.z + 15, 0, 0, 0 )
      --Global:CreateEntityAt( ASSETS_XML .. FLIES, vPos.x + 15, vPos.y - 10, vPos.z - 15, 0, 0, 0 )
      Global:SetCameraPositionVector( vPos.x - 20, vPos.y + 10, vPos.z - 20 )
    
      ffssStart = ffssStart + 1
      timeIncr = 0
      
      menu:SPT_GiveNumber( "go",  1.0 )
      menu:VIS_SetText( ffss[ ffssStart ] )
      
      Global:SetGameMode( false )
      Global:SetLetterboxDirection( 1.0 )
      
    end
    
  elseif( ffssStart < incrMin + ffss.max )
  then
    if( timeIncr > timeMax )
    then
      Lantern:SPT_GiveNumber( "TakeCamera", 1 )
    
      ffssStart = ffssStart + 1
      timeIncr = 0
      
      menu:SPT_GiveNumber( "go", 1.0 )
      menu:VIS_SetText( ffss[ ffssStart ] )
      
      if( ffssStart == incrMin + ffss.max ) 
      then
        Lantern:SPT_GiveNumber( "TakeCamera", 1 )
        Global:SetGameMode( true )
        Global:SetLetterboxDirection( -1.0 )
        
      end
      
    end
  
  end
  
  ------------------------------------------------------------------------------
  -- 'Bulb?'
  if( bulbStart == incrMin )
  then
    if( ffssStart == incrMin + ffss.max ) and ( Global.gVineMaturity() > .5 ) 
    then
      bulbStart = bulbStart + 1
      timeIncr = 0
      
      menu:SPT_GiveNumber( "go", 1.0 )
      menu:VIS_SetText( bulb[ bulbStart ] )
      
      Global:SetGameMode( false )
      Global:SetLetterboxDirection( 1.0 )
      
    end
  
  elseif( bulbStart < incrMin + bulb.max )
  then
    
    if( timeIncr > timeMax )
    then
      bulbStart = bulbStart + 1
      timeIncr = 0
      
      menu:SPT_GiveNumber( "go", 1.0 )
      menu:VIS_SetText( bulb[ bulbStart ] )
      
      if( bulbStart == incrMin + bulb.max ) 
      then
        Lantern:SPT_GiveNumber( "TakeCamera", 1 )
        Global:SetGameMode( true )
        Global:SetLetterboxDirection( -1.0 )
        
      end
      
    end
      
  end
  
  ------------------------------------------------------------------------------
  -- 'New bud?'
  if( nBudStart == incrMin )
  then
    if( bulbStart == incrMin + bulb.max ) and ( Global.gVineID() ~= nBud.vID ) 
    then
      nBudStart = nBudStart + 1
      timeIncr = 0
      
      menu:SPT_GiveNumber( "go", 1.0 )
      menu:VIS_SetText( nBud[ nBudStart ] )
      
      Global:SetGameMode( false )
      Global:SetLetterboxDirection( 1.0 )
      
    end
  
  elseif( nBudStart < incrMin + nBud.max )
  then
    
    if( timeIncr > timeMax )
    then
      nBudStart = nBudStart + 1
      timeIncr = 0
      
      menu:SPT_GiveNumber( "go", 1.0 )
      menu:VIS_SetText( nBud[ nBudStart ] )
      
      if( nBudStart == incrMin + nBud.max ) 
      then
        Lantern:SPT_GiveNumber( "TakeCamera", 1 )
        Global:SetGameMode( true )
        Global:SetLetterboxDirection( -1.0 )
        
      end
      
    end
      
  end

end
