summaryrefslogtreecommitdiff
path: root/Week1-Pacman/src/pacman.c
blob: 6b189aed952f4a7af294ffc90d196a0b55cf0f53 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <SDL2/SDL.h>
#include <stdbool.h>
#include "pacman.h"
#include "collision.h"

#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 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,
    .w = PACMAN_SIDE,
    .h = PACMAN_SIDE
  };

  if(keyboard[SDL_SCANCODE_UP] && pacman->facing != FACING_UP) {
    r.y--;

    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++;

    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--;

    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++;

    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;

    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;
}

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;
  }
}