#include <stdlib.h>
#include <math.h>
#include "vector.h"
#include "draw.h"
#include "bullet.h"
#include "ship.h"

Ship::Ship(const Vector &n, double d, const Vector &v, bool a){
	bullets=0;
	alive=a;
	set(n,d,v);
}

Ship::Ship(const Ship &copy){
	bullets=copy.bullets;
	alive=copy.alive;
	set(copy.center,copy.theta,copy.velocity);
	for(int i=0; i<bullets; i++)
		bullet[i]=copy.bullet[i];
}

Ship::~Ship(){
}

Vector Ship::getCenter(){
	return (center);
}

void Ship::draw(byte_t color, byte_t bulletcolor, byte_t* where){
	line(nose,leftwing,color, where);
	line(nose,rightwing,color, where);
	for(int i=0; i<bullets; i++)
		bullet[i].draw(bulletcolor, where);
}

void Ship::set(const Vector &n, double d, const Vector &v){
	center=n;
	theta=d;
	velocity=v;
	nose.set(center.x+5*cos(theta),center.y+5*sin(theta));
	leftwing.set(nose.x+10*cos(theta-Pi*.9), nose.y+10*sin(theta-Pi*.9));
	rightwing.set(nose.x+10*cos(theta+Pi*.9), nose.y+10*sin(theta+Pi*.9));
}

void Ship::jump(const Vector &n){
	set(n,theta,Vector(0,0));
}

void Ship::attitude(double d){
	set(center,d,velocity);
}

void Ship::left(){
	attitude(theta-Pi/16);
	while(theta<0)
	theta+=2*Pi;
}

void Ship::right(){
	attitude(theta+Pi/16);
	while(theta>2*Pi)
	theta-=2*Pi;
}

void Ship::turn(double t){
	attitude(theta+t);
	while(theta<0)
	theta+=2*Pi;
	while(theta>2*Pi)
	theta-=2*Pi;
}

void Ship::push(int ticks){
	//if(mag(velocity)<20)
	velocity=velocity+ticks*Vector(.01*cos(theta), .01*sin(theta))/5.0;
}

void Ship::slideUp(int ticks){
	center=center-ticks*Vector(0,0.1);
	velocity=velocity+Vector(0,-0.4);
}

void Ship::slideDown(int ticks){
	center=center+ticks*Vector(0,0.1);
	velocity=velocity+Vector(0,0.4);
}

void Ship::slideLeft(int ticks){
	center=center-ticks*Vector(0.1,0);
	velocity=velocity+Vector(-0.4,0);
}

void Ship::slideRight(int ticks){
	center=center+ticks*Vector(0.1,0);
	velocity=velocity+Vector(0.4,0);
}



void Ship::fly(int ticks){
	if (dead()) return;
	static int remainder=0;
	ticks+=remainder;
	remainder=ticks%5;
	//for(int i=0; i<ticks/5; i++){
	//	velocity=.995*velocity;
	//	center=center+velocity;
	//}
	while (center.x<0)
		center.x+=XMAX;
	while (center.x>=XMAX)
		center.x-=XMAX;
	while(center.y<0)
		center.y+=YMAX;
	while(center.y>=YMAX)
		center.y-=YMAX;
	attitude(theta);
	for(int i=0; i<bullets; i++)
		bullet[i].fly(ticks);
	if(bullets>0){
		for(int j=0; j<bullets; j++){
			if (bullet[j].dead()){
				bullets--;
				for(int i=j; i<bullets; i++)
					bullet[i]=bullet[i+1];
			}
		}
	}
	velocity=Vector(0,0);
}

void Ship::shoot(int i){
	if(i<=0){
		return;
	}
	if(i<=(MAXBULLETS-bullets)){
		double angle=(i/2)*0.1;
		if(i%2) angle=-angle;
		shoot(angle);
	}
	shoot(i-1);
}

void Ship::shoot(double angle){
	if (bullets<MAXBULLETS){
		bullet[bullets].shoot(nose,velocity+Vector(1.2*cos(theta+angle), 1.2*sin(theta+angle)));
		bullets++;
	}
}

int Ship::shots(){
	return (bullets);
}

bool Ship::collide(Circle c){
	return ((c.on(nose))||(c.on(leftwing))||(c.on(rightwing)));
}

void Ship::kill(){
	alive=false;
}

void Ship::live(){
	alive=true;
}

bool Ship::dead(){
	return(!alive);
}

Bullet& bullet(Ship& s, int i){
	if (i<=s.bullets)
		return (s.bullet[i]);
	return(s.bullet[0]);
}
