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

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"

#include "minig_class.hpp"
#include "funcs.hpp"

minis :: minis(){

	effect = Mix_LoadWAV( "sfx/mini_enter.wav" );

	sheet = SDL_DisplayFormat( IMG_Load( "minigames/map_sheet.png" ) );
	SDL_SetColorKey( sheet, SDL_SRCCOLORKEY, SDL_MapRGB( sheet->format, 0, 0xFF, 0xFF ) );
	there_is = false;
	frame = 0;

	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;
}


minis :: ~minis(){
	SDL_FreeSurface( sheet );
	Mix_FreeChunk( effect );
}

void minis :: place(){
	if( !there_is ){
		there_is = true;

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

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

		game = rand() % 3;
	}
}

void minis :: show( SDL_Surface* & screen ){
	if( there_is ){
		apply( x, y, sheet, screen, &offsets[ frame / 3 ] );
		frame++;
		if( frame >= 12 )frame = 0;
	}
}

void minis :: collect( int & score, SDL_Surface* & screen ){
	if( there_is ){
		Mix_PlayChannel( -1, effect, 0 );
		if( game == 0 ){
			mini1( score, screen );
		}
		if( game == 1 ){
			mini2( score, screen );
		}
		if( game == 2 ){
			mini3( score, screen );
		}


		there_is = false;
	}
}

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

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