/*
 * fight_class.cpp
 *
 *  Created on: 2010.04.26.
 *      Author: Szabó László
 */

#include "fight_class.hpp"

fighting :: fighting(){

	sprite_sheet = load( "minigames/fight_sheet.png" );

	x = rand() % 500 + 40;
	y = rand() % 300 + 40;

	shift_box();

	frame = 0;

	there_is = true;

	offsets[ 0 ].x = 0;
	offsets[ 0 ].y = 0;
	offsets[ 0 ].w = 40;
	offsets[ 0 ].h = 70;

	offsets[ 1 ].x = 40;
	offsets[ 1 ].y = 0;
	offsets[ 1 ].w = 40;
	offsets[ 1 ].h = 70;

	offsets[ 2 ].x = 80;
	offsets[ 2 ].y = 0;
	offsets[ 2 ].w = 40;
	offsets[ 2 ].h = 70;

	offsets[ 3 ].x = 120;
	offsets[ 3 ].y = 0;
	offsets[ 3 ].w = 40;
	offsets[ 3 ].h = 70;

}

fighting :: ~fighting(){

	SDL_FreeSurface( sprite_sheet );

}

void fighting :: shift_box(){

	hitbox.x = x;
	hitbox.y = y;
	hitbox.w = 40;
	hitbox.h = 40;

}

void fighting :: show( SDL_Surface* & screen ){
	frame++;
	if( frame >= 12 )frame = 0;

	if( there_is )
		apply( x, y, sprite_sheet, screen, &offsets[ frame / 3 ] );

}

void fighting :: replace(){
	if( there_is == false ){
		x = rand() % 500 + 40;
		y = rand() % 300 + 40;

		shift_box();

		there_is = true;
	}
}

SDL_Rect fighting :: get_hitbox(){
	return hitbox;
}

bool fighting :: is_on(){
	return there_is;
}

void fighting :: collect( int & score, SDL_Surface* & screen ){
	there_is = false;

	const int LEFT = 1;
	const int RIGHT = 0;
	const int VELOCITY = 10;

	TTF_Font* font = TTF_OpenFont( "text.ttf", 50 );
	SDL_Color textcolor = { 100, 50, 10 };
	SDL_Surface* message = NULL;

	Mix_Chunk* effect = Mix_LoadWAV( "sfx/sword1.wav" );

	int i,j;

	SDL_Event minievent;
	bool end = false;

	SDL_Surface* bg1 = load( "minigames/ship.png" );
	SDL_Surface* bg2 = load( "minigames/water.png" );
	int bgx = 0;

	//Other pirates

	SDL_Surface* pirates = load( "minigames/pirates.png" );

	int op_frames = 0;

	SDL_Rect p_offsets[ 4 ][ 2 ];

	for( i = 0; i < 4; i++ ){
		for( j = 0; j < 2; j++ ){
			p_offsets[ i ][ j ].x = j * 80;
			p_offsets[ i ][ j ].y = i * 80;
			p_offsets[ i ][ j ].w = 80;
			p_offsets[ i ][ j ].h = 80;
		}
	}

	typedef struct{
		int type;
		SDL_Rect hitbox;
	} pirate;
	pirate Pirates[ 6 ];

	for( i = 0; i < 6; i++ ){
		if( i % 2 == 1 ){
			Pirates[ i ].hitbox.x = -80;
			Pirates[ i ].hitbox.y = 115 + ( i * 50 );
			Pirates[ i ].hitbox.w = 60;
			Pirates[ i ].hitbox.h = 50;

			Pirates[ i ].type = rand() % 2 + 2;
		}
		else{
			Pirates[ i ].hitbox.x = 720;
			Pirates[ i ].hitbox.y = 200 + ( i * 50 );
			Pirates[ i ].hitbox.w = 80;
			Pirates[ i ].hitbox.h = 50;

			Pirates[ i ].type = rand() % 2;
		}
	}


	//PIRATES
	int pt_frame = 0;
	SDL_Surface* pt1 = load( "sprites/pirate_ssheet.png" );
	SDL_Surface* pt2 = load( "sprites/pirate2_ssheet.png" );

	SDL_Rect offsets[ 2 ][ 4 ];

	SDL_Rect shitbox1, shitbox2;

	int STATUS1 = LEFT, STATUS2 = LEFT;
	int xVel1=0, xVel2=0, yVel1=0, yVel2=0;
	bool moved1 = false, moved2 = false;

	SDL_Rect hitbox1, hitbox2;
	hitbox1.x = (640-80)/2;
	hitbox1.y = 200;
	hitbox1.w = 80;
	hitbox1.h = 80;

	hitbox2.x = (640-80)/2;
	hitbox2.y = 300;
	hitbox2.w = 80;
	hitbox2.h = 80;

	bool slash1 = false, slash2 = false;
	int slash1c = 0, slash2c = 0;

	for( i = 0; i < 2; i++ )
		for( j = 0; j < 4; j++ ){
			offsets[ i ][ j ].x = j*80;
			offsets[ i ][ j ].y = i*80;
			offsets[ i ][ j ].w = 80;
			offsets[ i ][ j ].h = 80;
		}

	while( !end ){

		while( SDL_PollEvent( &minievent ) ){

			if( minievent.type == SDL_QUIT || minievent.type == SDL_KEYDOWN && minievent.key.keysym.sym == SDLK_ESCAPE )
				end = true;


			if( minievent.type == SDL_KEYDOWN ){
				switch( minievent.key.keysym.sym ){

					case SDLK_UP : yVel1 -= VELOCITY; moved1 = true; break;
					case SDLK_DOWN : yVel1 += VELOCITY; moved1 = true; break;
					case SDLK_LEFT : xVel1 -= VELOCITY; moved1 = true; break;
					case SDLK_RIGHT : xVel1 += VELOCITY; moved1 = true; break;
					case SDLK_RCTRL :
										Mix_PlayChannel( -1, effect, 0 );
										slash1 = true;
										slash1c = 0;

										if( STATUS1 == RIGHT ){
											shitbox1.x = hitbox1.x + 80 + 20;
											shitbox1.y = hitbox1.y;
											shitbox1.w = 20;
											shitbox1.h = 80;
										}
										else{
											shitbox1.x = hitbox1.x - 40;
											shitbox1.y = hitbox1.y;
											shitbox1.w = 20;
											shitbox1.h = 80;
										}

										break;

					case SDLK_w : yVel2 -= VELOCITY; moved2 = true; break;
					case SDLK_s : yVel2 += VELOCITY; moved2 = true; break;
					case SDLK_a : xVel2 -= VELOCITY; moved2 = true; break;
					case SDLK_d : xVel2 += VELOCITY; moved2 = true; break;
					case SDLK_g :

										Mix_PlayChannel( -1, effect, 0 );
										slash2 = true;
										slash2c = 0;

										if( STATUS2 == RIGHT ){
											shitbox2.x = hitbox2.x + 80 + 20;
											shitbox2.y = hitbox2.y;
											shitbox2.w = 20;
											shitbox2.h = 80;
										}
										else{
											shitbox2.x = hitbox2.x - 40;
											shitbox2.y = hitbox2.y;
											shitbox2.w = 20;
											shitbox2.h = 80;
										}

										break;

					default : break;
				}
			}

			if( minievent.type == SDL_KEYUP ){
				switch( minievent.key.keysym.sym ){

					case SDLK_UP : if( moved1 ) yVel1 += VELOCITY; break;
					case SDLK_DOWN : if( moved1 )  yVel1 -= VELOCITY; break;
					case SDLK_LEFT : if( moved1 ) xVel1 += VELOCITY; break;
					case SDLK_RIGHT : if( moved1 ) xVel1 -= VELOCITY; break;

					case SDLK_w : if( moved2 ) yVel2 += VELOCITY; break;
					case SDLK_s : if( moved2 ) yVel2 -= VELOCITY; break;
					case SDLK_a : if( moved2 ) xVel2 += VELOCITY; break;
					case SDLK_d : if( moved2 ) xVel2 -= VELOCITY; break;

					default : break;
				}
			}

		}

			//moving pirates and setting STATUSES

			if( slash1 ){
				slash1c++;
				if( slash1c >= 3 )slash1 = false;
			}
			if( slash2 ){
				slash2c++;
				if( slash2c >= 3 )slash2 = false;
			}

			hitbox1.x += xVel1;
			if( hitbox1.x < 0 || hitbox1.x > 560 ) hitbox1.x -= xVel1;
			if( xVel1 > 0 )STATUS1 = RIGHT;
			if( xVel1 < 0 )STATUS1 = LEFT;
			hitbox2.x += xVel2;
			if( hitbox2.x < 0 || hitbox2.x > 560 ) hitbox2.x -= xVel2;
			if( xVel2 > 0 )STATUS2 = RIGHT;
			if( xVel2 < 0 )STATUS2 = LEFT;

			hitbox1.y += yVel1;
			if( hitbox1.y < 100 || hitbox1.y > 400 ) hitbox1.y -= yVel1;
			hitbox2.y += yVel2;
			if( hitbox2.y < 100 || hitbox2.y > 400 ) hitbox2.y -= yVel2;

			//checking die
			for( j = 0; j < 6; j++ ){
				if( collision( hitbox1, Pirates[ j ].hitbox ) )
					end = true;
				if( collision( hitbox2, Pirates[ j ].hitbox ) )
					end = true;
			}

			//moving other pirates
			for( int i = 0; i < 6; i++ ){
				if( i % 2 == 1 ){

					Pirates[ i ].hitbox.x += VELOCITY/2;

					if( Pirates[ i ].hitbox.x > 640 ){
						Pirates[ i ].hitbox.x = -80;
						Pirates[ i ].type = rand() % 2 + 2;
					}

				}
				else {

					Pirates[ i ].hitbox.x -= VELOCITY/2;

					if( Pirates[ i ].hitbox.x < -80 ) {
						Pirates[ i ].hitbox.x = 720;
						Pirates[ i ].type = rand() % 2;
					}

				}
			}

			//killing other pirates
			if( slash1 ){
				for( j = 0; j < 6; j++ ){
					if( collision( shitbox1, Pirates[ j ].hitbox ) ){
						if( j % 2 == 1 ){
							Pirates[ j ].hitbox.x = -80;
							Pirates[ j ].type = rand() % 2 + 2;
						}
						else{
							Pirates[ j ].hitbox.x = 720;
							Pirates[ j ].type = rand() % 2;
						}
						score += 1000;
					}
				}
			}
			if( slash2 ){
				for( j = 0; j < 6; j++ ){
					if( collision( shitbox2, Pirates[ j ].hitbox ) ){
						if( j % 2 == 1 ){
							Pirates[ j ].hitbox.x = -80;
							Pirates[ j ].type = rand() % 2 + 2;
						}
						else{
							Pirates[ j ].hitbox.x = 720;
							Pirates[ j ].type = rand() % 2;
						}
						score += 1000;
					}
				}
			}


			//RENDEREING****
			bgx-=2;
			if( bgx <= -640 ) bgx = 0;
			apply( bgx, 0, bg2, screen );
			apply( bgx+640, 0, bg2, screen );

			apply( 0, 0, bg1, screen );

			std::stringstream caption;
			caption<<"Score: "<<score;

			message = TTF_RenderText_Solid( font, caption.str().c_str(), textcolor );
			apply( 0, 5, message, screen );
			SDL_FreeSurface( message );


			//Other Pirates
				op_frames++;
				if( op_frames >= 4 )op_frames = 0;
				for( j = 5; j >= 0; j-- ){
					apply( Pirates[ j ].hitbox.x, Pirates[ j ].hitbox.y, pirates, screen, &p_offsets[ Pirates[ j ].type ][ op_frames / 2 ] );
				}

			//Other Pirates

			pt_frame++;
			if( pt_frame >= 9 )pt_frame = 0;
			if( !slash1 )
			apply( hitbox1.x, hitbox1.y, pt1, screen, &offsets[ STATUS1 ][ pt_frame / 3 ] );
			else
				apply( hitbox1.x, hitbox1.y, pt1, screen, &offsets[ STATUS1 ][ 3 ] );
			if( !slash2 )
			apply( hitbox2.x, hitbox2.y, pt2, screen, &offsets[ STATUS2 ][ pt_frame / 3 ] );
			else
				apply( hitbox2.x, hitbox2.y, pt2, screen, &offsets[ STATUS2 ][ 3 ] );

			SDL_Flip( screen );
			SDL_Delay( 40 );
	}

	SDL_Delay( 1000 );

	SDL_FreeSurface( bg1 );
	SDL_FreeSurface( bg2 );
	SDL_FreeSurface( pirates );
	SDL_FreeSurface( pt1 );
	SDL_FreeSurface( pt2 );
	Mix_FreeChunk( effect );
	TTF_CloseFont( font );
}
