#include <stdlib.h>
#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include "fichier.h"
#include "jonnysolojouer.h"
#include "fichiers.h"
void jonnysolojouer(SDL_Surface* ecran)
{
SDL_Surface *mechant = NULL;
SDL_Surface *mario[4] = {NULL}; // 4 surfaces pour chacune des directions de mario
SDL_Surface *mur = NULL, *objectif = NULL, *marioActuel = NULL;
SDL_Rect position, positionJoueur , objectifatteind ;
SDL_Event event;
int tempsPrecedent = 0, tempsActuel = 0;
SDL_Surface *texte = NULL;
SDL_Rect positiontexte;
TTF_Font *police = NULL;
SDL_Color couleurNoire = {154, 156, 200};
int continuer = 1, objectifsRestants = 0, i = 0, j = 0;
int carte[NB_BLOCS_LARGEUR][NB_BLOCS_HAUTEUR] = {0};
// Chargement des sprites (décors, personnage...)
mur = IMG_Load("image/objet/mur.jpg");
objectif = IMG_Load("image/objet/objectif.gif");
mario[BAS] = IMG_Load("image/hero/DarkKnight-Front.gif");
mario[GAUCHE] = IMG_Load("image/hero/DarkKnight-Left.gif");
mario[HAUT] = IMG_Load("image/hero/DarkKnight-Back.gif");
mario[DROITE] = IMG_Load("image/hero/darkknight gauche.GIF");
marioActuel = mario[BAS]; // Mario sera dirigé vers le bas au départ
police = TTF_OpenFont("angelina.ttf", 65);
/* Ecriture du texte dans la SDL_Surface "texte" en mode Blended (optimal) */
texte = TTF_RenderText_Blended(police, "s etait facile hein mais maintenant on vas voir", couleurNoire);
// Chargement du niveau
if (!chargerNiveau(carte))
exit(EXIT_FAILURE); // On arrête le jeu si on n'a pas pu charger le niveau
// Recherche de la position de Mario au départ
for (i = 0 ; i < NB_BLOCS_LARGEUR ; i++)
{
for (j = 0 ; j < NB_BLOCS_HAUTEUR ; j++)
{
if (carte[i][j] == MARIO) // Si Mario se trouve à cette position sur la carte
{
positionJoueur.x = i;
positionJoueur.y = j;
carte[i][j] = VIDE;
}
}
}
// Activation de la répétition des touches
SDL_EnableKeyRepeat(100, 100);
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = 0;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_UP:
marioActuel = mario[HAUT];
deplacerJoueur(carte, &positionJoueur, HAUT);
break;
case SDLK_DOWN:
marioActuel = mario[BAS];
deplacerJoueur(carte, &positionJoueur, BAS);
break;
case SDLK_RIGHT:
marioActuel = mario[DROITE];
deplacerJoueur(carte, &positionJoueur, DROITE);
break;
case SDLK_r:
jonnysolojouer(ecran);
break;
case SDLK_LEFT:
marioActuel = mario[GAUCHE];
deplacerJoueur(carte, &positionJoueur, GAUCHE);
break;
}
break;
}
// Effacement de l'écran
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
// Placement des objets à l'écran
objectifsRestants = 0;
for (i = 0 ; i < NB_BLOCS_LARGEUR ; i++)
{
for (j = 0 ; j < NB_BLOCS_HAUTEUR ; j++)
{
position.x = i * TAILLE_BLOC;
position.y = j * TAILLE_BLOC;
switch(carte[i][j])
{
case MUR:
SDL_BlitSurface(mur, NULL, ecran, &position);
break;
case OBJECTIF:
SDL_BlitSurface(objectif, NULL, ecran, &position);
objectifsRestants = 1;
objectifatteind.x = position.x;
objectifatteind.y = position.y;
break;
}
}
}
// Si on n'a trouvé aucun objectif sur la carte, c'est qu'on a gagné
if(carte[positionJoueur.x][positionJoueur.y] == OBJECTIF)
{
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
positiontexte.x = 60;
positiontexte.y = 370;
tempsActuel = SDL_GetTicks();
if (tempsActuel - tempsPrecedent > 10000)
{
modesimple(ecran);
tempsActuel=0;
tempsPrecedent = tempsActuel;
}
SDL_BlitSurface(texte, NULL, ecran, &positiontexte); /* Blit du texte par-dessus */
SDL_Flip(ecran);
TTF_CloseFont(police);
TTF_Quit();
}
// On place le joueur à la bonne position
position.x = positionJoueur.x * TAILLE_BLOC;
position.y = positionJoueur.y * TAILLE_BLOC;
SDL_BlitSurface(marioActuel, NULL, ecran, &position);
SDL_Flip(ecran);
}
// Désactivation de la répétition des touches (remise à 0)
SDL_EnableKeyRepeat(0, 0);
SDL_FreeSurface(texte);
SDL_FreeSurface(mur);
SDL_FreeSurface(objectif);
for (i = 0 ; i < 4 ; i++)
SDL_FreeSurface(mario[i]);
}
void deplacerJoueur(int carte[][NB_BLOCS_HAUTEUR], SDL_Rect *pos, int direction)
{
switch(direction)
{
case HAUT:
if (pos->y - 1 < 0) // Si le joueur dépasse l'écran, on arrête
break;
if (carte[pos->x][pos->y - 1] == MUR) // S'il y a un mur, on arrête
break;
// Si on veut pousser une caisse, il faut vérifier qu'il n'y a pas de mur derrière (ou une autre caisse, ou la limite du monde)
if ((carte[pos->x][pos->y - 1] == CAISSE || carte[pos->x][pos->y - 1] == CAISSE_OK) &&
(pos->y - 2 < 0 || carte[pos->x][pos->y - 2] == MUR ||
carte[pos->x][pos->y - 2] == CAISSE || carte[pos->x][pos->y - 2] == CAISSE_OK))
break;
// Si on arrive là, c'est qu'on peut déplacer le joueur !
// On vérifie d'abord s'il y a une caisse à déplacer
pos->y--; // On peut enfin faire monter le joueur (oufff !)
break;
case BAS:
if (pos->y + 1 >= NB_BLOCS_HAUTEUR)
break;
if (carte[pos->x][pos->y + 1] == MUR)
break;
if ((carte[pos->x][pos->y + 1] == CAISSE || carte[pos->x][pos->y + 1] == CAISSE_OK) &&
(pos->y + 2 >= NB_BLOCS_HAUTEUR || carte[pos->x][pos->y + 2] == MUR ||
carte[pos->x][pos->y + 2] == CAISSE || carte[pos->x][pos->y + 2] == CAISSE_OK))
break;
pos->y++;
break;
case GAUCHE:
if (pos->x - 1 < 0)
break;
if (carte[pos->x - 1][pos->y] == MUR)
break;
if ((carte[pos->x - 1][pos->y] == CAISSE || carte[pos->x - 1][pos->y] == CAISSE_OK) &&
(pos->x - 2 < 0 || carte[pos->x - 2][pos->y] == MUR ||
carte[pos->x - 2][pos->y] == CAISSE || carte[pos->x - 2][pos->y] == CAISSE_OK))
break;
pos->x--;
break;
case DROITE:
if (pos->x + 1 >= NB_BLOCS_LARGEUR)
break;
if (carte[pos->x + 1][pos->y] == MUR)
break;
if ((carte[pos->x + 1][pos->y] == CAISSE || carte[pos->x + 1][pos->y] == CAISSE_OK) &&
(pos->x + 2 >= NB_BLOCS_LARGEUR || carte[pos->x + 2][pos->y] == MUR ||
carte[pos->x + 2][pos->y] == CAISSE || carte[pos->x + 2][pos->y] == CAISSE_OK))
break;
pos->x++;
break;
}
}