//
// Copyright(C) 2014-2015 Samuel Villarreal
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// DESCRIPTION:
//      Level Script For Level42.map
//

#include "scripts/common.txt"

kVec3 destOrigin1;
kVec3 destOrigin2;
kVec3 destOrigin3;
kVec3 destOrigin4;

kVec3 startOrigin1;
kVec3 startOrigin2;
kVec3 startOrigin3;
kVec3 startOrigin4;

float lerpTime = 0;
float explodeBob = 15.0f;

kActor @arrow = null;

/*
==============================================================
Script 0
==============================================================
*/

$script 0
{
    bool bStartedIntro;
    kStr szShowIntros;
    
    GameVariables.GetBool("bStartedIntro", bStartedIntro);
    
    if(Sys.GetCvarValue("g_showgameintros", szShowIntros))
    {
        if(szShowIntros.Atoi() == 0)
        {
            bStartedIntro = true;
        }
    }
    
    Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_NO_LETTERBOX|CMF_NO_INITIAL_FADEOUT);
    Camera.fov = 74.0f;
    
    @arrow = World.GetActorByTID(5);
    
    arrow.AnimState().Set(anim_weaponFire, 4.0f, ANF_LOOP);
    arrow.Yaw() = Math::pi;
    
    // camera vectors
    startOrigin1.Set(0.0f, -1986.5601f, 51.2f);
    destOrigin1.Set(0.0f, -92.16f, 51.2f);
    
    // camera look at vectors
    startOrigin2.Set(0.0f, 153.6f, 51.2f);
    destOrigin2.Set(0.0f, 153.6f, 51.2f);
    
    // arrow vectors
    startOrigin3.Set(0.0f, -1955.84f, 40.96f);
    destOrigin3.Set(0.0f, -61.44f, 40.96f);
    
    // arrow dest vectors
    startOrigin4.Set(0.0f, -1955.84f, 40.96f);
    destOrigin4.Set(22.6304f, 16.9984f, 82.432f);
    
    if(bStartedIntro)
    {
        Camera.origin = destOrigin1;
        arrow.Origin() = destOrigin4;
        Camera.fov = 47.5f;
        Game.ShowMainMenu();
        return;
    }
    
    Game.CallDelayedMapScript(1, instigator, 0);
    GameVariables.SetValue("bStartedIntro", "1");
    
    delay(2.0f);
    Game.ShowMainMenu();
}

/*
==============================================================
Script 1
==============================================================
*/

$script 1
{
    kVec3 dir;
    float t = lerpTime * (4.0f * GAME_DELTA_TIME);
    float t2 = Math::CosArc(t) * 153.6f;
    
    lerpTime += GAME_FRAME_TIME;
    
    Camera.origin = startOrigin1;
    Camera.origin.Lerp(destOrigin1, t);
    Camera.origin.z += t2;
    
    arrow.Origin() = startOrigin3;
    arrow.Origin().Lerp(destOrigin3, t);
    arrow.Origin().z += t2;
    
    kVec3 vec1 = startOrigin2;
    kVec3 vec2 = startOrigin4;
    
    vec1.Lerp(destOrigin2, t);
    vec2.Lerp(destOrigin4, t);
    vec1.z += t2;
    vec2.z += t2;
    
    dir = vec1 - Camera.origin;
    
    Camera.yaw = dir.ToYaw();
    Camera.pitch = -dir.ToPitch();
    
    arrow.Origin().Lerp(vec2, Math::Pow(t, 8.0f));
    arrow.Roll() += Math::Deg2Rad(4.0f);
    
    Camera.fov = (47.5f - 74.0f) * t + 74.0f;
    
    if(t >= 1.0f)
    {
        lerpTime = 0;
        Game.CallDelayedMapScript(2, instigator, 0);
        
        Game.SpawnFx("fx/generic_39.kfx", kVec3(22.63f, 73.318f, 82.432f), 0);
        Game.PlaySound("sounds/shaders/generic_143.ksnd");
        return;
    }
    
    $restart;
}

/*
==============================================================
Script 2
==============================================================
*/

$script 2
{
    float t = lerpTime * (4.0f * GAME_DELTA_TIME);
    lerpTime += GAME_FRAME_TIME;
    
    Camera.origin.z = 51.2f + (Math::Sin(t * (Math::pi * 8.0f)) * explodeBob);
    explodeBob = (0.0f - explodeBob) * (4.0f * GAME_DELTA_TIME) + explodeBob;
    
    if(explodeBob <= 0.05f)
    {
        return;
    }
    
    $restart;
}
