From 1d1554652888fa4f0cd12a51fda8d9bd54dbcee6 Mon Sep 17 00:00:00 2001 From: BoredGuy Date: Thu, 2 Jan 2025 10:53:09 +0300 Subject: Movement Finished - Smooth edge turning means that movement is now convincingly (pacmanny) --- Week1-Pacman/src/pacman.c | 82 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 69 insertions(+), 13 deletions(-) (limited to 'Week1-Pacman/src/pacman.c') diff --git a/Week1-Pacman/src/pacman.c b/Week1-Pacman/src/pacman.c index 5a46a6d..6b189ae 100644 --- a/Week1-Pacman/src/pacman.c +++ b/Week1-Pacman/src/pacman.c @@ -5,11 +5,16 @@ #define PACMAN_SPEED 261 #define PACMAN_SIDE 40 +#define EDGE_TURN_LIMIT 5 + +extern const Uint8* keyboard; SDL_Rect* get_colliding_tile_raw(SDL_Rect*, struct map* map); SDL_Rect* get_colliding_tile(struct pacman* pacman, struct map* map); -void handle_pacman_input(const char* keyboard, struct pacman* pacman, struct map* map) { +void rect_step_one_pixel(SDL_Rect* r, enum facing); + +void handle_pacman_input(struct pacman* pacman, struct map* map) { SDL_Rect r = { .x = pacman->xpos, .y = pacman->ypos, @@ -17,30 +22,64 @@ void handle_pacman_input(const char* keyboard, struct pacman* pacman, struct map .h = PACMAN_SIDE }; - if(keyboard[SDL_SCANCODE_UP]) { + if(keyboard[SDL_SCANCODE_UP] && pacman->facing != FACING_UP) { r.y--; - if(get_colliding_tile_raw(&r, map) == NULL) - pacman->facing = FACING_UP; - } else if(keyboard[SDL_SCANCODE_DOWN]) { + for(int i = 0; i < EDGE_TURN_LIMIT + 1; i++) { + if(get_colliding_tile_raw(&r, map) == NULL) { + pacman->xpos = r.x; + pacman->facing = FACING_UP; + + break; + } + + rect_step_one_pixel(&r, pacman->facing); + } + } else if(keyboard[SDL_SCANCODE_DOWN] && pacman->facing != FACING_DOWN) { r.y++; - if(get_colliding_tile_raw(&r, map) == NULL) - pacman->facing = FACING_DOWN; - } else if(keyboard[SDL_SCANCODE_LEFT]) { + for(int i = 0; i < EDGE_TURN_LIMIT + 1; i++) { + if(get_colliding_tile_raw(&r, map) == NULL) { + pacman->xpos = r.x; + pacman->facing = FACING_DOWN; + + break; + } + + rect_step_one_pixel(&r, pacman->facing); + } + } else if(keyboard[SDL_SCANCODE_LEFT] && pacman->facing != FACING_LEFT) { r.x--; - if(get_colliding_tile_raw(&r, map) == NULL) - pacman->facing = FACING_LEFT; - } else if(keyboard[SDL_SCANCODE_RIGHT]) { + for(int i = 0; i < EDGE_TURN_LIMIT + 1; i++) { + if(get_colliding_tile_raw(&r, map) == NULL) { + pacman->ypos = r.y; + pacman->facing = FACING_LEFT; + + break; + } + + rect_step_one_pixel(&r, pacman->facing); + } + } else if(keyboard[SDL_SCANCODE_RIGHT] && pacman->facing != FACING_RIGHT) { r.x++; - if(get_colliding_tile_raw(&r, map) == NULL) - pacman->facing = FACING_RIGHT; + for(int i = 0; i < EDGE_TURN_LIMIT + 1; i++) { + if(get_colliding_tile_raw(&r, map) == NULL) { + pacman->ypos = r.y; + pacman->facing = FACING_RIGHT; + + break; + } + + rect_step_one_pixel(&r, pacman->facing); + } } } void update_pacman(struct pacman* pacman, float dt, struct map* map) { + handle_pacman_input(pacman, map); + if(pacman->facing == FACING_UP) { pacman->ypos -= PACMAN_SPEED * dt; @@ -126,3 +165,20 @@ SDL_Rect* get_colliding_tile_raw(SDL_Rect* r, struct map* map) { return NULL; } + +void rect_step_one_pixel(SDL_Rect* r, enum facing facing) { + switch(facing) { + case FACING_UP: + r->y--; + break; + case FACING_DOWN: + r->y++; + break; + case FACING_LEFT: + r->x--; + break; + case FACING_RIGHT: + r->x++; + break; + } +} -- cgit v1.2.3