Random gen arena prototype

This commit is contained in:
JackCarterSmith 2018-06-21 20:27:58 +02:00
parent b3a0b6619e
commit 2566ef569c
19 changed files with 2708 additions and 73 deletions

3
.gitignore vendored
View File

@ -41,6 +41,9 @@
*.su
*.idb
*.pdb
docs\
latex\
html\
# Kernel Module Compile Results
*.mod*

File diff suppressed because it is too large Load Diff

View File

@ -1,23 +1,13 @@
/*
* IAEngine.c
*
* Created on: 17 juin 2018
* Author: isen
*
* Fonction qui va définir le comportement joueur(s) "ordinateur"
*/
#include "playerInterface.h"
#include "SDL2/SDL.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "playerInterface.h"
int IAEngine(PLAYER * player1, PLAYER * player2)
{
char Race[20];
//char Race[20];
//Player 1 est le joueur, player 2 le bot
if((player1 != NULL) & (player2 !=NULL))

View File

@ -2,7 +2,6 @@
#include <stdio.h>
#include "fileHandler.h"
#include "logHelper.h"
#include "main.h"
#include "arenaEngine.h"
@ -10,9 +9,96 @@
* Arena generate functions
*/
TILE *createTileList(void) {
TILE* origin = NULL;
TILE *tile_0 = NULL, *tile_1 = NULL, *tile_2 = NULL, *tile_3 = NULL, *tile_4 = NULL;
tile_0 = calloc(1,sizeof(TILE));
tile_1 = calloc(1,sizeof(TILE));
tile_2 = calloc(1,sizeof(TILE));
tile_3 = calloc(1,sizeof(TILE));
tile_4 = calloc(1,sizeof(TILE));
tile_0->type_id = 0;
tile_0->texture = IMG_Load("data/tile_grass.png");
tile_0->canBeMined = 0;
tile_0->isGround = 1;
tile_0->nextTile = tile_1;
tile_1->type_id = 1;
tile_1->texture = IMG_Load("data/tile_rock.png");
tile_1->canBeMined = 0;
tile_1->isGround = 1;
tile_1->nextTile = tile_2;
tile_2->type_id = 2;
tile_2->texture = IMG_Load("data/tile_tree.png");
tile_2->canBeMined = 0;
tile_2->isGround = 0;
tile_2->nextTile = tile_3;
tile_3->type_id = 3;
tile_3->texture = IMG_Load("data/tile_water.png");
tile_3->canBeMined = 0;
tile_3->isGround = 0;
tile_3->nextTile = tile_4;
tile_4->type_id = 4;
tile_4->texture = IMG_Load("data/tile_gold.png");
tile_4->canBeMined = 1;
tile_4->isGround = 1;
tile_4->nextTile = NULL;
return tile_0;
}
PLAYER *createPlayerList(void) {
PLAYER *p0 = NULL, *p1 = NULL, *p2 = NULL, *p3 = NULL;
p0 = calloc(1,sizeof(PLAYER));
p1 = calloc(1,sizeof(PLAYER));
p2 = calloc(1,sizeof(PLAYER));
p3 = calloc(1,sizeof(PLAYER));
p0->Id = 0;
p0->PositionX = 0;
p0->PositionY = 0;
p0->texture[DOWN] = IMG_Load("data/sprite_player_0.png");
p0->texture[UP] = IMG_Load("data/sprite_player_1.png");
p0->texture[LEFT] = IMG_Load("data/sprite_player_2.png");
p0->texture[RIGHT] = IMG_Load("data/sprite_player_3.png");
p0->suiv = p1;
p1->Id = 1;
p1->PositionX = A_WIDTH;
p1->PositionY = A_HEIGHT;
p1->texture[DOWN] = IMG_Load("data/sprite_ia_0.png");
p1->texture[UP] = IMG_Load("data/sprite_ia_1.png");
p1->texture[LEFT] = IMG_Load("data/sprite_ia_2.png");
p1->texture[RIGHT] = IMG_Load("data/sprite_ia_3.png");
p1->suiv = p2;
p2->Id = 0;
p2->PositionX = 0;
p2->PositionY = A_HEIGHT;
//p2->texture[DOWN] = IMG_Load("data/sprite_player_0.png");
//p2->texture[UP] = IMG_Load("data/sprite_player_1.png");
//p2->texture[LEFT] = IMG_Load("data/sprite_player_2.png");
//p2->texture[RIGHT] = IMG_Load("data/sprite_player_3.png");
p2->suiv = p3;
p3->Id = 0;
p3->PositionX = A_WIDTH;
p3->PositionY = 0;
//p3->texture[DOWN] = IMG_Load("data/sprite_player_0.png");
//p3->texture[UP] = IMG_Load("data/sprite_player_1.png");
//p3->texture[LEFT] = IMG_Load("data/sprite_player_2.png");
//p3->texture[RIGHT] = IMG_Load("data/sprite_player_3.png");
p3->suiv = NULL;
return p1;
}
void clearRessourcesCache(TILE *t, PLAYER *p) {
return origin;
}
ARENA_H_TILE* createHTile(ARENA_H_TILE* prevHTile) {
@ -20,14 +106,14 @@ ARENA_H_TILE* createHTile(ARENA_H_TILE* prevHTile) {
if (prevHTile == NULL) {
tile_h = calloc(1,sizeof(ARENA_H_TILE)); //Using calloc because of resetting all memory allocated to 0
tile_h->type_id = 0;
tile_h->type_id = random_lim(5);
//tile_h->playerOnTile = NULL;
tile_h->nextRow = NULL;
tile_h->nextColumn = NULL;
} else if (prevHTile != NULL) {
tile_h = calloc(1,sizeof(ARENA_H_TILE));
prevHTile->nextRow = tile_h;
tile_h->type_id = 0;
tile_h->type_id = random_lim(5);
//tile_h->playerOnTile = NULL;
tile_h->nextRow = NULL;
tile_h->nextColumn = NULL;
@ -42,7 +128,7 @@ ARENA_W_TILE* createWTile(ARENA_H_TILE* prevHTile, ARENA_W_TILE* prevWTile) {
if (prevHTile != NULL && prevWTile == NULL) {
tile_w = calloc(1,sizeof(ARENA_W_TILE));
prevHTile->nextColumn = tile_w;
tile_w->type_id = 0;
tile_w->type_id = random_lim(5);
//tile_w->playerOnTile = NULL;
tile_w->nextColumn = NULL;
@ -50,7 +136,7 @@ ARENA_W_TILE* createWTile(ARENA_H_TILE* prevHTile, ARENA_W_TILE* prevWTile) {
} else if (prevHTile == NULL && prevWTile != NULL) {
tile_w = calloc(1,sizeof(ARENA_W_TILE));
prevWTile->nextColumn = tile_w;
tile_w->type_id = 0;
tile_w->type_id = random_lim(5);
//tile_w->playerOnTile = NULL;
tile_w->nextColumn = NULL;
}
@ -158,6 +244,22 @@ int getTileTypeID(ARENA_H_TILE* arena, int coord_x, int coord_y) {
return type_id;
}
SDL_Surface *getTileSurfaceFromID(TILE *t_list, int id) {
SDL_Surface *t_surface = NULL;
TILE *tmp_tile = NULL;
int i=0;
tmp_tile = t_list;
while (i<id) {
tmp_tile = tmp_tile->nextTile;
i++;
}
t_surface = tmp_tile->texture;
return t_surface;
}
int isPlayerAdjacent(PLAYER* p1, PLAYER* p2) {
int adjPlayer = -1;
@ -165,3 +267,28 @@ int isPlayerAdjacent(PLAYER* p1, PLAYER* p2) {
return adjPlayer;
}
int isMoveCorrect(int coord_x, int coord_y, int direction) {
if (!((coord_x <= 0 && direction == LEFT) || (coord_y <= 0 && direction == UP) || (coord_x >= A_HEIGHT && direction == RIGHT) || (coord_y >= A_WIDTH && direction == DOWN))) return 1;
return 0;
}
int getRelativeDirection(SDL_Rect pos1, SDL_Rect pos2) {
int _x,_y;
_x = pos2.x - pos1.x;
_y = pos2.y - pos1.y;
if (_x>1) {
return DOWN;
} else if (_x<1) {
return UP;
} else if (_y>1) {
return RIGHT;
} else if (_y<1) {
return LEFT;
} else {
return -1;
}
}

View File

@ -1,5 +1,6 @@
#include "playerInterface.h"
#include "main.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#ifndef ARENAENGINE_H_
#define ARENAENGINE_H_
@ -18,12 +19,34 @@ typedef struct items{
typedef struct tileType{
int type_id;
SDL_Surface texture;
SDL_Surface *texture;
int isGround;
int canBeMined;
struct tileType *nextTile;
}TILE;
typedef struct Player
{
int Id;
char Name[35];
//char Race[20];
SDL_Surface *texture[4];
int HealthPoints;
int AttacksPoints;
int DefensePoints;
int PositionX;
int PositionY;
//char Weapons[Max_Weapons];
//int Coins;
struct Player * suiv;
}PLAYER;
typedef struct arena_h_tile{ //Rows chained list
int type_id;
//PLAYER* playerOnTile;
@ -40,10 +63,16 @@ typedef struct arena_w_tile{ //Columns chained list
//Generation functions
TILE* createTileList(void);
PLAYER *createPlayerList(void);
void clearRessourcesCache(TILE *t, PLAYER *p);
ARENA_H_TILE* genNewArena(int size_h, int size_w);
void deleteArena(ARENA_H_TILE* arena);
//Status functions
int getTileTypeID(ARENA_H_TILE* arena, int coord_x, int coord_y);
SDL_Surface *getTileSurfaceFromID(TILE *t_list, int id);
int isPlayerAdjacent(PLAYER* p1, PLAYER* p2);
int isMoveCorrect(int coord_x, int coord_y, int direction);
int getRelativeDirection(SDL_Rect pos1, SDL_Rect pos2);
#endif

View File

@ -6,18 +6,29 @@
#include <SDL2/SDL_ttf.h>
#include "arenaGUI.h"
void displayArena(ARENA_H_TILE* arena, SDL_Window* window, SDL_Surface *res, int size_h, int size_w, int tile_size) {
void displayArena(ARENA_H_TILE* arena, SDL_Window* window, TILE *tiles, int size_h, int size_w, int tile_size) {
SDL_Rect tmp_tile_coord;
int i,j;
for (i=0; i<size_h; i++) {
for (j=0; j<size_w; j++) {
tmp_tile_coord.x = i * tile_size;
tmp_tile_coord.y = j * tile_size;
SDL_BlitSurface(res, NULL, SDL_GetWindowSurface(window), &tmp_tile_coord);
SDL_BlitSurface(getTileSurfaceFromID(tiles,getTileTypeID(arena,i,j)), NULL, SDL_GetWindowSurface(window), &tmp_tile_coord);
SDL_UpdateWindowSurface(window);
}
}
}
void updatePlayerPos(ARENA_H_TILE* arena, SDL_Window* window, PLAYER *player, TILE *tiles, SDL_Rect new_coord) {
SDL_Rect old_coord;
old_coord.x = player->PositionX;
old_coord.y = player->PositionY;
SDL_BlitSurface(getTileSurfaceFromID(tiles,getTileTypeID(arena,player->PositionX,player->PositionY)), NULL, SDL_GetWindowSurface(window), &old_coord);
SDL_BlitSurface(player->texture[getRelativeDirection(old_coord, new_coord)], NULL, SDL_GetWindowSurface(window), &new_coord);
SDL_UpdateWindowSurface(window);
}

View File

@ -4,6 +4,7 @@
#ifndef ARENAGUI_H_
#define ARENAGUI_H_
void displayArena(ARENA_H_TILE* arena, SDL_Window* windows, SDL_Surface *res, int size_h, int size_w, int tile_size);
void displayArena(ARENA_H_TILE* arena, SDL_Window* windows, TILE *tiles, int size_h, int size_w, int tile_size);
void updatePlayerPos(ARENA_H_TILE* arena, SDL_Window* window, PLAYER *player, TILE *tiles, SDL_Rect new_coord);
#endif

BIN
data/sprite_ia_0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.2 KiB

After

Width:  |  Height:  |  Size: 2.9 KiB

BIN
data/sprite_ia_2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

BIN
data/sprite_ia_3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

BIN
data/sprite_player_0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.2 KiB

After

Width:  |  Height:  |  Size: 3.3 KiB

BIN
data/sprite_player_2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

BIN
data/sprite_player_3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

26
main.c
View File

@ -1,6 +1,7 @@
#include "main.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "logHelper.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
@ -21,12 +22,23 @@ void initDisplayLib() {
}
}
int random_lim(int max) {
int r, d = RAND_MAX / max;
max *= d;
do { r = rand(); } while (r >= max);
return r / d;
}
int main(int argc, char *argv[]) {
addLogInfo("Starting game...");
srand(time(NULL));
addLogInfo("Try init SDL libs...");
initDisplayLib();
addLogInfo("Load ressources in memory...");
TILE *tile_ressources = createTileList();
PLAYER *player_ressources = createPlayerList();
/*
SDL_Surface *tile_grass = NULL, *tile_rock = NULL, *tile_tree = NULL, *tile_water = NULL, *sprite_player_act = NULL, *sprite_ia_act = NULL;
SDL_Surface *ressource[6] = {tile_grass, tile_rock, tile_tree, tile_water, sprite_player_act, sprite_ia_act};
SDL_Surface *sprite_player[4] = {NULL}, *sprite_ia[4] = {NULL};
@ -44,6 +56,7 @@ int main(int argc, char *argv[]) {
//sprite_ia[UP] = IMG_Load("data/sprite_ia_2.png");
//sprite_ia[LEFT] = IMG_Load("data/sprite_ia_3.png");
//sprite_ia[RIGHT] = IMG_Load("data/sprite_ia_4.png");
*/
addLogInfo("Create SDL windows instance...");
SDL_Window* gameWindows = SDL_CreateWindow("Arena Survival Tournament - alpha 0.2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOWS_WIDTH, WINDOWS_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
@ -58,17 +71,20 @@ int main(int argc, char *argv[]) {
addLogInfo("Successfully created arena.");
addLogInfo("Display arena GUI...");
sprite_player_act = sprite_player[DOWN];
sprite_ia_act = sprite_ia[DOWN];
displayArena(arena, gameWindows, tile_grass, A_HEIGHT, A_WIDTH, TILE_SIZE);
//sprite_player_act = sprite_player[DOWN];
//sprite_ia_act = sprite_ia[DOWN];
displayArena(arena, gameWindows, tile_ressources, A_HEIGHT, A_WIDTH, TILE_SIZE);
SDL_Delay(5000);
deleteArena(arena);
addLogInfo("Cleared arena.");
SDL_Delay(5000);
clearRessourcesCache(tile_ressources, player_ressources);
addLogInfo("Free ressources.");
addLogInfo("Unload SDL libs...");
SDL_Quit();
return EXIT_SUCCESS;

7
main.h
View File

@ -1,14 +1,15 @@
#ifndef MAIN_H_
#define MAIN_H_
#define A_WIDTH 20
#define A_HEIGHT 20
#define A_WIDTH 30
#define A_HEIGHT 30
#define TILE_SIZE 32
#define WINDOWS_WIDTH TILE_SIZE * A_WIDTH
#define WINDOWS_HEIGHT TILE_SIZE * A_HEIGHT
enum {DOWN, UP, LEFT, RIGHT};
enum {GRASS_T, ROCK_T, TREE_T, WATER_T, PLAYER_S, IA_S};
int random_lim(int max);
#endif

View File

@ -1,15 +1,6 @@
/*
* playerInterface.c
*
* Created on: 17 juin 2018
* Author: isen
*/
#include "playerInterface.h"
#include "SDL2/SDL.h"
#include <stdio.h>
#include <stdlib.h>
#include "playerInterface.h"
#define ArenaMAX 100

View File

@ -1,36 +1,10 @@
/*
* playerInterface.h
*
* Created on: 17 juin 2018
* Author: isen
*/
#include <SDL2/SDL.h>
#include "arenaEngine.h"
#include "IAEngine.h"
#ifndef PLAYERINTERFACE_H_
#define PLAYERINTERFACE_H_
/** Structure d'un joueur (IA ou utilisateur)
*
* */
typedef struct Player
{
int Id;
char Name[35];
char Race[20];
int HealthPoints;
int AttacksPoints;
int DefensePoints;
int PositionX;
int PositionY;
//char Weapons[Max_Weapons];
//int Coins;
struct Player * suiv;
}PLAYER;
//Prototypes
PLAYER* createPlayer(int Id);