fix wall collision

This commit is contained in:
Norman Bos
2024-04-10 23:19:08 +02:00
parent 874177fc94
commit 1a6473037a
2 changed files with 72 additions and 68 deletions
+54 -52
View File
@@ -79,52 +79,8 @@ void scroll_actor(Actor *actor)
if (playerIsMoving) if (playerIsMoving)
{ {
// If we are moving horizontally
if (actor->dx != 0)
{
// Check the tiles on our side
uint8_t solid =
world_position_is_solid(actor->next_x + actor->dx * PLAYER_HALF_WIDTH, actor->y - PLAYER_HALF_HEIGHT) ||
world_position_is_solid(actor->next_x + actor->dx * PLAYER_HALF_WIDTH, actor->y) ||
world_position_is_solid(actor->next_x + actor->dx * PLAYER_HALF_WIDTH, actor->y + PLAYER_HALF_HEIGHT);
// printf("%u\n", world_position_is_solid(actor->x + actor->dx * PLAYER_HALF_WIDTH, actor->y - PLAYER_HALF_HEIGHT));
// If none are solid
if (!solid)
// if (solid != 0)
{
// Update the player's x position
// actor->x += x;
actor->x = actor->next_x; actor->x = actor->next_x;
}
}
// If we are moving vertically
if (actor->dy != 0)
{
// Check the tiles above or below us
uint8_t solid =
world_position_is_solid(actor->x + PLAYER_HALF_WIDTH, actor->next_y + actor->dy * PLAYER_HALF_HEIGHT) ||
world_position_is_solid(actor->x, actor->next_y + actor->dy * PLAYER_HALF_HEIGHT) ||
world_position_is_solid(actor->x - PLAYER_HALF_WIDTH, actor->next_y + actor->dy * PLAYER_HALF_HEIGHT);
// If none are solid
if (!solid)
// if (solid != 0)
{
// Update the player's y position
// actor->y += y;
actor->y = actor->next_y; actor->y = actor->next_y;
}
}
// actor->x += x;
// actor->y += y;
move_actor(actor, actor->x, actor->y); move_actor(actor, actor->x, actor->y);
} }
@@ -142,26 +98,50 @@ void move_actor_with_joypad(Actor *actor, uint8_t framecount)
actor->dx = 0; actor->dx = 0;
actor->dy = 0; actor->dy = 0;
actor->can_move_x = TRUE;
actor->can_move_y = TRUE;
if (buttons & J_LEFT) if (buttons & J_LEFT)
{ {
actor->is_facing_right = FALSE; actor->is_facing_right = FALSE;
is_moving_x = TRUE; is_moving_x = TRUE;
if (framecount % 2 == 0) // move every 2nd frame keeps it smooth and without stutter actor->dx = -1;
uint8_t solid =
world_position_is_solid(actor->next_x + actor->dx * PLAYER_HALF_WIDTH, actor->y - PLAYER_HALF_HEIGHT + 1) ||
world_position_is_solid(actor->next_x + actor->dx * PLAYER_HALF_WIDTH, actor->y) ||
world_position_is_solid(actor->next_x + actor->dx * PLAYER_HALF_WIDTH, actor->y + PLAYER_HALF_HEIGHT - 1);
// If none are solid
if (solid)
{
actor->can_move_x = FALSE;
}
if (framecount % 2 == 0 && actor->can_move_x == TRUE) // move every 2nd frame keeps it smooth and without stutter
{ {
actor->next_x -= velocity; actor->next_x -= velocity;
} }
actor->dx = -1;
} }
else if (buttons & J_RIGHT) else if (buttons & J_RIGHT)
{ {
actor->is_facing_right = TRUE; actor->is_facing_right = TRUE;
is_moving_x = TRUE; is_moving_x = TRUE;
if (framecount % 2 == 0) actor->dx = 1;
uint8_t solid =
world_position_is_solid(actor->next_x + actor->dx * PLAYER_HALF_WIDTH, actor->y - PLAYER_HALF_HEIGHT + 1) ||
world_position_is_solid(actor->next_x + actor->dx * PLAYER_HALF_WIDTH, actor->y) ||
world_position_is_solid(actor->next_x + actor->dx * PLAYER_HALF_WIDTH, actor->y + PLAYER_HALF_HEIGHT - 1);
// If none are solid
if (solid)
{
actor->can_move_x = FALSE;
}
if (framecount % 2 == 0 && actor->can_move_x == TRUE)
{ {
actor->next_x += velocity; actor->next_x += velocity;
} }
actor->dx = 1;
} }
else else
{ {
@@ -171,20 +151,42 @@ void move_actor_with_joypad(Actor *actor, uint8_t framecount)
if (buttons & J_UP) if (buttons & J_UP)
{ {
is_moving_y = TRUE; is_moving_y = TRUE;
if (framecount % 2 == 0) actor->dy = -1;
uint8_t solid =
world_position_is_solid(actor->x + PLAYER_HALF_WIDTH - 1, actor->next_y + actor->dy * PLAYER_HALF_HEIGHT) ||
world_position_is_solid(actor->x, actor->next_y + actor->dy * PLAYER_HALF_HEIGHT) ||
world_position_is_solid(actor->x - PLAYER_HALF_WIDTH + 1, actor->next_y + actor->dy * PLAYER_HALF_HEIGHT);
// If none are solid
if (solid)
{
actor->can_move_y = FALSE;
}
if (framecount % 2 == 0 && actor->can_move_y == TRUE)
{ {
actor->next_y -= velocity; actor->next_y -= velocity;
} }
actor->dy = -1;
} }
else if (buttons & J_DOWN) else if (buttons & J_DOWN)
{ {
is_moving_y = TRUE; is_moving_y = TRUE;
if (framecount % 2 == 0) actor->dy = 1;
uint8_t solid =
world_position_is_solid(actor->x + PLAYER_HALF_WIDTH - 1, actor->next_y + actor->dy * PLAYER_HALF_HEIGHT) ||
world_position_is_solid(actor->x, actor->next_y + actor->dy * PLAYER_HALF_HEIGHT) ||
world_position_is_solid(actor->x - PLAYER_HALF_WIDTH + 1, actor->next_y + actor->dy * PLAYER_HALF_HEIGHT);
// If none are solid
if (solid)
{
actor->can_move_y = FALSE;
}
if (framecount % 2 == 0 && actor->can_move_y == TRUE)
{ {
actor->next_y += velocity; actor->next_y += velocity;
} }
actor->dy = 1;
} }
else else
{ {
+2
View File
@@ -24,6 +24,8 @@ typedef struct Actor
int8_t dy; // The sprite y moving direction int8_t dy; // The sprite y moving direction
uint8_t velocity_x; // The current speed of travel in the X direction uint8_t velocity_x; // The current speed of travel in the X direction
uint8_t velocity_y; // The current speed of travel in the y direction uint8_t velocity_y; // The current speed of travel in the y direction
bool can_move_x;
bool can_move_y;
bool is_facing_right; // The direction the player sprite is facing bool is_facing_right; // The direction the player sprite is facing
bool is_moving; // Is the actor moving or not bool is_moving; // Is the actor moving or not
const unsigned char *tilemap; // A pointer to the tilemap (may be obsolete with metasprite) const unsigned char *tilemap; // A pointer to the tilemap (may be obsolete with metasprite)