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
+56 -54
View File
@@ -79,52 +79,8 @@ void scroll_actor(Actor *actor)
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;
}
}
// 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->x += x;
// actor->y += y;
actor->x = actor->next_x;
actor->y = actor->next_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->dy = 0;
actor->can_move_x = TRUE;
actor->can_move_y = TRUE;
if (buttons & J_LEFT)
{
actor->is_facing_right = FALSE;
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->dx = -1;
}
else if (buttons & J_RIGHT)
{
actor->is_facing_right = 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->dx = 1;
}
else
{
@@ -171,20 +151,42 @@ void move_actor_with_joypad(Actor *actor, uint8_t framecount)
if (buttons & J_UP)
{
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->dy = -1;
}
else if (buttons & J_DOWN)
{
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->dy = 1;
}
else
{
+16 -14
View File
@@ -10,20 +10,22 @@
// A structure of the actor
typedef struct Actor
{
uint8_t sprite_id; // The starting ID of the sprite for this actor. Remaining sprites should follow in sequency
uint8_t sprite_tile_width; // The number of tiles wide this sprite is
uint8_t sprite_tile_height; // The number tiles high this sprite is
uint8_t sprite_frame; // The total number of animated sprites
uint8_t sprite_current_frame; // The current frame the sprite is on
uint8_t tilemap_start; // The start index where the tiles start for this sprite
uint8_t x; // The sprite x position (top left)
uint8_t y; // The sprite y position (top left)
uint8_t next_x; // The future sprite x position (top left)
uint8_t next_y; // The future sprite y position (top left)
int8_t dx; // The sprite x 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_y; // The current speed of travel in the y direction
uint8_t sprite_id; // The starting ID of the sprite for this actor. Remaining sprites should follow in sequency
uint8_t sprite_tile_width; // The number of tiles wide this sprite is
uint8_t sprite_tile_height; // The number tiles high this sprite is
uint8_t sprite_frame; // The total number of animated sprites
uint8_t sprite_current_frame; // The current frame the sprite is on
uint8_t tilemap_start; // The start index where the tiles start for this sprite
uint8_t x; // The sprite x position (top left)
uint8_t y; // The sprite y position (top left)
uint8_t next_x; // The future sprite x position (top left)
uint8_t next_y; // The future sprite y position (top left)
int8_t dx; // The sprite x 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_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_moving; // Is the actor moving or not
const unsigned char *tilemap; // A pointer to the tilemap (may be obsolete with metasprite)