summaryrefslogtreecommitdiff
path: root/Week1-Pacman/src/pacman.c
blob: de9774d8f31164108e402c1975d6aff4c9c9f857 (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
#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(struct pacman* pacman, struct map* map);

void handle_pacman_input(SDL_Event* e, struct pacman* pacman) {

  if(e->type == SDL_KEYDOWN) {
    switch(e->key.keysym.sym) {
    case SDLK_UP:
      pacman->facing = FACING_UP;
      break;
    case SDLK_DOWN:
      pacman->facing = FACING_DOWN;
      break;
    case SDLK_LEFT:
      pacman->facing = FACING_LEFT;
      break;
    case SDLK_RIGHT:
      pacman->facing = FACING_RIGHT;
      break;
    }
  }
}

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

  for(int i = 0; i < map->collider_count; i++) {
    if(is_colliding(&r, &map->colliders[i])) {
      return &map->colliders[i];
    }
  }

  return NULL;
}