Updated collision check
This commit is contained in:
parent
1886bd7f16
commit
4bb7feaf18
@ -1,5 +1,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "arenaEngine.h"
|
||||
|
||||
|
||||
@ -109,16 +110,8 @@ PLAYER *createPlayerList(void) {
|
||||
p1->texture[LEFT] = IMG_Load("data/sprite_ia_2.png");
|
||||
p1->texture[RIGHT] = IMG_Load("data/sprite_ia_3.png");
|
||||
p1->suiv = p2;
|
||||
printf("Entrer le nom du joueur\n");
|
||||
scanf("%s",p1->Name);
|
||||
|
||||
do
|
||||
{
|
||||
printf("Race? 1:Orc 2: Elf 3:Humain \n");
|
||||
scanf("%d",&race);
|
||||
} while((race < 1) || (race > 3));
|
||||
|
||||
p1->Race = race;
|
||||
strcpy(p1->Name,"SKY-NUT");
|
||||
p1->Race = random_lim(4);
|
||||
|
||||
switch(race)
|
||||
{
|
||||
@ -522,20 +515,38 @@ int isPlayerAdjacent(PLAYER* p1, PLAYER* p2) {
|
||||
return adjPlayer;
|
||||
}
|
||||
|
||||
int isMoveCorrect(ARENA_H_TILE* arena, TILE *t_list, int coord_x, int coord_y, int direction) {
|
||||
if (!((coord_x <= 0 && direction == LEFT) || (coord_y <= 0 && direction == UP) || (coord_x >= A_HEIGHT-1 && direction == RIGHT) || (coord_y >= A_WIDTH-1 && direction == DOWN))) {
|
||||
if (direction==UP && isGroundTile(t_list,getTileTypeID(arena,coord_x,coord_y-1))) {
|
||||
return 1;
|
||||
} else if (direction==DOWN && isGroundTile(t_list,getTileTypeID(arena,coord_x,coord_y+1))) {
|
||||
return 1;
|
||||
} else if (direction==LEFT && isGroundTile(t_list,getTileTypeID(arena,coord_x-1,coord_y))) {
|
||||
return 1;
|
||||
} else if (direction==RIGHT && isGroundTile(t_list,getTileTypeID(arena,coord_x+1,coord_y))) {
|
||||
return 1;
|
||||
}
|
||||
int isMoveCorrect(ARENA_H_TILE* arena, TILE *t_list, PLAYER *p_list, int coord_x, int coord_y, int direction) {
|
||||
PLAYER *tmp_player = NULL;
|
||||
int _x,_y,allow=0;
|
||||
|
||||
if (direction==UP) {
|
||||
_x = coord_x;
|
||||
_y = coord_y-1;
|
||||
} else if (direction==DOWN) {
|
||||
_x = coord_x;
|
||||
_y = coord_y+1;
|
||||
} else if (direction==LEFT) {
|
||||
_x = coord_x-1;
|
||||
_y = coord_y;
|
||||
} else if (direction==RIGHT) {
|
||||
_x = coord_x+1;
|
||||
_y = coord_y;
|
||||
} else {
|
||||
_x = coord_x;
|
||||
_y = coord_y;
|
||||
}
|
||||
|
||||
return 0;
|
||||
if (_x >= 0 && _y >= 0 && _x < A_HEIGHT && _y < A_WIDTH) {
|
||||
if (isGroundTile(t_list,getTileTypeID(arena,_x,_y))) allow=1;
|
||||
}
|
||||
|
||||
tmp_player = p_list;
|
||||
do {
|
||||
if (tmp_player->PositionX == _x && tmp_player->PositionY == _y) allow=0;
|
||||
tmp_player = tmp_player->suiv;
|
||||
} while (tmp_player != NULL);
|
||||
|
||||
return allow;
|
||||
}
|
||||
|
||||
int NumberPlayerAlive(PLAYER *Head)
|
||||
@ -620,7 +631,7 @@ void AttackPlayer(PLAYER *player1, PLAYER *player2)
|
||||
}
|
||||
}
|
||||
|
||||
void ActionPlayer(ARENA_H_TILE* arena,TILE *t_list,PLAYER *player,int action)
|
||||
void ActionPlayer(ARENA_H_TILE* arena,TILE *t_list,PLAYER *p_list,PLAYER *player,int action)
|
||||
{
|
||||
/* action = 1 --> Déplacement d'1 carreau vers le haut
|
||||
* action = 2 --> Déplacement d'1 carreau vers la droite
|
||||
@ -648,7 +659,7 @@ void ActionPlayer(ARENA_H_TILE* arena,TILE *t_list,PLAYER *player,int action)
|
||||
|
||||
|
||||
|
||||
if(isMoveCorrect(arena,t_list,current_pos.x,current_pos.y,UP))
|
||||
if(isMoveCorrect(arena,t_list,p_list,current_pos.x,current_pos.y,UP))
|
||||
{
|
||||
player->PositionY = (current_pos.y)-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);
|
||||
@ -663,7 +674,7 @@ void ActionPlayer(ARENA_H_TILE* arena,TILE *t_list,PLAYER *player,int action)
|
||||
printf("On veut aller vers la droite\n");
|
||||
printf("ID case[%i][%i] : %i \n",current_pos.x+1,current_pos.y,getTileTypeID(arena, current_pos.x+1, current_pos.y));
|
||||
|
||||
if(isMoveCorrect(arena,t_list,current_pos.x,current_pos.y,RIGHT))
|
||||
if(isMoveCorrect(arena,t_list,p_list,current_pos.x,current_pos.y,RIGHT))
|
||||
{
|
||||
player->PositionX = (current_pos.x)+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);
|
||||
@ -678,7 +689,7 @@ void ActionPlayer(ARENA_H_TILE* arena,TILE *t_list,PLAYER *player,int action)
|
||||
printf("On veut aller vers le bas\n");
|
||||
printf("ID case[%i][%i] : %i \n",current_pos.x,current_pos.y+1,getTileTypeID(arena, current_pos.x, current_pos.y+1));
|
||||
|
||||
if(isMoveCorrect(arena,t_list,current_pos.x,current_pos.y,DOWN))
|
||||
if(isMoveCorrect(arena,t_list,p_list,current_pos.x,current_pos.y,DOWN))
|
||||
{
|
||||
player->PositionY = (current_pos.y)+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);
|
||||
@ -693,7 +704,7 @@ void ActionPlayer(ARENA_H_TILE* arena,TILE *t_list,PLAYER *player,int action)
|
||||
printf("On veut aller vers la gauche\n");
|
||||
printf("ID case[%i][%i] : %i \n",current_pos.x-1,current_pos.y,getTileTypeID(arena, current_pos.x-1, current_pos.y));
|
||||
|
||||
if(isMoveCorrect(arena,t_list,current_pos.x,current_pos.y,LEFT))
|
||||
if(isMoveCorrect(arena,t_list,p_list,current_pos.x,current_pos.y,LEFT))
|
||||
{
|
||||
player->PositionX = (current_pos.x)-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);
|
||||
|
@ -66,12 +66,12 @@ int getTileTypeID(ARENA_H_TILE* arena, int coord_x, int coord_y);
|
||||
int isGroundTile(TILE *t_list, int id);
|
||||
SDL_Surface *getTileSurfaceFromID(TILE *t_list, int id);
|
||||
int isPlayerAdjacent(PLAYER* p1, PLAYER* p2);
|
||||
int isMoveCorrect(ARENA_H_TILE* arena, TILE *t_list, int coord_x, int coord_y, int direction);
|
||||
int isMoveCorrect(ARENA_H_TILE* arena, TILE *t_list, PLAYER *p_list, int coord_x, int coord_y, int direction);
|
||||
int getRelativeDirection(SDL_Rect pos1, SDL_Rect pos2);
|
||||
|
||||
int NumberPlayerAlive(PLAYER *Head);
|
||||
int canAttackPlayer(PLAYER *p1, PLAYER *p2);
|
||||
void AttackPlayer(PLAYER *player1, PLAYER *player2);
|
||||
void ActionPlayer(ARENA_H_TILE* arena,TILE *t_list,PLAYER * player, int action);
|
||||
void ActionPlayer(ARENA_H_TILE* arena,TILE *t_list,PLAYER *p_list,PLAYER *player,int action);
|
||||
|
||||
#endif
|
||||
|
@ -17,7 +17,7 @@ void displayArena(ARENA_H_TILE* arena, SDL_Window* window, TILE *tiles, int size
|
||||
}
|
||||
}
|
||||
|
||||
int updateArena(SDL_Window* window, ARENA_H_TILE* arena, TILE *tiles, PLAYER *player) {
|
||||
int updateArena(SDL_Window* window, ARENA_H_TILE* arena, TILE *tiles, PLAYER *p_list) {
|
||||
//Ajouté
|
||||
SDL_Rect position;
|
||||
SDL_Rect oldposition;
|
||||
@ -26,12 +26,12 @@ int updateArena(SDL_Window* window, ARENA_H_TILE* arena, TILE *tiles, PLAYER *pl
|
||||
|
||||
PLAYER *p1=NULL,*p2=NULL;
|
||||
|
||||
p1=player;p2=player->suiv;
|
||||
p1=p_list;p2=p_list->suiv;
|
||||
|
||||
printf("Nom player 1 : %s\n",p1->Name);
|
||||
printf("Nom player 2 : %s\n",p2->Name);
|
||||
|
||||
printf("Nombre de joueur en vie : %i\n",NumberPlayerAlive(player));
|
||||
printf("Nombre de joueur en vie : %i\n",NumberPlayerAlive(p_list));
|
||||
|
||||
int action=0,actionIA=0;
|
||||
|
||||
@ -57,15 +57,15 @@ int updateArena(SDL_Window* window, ARENA_H_TILE* arena, TILE *tiles, PLAYER *pl
|
||||
}
|
||||
|
||||
if(action == -1){
|
||||
return -1;
|
||||
return 0;
|
||||
} else if(action == 1) {
|
||||
ActionPlayer(arena,tiles,p1,1); //Déplacement vers le haut
|
||||
ActionPlayer(arena,tiles,p_list,p1,1); //Déplacement vers le haut
|
||||
} else if(action == 2) {
|
||||
ActionPlayer(arena,tiles,p1,2); //Déplacement vers la droite
|
||||
ActionPlayer(arena,tiles,p_list,p1,2); //Déplacement vers la droite
|
||||
} else if(action == 3) {
|
||||
ActionPlayer(arena,tiles,p1,3); //Déplacement vers le bas
|
||||
ActionPlayer(arena,tiles,p_list,p1,3); //Déplacement vers le bas
|
||||
} else if(action == 4) {
|
||||
ActionPlayer(arena,tiles,p1,4); //Déplacement vers la gauche
|
||||
ActionPlayer(arena,tiles,p_list,p1,4); //Déplacement vers la gauche
|
||||
} else if(action == 5) {
|
||||
//Regarder le perso en face, le plus près
|
||||
if (canAttackPlayer(p1,p2)==1) {
|
||||
@ -77,9 +77,11 @@ int updateArena(SDL_Window* window, ARENA_H_TILE* arena, TILE *tiles, PLAYER *pl
|
||||
position.x = p1->PositionX * TILE_SIZE;
|
||||
position.y = p1->PositionY * TILE_SIZE;
|
||||
|
||||
SDL_BlitSurface(getTileSurfaceFromID(tiles,getTileTypeID(arena,(oldposition.x)/TILE_SIZE,(oldposition.y)/TILE_SIZE)), NULL, SDL_GetWindowSurface(window), &oldposition);
|
||||
//SDL_BlitSurface(p1->texture[DOWN], NULL, SDL_GetWindowSurface(window), &position);
|
||||
SDL_BlitSurface(p1->texture[getRelativeDirection(oldposition, position)], NULL, SDL_GetWindowSurface(window), &position);
|
||||
if (position.x!=oldposition.x || position.y!=oldposition.y) {
|
||||
SDL_BlitSurface(getTileSurfaceFromID(tiles,getTileTypeID(arena,(oldposition.x)/TILE_SIZE,(oldposition.y)/TILE_SIZE)), NULL, SDL_GetWindowSurface(window), &oldposition);
|
||||
//SDL_BlitSurface(p1->texture[DOWN], NULL, SDL_GetWindowSurface(window), &position);
|
||||
SDL_BlitSurface(p1->texture[getRelativeDirection(oldposition, position)], NULL, SDL_GetWindowSurface(window), &position);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -92,21 +94,23 @@ int updateArena(SDL_Window* window, ARENA_H_TILE* arena, TILE *tiles, PLAYER *pl
|
||||
}
|
||||
else if ((actionIA >= 1) && (actionIA <= 4))
|
||||
{
|
||||
ActionPlayer(arena,tiles,p2,actionIA);
|
||||
ActionPlayer(arena,tiles,p_list,p2,actionIA);
|
||||
}
|
||||
|
||||
IAposition.x = p2->PositionX * TILE_SIZE;
|
||||
IAposition.y = p2->PositionY * TILE_SIZE;
|
||||
|
||||
SDL_BlitSurface(getTileSurfaceFromID(tiles,getTileTypeID(arena,(oldIAposition.x)/TILE_SIZE,(oldIAposition.y)/TILE_SIZE)), NULL, SDL_GetWindowSurface(window), &oldIAposition);
|
||||
//SDL_BlitSurface(p2->texture[DOWN], NULL, SDL_GetWindowSurface(window), &IAposition);
|
||||
SDL_BlitSurface(p2->texture[getRelativeDirection(oldIAposition, IAposition)], NULL, SDL_GetWindowSurface(window), &IAposition);
|
||||
if (IAposition.x!=oldIAposition.x || IAposition.y!=oldIAposition.y) {
|
||||
SDL_BlitSurface(getTileSurfaceFromID(tiles,getTileTypeID(arena,(oldIAposition.x)/TILE_SIZE,(oldIAposition.y)/TILE_SIZE)), NULL, SDL_GetWindowSurface(window), &oldIAposition);
|
||||
//SDL_BlitSurface(p2->texture[DOWN], NULL, SDL_GetWindowSurface(window), &IAposition);
|
||||
SDL_BlitSurface(p2->texture[getRelativeDirection(oldIAposition, IAposition)], NULL, SDL_GetWindowSurface(window), &IAposition);
|
||||
}
|
||||
|
||||
SDL_UpdateWindowSurface(window);
|
||||
|
||||
action=0;actionIA=0;
|
||||
|
||||
} while((p1->HealthPoints > 0) && (NumberPlayerAlive(player) > 1));
|
||||
} while((p1->HealthPoints > 0) && (NumberPlayerAlive(p_list) > 1));
|
||||
|
||||
printf("La partie est terminée\n");
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user