Mentions légales du service

Skip to content
Snippets Groups Projects
Commit d5f5da00 authored by BAROLLET Theo's avatar BAROLLET Theo
Browse files

removed direction as a feature and added it to the first engine

parent fdb1db8a
No related branches found
No related tags found
No related merge requests found
......@@ -14,9 +14,6 @@ LEVEL_END
- ajout des defines ACTIVATED et DEACTIVATED pour les leviers
LEVERS (cest tout petit cest deux lignes)
- ajout de la direction
DIRECTIONS (cest aussi petit)
- inventaire array
INVENTORY_ARRAY
......
......@@ -40,55 +40,47 @@ void player_right() {
void up(int y_start, int x_start) {
#ifdef DIRECTIONS
current_map()->player_direction = UP;
#endif
move_entity(y_start, x_start, y_start-1, x_start);
}
void down(int y_start, int x_start) {
#ifdef DIRECTIONS
current_map()->player_direction = DOWN;
#endif
move_entity(y_start, x_start, y_start+1, x_start);
}
void left(int y_start, int x_start){
#ifdef DIRECTIONS
current_map()->player_direction = LEFT;
#endif
move_entity(y_start, x_start, y_start, x_start-1);
}
void right(int y_start, int x_start) {
#ifdef DIRECTIONS
current_map()->player_direction = RIGHT;
#endif
move_entity(y_start, x_start, y_start, x_start+1);
}
#ifdef COMMAND_ARGS
void up_n(int y_start, int x_start, unsigned n) {
for(unsigned i = 0; i < n; i++) {
player_up()
player_up();
}
}
void down_n(int y_start, int x_start, unsigned n) {
for(unsigned i = 0; i < n; i++) {
player_down()
player_down();
}
}
void left_n(int y_start, int x_start, unsigned n) {
for(unsigned i = 0; i < n; i++) {
player_left()
player_left();
}
}
void right_n(int y_start, int x_start, unsigned n) {
for(unsigned i = 0; i < n; i++) {
player_right()
player_right();
}
}
#endif
......
......@@ -3,14 +3,17 @@
// when true, the main loop ends. Should only be used in level_failed and level_success
bool exit_main_loop = false;
int level_exit_code = 0;
void level_failed(void) {
printf("DEFEAT!\n");
exit_main_loop = true;
level_exit_code = 1;
}
void level_success() {
printf("VICTORY!\n");
exit_main_loop = true;
level_exit_code = 0;
}
#endif
\ No newline at end of file
#include "map.h"
// Map initialization and creation
#ifdef MAP_STACK
static map_stack mstack;
void init_map_stack(void) {
mstack.length = 0;
}
map *current_map(void) {
if (mstack.length <= 0)
return NULL;
return &mstack.maps[mstack.length-1];
}
void init_map_stack(void) {
mstack.length = 0;
}
#else
static map global_map;
map *current_map(void) {
return &global_map;
}
void init_map(int height, int width) {
global_map.width = width;
global_map.height = height;
// Init empty tiles everywhere
global_map.tiles = malloc(sizeof(char) * height);
global_map.tiles = (char **) malloc(sizeof(char) * height);
for(int y = 0; y < height; y++) {
global_map.tiles[y] = malloc(sizeof(char) * width);
global_map.tiles[y] = (char *) malloc(sizeof(char) * width);
for(int x = 0; x < width; x++) {
global_map.tiles[y][x] = ' ';
}
}
}
#endif
map *current_map(void) {
return &global_map;
#ifdef MAP_STACK
// Internal function to init a map with a name
map * internal_init_map_with_name(const char * name) {
if (mstack.length >= MAX_MAPS - 1) {
printf("load_map: can't load a new map, the maximum number has been reached.\n");
return current_map();
}
mstack.length++;
map* m = current_map();
/* allocating enough space to hold the map */
m->name = (char *) malloc(sizeof(char) * (strlen(name) + 1));
strcpy(m->name, name);
return m;
}
// Internal function for map array initialization
void init_map_arrays(map * m) {
#ifdef ENTITY_STRUCT
m->entities = (entity ***) malloc(sizeof(entity **) * m->height);
m->floor = (tile ***) malloc(sizeof(tile **) * m->height);
// Allocate an empty floor tile used for every tile.
tile *t = (tile *) malloc(sizeof(tile));
t->category = EMPTY;
t->obstacle = false;
for(int y = 0; y < m->height; y++) {
m->entities[y] = (entity **) calloc(sizeof(entity*), m->width);
m->floor[y] = (tile **) malloc(sizeof(tile*) * m->width);
for(int x = 0; x < m->width; x++) {
m->floor[y][x] = t;
}
}
#else
map->tiles = (char **) malloc(sizeof(char*) * map->height);
for(int y = 0; y < map->height; y++) {
map->tiles[y] = (char *) malloc(sizeof(char) * map->width);
for(int x = 0; x < map->width; x++) {
map->tiles[y][x] = ' ';
}
}
#endif
}
map * create_empty_map(const char * name, const int width, const int height) {
map * m = internal_init_map_with_name(name);
m->width = width;
m->height = height;
init_map_arrays(m);
return m;
}
#endif
// Entity placement
void place_player(int y, int x) {
map *m = current_map();
m->player_x = x;
m->player_y = y;
#ifdef DIRECTIONS
m->player_direction = UNKNOWN;
#endif
}
#ifndef ENTITY_STRUCT
......@@ -70,7 +129,7 @@ void place_entity(int y, int x, int id, int category, int *stats, char display_s
e->display_symbol = display_symbol;
// Place it on the map
m->entities[coord_idx(y, x)] = e;
m->entities[y][x] = e;
}
entity remove_entity(int y, int x) {
......@@ -108,49 +167,6 @@ direction char_dir(char dir) {
}
}
// Internal function to init a map with a name
map * internal_init_map_with_name(const char * name) {
if (mstack.length >= MAX_MAPS - 1) {
printf("load_map: can't load a new map, the maximum number has been reached.\n");
return current_map();
}
map * m;
mstack.length++;
m = current_map();
/* allocating enough space to hold the map */
m->name = (char *) malloc(sizeof(char) * (strlen(name) + 1));
strcpy(m->name, name);
return m;
}
// Internal function for map array initialization
void init_map_arrays(map * m) {
m->entities = (entity **) calloc(sizeof(entity *), m->width * m->height);
m->floor = (tile **) malloc(sizeof(tile *) * m->width * m->height);
// Allocate an empty floor tile for every tile.
tile *t;
for(int i = 0; i < m->width*m->height; i++) {
t = malloc(sizeof(tile));
t->category = EMPTY;
t->obstacle = false;
m->floor[i] = t;
}
}
map * create_empty_map(const char * name, const int width, const int height) {
map * m = internal_init_map_with_name(name);
m->width = width;
m->height = height;
init_map_arrays(m);
return m;
}
map * load_map(const char * name, const char * data) {
map * m = internal_init_map_with_name(name);;
......@@ -209,8 +225,8 @@ map * load_map(const char * name, const char * data) {
default:
fprintf(stderr, "Unknown map character '%c'!\n",*ch);
}
m->floor[y*m->width + x] = f;
m->entities[y*m->width+x] = i;
m->floor[y][x] = f;
m->entities[y][x] = i;
ch++;
}
assert (*ch == '\n');
......@@ -330,4 +346,4 @@ void show_map(void) {
printf("\n");
}
}
#endif
\ No newline at end of file
#endif
......@@ -13,11 +13,9 @@
#define MAX_MAPS 256
#endif
#endif
#ifdef DIRECTIONS
typedef enum Direction { UP, DOWN, LEFT, RIGHT, UNKNOWN } direction;
#endif
#ifdef MAP_ENTITY_LAYERS
#ifdef ENTITY_STRUCT
typedef struct {
char *name; // shop, overworld, dungeon_Nth_level...
......@@ -37,9 +35,7 @@ typedef struct {
char **tiles;
int player_x;
int player_y;
#ifdef DIRECTIONS
direction player_direction;
#endif
} map;
#endif
#ifdef MAP_STACK
......@@ -50,7 +46,7 @@ typedef struct {
} map_stack;
#endif
#ifndef MAP_ENTITY_LAYERS
#ifndef ENTITY_STRUCT
typedef char entity;
#else
......@@ -62,7 +58,7 @@ typedef struct {
char display_symbol; // The symbol of the Entity on an ASCII map
} entity;
#endif
#ifdef MAP_ENTITY_LAYERS
#ifdef ENTITY_STRUCT
typedef struct {
int category; // identifier to display the tile
......@@ -101,6 +97,9 @@ void free_map_stack(void);
void init_map(int height, int width);
void free_map(map *map);
#endif
#ifdef READ_MAP_STR
#endif
#ifdef MAP_STACK
map *load_map(const char *name, const char *data);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment