634 lines
14 KiB
C
634 lines
14 KiB
C
/*
|
||
* playerInterface.c
|
||
*
|
||
* Created on: 17 juin 2018
|
||
* Author: isen
|
||
*/
|
||
|
||
#include "playerInterface.h"
|
||
//#include "arenaEngine.h"
|
||
|
||
#include "SDL2/SDL.h"
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
|
||
|
||
enum {GRASS, ROCK, TREE,FIRSTPLAYER, IA1};
|
||
#define BLOC_SIZE 32 // Taille d'un bloc (carré) en pixels
|
||
#define NB_BLOCS_LARGEUR 20
|
||
#define NB_BLOCS_HAUTEUR 20
|
||
#define LARGEUR_FENETRE BLOC_SIZE * NB_BLOCS_LARGEUR
|
||
#define HAUTEUR_FENETRE BLOC_SIZE * NB_BLOCS_HAUTEUR
|
||
|
||
|
||
int PlayerInterface(SDL_Window* pWindow,ARENA_H_TILE* arena,SDL_Surface **Sprites)
|
||
{
|
||
|
||
//Ajouté
|
||
SDL_Rect position;
|
||
SDL_Rect oldposition;
|
||
SDL_Rect IAposition;
|
||
SDL_Rect oldIAposition;
|
||
|
||
|
||
PLAYER* List=NULL;
|
||
PLAYER* Element=NULL;
|
||
int id=0;
|
||
int numberAlive;
|
||
|
||
PLAYER* player1=NULL;
|
||
PLAYER* player2=NULL;
|
||
|
||
while(id<2)
|
||
{
|
||
id = id + 1;
|
||
Element = createPlayer(id);
|
||
List = insertRightPlaceRecursive(List,Element);
|
||
}
|
||
|
||
displayList(List);
|
||
|
||
player1 = SearchPlayer(List, 1);
|
||
player2 = SearchPlayer(List, 2);
|
||
|
||
printf("Nom player 1 : %s\n",player1->Name);
|
||
printf("Nom player 2 : %s\n",player2->Name);
|
||
|
||
numberAlive = NumberPlayerAlive(List);
|
||
printf("Nombre de joueur en vie : %i\n",numberAlive);
|
||
|
||
int action=0;
|
||
|
||
|
||
do
|
||
{
|
||
|
||
oldposition.x = player1->PositionX * BLOC_SIZE;
|
||
oldposition.y = player1->PositionY * BLOC_SIZE;
|
||
oldIAposition.x = player2->PositionX * BLOC_SIZE;
|
||
oldIAposition.y = player2->PositionY * BLOC_SIZE;
|
||
|
||
//Affichage des joueurs au début de la partie
|
||
SDL_BlitSurface(Sprites[FIRSTPLAYER], NULL, SDL_GetWindowSurface(pWindow), &oldposition);
|
||
SDL_BlitSurface(Sprites[IA1], NULL, SDL_GetWindowSurface(pWindow), &oldIAposition);
|
||
|
||
SDL_UpdateWindowSurface(pWindow);
|
||
|
||
while(action == 0)
|
||
{
|
||
action=getEvent();
|
||
}
|
||
|
||
if(action == -1)
|
||
{
|
||
return -1;
|
||
}
|
||
else if(action == 1)
|
||
{
|
||
ActionPlayer(arena,player1,1); //Déplacement vers le haut
|
||
}
|
||
else if(action == 2)
|
||
{
|
||
ActionPlayer(arena,player1,2); //Déplacement vers la droite
|
||
}
|
||
else if(action == 3)
|
||
{
|
||
ActionPlayer(arena,player1,3); //Déplacement vers le bas
|
||
}
|
||
else if(action == 4)
|
||
{
|
||
ActionPlayer(arena,player1,4); //Déplacement vers la gauche
|
||
}
|
||
else if(action == 5)
|
||
{
|
||
//Regarder le perso en face, le plus près
|
||
AttackPlayer(player1,player2); //Voir quel player on choisit d'attaquer
|
||
}
|
||
|
||
|
||
//Partie ajoutée
|
||
// On place le joueur à la bonne position
|
||
position.x = player1->PositionX * BLOC_SIZE;
|
||
position.y = player1->PositionY * BLOC_SIZE;
|
||
//SDL_BlitSurface(actualPlayer, NULL, SDL_GetWindowSurface(pWindow), &position);
|
||
|
||
// Effacement de l'écran
|
||
//SDL_FillRect(SDL_GetWindowSurface(pWindow), NULL, SDL_MapRGB(SDL_GetWindowSurface(pWindow)->format, 255, 255, 255));
|
||
|
||
SDL_BlitSurface(Sprites[FIRSTPLAYER], NULL, SDL_GetWindowSurface(pWindow), &position);
|
||
SDL_BlitSurface(Sprites[GRASS], NULL, SDL_GetWindowSurface(pWindow), &oldposition);
|
||
|
||
|
||
|
||
//Tour de l'IA
|
||
IAEngine(arena,player1,player2);
|
||
|
||
IAposition.x = player2->PositionX * BLOC_SIZE;
|
||
IAposition.y = player2->PositionY * BLOC_SIZE;
|
||
|
||
SDL_BlitSurface(Sprites[IA1], NULL, SDL_GetWindowSurface(pWindow), &IAposition);
|
||
SDL_BlitSurface(Sprites[GRASS], NULL, SDL_GetWindowSurface(pWindow), &oldIAposition);
|
||
|
||
SDL_UpdateWindowSurface(pWindow);
|
||
|
||
action=0;
|
||
|
||
}
|
||
|
||
while((player1->HealthPoints >0) && (NumberPlayerAlive(List) > 1));
|
||
|
||
printf("La partie est terminée\n");
|
||
|
||
if(player1->HealthPoints > 0)
|
||
{
|
||
printf("Vous avez gagné\n");
|
||
}
|
||
else
|
||
{
|
||
printf("Vous avez perdu\n");
|
||
}
|
||
|
||
//Si une des IA meurt, voir comment on le gère
|
||
return -1;
|
||
}
|
||
|
||
|
||
int getEvent()
|
||
{
|
||
int action;
|
||
SDL_Event event;
|
||
SDL_WaitEvent(&event); /* Récupération de l'événement dans event */
|
||
|
||
switch(event.type) /* Test du type d'événement */
|
||
{
|
||
/* Si c'est un événement de type "Quitter" */
|
||
case SDL_QUIT : printf("QUITTER\n");
|
||
//action = -1;
|
||
//break;
|
||
return -1;
|
||
case SDL_KEYUP:
|
||
switch(event.key.keysym.sym) //La valeur de touche
|
||
{
|
||
case SDLK_UP: printf("FLECHE DU HAUT\n");
|
||
return 1;
|
||
|
||
case SDLK_DOWN: printf("FLECHE DU BAS\n");
|
||
return 3;
|
||
|
||
case SDLK_RIGHT:printf("FLECHE DE DROITE\n");
|
||
return 2;
|
||
|
||
case SDLK_LEFT: printf("FLECHE DE GAUCHE\n");
|
||
return 4;
|
||
|
||
case SDLK_SPACE:printf("BARRE D'ESPACE\n");
|
||
return 5;
|
||
}
|
||
break;
|
||
}
|
||
|
||
|
||
|
||
return 0;
|
||
}
|
||
|
||
|
||
/**Fonction qui crée un joueur
|
||
*
|
||
* */
|
||
PLAYER* createPlayer(int Id)
|
||
{
|
||
PLAYER* player=NULL;
|
||
//Utilisé la fonction de récupération de la taille de l'arène
|
||
|
||
int race;
|
||
player=(PLAYER*)malloc(sizeof(PLAYER));
|
||
if(player==NULL)
|
||
{
|
||
printf("ERREUR Allocation joueur ");
|
||
}
|
||
|
||
printf("Entrer le nom du joueur\n");
|
||
scanf("%s",player->Name);
|
||
|
||
do
|
||
{
|
||
printf("Race? 1:Orc 2: Elf 3:Humain \n");
|
||
scanf("%d",&race);
|
||
}
|
||
while((race < 1) || (race > 3));
|
||
|
||
player->Race = race;
|
||
|
||
switch(race)
|
||
{
|
||
//Race ORC
|
||
case 1: player->HealthPoints = 100;
|
||
player->AttacksPoints = 10;
|
||
break;
|
||
|
||
//Race Elfe
|
||
case 2: player->HealthPoints = 80;
|
||
player->AttacksPoints = 9;
|
||
break;
|
||
|
||
//Race Humaine
|
||
case 3: player->HealthPoints = 90;
|
||
player->AttacksPoints = 9;
|
||
break;
|
||
}
|
||
|
||
player->Id = Id;
|
||
//player->AttacksPoints = 10;
|
||
|
||
if(Id == 1)
|
||
{
|
||
player->PositionX=0;
|
||
player->PositionY=0;
|
||
}
|
||
else if(Id ==2)
|
||
{
|
||
player->PositionX=ARENAMAX - 1;
|
||
//player->PositionX=5;
|
||
player->PositionY=0;
|
||
}
|
||
else if(Id ==3)
|
||
{
|
||
player->PositionX=0;
|
||
player->PositionY=ARENAMAX - 1;
|
||
}
|
||
else
|
||
{
|
||
player->PositionX=ARENAMAX - 1;
|
||
player->PositionY=ARENAMAX - 1;
|
||
}
|
||
|
||
player->suiv=NULL;
|
||
|
||
return player;
|
||
}
|
||
|
||
/**Fonction qui ajoute un joueur à la liste
|
||
*
|
||
* */
|
||
|
||
PLAYER* insertRightPlaceRecursive(PLAYER* Head, PLAYER* Element)
|
||
{
|
||
if(Head == NULL)
|
||
{
|
||
return Element;
|
||
}
|
||
|
||
if(Element->Id < Head->Id)
|
||
{
|
||
Element->suiv = Head;
|
||
return Element;
|
||
}
|
||
else
|
||
{
|
||
if(Element->Id == Head->Id)
|
||
{
|
||
printf("Le joueur à l'id %d exite déja dans la liste\n",Element->Id);
|
||
free(Element);
|
||
return Head;
|
||
}
|
||
else
|
||
{
|
||
Head->suiv = insertRightPlaceRecursive(Head->suiv,Element);
|
||
return Head;
|
||
}
|
||
}
|
||
}
|
||
|
||
void displayList(PLAYER * Head)
|
||
{
|
||
if(Head == NULL)
|
||
{
|
||
printf("La liste est vide\n");
|
||
}
|
||
while(Head != NULL)
|
||
{
|
||
printf("****Player %d****\n",Head->Id);
|
||
printf("Nom : %s\n",Head->Name);
|
||
Head = Head->suiv;
|
||
}
|
||
}
|
||
|
||
|
||
PLAYER * freeElement(PLAYER *Head, int Id)
|
||
{
|
||
PLAYER * Element;
|
||
|
||
if(Head == NULL)
|
||
{
|
||
printf("La liste est vide\n");
|
||
}
|
||
|
||
if(Id == Head->Id)
|
||
{
|
||
Element = Head;
|
||
Head = Head->suiv;
|
||
printf("Le joueur %d a été supprimé de la liste\n",Element->Id);
|
||
free(Element);
|
||
return Head;
|
||
}
|
||
else
|
||
{
|
||
Head->suiv = freeElement(Head->suiv,Id);
|
||
return Head;
|
||
}
|
||
}
|
||
|
||
|
||
PLAYER * freeList(PLAYER *Head)
|
||
{
|
||
if(Head != NULL)
|
||
{
|
||
freeList(Head->suiv);
|
||
|
||
}
|
||
free(Head);
|
||
Head=NULL;
|
||
|
||
return Head;
|
||
}
|
||
|
||
int NumberPlayerAlive(PLAYER *Head)
|
||
{
|
||
int numberAlive=0;
|
||
if(Head == NULL)
|
||
{
|
||
printf("La liste est vide\n");
|
||
return -1;
|
||
}
|
||
else
|
||
{
|
||
do
|
||
{
|
||
if(Head->HealthPoints > 0)
|
||
{
|
||
numberAlive = numberAlive + 1;
|
||
}
|
||
Head=Head->suiv;
|
||
}
|
||
while(Head != NULL);
|
||
return numberAlive;
|
||
}
|
||
}
|
||
|
||
PLAYER * SearchPlayer(PLAYER *Head, int idPlayer)
|
||
{
|
||
printf("Fonction Recherche\n");
|
||
|
||
if(Head == NULL)
|
||
{
|
||
printf("La liste est vide\n");
|
||
}
|
||
|
||
if(Head->Id == idPlayer)
|
||
{
|
||
printf("Trouvé\n");
|
||
|
||
printf(" Joueur %s\n",Head->Name);
|
||
return Head;
|
||
}
|
||
else
|
||
{
|
||
Head->suiv = SearchPlayer(Head->suiv,idPlayer);
|
||
|
||
return Head->suiv;
|
||
}
|
||
}
|
||
|
||
void AttackPlayer( PLAYER *player1, PLAYER *player2)
|
||
{
|
||
printf("Fonction Attaque\n");
|
||
|
||
|
||
if((player1 != NULL) && (player2 != NULL))
|
||
{
|
||
|
||
if((player1->HealthPoints > 0) && (player2->HealthPoints > 0))
|
||
{
|
||
|
||
printf("\n\n** Tour **\n");
|
||
|
||
printf("Attaque Joueur %d sur joueur %d \n",player1->Id,player2->Id);
|
||
|
||
player2->HealthPoints = player2->HealthPoints - player1->AttacksPoints;
|
||
|
||
printf("Joueur %d : %s PV = %d\n ",player1->Id,player1->Name,player1->HealthPoints);
|
||
printf("Joueur %d : %s PV = %d\n ",player2->Id,player2->Name,player2->HealthPoints);
|
||
|
||
/*if((player1->HealthPoints > 0) && (player2->HealthPoints > 0))
|
||
{
|
||
printf("\nAttaque Joueur %d sur joueur %d\n ",player2->Id,player1->Id);
|
||
player1->HealthPoints = player1->HealthPoints - player2->AttacksPoints;
|
||
|
||
printf("Joueur %d : %s PV = %d\n ",player1->Id,player1->Name,player1->HealthPoints);
|
||
printf("Joueur %d : %s PV = %d \n",player2->Id,player2->Name,player2->HealthPoints);
|
||
}*/
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
|
||
void ActionPlayer(ARENA_H_TILE* arena,PLAYER * player, int action)
|
||
{
|
||
/* action = 1 --> Déplacement d'1 carreau vers le haut
|
||
* action = 2 --> Déplacement d'1 carreau vers la droite
|
||
* action = 3 --> Déplacement d'1 carreau vers le bas
|
||
* action = 4 --> Déplacement d'1 carreau vers la gauche
|
||
*
|
||
* */
|
||
int ID;
|
||
|
||
|
||
if(player != NULL)
|
||
{
|
||
// récupère la position du personnage. La position de l'instance sur l'arène est récupérer par une fonction
|
||
int positionX = player->PositionX;
|
||
int positionY = player->PositionY;
|
||
|
||
|
||
|
||
printf("\n**** Player %s ****\n",player->Name);
|
||
|
||
if(action == 1)
|
||
{
|
||
printf("On veut aller vers le haut\n");
|
||
ID = getTileTypeID(arena, positionX, positionY -1);
|
||
printf("ID case[%i][%i] : %i \n",positionX,positionY-1,ID);
|
||
|
||
if((positionY-1 >= 0) && (ID == 0))
|
||
{
|
||
player->PositionY = positionY-1;
|
||
printf("Déplacement de 1 vers le haut\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
|
||
}
|
||
else
|
||
{
|
||
printf("Impossible d'aller vers le haut\n");
|
||
}
|
||
}
|
||
else if(action == 2)
|
||
{
|
||
printf("On veut aller vers la droite\n");
|
||
ID = getTileTypeID(arena, positionX + 1, positionY);
|
||
printf("ID case[%i][%i] : %i \n",positionX+1,positionY,ID);
|
||
|
||
if((positionX + 1 < ARENAMAX) && (ID == 0))
|
||
{
|
||
player->PositionX = positionX + 1;
|
||
printf("Déplacement de 1 vers la droite\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
|
||
}
|
||
else
|
||
{
|
||
printf("Impossible d'aller vers la droite\n");
|
||
}
|
||
}
|
||
else if(action == 3)
|
||
{
|
||
printf("On veut aller vers le bas\n");
|
||
ID = getTileTypeID(arena, positionX, positionY + 1);
|
||
printf("ID case[%i][%i] : %i \n",positionX,positionY+1,ID);
|
||
|
||
if((positionY + 1 < ARENAMAX) && (ID == 0))
|
||
{
|
||
player->PositionY = positionY + 1;
|
||
printf("Déplacement de 1 vers le bas\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
|
||
}
|
||
else
|
||
{
|
||
printf("Impossible d'aller vers le bas\n");
|
||
}
|
||
}
|
||
else if(action == 4)
|
||
{
|
||
printf("On veut aller vers la gauche\n");
|
||
ID = getTileTypeID(arena, positionX - 1, positionY);
|
||
printf("ID case[%i][%i] : %i \n",positionX-1,positionY,ID);
|
||
|
||
if((positionX - 1 >= 0) && (ID == 0))
|
||
{
|
||
player->PositionX = positionX - 1;
|
||
printf("Déplacement de 1 vers la gauche\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
|
||
}
|
||
else
|
||
{
|
||
printf("Impossible d'aller vers la gauche\n");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
printf("Autre action\n");
|
||
ID = getTileTypeID(arena, positionX, positionY -1);
|
||
printf("ID case[%i][%i] : %i \n",positionX,positionY-1,ID);
|
||
|
||
ID = getTileTypeID(arena, positionX + 1, positionY);
|
||
printf("ID case[%i][%i] : %i \n",positionX+1,positionY,ID);
|
||
|
||
ID = getTileTypeID(arena, positionX, positionY + 1);
|
||
printf("ID case[%i][%i] : %i \n",positionX+1,positionY,ID);
|
||
|
||
ID = getTileTypeID(arena, positionX - 1, positionY);
|
||
printf("ID case[%i][%i] : %i \n",positionX-1,positionY,ID);
|
||
|
||
|
||
}
|
||
}
|
||
else
|
||
{
|
||
printf("Echec, le joueur est NULL\n");
|
||
}
|
||
}
|
||
|
||
|
||
/*
|
||
void ActionPlayer(PLAYER * player, int action)
|
||
{
|
||
/* action = 1 --> Déplacement d'1 carreau vers le haut
|
||
* action = 2 --> Déplacement d'1 carreau vers la droite
|
||
* action = 3 --> Déplacement d'1 carreau vers le bas
|
||
* action = 4 --> Déplacement d'1 carreau vers la gauche
|
||
*
|
||
*
|
||
|
||
if(player != NULL)
|
||
{
|
||
// récupère la position du personnage. La position de l'instance sur l'arène est récupérer par une fonction
|
||
int positionX = player->PositionX;
|
||
int positionY = player->PositionY;
|
||
|
||
|
||
|
||
printf("\n**** Player %s ****\n",player->Name);
|
||
|
||
if(action == 1)
|
||
{
|
||
printf("On veut aller vers le haut\n");
|
||
|
||
if(positionY-1 >= 0)
|
||
{
|
||
player->PositionY = positionY-1;
|
||
printf("Déplacement de 1 vers le haut\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
|
||
}
|
||
else
|
||
{
|
||
printf("Impossible d'aller vers le haut\n");
|
||
}
|
||
}
|
||
else if(action == 2)
|
||
{
|
||
printf("On veut aller vers la droite\n");
|
||
|
||
if(positionX + 1 < ARENAMAX)
|
||
{
|
||
player->PositionX = positionX + 1;
|
||
printf("Déplacement de 1 vers la droite\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
|
||
}
|
||
else
|
||
{
|
||
printf("Impossible d'aller vers la droite\n");
|
||
}
|
||
}
|
||
else if(action == 3)
|
||
{
|
||
printf("On veut aller vers le bas\n");
|
||
|
||
if(positionY + 1 < ARENAMAX)
|
||
{
|
||
player->PositionY = positionY + 1;
|
||
printf("Déplacement de 1 vers le bas\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
|
||
}
|
||
else
|
||
{
|
||
printf("Impossible d'aller vers le bas\n");
|
||
}
|
||
}
|
||
else if(action == 4)
|
||
{
|
||
printf("On veut aller vers la gauche\n");
|
||
|
||
if(positionX - 1 >= 0)
|
||
{
|
||
player->PositionX = positionX - 1;
|
||
printf("Déplacement de 1 vers la gauche\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
|
||
}
|
||
else
|
||
{
|
||
printf("Impossible d'aller vers la gauche\n");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
printf("Autre action\n");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
printf("Echec, le joueur est NULL\n");
|
||
}
|
||
}*/
|
||
|