diff options
Diffstat (limited to 'Week1-Pacman/src/pacman.c')
-rw-r--r-- | Week1-Pacman/src/pacman.c | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/Week1-Pacman/src/pacman.c b/Week1-Pacman/src/pacman.c index de9774d..e91e773 100644 --- a/Week1-Pacman/src/pacman.c +++ b/Week1-Pacman/src/pacman.c @@ -6,23 +6,41 @@ #define PACMAN_SPEED 261 #define PACMAN_SIDE 40 +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(SDL_Event* e, struct pacman* pacman) { +void handle_pacman_input(SDL_Event* e, struct pacman* pacman, struct map* map) { + SDL_Rect r = { + .x = pacman->xpos, + .y = pacman->ypos, + .w = PACMAN_SIDE, + .h = PACMAN_SIDE + }; if(e->type == SDL_KEYDOWN) { switch(e->key.keysym.sym) { case SDLK_UP: - pacman->facing = FACING_UP; + r.y--; + if(get_colliding_tile_raw(&r, map) == NULL) + pacman->facing = FACING_UP; break; case SDLK_DOWN: - pacman->facing = FACING_DOWN; + r.y++; + + if(get_colliding_tile_raw(&r, map) == NULL) + pacman->facing = FACING_DOWN; break; case SDLK_LEFT: - pacman->facing = FACING_LEFT; + r.x--; + + if(get_colliding_tile_raw(&r, map) == NULL) + pacman->facing = FACING_LEFT; break; case SDLK_RIGHT: - pacman->facing = FACING_RIGHT; + r.x++; + + if(get_colliding_tile_raw(&r, map) == NULL) + pacman->facing = FACING_RIGHT; break; } } @@ -102,8 +120,12 @@ SDL_Rect* get_colliding_tile(struct pacman* pacman, struct map* map) { .h = PACMAN_SIDE }; + return get_colliding_tile_raw(&r, map); +} + +SDL_Rect* get_colliding_tile_raw(SDL_Rect* r, struct map* map) { for(int i = 0; i < map->collider_count; i++) { - if(is_colliding(&r, &map->colliders[i])) { + if(is_colliding(r, &map->colliders[i])) { return &map->colliders[i]; } } |