#include "levels.h"
#define DIM_H 1024
#define DIM_V 768
Levels::Levels()
{
level = 0;
}
//funzione che setta lo stage e chiama background per il caricamento dello sfondo
void Levels::set_level()
{
//svuoto i vettori nel caso contenessero oggetti della partita precedente
nome_foto.clear();
nome_mirror.clear();
err_x.clear();
err_y.clear();
ifstream read("data/data.txt",ios::in); // apre il file 'data.txt' in lettura
//fstream read_write("data.txt",ios::in | ios::out);
/*
if(!read) {
cout << "Il file non esiste!";
return -1;
}
*/
//ofstream write("data/rubrica.txt",ios::out); //apre il file 'rubrica' in scrittura
//funzione che legge riga per riga il mio file data.txt
//la struttura del file txt deve essere del tipo: stringa int int;int int;int int;int int;int int;
while(!read.eof()) //mentre non siamo arrivati alla fine del file
{
getline(read ,linea,' '); //leggo dal file la prima parola e la memorizzo in "linea"
linea.resize(remove(linea.begin(), linea.end(), '\n') - linea.begin() ); //rimuovo il carattere di ritorno a capo all'inizio della stringa
nome_foto.push_back("data/original/" + linea); //la memorizzo nel vettore "nome_foto"
nome_mirror.push_back("data/copy/" + linea); //la memorizzo nel vettore "nome_mirror"
for(int i=0;i<=4;i++)
{
getline(read ,linea,' ');
err_x.push_back(atoi(linea.c_str())-ERRORE_CENTER_X); //con la funzione atoi trasformo una stringa in int
getline(read ,linea,';');
err_y.push_back(atoi(linea.c_str())-ERRORE_CENTER_Y);
}
}
nome_foto.pop_back(); //tolgo l'ultimo elemento al vettore nome_img in quanto è il carattere terminatore EOF
nome_mirror.pop_back(); //lo stesso faccio col vettore nome_mirror
//chiudo il file di testo
read.close();
/*
for(int r = 0;r<=err_x.size()-1;r++)
write << err_x[r] << err_y[r] << '\n' ;
write.close();
*/
//carico e imposto lo sfondo dei livelli
background.load_bg(1);
//memorizzo l'indirizzo dell'immagine errore e ok nel vettore errore_files e ok_files
errore_files.push_back("data/errore.png");
ok_files.push_back("data/ok.png");
//setto errore e ok
errore.set_sprite(errore_files, 0, 0, 0);
ok.set_sprite(ok_files, 0, 0, 0);
//disegno l'immagine
//foto.set_sprite(nome_foto, 38, 37, 0);
//disegno l'immagine specchio
//mirror.set_sprite(nome_mirror, 527, 37, 0);
//imposto immagine e coordinate per lo sprite tempo
tempo_files.push_back("data/time.png");
tempo.set_sprite(tempo_files, TEMPO_X, TEMPO_Y, 0);
//imposto a 0 tutti e 5 gli elementi del vettore disegna_errori per la partenza della partita
for(int i=0;i<=4;i++) disegna_errori.push_back(0);
//setto il livello iniziale e imposto la variabile load = 0 per caricare il necessario alla partenza del gioco
//imposto ciclo a 0 e il taglio del tempo a 0
level = 1;
load = 0;
cut = 0;
ciclo = 0;
score = 0;
}
int Levels::exec()
{
//disegno lo sfondo
background.draw_bgmenu();
//se è scaduto il tempo..
if(tempo.get_w() <= cut)
{
//mi occupo di disegnare la scritta "gameover"
gameover.load_font(0xFF,0xFF,0xFF,COMPLETED_FONT_X,COMPLETED_FONT_Y,"Game Over");
gameover.draw_menu_font();
gameover.load_font(0xFF,0xFF,0xFF,COMPLETED_FONT_X + MIRROR_DIFF_X,COMPLETED_FONT_Y,"Game Over");
gameover.draw_menu_font();
//aggiorno lo schermo
SDL_Flip(screen);
//attendo 2 sec a meno che nn ci sia pressione del mouse
wait(2);
//ridisegno lo sfondo per sovrascrivere la scritta Game Over
background.draw_bgmenu();
//converto lo score da int a string e lo salvo in st_livello
itoa(score, st_livello, 10);
//carico e disegno le scritte "your score" e il punteggio
gameover.load_font(0xFF,0xFF,0xFF,COMPLETED_FONT_X,COMPLETED_FONT_Y,"Your score");
gameover.draw_menu_font();
completed.load_font(0xFF,0xFF,0xFF,COMPLETED_FONT_X,280, st_livello);
completed.draw_menu_font();
//aggiorno lo schermo
SDL_Flip(screen);
//attendo 2 sec a meno che nn ci sia pressione del mouse
wait(2);
return 2;
}
//----------------------------------------------------------
if(load == 0)
{
if(level > 1)
{
//dal secondo livello in poi, mi occupo di cancellare la foto del livello appena fatto sia da foto che da mirror
nome_foto.erase(nome_foto.begin()+(estr));
nome_mirror.erase(nome_mirror.begin()+(estr));
//cancello le coordinate degli errori relative alla foto eliminata
for(int t=0;t<=4;t++)
{
err_x.erase(err_x.begin()+estr*5);
err_y.erase(err_y.begin()+estr*5);
}
//mi occupo di disegnare la scritta "completed"
completed.load_font(0xFF,0xFF,0xFF,COMPLETED_FONT_X,COMPLETED_FONT_Y,"Completed");
completed.draw_menu_font();
completed.load_font(0xFF,0xFF,0xFF,COMPLETED_FONT_X + MIRROR_DIFF_X,COMPLETED_FONT_Y,"Completed");
completed.draw_menu_font();
//aggiorno lo schermo
SDL_Flip(screen);
//attendo 2 sec a meno che nn ci sia pressione del mouse
wait(2);
//disegno lo sfondo di nuovo per sovrascrivere le scritte
background.draw_bgmenu();
//calcolo il punteggio di fine livello
score = score + (tempo.get_w()-cut);
//converto lo score da int a string e lo salvo in st_livello
itoa(score, st_livello, 10);
//carico e disegno le scritte "score" e il punteggio
completed.load_font(0xFF,0xFF,0xFF,COMPLETED_FONT_X,COMPLETED_FONT_Y, "Score");
completed.draw_menu_font();
completed.load_font(0xFF,0xFF,0xFF,COMPLETED_FONT_X,280, st_livello);
completed.draw_menu_font();
//aggiorno lo schermo
SDL_Flip(screen);
//attendo 2 sec a meno che nn ci sia pressione del mouse
wait(2);
//SDL_Delay(1000);
//disegno lo sfondo di nuovo per sovrascrivere le scritte
background.draw_bgmenu();
//se non ci sono più foto per i livelli, si ritorna al menu(provvisorio)
if(nome_foto.size() == 0)
{
return 2;
}
}
//copio la scritta Livello nella variabile st_livello
strcpy(st_livello, "Livello ");
//trasformo da int a string la variabile level e la salvo in st_level
itoa(level, st_level, 10);
//carico le impostazioni per la scritta "livello" concatenando la stringa st_livello con la st_level
livello.load_font(0xFF,0xFF,0xFF,LIVELLO_FONT_X,LIVELLO_FONT_Y,strcat(st_livello, st_level));
//disegno la scritta livello
livello.draw_menu_font();
//carico le impostazioni per la scritta "livello" nel mirror
livello.load_font(0xFF,0xFF,0xFF,LIVELLO_FONT_X + MIRROR_DIFF_X,LIVELLO_FONT_Y,st_livello);
//disegno la scritta livello nel mirror
livello.draw_menu_font();
//aggiorno lo schermo
SDL_Flip(screen);
//SDL_Delay(2000);
//attendo 2 sec a meno che nn ci sia pressione del mouse
wait(2);
//estraggo un numero casuale da 0 alla dimensione del vettore nome_foto e lo salvo in estr
estr = casual(nome_foto.size());
//scelgo la foto col numero casuale
//foto.set_sprite_frame(estr);
//setto la foto relativa al numero casuale per l'originale
foto.set_sprite(nome_foto, FOTO_MARGIN_X, FOTO_MARGIN_Y, estr);
//scelgo la foto specchio relativa
//mirror.set_sprite_frame(estr);
//setto la foto relativa al numero casuale per il mirror
mirror.set_sprite(nome_mirror, FOTO_MARGIN_X + MIRROR_DIFF_X, FOTO_MARGIN_Y, estr);
//riporto a 0 tutti e 5 gli elementi del vettore disegna_errori alla partenza del nuovo livello
for(int i=0;i<=4;i++) disegna_errori[i] = 0;
//pongo load = 1 per non ripetere queste operazioni sopra fino a quando nn siano stati trovati i 5 errori nel gioco
load = 1;
//imposto il ciclo e il taglio al tempo a zero
ciclo = 0;
cut = 0;
}
//----------------------------------------------------------
//disegno la foto originale
foto.draw_sprite();
//disegno la foto del mirror
mirror.draw_sprite();
//segno ogni iterazione del livello
ciclo = ciclo+1;
//se le iterazioni sono maggiori di FPS = 50 (cioè un secondo)...
if(ciclo > FPS)
{
//...si controlla se siamo prima del livello 9, in ta caso
if(level <= 9)
{
//si diminuisce il tempo in base a questa formula e si riporta il ciclo a 0
cut = cut + (tempo.get_w()/ (BASE_TIME - ((level*LEVEL_DIFF_TIME)-LEVEL_DIFF_TIME)) ); //15 per ottenere 60 sec.
ciclo = 0;
}
else
{
//altra formula per diminuire il tempo se siamo a livello sopra il 9
cut = cut + (tempo.get_w()/ (BASE_TIME - 40)); //cut = cut + ((tempo.get_w()/BASE_TIME)-40);
ciclo = 0;
}
}
//disegno la barra del tempo
tempo.draw_sprite_time(cut);
//a seconda di quale dei 5 errori siano stati trovati disegno il relativo cerchietto nella giusta posizione e il relativo ok
if(disegna_errori[0] == 1)
{
errore.set_sprite_pos(err_x[estr*5] + FOTO_MARGIN_X, err_y[estr*5] + FOTO_MARGIN_Y);
errore.draw_sprite();
errore.set_sprite_pos(err_x[estr*5] + FOTO_MARGIN_X + MIRROR_DIFF_X, err_y[estr*5] + FOTO_MARGIN_Y);
errore.draw_sprite();
ok.set_sprite_pos(ERRORE_MARGIN_X, ERRORE_MARGIN_Y);
ok.draw_sprite();
}
else
{
errore.set_sprite_pos(ERRORE_MARGIN_X, ERRORE_MARGIN_Y);
errore.draw_sprite();
}
if(disegna_errori[1] == 1)
{
errore.set_sprite_pos(err_x[estr*5+1]+ FOTO_MARGIN_X, err_y[estr*5+1] + FOTO_MARGIN_Y);
errore.draw_sprite();
errore.set_sprite_pos(err_x[estr*5+1]+ FOTO_MARGIN_X + MIRROR_DIFF_X, err_y[estr*5+1] + FOTO_MARGIN_Y);
errore.draw_sprite();
ok.set_sprite_pos(ERRORE_MARGIN_X + ERRORE_SPACE_X, ERRORE_MARGIN_Y);
ok.draw_sprite();
}
else
{
errore.set_sprite_pos(ERRORE_MARGIN_X + ERRORE_SPACE_X, ERRORE_MARGIN_Y);
errore.draw_sprite();
}
if(disegna_errori[2] == 1)
{
errore.set_sprite_pos(err_x[estr*5+2]+ FOTO_MARGIN_X, err_y[estr*5+2] + FOTO_MARGIN_Y);
errore.draw_sprite();
errore.set_sprite_pos(err_x[estr*5+2]+ FOTO_MARGIN_X + MIRROR_DIFF_X, err_y[estr*5+2] + FOTO_MARGIN_Y);
errore.draw_sprite();
ok.set_sprite_pos(ERRORE_MARGIN_X + ERRORE_SPACE_X*2, ERRORE_MARGIN_Y);
ok.draw_sprite();
}
else
{
errore.set_sprite_pos(ERRORE_MARGIN_X + ERRORE_SPACE_X*2, ERRORE_MARGIN_Y);
errore.draw_sprite();
}
if(disegna_errori[3] == 1)
{
errore.set_sprite_pos(err_x[estr*5+3]+ FOTO_MARGIN_X, err_y[estr*5+3] + FOTO_MARGIN_Y);
errore.draw_sprite();
errore.set_sprite_pos(err_x[estr*5+3]+ FOTO_MARGIN_X + MIRROR_DIFF_X, err_y[estr*5+3] + FOTO_MARGIN_Y);
errore.draw_sprite();
ok.set_sprite_pos(ERRORE_MARGIN_X + ERRORE_SPACE_X*3, ERRORE_MARGIN_Y);
ok.draw_sprite();
}
else
{
errore.set_sprite_pos(ERRORE_MARGIN_X + ERRORE_SPACE_X*3, ERRORE_MARGIN_Y);
errore.draw_sprite();
}
if(disegna_errori[4] == 1)
{
errore.set_sprite_pos(err_x[estr*5+4]+ FOTO_MARGIN_X, err_y[estr*5+4] + FOTO_MARGIN_Y);
errore.draw_sprite();
errore.set_sprite_pos(err_x[estr*5+4]+ FOTO_MARGIN_X + MIRROR_DIFF_X, err_y[estr*5+4] + FOTO_MARGIN_Y);
errore.draw_sprite();
ok.set_sprite_pos(ERRORE_MARGIN_X + ERRORE_SPACE_X*4, ERRORE_MARGIN_Y);
ok.draw_sprite();
}
else
{
errore.set_sprite_pos(ERRORE_MARGIN_X + ERRORE_SPACE_X*4, ERRORE_MARGIN_Y);
errore.draw_sprite();
}
//se sono stati trovati tutti e 5 gli errori si passa al livello successivo e si pone load = 0 per scegliere una nuova foto
if(disegna_errori[0] == 1 && disegna_errori[1] == 1 && disegna_errori[2] == 1 && disegna_errori[3] == 1 && disegna_errori[4] == 1)
{
level = level + 1;
load = 0;
}
//Controllo l'eventuale pressione di un tasto con una chiamata alla funzione exec di input
switch(input_game.exec())
{
case 0: //cioè è stato premuto ESC
{
return 2; //cioè GAME_MENU
}break;
case 1: //cioè è stato premuto il pulsante SX del mouse
{
//si controlla se il click è avvenuto a una delle coordinate dell'errore ed eventualmente si segnala quale
for(int s=0;s<=4;s++)
{
if(collision_2(err_x[estr*5+s]+FOTO_MARGIN_X, err_y[estr*5+s] + FOTO_MARGIN_Y) || collision_2(err_x[estr*5+s]+FOTO_MARGIN_X + MIRROR_DIFF_X, err_y[estr*5+s] + FOTO_MARGIN_Y)) disegna_errori[s] = 1;
}
//nel caso in cui il click del mouse non corrisponda alle coordinate di un errore...
if((collision_2(err_x[estr*5+0]+FOTO_MARGIN_X, err_y[estr*5+0] + FOTO_MARGIN_Y)) || (collision_2(err_x[estr*5+0]+FOTO_MARGIN_X + MIRROR_DIFF_X, err_y[estr*5+0] + FOTO_MARGIN_Y))
|| (collision_2(err_x[estr*5+1]+FOTO_MARGIN_X, err_y[estr*5+1] + FOTO_MARGIN_Y)) || (collision_2(err_x[estr*5+1]+FOTO_MARGIN_X + MIRROR_DIFF_X, err_y[estr*5+1] + FOTO_MARGIN_Y))
|| (collision_2(err_x[estr*5+2]+FOTO_MARGIN_X, err_y[estr*5+2] + FOTO_MARGIN_Y)) || (collision_2(err_x[estr*5+2]+FOTO_MARGIN_X + MIRROR_DIFF_X, err_y[estr*5+2] + FOTO_MARGIN_Y))
|| (collision_2(err_x[estr*5+3]+FOTO_MARGIN_X, err_y[estr*5+3] + FOTO_MARGIN_Y)) || (collision_2(err_x[estr*5+3]+FOTO_MARGIN_X + MIRROR_DIFF_X, err_y[estr*5+3] + FOTO_MARGIN_Y))
|| (collision_2(err_x[estr*5+4]+FOTO_MARGIN_X, err_y[estr*5+4] + FOTO_MARGIN_Y)) || (collision_2(err_x[estr*5+4]+FOTO_MARGIN_X + MIRROR_DIFF_X, err_y[estr*5+4] + FOTO_MARGIN_Y)))
{
;
}
//...si fa questo
else
{
//nel caso di click sbagliato sottraggo un po' di tempo a disposizione MALUS
cut = cut + MALUS;
}
return 4; //cioè GAME_RUN
}break;
default: //cioè nessun tasto è stato premuto
{
return 4;
}break;
}//end switch
}
//funzione per estrarre un numero casuale
int Levels::casual(int num)
{
srand((unsigned)time(NULL));
a = rand() % num;
return a;
}
bool Levels::collision(int width, int height, int x, int y)
{
if(get_mouse_pos(0) > x && get_mouse_pos(0) < x + width && get_mouse_pos(1) > y && get_mouse_pos(0) < y + height) return 1;
else return 0;
}
bool Levels::collision_2(int err_pos_x, int err_pos_y)
{
if(get_mouse_pos(0) > err_pos_x && get_mouse_pos(0) < err_pos_x + 50 && get_mouse_pos(1) > err_pos_y && get_mouse_pos(1) < err_pos_y + 50) return 1;
else return 0;
}
//funzione che restituisce le coordinate del mouse
//ritorna l'ascissa o l'ordinata del mouse
//se l'argomento passato è 0 ritorna l'ascissa, se è 1 ritorna l'ordinata
int Levels::get_mouse_pos(int xy)
{
SDL_GetMouseState(&pos_x, &pos_y);
if(xy == 0) return pos_x;
if(xy == 1) return pos_y;
}
//funzione di attesa
void Levels::wait(int seconds)
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait)
{
if(input_game.exec()==1) break;
}
}
Levels::~Levels()
{
;
}