/*
 * mini1.cpp
 *
 *  Created on: 2010.04.16.
 *      Author: Szabó László
 */

#include "mini1.hpp"
#include "funcs.hpp"
#include <sstream>

const int GROUND = 0;
const int TRAP = 1;
const int EMPTY = 2;

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

	int frame = 0;
	int i, jump_h1, jump_h2;
	bool end = false;
	bool coll1 = false;
	bool coll2 = false;
	bool jump1 = true;
	bool jump2 = true;

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


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

	SDL_Event minievent;

	SDL_Rect hitbox1, hitbox2;

	SDL_Rect offsets[ 3 ];

	SDL_Surface* pirate1_sheet = load( "./sprites/pirate_ssheet.png" );
	SDL_Surface* pirate2_sheet = load( "./sprites/pirate2_ssheet.png" );

	int bgx;
	SDL_Surface* bg = load("minigames/bg.png");


	//map element
	SDL_Surface* ground = load( "minigames/ground.png" );
	SDL_Surface* trap = load( "minigames/trap.png" );

	int map[ 8 ];
	SDL_Rect map_collision[ 8 ];
	for( i = 0; i < 8; i++ ){
		map[ i ] = GROUND;

		map_collision[ i ].x = i*80;
		map_collision[ i ].y = 320;
		map_collision[ i ].w = 80;
		map_collision[ i ].h = 80;
	}

	hitbox1.x = 0;
	hitbox1.y = 240;
	hitbox1.w = 70;
	hitbox1.h = 80;

	hitbox2.x = 80;
	hitbox2.y = 240;
	hitbox2.w = 70;
	hitbox2.h = 80;

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

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

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


	bgx = 0;
	while( !end ){

		coll1 = false;
		coll2 = false;




		//applying elements

		apply( bgx, 0, bg, screen );
		apply( bgx + 640, 0, bg, screen );
		bgx -= 2;
		if( bgx == -640 )bgx = 0;

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


		apply( hitbox1.x, hitbox1.y, pirate1_sheet, screen, &offsets[ frame ] );
		apply( hitbox2.x, hitbox2.y, pirate2_sheet, screen, &offsets[ frame ] );
		for( i = 0; i < 8; i++ ){
				if( map[ i ] == GROUND )apply( map_collision[ i ].x, map_collision[ i ].y, ground, screen );
				if( map[ i ] == TRAP )apply( map_collision[ i ].x, map_collision[ i ].y, trap, screen );
		}


		//moving map and frame
		for( i = 0; i < 8; i++ ){
			map_collision[ i ].x -= 10;

			//quitting element
			if( map_collision[ i ].x <= -80 ){
				map_collision[ i ].x = 560;


				if( rand() % 2 ){
					map_collision[ i ].y -= 80;
				}
				else {
					map_collision[ i ].y += 80;
				}


				if( map_collision[ i ].y < 0 )map_collision[ i ].y = 0;
				if( map_collision[ i ].y > 400 )map_collision[ i ].y = 400;

				map[ i ] = rand() % 2;

				for( int h = 0; h < 8; h++ )
					if( i != h && map[ h ] == TRAP )map[ i ] = GROUND;


				score += 250;
			}
		}
		frame++;
		if( frame >= 3 )frame = 0;


		while( SDL_PollEvent( &minievent ) ){
			if( minievent.type == SDL_QUIT )
				end = true;

			if( minievent.type == SDL_KEYDOWN ){
				if( minievent.key.keysym.sym == SDLK_UP && jump1 && jump_h1 == 0 ){
					jump1 = false;
					jump_h1 = 0;
					Mix_PlayChannel( -1, effect, 0 );
				}
				if( minievent.key.keysym.sym == SDLK_w && jump2 && jump_h2 == 0 ){
					jump2 = false;
					jump_h2 = 0;
					Mix_PlayChannel( -1, effect, 0 );
				}
				if( minievent.key.keysym.sym == SDLK_ESCAPE ){
					end = true;
				}
			}
		}

		SDL_Flip( screen );
		SDL_Delay( 40 );


		//jumping
		if( jump1 == false && jump_h1 <= 5 ){
			hitbox1.y -= 40;
			jump_h1++;
		}

		if( jump2 == false && jump_h2 <= 5 ){
			hitbox2.y -= 40;
			jump_h2++;
		}

		if( jump_h1 >= 5 ){
			jump_h1++;
			if( jump_h1 > 8 )
			jump1 = true;
		}
		if( jump_h2 >= 5 ){
			jump_h2++;
			if( jump_h2 > 8 )
			jump2 = true;
		}

		//collision
		for( i = 0; i < 8; i++ ){
			if( map_collision[ i ].y >= hitbox1.y + hitbox1.h && map[ i ] != EMPTY )
			if( collision( hitbox1, map_collision[ i ])){
				coll1=true;
				jump1 = true;
				jump_h1 = 0;

				if( map[ i ] == TRAP && ( ( hitbox1.x + 10 > map_collision[ i ].x )  )){
					end = true;
				}
				if( map[ i ] == TRAP && ( ( hitbox1.x + hitbox1.w - 10 < map_collision[ i ].x + map_collision[ i ].w ) )){
					end = true;
				}
			}
			if( map_collision[ i ].y >= hitbox2.y + hitbox2.h && map[ i ] != EMPTY )
			if( collision( hitbox2, map_collision[ i ])){
				coll2=true;
				jump2 = true;
				jump_h2 = 0;

				if( map[ i ] == TRAP && ( ( hitbox2.x + 10 > map_collision[ i ].x ) )){
					end = true;
				}
				if( map[ i ] == TRAP && ( ( hitbox2.x + hitbox2.w - 10 < map_collision[ i ].x + map_collision[ i ].w ) )){
					end = true;
				}
			}
		}


		if( !coll1 && jump1 )
		hitbox1.y += 20;
		if( !coll2 && jump2 )
		hitbox2.y += 20;

		if( hitbox1.y > 480 || hitbox2.y > 480 ) end = true;
	}

	SDL_Delay( 1000 );

	SDL_FreeSurface( pirate1_sheet );
	SDL_FreeSurface( pirate2_sheet );
	SDL_FreeSurface( ground );
	SDL_FreeSurface( trap );
	SDL_FreeSurface( bg );

	Mix_FreeChunk( effect );

	TTF_CloseFont( font );
}
