#include "candle.hpp"

candles :: candles(){

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

	CandleSheet = load_image( "items/candle.png" );
	CandleLight = load_image( "items/candle_light.png" );
	SDL_SetAlpha( CandleLight, SDL_SRCALPHA, 75 );

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

	Coords[ 0 ].x = 298;
	Coords[ 0 ].y = 495;
	Coords[ 1 ].x = 589;
	Coords[ 1 ].y = 495;
	Coords[ 2 ].x = 1161;
	Coords[ 2 ].y = 1016;
	Coords[ 3 ].x = 1840;
	Coords[ 3 ].y = 1001;
	Coords[ 4 ].x = 2023;
	Coords[ 4 ].y = 889;
	Coords[ 5 ].x = 1807;
	Coords[ 5 ].y = 652;
	Coords[ 6 ].x = 2979;
	Coords[ 6 ].y = 519;
	Coords[ 7 ].x = 2871;
	Coords[ 7 ].y = 624;
	Coords[ 8 ].x = 3060;
	Coords[ 8 ].y = 624;
	Coords[ 9 ].x = 2928;
	Coords[ 9 ].y = 711;
	Coords[ 10 ].x = 3027;
	Coords[ 10 ].y = 711;
	Coords[ 11 ].x = 4697;
	Coords[ 11 ].y = 1751;
	Coords[ 12 ].x = 5460;
	Coords[ 12 ].y = 413;
	Coords[ 13 ].x = 6226;
	Coords[ 13 ].y = 762;
	Coords[ 14 ].x = 7298;
	Coords[ 14 ].y = 894;
	Coords[ 15 ].x = 7323;
	Coords[ 15 ].y = 1973;
	Coords[ 16 ].x = 8236;
	Coords[ 16 ].y = 2575;
	Coords[ 17 ].x = 6357;
	Coords[ 17 ].y = 3170;
	Coords[ 18 ].x = 4637;
	Coords[ 18 ].y = 3371;
	Coords[ 19 ].x = 3417;
	Coords[ 19 ].y = 3427;
	Coords[ 20 ].x = 1977;
	Coords[ 20 ].y = 3866;
	Coords[ 21 ].x = 738;
	Coords[ 21 ].y = 3881;
	Coords[ 22 ].x = 433;
	Coords[ 22 ].y = 3755;
	Coords[ 23 ].x = 786;
	Coords[ 23 ].y = 4737;
	Coords[ 24 ].x = 781;
	Coords[ 24 ].y = 5821;
	Coords[ 25 ].x = 2046;
	Coords[ 25 ].y = 5655;
	Coords[ 26 ].x = 3773;
	Coords[ 26 ].y = 5580;
	Coords[ 27 ].x = 5356;
	Coords[ 27 ].y = 5572;
	Coords[ 28 ].x = 6326;
	Coords[ 28 ].y = 5066;
	Coords[ 29 ].x = 7540;
	Coords[ 29 ].y = 5682;
	Coords[ 30 ].x = 8118;
	Coords[ 30 ].y = 4318;
	Coords[ 31 ].x = 9315;
	Coords[ 31 ].y = 3805;
	Coords[ 32 ].x = 9168;
	Coords[ 32 ].y = 3937;
	Coords[ 33 ].x = 9453;
	Coords[ 33 ].y = 3937;
	Coords[ 34 ].x = 9378;
	Coords[ 34 ].y = 4039;
	Coords[ 35 ].x = 9249;
	Coords[ 35 ].y = 4039;
	Coords[ 36 ].x = 8939;
	Coords[ 36 ].y = 2815;
	Coords[ 37 ].x = 6909;
	Coords[ 37 ].y = 500;
	Coords[ 38 ].x = 6219;
	Coords[ 38 ].y = 503;
	Coords[ 39 ].x = 8369;
	Coords[ 39 ].y = 533;
	Coords[ 40 ].x = 6817;
	Coords[ 40 ].y = 3137;
	Coords[ 41 ].x = 4502;
	Coords[ 41 ].y = 4287;
	Coords[ 42 ].x = 2557;
	Coords[ 42 ].y = 4146;
	Coords[ 43 ].x = 2247;
	Coords[ 43 ].y = 4577;
	Coords[ 44 ].x = 2516;
	Coords[ 44 ].y = 5291;
	Coords[ 45 ].x = 449;
	Coords[ 45 ].y = 5421;
	Coords[ 46 ].x = 1775;
	Coords[ 46 ].y = 3018;
	Coords[ 47 ].x = 1744;
	Coords[ 47 ].y = 2510;
	Coords[ 48 ].x = 1638;
	Coords[ 48 ].y = 1890;
	Coords[ 49 ].x = 1080;
	Coords[ 49 ].y = 1419;
	Coords[ 50 ].x = 3535;
	Coords[ 50 ].y = 1597;
	Coords[ 51 ].x = 4347;
	Coords[ 51 ].y = 2160;

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

	Frame = 0;

	LFrame = 0;

	for( int i = 0; i < 3; i++ ){

		Offsets[ i ].x = i * 11;
		Offsets[ i ].y = 0;
		Offsets[ i ].w = 11;
		Offsets[ i ].h = 30;

	}

	for( int i = 0; i < 4; i++ ){

		LOffsets[ i ].x = i * 62;
		LOffsets[ i ].y = 0;
		LOffsets[ i ].w = 62;
		LOffsets[ i ].h = 62;

	}

}

candles :: ~candles(){

	Mix_FreeChunk( CollectSound );

	SDL_FreeSurface( CandleSheet );
	SDL_FreeSurface( CandleLight );

}

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

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

	}
}

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

	Frame++;
	if( Frame >= 3 ) Frame = 0;

	for( int i = 0; i < CandleNumber; 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, CandleSheet, screen, &Offsets[ Frame ] );
	}

}

void candles :: show_light( SDL_Surface* & screen ){

	LFrame++;
	if( LFrame >= 7 )LFrame = 0;

	for( int i = 0; i < CandleNumber; 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 - 26, Coords[ i ].y - 30, CandleLight, screen, &LOffsets[ LFrame / 2 ] );
	}

}

void candles :: collect( int& RHCandles ){

	SDL_Rect RHHitbox;

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

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

}

SDL_Surface* candles :: getSheet(){
	return CandleSheet;
}

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

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

int & candles :: getFrame(){
	return Frame;
}

SDL_Rect* candles :: getOffsets(){
	return Offsets;
}
