Subversion Repositories Code-Repo

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
241 Kevin 1
#include "defines.h"
2
#include "CONTROLLERS.h"
3
#include "TRON.h"
4
 
5
static TRON_DATA *data_p;
6
 
7
void Tron_Init(TRON_DATA *data) {
8
    data_p = data;
9
 
10
    // Set starting point
11
    data_p->p1_body[0] = (TRON_POINT){0,0,7};
12
    data_p->p2_body[0] = (TRON_POINT){7,7,7};
13
 
14
    data_p->length = 1;
15
    data_p->delay = 800;
16
 
17
    Cube_Clear();
18
    Cube_Overlay_Clear();
19
 
20
    // Draw player 1 light trail
21
    uint32_t index = 0;
22
    Cube_Set_Pixel(data_p->p1_body[index].z, data_p->p1_body[index].x, data_p->p1_body[index].y, TRON_PLAYER_1_HEAD);
23
    for (index = 1; index < data_p->length; index++) {
24
        Cube_Set_Pixel(data_p->p1_body[index].z, data_p->p1_body[index].x, data_p->p1_body[index].y, TRON_PLAYER_1_COLOR);
25
    }
26
 
27
    // Draw player 2 light trail
28
    index = 0;
29
    Cube_Set_Pixel(data_p->p2_body[index].z, data_p->p2_body[index].x, data_p->p2_body[index].y, TRON_PLAYER_2_HEAD);
30
    for (index = 1; index < data_p->length; index++) {
31
        Cube_Set_Pixel(data_p->p2_body[index].z, data_p->p2_body[index].x, data_p->p2_body[index].y, TRON_PLAYER_2_COLOR);
32
    }
33
}
34
 
35
void Tron_Main(void) {
36
    // Main function, loops and delays while updating the frame every x milliseconds
242 Kevin 37
    Tron_Update_Direction(0x04,0x04);
241 Kevin 38
    Delay_MS(2000);
39
    while (1) {
40
        Tron_Update_Frame();
41
        Delay_MS(data_p->delay);
42
    }
43
}
44
 
45
void Tron_Update_Direction(uint8_t p1, uint8_t p2) {
46
    // Determine the next direction for the trails based off the last button press
47
    if (p1) {
48
        TRON_P1_DIRECTION p1_dir;
49
        p1_dir.value = p1;
50
        data_p->p1_last_direction = p1_dir.value;
51
        TRON_POINT p1_point = data_p->p1_body[data_p->length - 1];
52
        if (p1_dir.up) {
53
            p1_point.z = (p1_point.z == CUBE_LAYER_COUNT - 1) ? 0 : p1_point.z + 1;
54
        } else if (p1_dir.down) {
55
            p1_point.z = (p1_point.z == 0) ? CUBE_LAYER_COUNT - 1 : p1_point.z - 1;
56
        } else if (p1_dir.forward) {
57
            p1_point.x = (p1_point.x == CUBE_ROW_COUNT - 1) ? 0 : p1_point.x + 1;
58
        } else if (p1_dir.right) {
59
            p1_point.y = (p1_point.y == CUBE_COLUMN_COUNT - 1) ? 0 : p1_point.y + 1;
60
        } else if (p1_dir.backward) {
61
            p1_point.x = (p1_point.x == 0) ? CUBE_ROW_COUNT - 1 : p1_point.x - 1;
62
        } else if (p1_dir.left) {
63
            p1_point.y = (p1_point.y== 0) ? CUBE_COLUMN_COUNT - 1 : p1_point.y - 1;
64
        }
65
        data_p->p1_direction = p1_point;
66
    }
67
    if (p2) {
68
        TRON_P2_DIRECTION p2_dir;
69
        p2_dir.value = p2;
70
        data_p->p2_last_direction = p2_dir.value;
71
        TRON_POINT p2_point = data_p->p2_body[data_p->length - 1];
72
        if (p2_dir.up) {
73
            p2_point.z = (p2_point.z == CUBE_LAYER_COUNT - 1) ? 0 : p2_point.z + 1;
74
        } else if (p2_dir.down) {
75
            p2_point.z = (p2_point.z == 0) ? CUBE_LAYER_COUNT - 1 : p2_point.z - 1;
76
        } else if (p2_dir.forward) {
77
            p2_point.x = (p2_point.x == CUBE_ROW_COUNT - 1) ? 0 : p2_point.x + 1;
78
        } else if (p2_dir.right) {
79
            p2_point.y = (p2_point.y == CUBE_COLUMN_COUNT - 1) ? 0 : p2_point.y + 1;
80
        } else if (p2_dir.backward) {
81
            p2_point.x = (p2_point.x == 0) ? CUBE_ROW_COUNT - 1 : p2_point.x - 1;
82
        } else if (p2_dir.left) {
83
            p2_point.y = (p2_point.y== 0) ? CUBE_COLUMN_COUNT - 1 : p2_point.y - 1;
84
        }
85
        data_p->p2_direction = p2_point;
86
    }
87
}
88
 
89
void Tron_Update_Frame(void) {
90
    // Check if there is a head-on collision
91
    if (data_p->p1_direction.x == data_p->p2_direction.x &&
92
            data_p->p1_direction.y == data_p->p2_direction.y &&
93
            data_p->p1_direction.z == data_p->p2_direction.z) {
94
        Cube_Set_Pixel(data_p->p1_direction.z, data_p->p1_direction.x, data_p->p1_direction.y, TRON_COLLISION);
95
        Delay_MS(3000);
242 Kevin 96
        Animation_Cube_In_Out(200, TRON_COLLISION);
241 Kevin 97
        Reset_Board(BOARD_MODE_IDLE);
98
    }
99
 
100
    // Check if the location that we are moving to is overlapping either trails
101
    uint32_t index, p1_collision = 0, p2_collision = 0;
102
    for (index = 0; index < data_p->length; index++) {
103
        if (data_p->p1_direction.x == data_p->p1_body[index].x &&
104
                data_p->p1_direction.y == data_p->p1_body[index].y &&
105
                data_p->p1_direction.z == data_p->p1_body[index].z) {
106
            p1_collision = 1;
107
        }
108
        if (data_p->p1_direction.x == data_p->p2_body[index].x &&
109
                data_p->p1_direction.y == data_p->p2_body[index].y &&
110
                data_p->p1_direction.z == data_p->p2_body[index].z) {
111
            p1_collision = 1;
112
        }
113
        if (data_p->p2_direction.x == data_p->p2_body[index].x &&
114
                data_p->p2_direction.y == data_p->p2_body[index].y &&
115
                data_p->p2_direction.z == data_p->p2_body[index].z) {
116
            p2_collision = 1;
117
        }
118
        if (data_p->p2_direction.x == data_p->p1_body[index].x &&
119
                data_p->p2_direction.y == data_p->p1_body[index].y &&
120
                data_p->p2_direction.z == data_p->p1_body[index].z) {
121
            p2_collision = 1;
122
        }
123
    }
124
 
125
    // Save the new head location of each trail
126
    data_p->length++;
127
    data_p->p1_body[data_p->length - 1] = data_p->p1_direction;
128
    data_p->p2_body[data_p->length - 1] = data_p->p2_direction;
129
 
130
    Cube_Clear();
131
    // Draw player 1 light trail
132
    index = 0;
133
    Cube_Set_Pixel(data_p->p1_body[index].z, data_p->p1_body[index].x, data_p->p1_body[index].y, TRON_PLAYER_1_HEAD);
134
    for (index = 1; index < data_p->length; index++) {
135
        Cube_Set_Pixel(data_p->p1_body[index].z, data_p->p1_body[index].x, data_p->p1_body[index].y, TRON_PLAYER_1_COLOR);
136
    }
137
 
138
    // Draw player 2 light trail
139
    index = 0;
140
    Cube_Set_Pixel(data_p->p2_body[index].z, data_p->p2_body[index].x, data_p->p2_body[index].y, TRON_PLAYER_2_HEAD);
141
    for (index = 1; index < data_p->length; index++) {
142
        Cube_Set_Pixel(data_p->p2_body[index].z, data_p->p2_body[index].x, data_p->p2_body[index].y, TRON_PLAYER_2_COLOR);
143
    }
144
 
145
    // Indicate the point of collision and reset to idle mode
146
    if (p1_collision || p2_collision) {
147
        uint32_t ind = data_p->length - 1;
148
        if (p1_collision)
149
            Cube_Set_Pixel(data_p->p1_body[ind].z, data_p->p1_body[ind].x, data_p->p1_body[ind].y, TRON_COLLISION);
150
        if (p2_collision)
151
            Cube_Set_Pixel(data_p->p2_body[ind].z, data_p->p2_body[ind].x, data_p->p2_body[ind].y, TRON_COLLISION);
152
        Delay_MS(3000);
242 Kevin 153
        if (p1_collision && p2_collision)
154
            Animation_Cube_In_Out(200, TRON_COLLISION);
155
        else if (p1_collision)
156
            Animation_Cube_In_Out(200, TRON_PLAYER_2_HEAD);
157
        else if (p2_collision)
158
            Animation_Cube_In_Out(200, TRON_PLAYER_1_HEAD);
241 Kevin 159
        Reset_Board(BOARD_MODE_IDLE);
160
    }
161
 
162
    // Determine the next direction to take
163
    Tron_Update_Direction(data_p->p1_last_direction, data_p->p2_last_direction);
164
 
165
    // Decrease the delay between frame updates by 5ms
166
    data_p->delay -= 5; 
167
}