//
// 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 Level50.map
//

#include "scripts/common.txt"

const int       g_nCharacters       = 31;
const int       g_galleryWidth      = 5;
const float     g_galleryGap        = ( 119.6f  * GAME_SCALE);
const float     g_gallery_x         = (-239.19f * GAME_SCALE);
const float     g_gallery_y         = ( 328.8f  * GAME_SCALE);
const float     g_gallery_focus_y   = ( 358.8f  * GAME_SCALE);

int g_portrait = 0;
float g_rotate = 0;

kVec3 g_vEye(-239.19f*GAME_SCALE, 328.8f*GAME_SCALE, 0.0f);
kVec3 g_vFocus(-239.19f*GAME_SCALE, 358.8f*GAME_SCALE, 0.0f);

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

$script 0
{
    Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_NO_LETTERBOX|CMF_NO_INITIAL_FADEOUT);
    Camera.fov = 47.5f;
    Camera.origin = g_vEye;
    
    Game.CallDelayedMapScript(1, instigator, 0);
}

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

$script 1
{
    uint16 buttons = Player.Buttons();
    
    if((buttons & (BC_FORWARD|BC_BACKWARD|BC_STRAFELEFT|BC_STRAFERIGHT|BC_WEAPONLEFT|BC_WEAPONRIGHT) == 0))
    {
        if(Camera.UserInterrupted())
        {
            Game.Restart();
            return;
        }
    }
    
    if((buttons & (BC_WEAPONLEFT|BC_WEAPONRIGHT)) != 0)
    {
        g_rotate = 0;
        
        if((buttons & BC_WEAPONRIGHT) != 0)
        {
            g_portrait++;
            
            if(g_portrait >= g_nCharacters)
            {
                g_portrait = 0;
            }
        }
        else
        {
            g_portrait--;
            
            if(g_portrait < 0)
            {
                g_portrait = g_nCharacters - 1;
            }
        }
        
        g_vEye.x = float(g_portrait % g_galleryWidth) * g_galleryGap + g_gallery_x;
        g_vEye.y = float(g_portrait / g_galleryWidth) * -g_galleryGap + g_gallery_y;
        
        g_vFocus.x = float(g_portrait % g_galleryWidth) * g_galleryGap + g_gallery_x;
        g_vFocus.y = float(g_portrait / g_galleryWidth) * -g_galleryGap + g_gallery_focus_y;
        
        Camera.origin = g_vEye;
        Camera.yaw = 0;
    }
    
    if((buttons & BC_STRAFERIGHT) != 0)
    {
        g_rotate += Math::Deg2Rad(4.0f);
        g_vEye.x = g_vFocus.x + (Math::Sin(g_rotate) * 200.0f);
        g_vEye.y = g_vFocus.y + (Math::Cos(g_rotate) * 200.0f);
        
        Camera.origin = g_vEye;
        
        Camera.yaw = Math::ATan2(g_vFocus.x - Camera.origin.x, g_vFocus.y - Camera.origin.y);
    }
    
    if((buttons & BC_STRAFELEFT) != 0)
    {
        g_rotate -= Math::Deg2Rad(4.0f);
        g_vEye.x = g_vFocus.x + (Math::Sin(g_rotate) * 200.0f);
        g_vEye.y = g_vFocus.y + (Math::Cos(g_rotate) * 200.0f);
        
        Camera.origin = g_vEye;
        
        Camera.yaw = Math::ATan2(g_vFocus.x - Camera.origin.x, g_vFocus.y - Camera.origin.y);
    }
    
    $restart;
}
