Collisions are not sticky anymore and the player can't go trough the blocks but he is shaking when you collide with a wall and is not stopped by the borders of the map.

This commit is contained in:
mibi88 2023-07-09 13:22:20 +02:00
parent 23dde6593c
commit 909e0760c5
2 changed files with 32 additions and 12 deletions

View file

@ -44,11 +44,15 @@ void player_move(Map *map_level, Player *player, Direction direction) {
const char dx = one_px_mov[direction*2]*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)){
/* I fix his position so he won't be partially in the tile. */
player_fix_position(player, 1, 1);
if(player_collision(map_level, player, direction, P_CENTER)){
player_fix_position(player, dx, dy);
}else{
/* If he won't collide I just move him normally */
if(player_collision(map_level, player, direction, P_RIGHTDOWN) ||
player_collision(map_level, player, direction, P_LEFTUP)){
/* I fix his position so he won't be partially in the tile. */
player_fix_position(player, dx==0, dy==0);
}
player->x += dx;
player->y += dy;
}
@ -58,19 +62,27 @@ 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,
Checkpos nomov_axis_check) {
/* What's the tile the player is going to. */
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 dy = one_px_mov[direction*2+1];
char dx = one_px_mov[direction*2];
char dy = one_px_mov[direction*2+1];
if(!dx){
dx += nomov_axis_check;
dx = dx*(P_WIDTH/2+(nomov_axis_check == P_CENTER));
}else if(!dy){
dy += nomov_axis_check;
dy = dy*(P_HEIGHT/2+(nomov_axis_check == P_CENTER));
}
/* The tile he will go to. */
int player_tile_x = player->x/T_WIDTH;
int player_tile_y = player->y/T_HEIGHT;
int player_tile_x = (player->x+dx)/T_WIDTH;
int player_tile_y = (player->y+dy)/T_HEIGHT;
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,
get_tile(map_level, player_tile_x+dx, player_tile_y+dy, i))){
get_tile(map_level, player_tile_x, player_tile_y, i))){
return true; /* He will collide with it. */
}
}

View file

@ -14,9 +14,15 @@ typedef enum {
D_RIGHT
} Direction;
typedef enum {
P_LEFTUP = -1,
P_CENTER = 0,
P_RIGHTDOWN = 1
} Checkpos;
/* Struct that define player parameters */
typedef struct {
unsigned short int x, y; /* The position of the player */
int x, y; /* The position of the player */
unsigned char px, py; /* The position of the player on screen */
unsigned short int life; /* How many lives the player still has between 0
* and 100. */
@ -32,8 +38,10 @@ void player_move(Map *map_level, Player *player, Direction direction);
/* (Mibi88) TODO: Describe this function please, I've no idea what she's for! */
void player_action(Player *player);
/* Check if the player is in collision with the map or a NPC. */
bool player_collision(Map *map_level, Player *player, Direction direction);
/* Check if the player is in collision with the map or a NPC. Checkpos is used
* to check the axis where the player is not moving. */
bool player_collision(Map *map_level, Player *player, Direction direction,
Checkpos nomov_axis_check);
/* Fix the position of the player so that he's not a bit inside of a hard block
* after a collision. */