back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/pac.c
blob: d3aa96cb7edf120262c509370339f2bd926e7e39 (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
#include "pac.h"
#include "ncurses.h"
#include "queue.h"

void initialize_pac(struct pacman *pac)
{
    pac->lives = max_live;
    pac->score = 0;
    pac->position.y = pac_y;
    pac->position.x = pac_x;
    pac->direction = none;
    pac->is_energizer_eaten = 0;
}

static enum movement_direction get_matching_for_directions(game_space field,
                                                           struct pacman *pac,
                                            enum movement_direction direction)
{
    struct free_directions free_paths =
        find_free_directions(field, pac->position.x, pac->position.y);
    switch(direction) {
    case left:
        return free_paths.left ? left : none;
    case right:
        return free_paths.right ? right : none;
    case up:
        return free_paths.up ? up : none;
    case down:
        return free_paths.down ? down : none;
    default:
        return none;
    }

}

void check_remaining_direction(game_space field, struct pacman *pac,
                            enum movement_direction *stored_direction)
{
    enum movement_direction matching_result;
    if(*stored_direction == none)
        pac->direction =
            get_matching_for_directions(field, pac, pac->direction);
    else {
        matching_result =
            get_matching_for_directions(field, pac, *stored_direction);
        if(matching_result != none)
            pac->direction = matching_result;
        else
            pac->direction =
                get_matching_for_directions(field, pac, pac->direction);
        *stored_direction = none;
   }
}

static void check_remaining_direction_after_pressed_key(game_space field,
                                                        struct pacman *pac)
{
    pac->direction =
        get_matching_for_directions(field, pac, pac->direction);
}

void change_pac_direction(game_space field, struct pacman *pac, int key,
                          enum movement_direction *stored_direction)
{
    enum state { no, yes } is_changed_direction;
    is_changed_direction = no;
    struct free_directions path = find_free_directions(field, pac->position.x,
                                                       pac->position.y);
    switch(key) {
    case KEY_LEFT:
        if(path.left) {
            pac->direction = left;
            is_changed_direction = yes;
        } else
            *stored_direction = left;
        break;
    case KEY_RIGHT:
         if(path.right) {
             pac->direction = right;
             is_changed_direction = yes;
         } else
             *stored_direction = right;
         break;
    case KEY_UP:
         if(path.up) {
             pac->direction = up;
             is_changed_direction = yes;
         } else
             *stored_direction = up;
         break;
    case KEY_DOWN:
         if(path.down) {
             pac->direction = down;
             is_changed_direction = yes;
         } else
             *stored_direction = down;
         break;
    }
    if(is_changed_direction == no)
        check_remaining_direction_after_pressed_key(field, pac);
}

static void eat_energizer(game_space field, struct pacman *pac)
{
    if(field_has_energizer(field, pac->position.x, pac->position.y)) {
        pac->is_energizer_eaten = 1;
        clear_energizer(field, pac->position);
    }
}

void make_pac_move(game_space field, struct pacman *pac,
                   struct queue *eaten_coins)
{
    clear_or_revert_symbol(field, pac->position, pac_char, eaten_coins);
    switch(pac->direction) {
    case left:
        --pac->position.x;
        break;
    case right:
        ++pac->position.x;
        break;
    case up:
        --pac->position.y;
        break;
    case down:
        ++pac->position.y;
        break;
    default:
        return;
    }
    if(check_coin_for_pac(field, pac->position, eaten_coins)) {
        queue_push(eaten_coins, &pac->position);
        ++pac->score;
    }
    change_point_if_outside_tunnel(&pac->position);
    eat_energizer(field, pac);
}

void catch_pac(struct pacman *pac)
{
    --pac->lives;
    erase_life(pac->lives);
    pac->direction = none;
}