#include "apple.hpp"

apples :: apples(){

	CollectSound = Mix_LoadWAV( "sounds/collect.wav" );

	AppleSheet = load_image( "items/apple.png" );

	for( int i = 0; i < AppleNumber; i++ ){
		Coords[ i ].x = - 50;
		Coords[ i ].y = - 50;
	}

	Coords[ 0 ].x = 583;
	Coords[ 0 ].y = 694;
	Coords[ 1 ].x = 715;
	Coords[ 1 ].y = 559;
	Coords[ 2 ].x = 1964;
	Coords[ 2 ].y = 995;
	Coords[ 3 ].x = 3161;
	Coords[ 3 ].y = 777;
	Coords[ 4 ].x = 3770;
	Coords[ 4 ].y = 1312;
	Coords[ 5 ].x = 4876;
	Coords[ 5 ].y = 1805;
	Coords[ 6 ].x = 5379;
	Coords[ 6 ].y = 1560;
	Coords[ 7 ].x = 6447;
	Coords[ 7 ].y = 586;
	Coords[ 8 ].x = 6754;
	Coords[ 8 ].y = 477;
	Coords[ 9 ].x = 7945;
	Coords[ 9 ].y = 1213;
	Coords[ 10 ].x = 7480;
	Coords[ 10 ].y = 2222;
	Coords[ 11 ].x = 7136;
	Coords[ 11 ].y = 2627;
	Coords[ 12 ].x = 5578;
	Coords[ 12 ].y = 2817;
	Coords[ 13 ].x = 5308;
	Coords[ 13 ].y = 2765;
	Coords[ 14 ].x = 4540;
	Coords[ 14 ].y = 3485;
	Coords[ 15 ].x = 3488;
	Coords[ 15 ].y = 3516;
	Coords[ 16 ].x = 2242;
	Coords[ 16 ].y = 2881;
	Coords[ 17 ].x = 1525;
	Coords[ 17 ].y = 2881;
	Coords[ 18 ].x = 693;
	Coords[ 18 ].y = 3597;
	Coords[ 19 ].x = 1197;
	Coords[ 19 ].y = 3960;
	Coords[ 20 ].x = 1657;
	Coords[ 20 ].y = 4661;
	Coords[ 21 ].x = 506;
	Coords[ 21 ].y = 4883;
	Coords[ 22 ].x = 2169;
	Coords[ 22 ].y = 4920;
	Coords[ 23 ].x = 3547;
	Coords[ 23 ].y = 4353;
	Coords[ 24 ].x = 4599;
	Coords[ 24 ].y = 4354;
	Coords[ 25 ].x = 5124;
	Coords[ 25 ].y = 4668;
	Coords[ 26 ].x = 6067;
	Coords[ 26 ].y = 4834;
	Coords[ 27 ].x = 6517;
	Coords[ 27 ].y = 5187;
	Coords[ 28 ].x = 7488;
	Coords[ 28 ].y = 5348;
	Coords[ 29 ].x = 7519;
	Coords[ 29 ].y = 5859;
	Coords[ 30 ].x = 8641;
	Coords[ 30 ].y = 5642;


	for( int i = 0; i < AppleNumber; i++ ){
		Coords[ i ].w = 11;
		Coords[ i ].h = 30;
		 isOut[ i ] = true;
	}

}

apples :: ~apples(){

	Mix_FreeChunk( CollectSound );
	SDL_FreeSurface( AppleSheet );

}

void apples :: move( int xVel, int yVel, bool collision ){
	if( !collision ){

		for( int i = 0; i < AppleNumber; i++ ){
			Coords[ i ].x -= xVel;
			Coords[ i ].y -= yVel;
		}

	}
}

void apples :: show( SDL_Surface* & screen ){

	for( int i = 0; i < AppleNumber; i++ ){
		if( Coords[ i ].x > - 21 && Coords[ i ].x < 800 &&
			Coords[ i ].y > - 61 && Coords[ i ].y < 600 && isOut[ i ] )
		apply_surface( Coords[ i ].x, Coords[ i ].y, AppleSheet, screen );
	}

}

void apples :: collect( int& RHApples){

	SDL_Rect RHHitbox;

	RHHitbox.x = 400;
	RHHitbox.y = 300;
	RHHitbox.w = 31;
	RHHitbox.h = 40;

	for( int i = 0; i < AppleNumber; i++ ){
		if( isOut[ i ] && check_collision( RHHitbox, Coords[ i ] ) && RHApples < 3 ){
			isOut[ i ] = false;
			RHApples++;
			Mix_PlayChannel( 3, CollectSound, 0 );
		}
	}

}

SDL_Surface* apples :: getSheet(){
	return AppleSheet;
}

bool* apples :: getIsOut(){
	return isOut;
}

SDL_Rect* apples :: getCoords(){
	return Coords;
}
