diff options
author | BoredGuy <osome3717@gmail.com> | 2024-12-31 09:57:35 +0300 |
---|---|---|
committer | BoredGuy <osome3717@gmail.com> | 2024-12-31 09:57:35 +0300 |
commit | df5888dde7755175f4ee300a87c2e983c5b384c4 (patch) | |
tree | 10a8910640ab684b1f1e024f545b562042055ede | |
parent | 5f347311d117a8f453779c37c67fb2ec387c084b (diff) |
Pacman Movement Complete
- The movement system doesn't let you turn into walls now
- Minor maze improvements(more to come)
-rw-r--r-- | Week1-Pacman/assets/Maps/maze.csv | 4 | ||||
-rw-r--r-- | Week1-Pacman/src/main.c | 2 | ||||
-rw-r--r-- | Week1-Pacman/src/pacman.c | 34 | ||||
-rw-r--r-- | Week1-Pacman/src/pacman.h | 2 |
4 files changed, 32 insertions, 10 deletions
diff --git a/Week1-Pacman/assets/Maps/maze.csv b/Week1-Pacman/assets/Maps/maze.csv index 3cb43bb..551a42f 100644 --- a/Week1-Pacman/assets/Maps/maze.csv +++ b/Week1-Pacman/assets/Maps/maze.csv @@ -8,8 +8,8 @@ 319,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,322 319,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,322 319,-1,-1,-1,-1,-1,-1,-1,192,194,-1,-1,-1,-1,-1,197,199,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,322 -319,-1,-1,-1,-1,-1,-1,-1,228,265,-1,-1,-1,-1,-1,-1,271,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,322 -319,-1,-1,-1,-1,-1,-1,-1,228,265,-1,-1,-1,-1,-1,-1,271,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,322 +319,-1,-1,-1,-1,-1,-1,-1,271,-1,-1,-1,-1,-1,-1,-1,271,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,322 +319,-1,-1,-1,-1,-1,-1,-1,271,-1,-1,-1,-1,-1,-1,-1,271,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,322 319,-1,-1,-1,-1,-1,-1,-1,336,337,337,337,337,337,337,337,343,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,322 319,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,322 319,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,322 diff --git a/Week1-Pacman/src/main.c b/Week1-Pacman/src/main.c index 51641de..0fe38bf 100644 --- a/Week1-Pacman/src/main.c +++ b/Week1-Pacman/src/main.c @@ -77,7 +77,7 @@ int main(int argc, char** argv) { running = false; } - handle_pacman_input(&e, &pacman); + handle_pacman_input(&e, &pacman, &map); } int now = SDL_GetTicks(); 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]; } } diff --git a/Week1-Pacman/src/pacman.h b/Week1-Pacman/src/pacman.h index 3bb0681..c800910 100644 --- a/Week1-Pacman/src/pacman.h +++ b/Week1-Pacman/src/pacman.h @@ -26,7 +26,7 @@ struct pacman { struct animation animations[4]; }; -void handle_pacman_input(SDL_Event* e, struct pacman* pacman); +void handle_pacman_input(SDL_Event* e, struct pacman* pacman, struct map* map); void update_pacman(struct pacman* pacman, float dt, struct map* map); void draw_pacman(struct demo* demo, const struct pacman* pacman); |