/*
 * mini3.cpp
 *
 *  Created on: 2010.04.19.
 *      Author: Szabó László
 */

#include "mini3.hpp"
#include <sstream>

void mini3( int & score, SDL_Surface* & screen ){

	SDL_Surface* ball = load( "./sprites/ball.bmp" );
	SDL_Surface* p1_sheet = load( "./sprites/pirate_ssheet.png" );
	SDL_Surface* p2_sheet = load( "./sprites/pirate2_ssheet.png" );
	SDL_Surface* bg = load( "minigames/shore.png" );

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

	TTF_Font * font = TTF_OpenFont( "./text.ttf", 36 );
	SDL_Color textcolor = { 200, 150, 0 };
	SDL_Surface* message = NULL;

	bool moved1 = false, moved2 = false;

	SDL_Rect offsets[ 3 ];
	SDL_Rect hitbox1, hitbox2;

	int i,j,speed = 1,frame=0;
	bool end = false;

	offsets[ 0 ].x = 0;
	offsets[ 0 ].y = 160;
	offsets[ 0 ].w = 80;
	offsets[ 0 ].h = 80;

	offsets[ 1 ].x = 80;
	offsets[ 1 ].y = 160;
	offsets[ 1 ].w = 80;
	offsets[ 1 ].h = 80;

	offsets[ 2 ].x = 160;
	offsets[ 2 ].y = 160;
	offsets[ 2 ].w = 80;
	offsets[ 2 ].h = 80;

	hitbox1.x = 560;
	hitbox1.y = 400;
	hitbox1.w = 80;
	hitbox1.h = 80;

	hitbox2.x = 0;
	hitbox2.y = 400;
	hitbox2.w = 80;
	hitbox2.h = 80;

	bool balls[ 2 ][ 8 ];
	SDL_Rect balls_hitb[ 2 ][ 8 ];

	for( i = 0; i < 2; i++ ){
		for( j = 0; j < 8; j++ ){
			balls[ i ][ j ] = true;

			balls_hitb[ i ][ j ].x = (640/8)*j;
			balls_hitb[ i ][ j ].y = i*240;
			balls_hitb[ i ][ j ].w = 20;
			balls_hitb[ i ][ j ].h = 10;

		}
	}

	balls[ 0 ][ rand() % 8 ] = false;
	balls[ 0 ][ rand() % 8 ] = false;
	balls[ 1 ][ rand() % 8 ] = false;
	balls[ 1 ][ rand() % 8 ] = false;

	SDL_Event minievent;


	int xVel1 = 0, yVel1 = 0, xVel2 = 0, yVel2 = 0;

	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 -= 10; moved1 = true; break;
					case SDLK_DOWN : yVel1 += 10; moved1 = true; break;
					case SDLK_LEFT : xVel1 -= 10; moved1 = true; break;
					case SDLK_RIGHT : xVel1 += 10; moved1 = true; break;

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

					default : break;
				}

			}

			if( minievent.type == SDL_KEYUP ){

							switch( minievent.key.keysym.sym ){

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

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

								default : break;
							}

						}
		}

		//move pirates
		hitbox1.x += xVel1;
		hitbox1.y += yVel1;
		if( hitbox1.x < 0 || hitbox1.x > 560 ) hitbox1.x -= xVel1;
		if( hitbox1.y < 0 || hitbox1.y > 400 ) hitbox1.y -= yVel1;
		hitbox2.x += xVel2;
		hitbox2.y += yVel2;
		if( hitbox2.x < 0 || hitbox2.x > 560 ) hitbox2.x -= xVel2;
		if( hitbox2.y < 0 || hitbox2.y > 400 ) hitbox2.y -= yVel2;


		//checking collision
		for( i = 0; i < 2; i++ ){
			for( j = 0; j < 8; j++ ){
				if( balls[ i ][ j ])
				if( collision( hitbox1, balls_hitb[ i ][ j ]) || collision( hitbox2, balls_hitb[ i ][ j ] ) )end = true;
			}
		}

		apply( 0, 0, bg, screen );
		std::stringstream str;
		str<<score;
		message = TTF_RenderText_Solid( font, "Score:", textcolor );
		apply( ( 640 - message->w )/2, ( 480 - message->h )/2 - 25, message, screen );
		SDL_FreeSurface( message );
		message = TTF_RenderText_Solid( font, str.str().c_str(), textcolor );
		apply( ( 640 - message->w )/2, ( 480 - message->h )/2, message, screen );
		SDL_FreeSurface( message );

		//move balls
		for( i = 0; i < 2; i++ ){
			for( j = 0; j < 8; j++ ){
				balls_hitb[ i ][ j ].y += (speed/10)+1;
				if( balls_hitb[ i ][ j ].y > 480 ){
					balls_hitb[ i ][ j ].y = 0;
					balls[ i ][ j ] = true;

					if( j == 0 )Mix_PlayChannel( -1, effect, 0 );
				}
			}
			if(balls_hitb[ i ][ 0 ].y == 0 ){
				balls[ i ][ rand() % 8 ] = false;
				balls[ i ][ rand() % 8 ] = false;

				score += 3500;

				speed+=5;
			}
		}
		//and show balls
		for( i = 0; i < 2; i++ ){
			for( j = 0; j < 8; j++ ){
				if( balls[ i ][ j ] )
					apply( balls_hitb[ i ][ j ].x, balls_hitb[ i ][ j ].y, ball, screen );
				balls_hitb[ i ][ j ].y++;

				if( balls_hitb[ i ][ j ].y > 480 )balls_hitb[ i ][ j ].y = 0;
			}
		}
		//show pirates
		apply( hitbox1.x, hitbox1.y, p1_sheet, screen, &offsets[ frame / 3 ] );
		apply( hitbox2.x, hitbox2.y, p2_sheet, screen, &offsets[ frame / 3 ] );

		frame++;
		if( frame >= 9 )frame = 0;

		SDL_Flip( screen );
		SDL_Delay( 40 );
	}

	Mix_FreeChunk( effect );
	SDL_Delay( 1000 );
	SDL_FreeSurface( ball );
	SDL_FreeSurface( p1_sheet );
	SDL_FreeSurface( p2_sheet );
	SDL_FreeSurface( bg );
	TTF_CloseFont( font );
}
