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

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

static enum movement_direction get_correct_path(struct pacman *pac,
                                                struct free_directions 
                                                                    free_path,
                                                enum movement_direction 
                                                                    direction)
{
    switch(direction) {
        case left:
            return free_path.left ? left : none;
        case right:
            return free_path.right ? right : none;
        case up:
            return free_path.up ? up : none;
        case down:
            return free_path.down ? down : none;
        default:
            return none;
    }

}

void check_remaining_direction(game_space field, struct pacman *pac, 
                               enum movement_direction *stored_direction)
{
    enum movement_direction temp_direction;
    struct free_directions free_path = 
        find_free_directions(field, pac->position.y, pac->position.x);
    enum movement_direction current_direction = 
        stored_direction ? *stored_direction : pac->direction;
    temp_direction = get_correct_path(pac, free_path, current_direction);
    if(temp_direction != none)
        pac->direction = temp_direction;
    else {
        current_direction = pac->direction;
        pac->direction = get_correct_path(pac, free_path, current_direction);
    }
    if(stored_direction)
        *stored_direction = none;
}

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.y,
                                                       pac->position.x);
    switch(key) {
        case KEY_LEFT:
            if(path.left) {
                pac->direction = left;
                is_changed_direction = yes;
            } else
                *stored_direction = left;
            break;
        case KEY_RIGHT:
             if(key == KEY_RIGHT && path.right) {
                 pac->direction = right;
                 is_changed_direction = yes;
             } else
                 *stored_direction = right;
             break;
        case KEY_UP:
             if(key == KEY_UP && path.up) {
                 pac->direction = up;
                 is_changed_direction = yes;
             } else
                 *stored_direction = up;
             break;
        case KEY_DOWN:
             if(key == KEY_DOWN && path.down) {
                 pac->direction = down;
                 is_changed_direction = yes;
             } else
                 *stored_direction = down;
             break;
    }
    if(is_changed_direction == no)
        check_remaining_direction(field, pac, NULL);
}

void make_pac_move(game_space field, struct pacman *pac)
{
    clear_symbol(field, pac->position.y, pac->position.x, pac_char);
    switch(pac->direction) {
        case left:
            --pac->position.x;
            return;
        case right:
            ++pac->position.x;
            return;
        case up:
            --pac->position.y;
            return;
        case down:
            ++pac->position.y;
            return;
        default:
            return;
    }
}