#include <allegro.h>
#include <cstdlib>
#include <time.h>
#include "classes.h"
#include "1pmode.h"
#include "globals.h"
#include "lasers.h"
#include "ships.h"
#include "bots.h"
#include "render.h"
#include "collisions.h"
#include "particles.h"
#include "stars.h"

void opmode_init(){
    srand (time(NULL));
    int typebuf;
    p1.playership_init(1);
    create_stars(1);
    secs1 = 0;
    botc = 0;
    secs = 0;
    kb = 0;
    for (int c = 0; c<parts.size(); c++){
    parts.erase(parts.begin()+c);}
    for (int c = bots.size(); c>0; c--){
    bots.erase(bots.begin()+c);}
    for (int c = 0; c<=17; c++){
    typebuf = rand() % 5 + 1;
    bots.push_back(botship(c*120, c*100, typebuf));} }
    
void opmode_cam(){
    //this loop is for the bots
    for (int c1 = 0; c1<bots.size(); c1++){
        if (bots[c1].health>0 && bots[c1].x!=0){//so we don't draw dead bots
            if (bots[c1].x>p1.x)
            bots[c1].sx1 = 320 + (bots[c1].x - p1.x);
            else
            bots[c1].sx1 = 320 - (p1.x - bots[c1].x);
            if (bots[c1].y>p1.y)
            bots[c1].sy1 = 240 + (bots[c1].y - p1.y);
            else
            bots[c1].sy1 = 240 - (p1.y - bots[c1].y);}}
            
    //this loop is for the lasers
    for (int c1 = 0; c1<las.size(); c1++){
        if (las[c1].x!=0){//so we don't draw unshot lasers
            if (las[c1].x>p1.x)
            las[c1].sx1 = 320 + (las[c1].x - p1.x);
            else
            las[c1].sx1 = 320 - (p1.x - las[c1].x);
            if (las[c1].y>p1.y)
            las[c1].sy1 = 240 + (las[c1].y - p1.y);
            else
            las[c1].sy1 = 240 - (p1.y - las[c1].y);}}
            
    //this loop is for particles
    for (int c1 = 0; c1<parts.size(); c1++){
        if (parts[c1].x!=0){//to be safe
            if (parts[c1].x>p1.x)
            parts[c1].sx1 = 320 + (parts[c1].x - p1.x);
            else
            parts[c1].sx1 = 320 - (p1.x - parts[c1].x);
            if (parts[c1].y>p1.y)
            parts[c1].sy1 = 240 + (parts[c1].y - p1.y);
            else
            parts[c1].sy1 = 240 - (p1.y - parts[c1].y);}}
            
    //this loop is for the stars
    for (int c1 = 0; c1<p1_stars.size(); c1++){
        if (p1_stars[c1].x>p1.x)
            p1_stars[c1].sx1 = 320 + (p1_stars[c1].x - p1.x);
            else
            p1_stars[c1].sx1 = 320 - (p1.x - p1_stars[c1].x);
            if (p1_stars[c1].y>p1.y)
            p1_stars[c1].sy1 = 240 + (p1_stars[c1].y - p1.y);
            else
            p1_stars[c1].sy1 = 240 - (p1.y - p1_stars[c1].y);}}
            
    
void opmode_after_game(){
    stop_sample(gback);
    if (mute==1)
    play_sample(menback, 255, 128, 1000, 1);
    rectfill(screen, 0, 0, 640, 480, makecol(60,60,60));
    rect(screen, 315, 195, 456, 335, makecol(255,0,0));
    textout_ex(screen, font, "GAME OVER", 320, 200, makecol(255,0,0), makecol(60,60,60));
    textout_ex(screen, font, "Score:", 320, 250, makecol(255,0,0), makecol(60,60,60));
    textprintf(screen ,font, 370, 250, makecol(255,0,0), "%i", p1.score);
    textout_ex(screen, font, "Bots killed:", 320, 280, makecol(255,0,0), makecol(60,60,60));
    textprintf(screen ,font, 416, 280, makecol(255,0,0), "%i", kb);
    textout_ex(screen, font, "Time survived:", 320, 320, makecol(255,0,0), makecol(60,60,60));
    textprintf(screen ,font, 432, 320, makecol(255,0,0), "%i", secs1);
    textout_ex(screen, font, "Press ESC to go back to the menu", 100, 400, makecol(255,0,0), makecol(60,60,60));
    rest(1000);
        while (1==1){
            if (key[KEY_ESC])
            return;}}
    
bool opmode_pause(){
    if (mute==1)
    play_sample(pause, 255, 128, 1000, 0);
    remove_int(*inc_ptim1);
    rectfill(screen, 230, 100, 350, 200, makecol(60,60,60));
    textout_ex(screen, font, "PAUSED:", 280, 110, makecol(255,0,0), makecol(60,60,60));
    textout_ex(screen, font, "1.Continue", 250, 140, makecol(255,0,0), makecol(60,60,60));
    textout_ex(screen, font, "2.Quit", 250, 160, makecol(255,0,0), makecol(60,60,60));
    while (1==1){
    if (key[KEY_1]){
    install_int(*inc_ptim1, 1000);
    return true;}
    if (key[KEY_2])
    return false;}}
    
void opmode_countdown(){
    draw_sprite(screen, buffer, 0, 0);
    textout_ex(screen, font, "3", 320, 200, makecol(255,0,0), makecol(0,0,0));
    rest(1000);
    textout_ex(screen, font, "2", 320, 200, makecol(255,0,0), makecol(0,0,0));
    rest(1000);
    textout_ex(screen, font, "1", 320, 200, makecol(255,0,0), makecol(0,0,0));
    rest(1000);
    textout_ex(screen, font, "GO!!", 320, 200, makecol(0,255,0), makecol(0,0,0));
    rest(1000);}
    
void opmode_loop(){
    int countd = 1;
    fps = 35;
    
    while (con){
        if (exec_loop-te>=1 || fps<=22 && cycles>150){ //check if timer says to loop or if fps is too low
    //physics/gameplay
    get_p1_input();
    handle_bots();
    handle_parts();//needs to be before pts_4... so particle system can be created
    handle_stars();
    pts_4_dead_bots();
    respawn_dead_bots();
    handle_las();
    handle_col();
    
    //rendering
    opmode_cam();
    draw_1p_screen();
    
    //help prevent lag
    cycles++;
    
    //top-level input
    if (key[KEY_ENTER])
    con = opmode_pause();
    
    if (countd==1){
    opmode_countdown(); //3,2,1 stuff
    countd = 2;}
    
    te = exec_loop;//reset timer
}
calc_fps();}//************END_OF_LOOP******************//
    
    p1.score+= secs1 * 500;
    
    //debreif
    opmode_after_game();
    drawmen = 1;
    
    //reset for replay
    con = true;}
