summaryrefslogtreecommitdiff
path: root/Week1-Pacman/src/pacman.c
blob: 5a46a6db71d042817f2b4f25267eed110b9534c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <SDL2/SDL.h>
#include <stdbool.h>
#include "pacman.h"
#include "collision.h"

#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(const char* keyboard, struct pacman* pacman, struct map* map) {
  SDL_Rect r = {
    .x = pacman->xpos,
    .y = pacman->ypos,
    .w = PACMAN_SIDE,
    .h = PACMAN_SIDE
  };

  if(keyboard[SDL_SCANCODE_UP]) {
    r.y--;

    if(get_colliding_tile_raw(&r, map) == NULL)
      pacman->facing = FACING_UP;
  } else if(keyboard[SDL_SCANCODE_DOWN]) {
    r.y++;

    if(get_colliding_tile_raw(&r, map) == NULL)
      pacman->facing = FACING_DOWN;
  } else if(keyboard[SDL_SCANCODE_LEFT]) {
    r.x--;

    if(get_colliding_tile_raw(&r, map) == NULL)
      pacman->facing = FACING_LEFT;
  } else if(keyboard[SDL_SCANCODE_RIGHT]) {
    r.x++;

    if(get_colliding_tile_raw(&r, map) == NULL)
      pacman->facing = FACING_RIGHT;
  }
}

void update_pacman(struct pacman* pacman, float dt, struct map* map) {
  if(pacman->facing == FACING_UP) {
    pacman->ypos -= PACMAN_SPEED * dt;

    SDL_Rect* colliding_tile =
      get_colliding_tile(pacman, map);

    if(colliding_tile != NULL) {
      pacman->ypos =
        colliding_tile->y + colliding_tile->h;
    } else {
      update_animation(&pacman->animations[pacman->facing], dt);
    }
  } else if(pacman->facing == FACING_DOWN) {
    pacman->ypos += PACMAN_SPEED * dt;

    SDL_Rect* colliding_tile =
      get_colliding_tile(pacman, map);

    if(colliding_tile != NULL) {
      pacman->ypos =
        colliding_tile->y - PACMAN_SIDE;
    } else {
      update_animation(&pacman->animations[pacman->facing], dt);
    }
  } else if(pacman->facing == FACING_LEFT) {
    pacman->xpos -= PACMAN_SPEED * dt;

    SDL_Rect* colliding_tile =
      get_colliding_tile(pacman, map);

    if(colliding_tile != NULL) {
      pacman->xpos =
        colliding_tile->x + colliding_tile->w;
    } else {
      update_animation(&pacman->animations[pacman->facing], dt);
    }
  } else if(pacman->facing == FACING_RIGHT) {
    pacman->xpos += PACMAN_SPEED * dt;

    SDL_Rect* colliding_tile =
      get_colliding_tile(pacman, map);

    if(colliding_tile != NULL) {
      pacman->xpos =
        colliding_tile->x - PACMAN_SIDE;
    } else {
      update_animation(&pacman->animations[pacman->facing], dt);
    }
  }
}

void draw_pacman(struct demo* demo, const struct pacman* pacman) {
  const struct animation* current_animation =
    &pacman->animations[pacman->facing];

  SDL_Rect d_rect = {
    .x = pacman->xpos,
    .y = pacman->ypos,
    .w = PACMAN_SIDE,
    .h = PACMAN_SIDE
  };

  draw_animation(demo, current_animation, &d_rect);
}

SDL_Rect* get_colliding_tile(struct pacman* pacman, struct map* map) {
  SDL_Rect r = (SDL_Rect){
    .x = pacman->xpos,
    .y = pacman->ypos,
    .w = PACMAN_SIDE,
    .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])) {
      return &map->colliders[i];
    }
  }

  return NULL;
}