Added some comments.

This commit is contained in:
mibi88 2023-07-08 16:57:04 +02:00
parent 8d316d0d0d
commit 94b2187ad4
4 changed files with 26 additions and 5 deletions

View file

@ -7,6 +7,7 @@ void game_logic(Game *game) {
} }
void draw(Game *game) { void draw(Game *game) {
/* Draw everything. */
render_map(&game->player, game->map_level); render_map(&game->player, game->map_level);
player_draw(&game->player); player_draw(&game->player);
} }

View file

@ -31,10 +31,18 @@ Game game = {
}; };
/* some global variables */ /* some global variables */
bool exittoOS = false; // set to true when asked for exit
bool screenshot = false; // set to true when screenshot is required /* Set to true when asked for exit */
bool record = false; // set to true when bool exittoOS = false;
/* Set to true when screenshot is required */
bool screenshot = false;
/* Set to true when recording a video of the screen is required */
bool record = false;
/* How many ms the frame already took. */
long int frame_duration;
/* Key management */ /* Key management */
@ -162,3 +170,4 @@ int main(void) {
return 1; return 1;
} }

View file

@ -97,6 +97,8 @@ void render_map(Player *player, Map *map_level) {
} */ } */
short int get_tile(Map *map_level, int x, int y, int l) { short int get_tile(Map *map_level, int x, int y, int l) {
/* Get the tile at (x, y) on layer l. Returns the tile ID or MAP_OUTSIDE if
* she's not found. */
return x>=0 && x < map_level->w && y>=0 && y < map_level->h ? return x>=0 && x < map_level->w && y>=0 && y < map_level->h ?
map_level->layers[l][y * map_level->w + x] : MAP_OUTSIDE; map_level->layers[l][y * map_level->w + x] : MAP_OUTSIDE;
} }

View file

@ -26,6 +26,7 @@ const char one_px_mov[8] = {
/* TODO: Search for all hard tiles in the tileset. hard_tiles is a list of their /* TODO: Search for all hard tiles in the tileset. hard_tiles is a list of their
* IDs */ * IDs */
/* The tiles where the player can't go trough. */
#define HARD_TILES_AMOUNT 5 #define HARD_TILES_AMOUNT 5
const short int hard_tiles[HARD_TILES_AMOUNT] = { const short int hard_tiles[HARD_TILES_AMOUNT] = {
MAP_OUTSIDE, 124, 148, 125, 149 MAP_OUTSIDE, 124, 148, 125, 149
@ -41,9 +42,12 @@ void player_move(Map *map_level, Player *player, Direction direction) {
/* How this player movement will modify the player x and y. */ /* How this player movement will modify the player x and y. */
const char dx = one_px_mov[direction*2]*SPEED; const char dx = one_px_mov[direction*2]*SPEED;
const char dy = one_px_mov[direction*2+1]*SPEED; const char dy = one_px_mov[direction*2+1]*SPEED;
/* If the player will collide with a hard tile. */
if(player_collision(map_level, player, direction)){ if(player_collision(map_level, player, direction)){
/* I fix his position so he won't be partially in the tile. */
player_fix_position(player, dx, dy); player_fix_position(player, dx, dy);
}else{ }else{
/* If he won't collide I just move him normally */
player->x += dx; player->x += dx;
player->y += dy; player->y += dy;
} }
@ -56,20 +60,25 @@ void player_action(Player *player) {
bool player_collision(Map *map_level, Player *player, Direction direction) { bool player_collision(Map *map_level, Player *player, Direction direction) {
/* What's the tile the player is going to. */ /* What's the tile the player is going to. */
short int i; short int i;
/* Where is the tile where he will go to from his position. */
const char dx = one_px_mov[direction*2]; const char dx = one_px_mov[direction*2];
const char dy = one_px_mov[direction*2+1]; const char dy = one_px_mov[direction*2+1];
/* The tile he will go to. */
int player_tile_x = player->x/T_WIDTH; int player_tile_x = player->x/T_WIDTH;
int player_tile_y = player->y/T_HEIGHT; int player_tile_y = player->y/T_HEIGHT;
for(i=0;i<map_level->nblayers;i++){ for(i=0;i<map_level->nblayers;i++){
/* if he's on a hard tile */
if(is_in((short int*)hard_tiles, HARD_TILES_AMOUNT, if(is_in((short int*)hard_tiles, HARD_TILES_AMOUNT,
get_tile(map_level, player_tile_x+dx, player_tile_y+dy, i))){ get_tile(map_level, player_tile_x+dx, player_tile_y+dy, i))){
return true; return true; /* He will collide with it. */
} }
} }
return false; return false; /* He won't collide with a hard tile. */
} }
void player_fix_position(Player *player, bool fix_x, bool fix_y) { void player_fix_position(Player *player, bool fix_x, bool fix_y) {
/* I fix his poition on x or/and on y if y need to, so that he won't be over
* the hard tile that he collided with. */
if(fix_x) player->x = player->x/T_WIDTH*T_WIDTH+P_WIDTH/2; if(fix_x) player->x = player->x/T_WIDTH*T_WIDTH+P_WIDTH/2;
if(fix_y) player->y = player->y/T_HEIGHT*T_HEIGHT+P_HEIGHT/2; if(fix_y) player->y = player->y/T_HEIGHT*T_HEIGHT+P_HEIGHT/2;
} }