//ships.cpp                      //contains movement functions, check_hit, etc
#include <allegro.h>
#include "globals.h"
#include "classes.h"
#include "1pmode.h"
#include "2pmode.h"
#include "lasers.h"
#include "bots.h"
#include "render.h"
#include "collisions.h"

void ship::shoot(){ 
    las.push_back(laser(x, y, dir));
    int c1;
    c1 = las.size()-1; //access laser created
    las[c1].change_xy(dir, rad/8+1);} //push laser outside of ship
    
void playership::playership_init(int p){
    score = 0;
    speed = 1;
    isdead = false;
    turbo = false;
    rtim = 0;
    ttim = 0;
    rad = 27;
    cst = 0;
    if (p==1){
        x = -500;
        y = -500;
        if (mode==2){
            dir = 8;
            health = 50;}
        else {
            dir = 0;
            health = 70;}}
            
    if (p==2){
        x = -400;
        y = -500;
        dir = 24;
        health = 50;}}
    
void get_p1_input(){ //also handles player variables
p1.tx = p1.x;
p1.ty = p1.y;

  if (p1.health>0){
    if (key[KEY_X])
        p1.dir--;
    if (key[KEY_C])
        p1.dir++;
        
    if (key[KEY_SPACE]){
        p1.cst++;
        p1.change_xy(p1.dir, p1.speed);}
        
    if (key[KEY_N] && p1.turbo!=true){
        if (mute==1)
        play_sample(turbofx, 255, 128, 1000, 0);
        p1.turbo = true;
        p1.ttim = 60;
        p1.speed = 2;}
        
    if (key[KEY_B] && p1.shooting==0){
        if (mute==1)
        play_sample(lassfx, 255, 128, 1000, 0);
        p1.shoot();
        p1.shooting = 8;}
        
    if (p1.ttim>0)
        p1.ttim--;
        
    if (p1.rtim>0)
        p1.rtim--;
        
    if (p1.ttim==1){
        p1.speed = 1;
        p1.rtim = 300;}
    
    if (p1.rtim==1)
        p1.turbo = false;
        
    if (p1.shooting>0)
        p1.shooting--;
        
    if (p1.htim>0)
    p1.htim--;
    
    if (mode==1){
        if (p1.htim==0 && p1.health<70)
        p1.health++;}
    if (mode==2){
        if (p1.htim==0){
        if (p1.health<50 && ovom==false)
        p1.health++;
        if (p1.health<100 && ovom==true)
        p1.health++;}}
        
    if (p1.dir>32)
     p1.dir = 0;
    if (p1.dir<0)
     p1.dir = 32;}
     
    //****************************//
    if (p1.health<=0){//stops timer when dead
    remove_int(*inc_ptim1);
    if (mode==1)
    con = false;}}
     
void get_p2_input(){ //also handles player variables
p2.tx = p2.x;
p2.ty = p2.y;

  if (p2.health>0){
    if (key[KEY_LEFT])
        p2.dir--;
    if (key[KEY_RIGHT])
        p2.dir++;
        
    if (key[KEY_RCONTROL]){
        p2.cst++;
        p2.change_xy(p2.dir, p2.speed);}
        
    if (key[KEY_RSHIFT] && p2.turbo!=true){
        if (mute==1)
        play_sample(turbofx, 225, 128, 1000, 0);
        p2.turbo = true;
        p2.ttim = 60;
        p2.speed = 2;}
        
    if (key[KEY_SLASH] && p2.shooting==0){
        if (mute==1)
        play_sample(lassfx, 225, 128, 1000, 0);
        p2.shooting = 8;
        p2.shoot();}
        
    if (p2.ttim>0)
        p2.ttim--;
        
    if (p2.rtim>0)
        p2.rtim--;
        
    if (p2.ttim==1){
        p2.speed = 1;
        p2.rtim = 300;}
    
    if (p2.rtim==1)
        p2.turbo = false;
        
    if (p2.shooting>0)
        p2.shooting--;
        
    if (p2.htim>0)
    p2.htim--;
    
    if (p2.htim==0){
        if (p2.health<100 && ovom==true)
        p2.health++;
        if (p2.health<50 && ovom==false)
        p2.health++;}
        
    if (p2.dir>32)
     p2.dir = 0;
    if (p2.dir<0)
     p2.dir = 32;}
     
    //*****************************************//
    if (p2.health<=0){ //so timer stops when dead
    remove_int(*inc_ptim2);}
    
    if (p2.health<=0 && p1.health<=0) //both players DEAD
    con = false;}
        
        
void thing::change_xy(int ir, int sp){
     if (ir==0){
        x+=0*sp;
        y-=8*sp;}
     if (ir==1){
        x+=1*sp;
        y-=8*sp;}
     if (ir==2){
        x+=3*sp;
        y-=7*sp;}
     if (ir==3){
        x+=4*sp;
        y-=7*sp;}
     if (ir==4){
        x+=6*sp;
        y-=6*sp;}
     if (ir==5){
        x+=7*sp;
        y-=4*sp;}
     if (ir==6){
        x+=7*sp;
        y-=2*sp;}
     if (ir==7){
        x+=8*sp;
        y-=1*sp;}
        
     if (ir==8){
        x+=8*sp;
        y-=0*sp;} 
     if (ir==9){
        x+=8*sp;
        y+=1*sp;}
     if (ir==10){
        x+=7*sp;
        y+=3*sp;}
     if (ir==11){
        x+=7*sp;
        y+=4*sp;}
     if (ir==12){
        x+=6*sp;
        y+=6*sp;}
     if (ir==13){
        x+=4*sp;
        y+=7*sp;}
     if (ir==14){
        x+=2*sp;
        y+=7*sp;}
     if (ir==15){
        x+=1*sp;
        y+=8*sp;}
     if (ir==16){
        x+=0*sp;
        y+=8*sp;}
     
     if (ir==17){
        x-=1*sp;
        y+=8*sp;}
     if (ir==18){
        x-=3*sp;
        y+=7*sp;}
     if (ir==19){
        x-=4*sp;
        y+=7*sp;}
     if (ir==20){
        x-=6*sp;
        y+=6*sp;}
     if (ir==21){
        x-=7*sp;
        y+=4*sp;}
     if (ir==22){
        x-=7*sp;
        y+=2*sp;}
     if (ir==23){
        x-=8*sp;
        y+=1*sp;}
     if (ir==24){
        x-=8*sp;
        y+=0*sp;}
        
     if (ir==25){
        x-=8*sp;
        y-=1*sp;}
     if (ir==26){
        x-=7*sp;
        y-=3*sp;}
     if (ir==27){
        x-=7*sp;
        y-=4*sp;}
     if (ir==28){
        x-=6*sp;
        y-=6*sp;}
     if (ir==29){
        x-=4*sp;
        y-=7*sp;}
     if (ir==30){
        x-=2*sp;
        y-=7*sp;}
     if (ir==31){
        x-=1*sp;
        y-=8*sp;}
     if (ir==32){
        x-=0*sp;
        y-=8*sp;}}
