Subversion Repositories Code-Repo

Rev

Rev 276 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 276 Rev 278
Line 1... Line 1...
1
#include "defines.h"
1
#include "defines.h"
2
#include "CONTROLLERS.h"
2
#include "CONTROLLERS.h"
3
#include "TRON.h"
3
#include "TRON.h"
-
 
4
#include "TIMER4.h"
4
 
5
 
5
static TRON_DATA *data_p;
6
static TRON_DATA *tron_data_p;
6
 
7
 
7
void Tron_Init(TRON_DATA *data) {
8
void Tron_Init(TRON_DATA *data) {
8
    data_p = data;
9
    tron_data_p = data;
9
 
10
 
-
 
11
    // Set players 1-4 colors
-
 
12
    tron_data_p->Color_Head_R[0] = 0xFF;
-
 
13
    tron_data_p->Color_Head_G[0] = 0x00;
-
 
14
    tron_data_p->Color_Head_B[0] = 0x00;
-
 
15
    tron_data_p->Color_Body_R[0] = 0x20;
-
 
16
    tron_data_p->Color_Body_G[0] = 0x00;
-
 
17
    tron_data_p->Color_Body_B[0] = 0x00;
-
 
18
 
-
 
19
    tron_data_p->Color_Head_R[1] = 0x00;
-
 
20
    tron_data_p->Color_Head_G[1] = 0x00;
-
 
21
    tron_data_p->Color_Head_B[1] = 0xFF;
-
 
22
    tron_data_p->Color_Body_R[1] = 0x00;
-
 
23
    tron_data_p->Color_Body_G[1] = 0x00;
-
 
24
    tron_data_p->Color_Body_B[1] = 0x20;
-
 
25
 
-
 
26
    tron_data_p->Color_Head_R[2] = 0x00;
-
 
27
    tron_data_p->Color_Head_G[2] = 0xFF;
-
 
28
    tron_data_p->Color_Head_B[2] = 0x00;
-
 
29
    tron_data_p->Color_Body_R[2] = 0x00;
-
 
30
    tron_data_p->Color_Body_G[2] = 0x20;
-
 
31
    tron_data_p->Color_Body_B[2] = 0x00;
-
 
32
 
-
 
33
    tron_data_p->Color_Head_R[3] = 0xFF;
-
 
34
    tron_data_p->Color_Head_G[3] = 0x60;
-
 
35
    tron_data_p->Color_Head_B[3] = 0x00;
-
 
36
    tron_data_p->Color_Body_R[3] = 0x20;
-
 
37
    tron_data_p->Color_Body_G[3] = 0x10;
-
 
38
    tron_data_p->Color_Body_B[3] = 0x00;
-
 
39
 
10
    // Set starting point
40
//    // Set starting points for players
11
    data_p->p1_body[0] = (TRON_POINT){0,0,7};
41
    tron_data_p->body[0][0] = (TRON_POINT){0,0,7};
12
    data_p->p2_body[0] = (TRON_POINT){7,7,7};
42
    tron_data_p->body[1][0] = (TRON_POINT){7,7,7};
-
 
43
    tron_data_p->body[2][0] = (TRON_POINT){0,7,7};
-
 
44
    tron_data_p->body[3][0] = (TRON_POINT){7,0,7};
13
 
45
 
14
    data_p->length = 1;
-
 
15
    data_p->delay = 800;
46
    tron_data_p->delay = TRON_DELAY;
16
 
47
 
17
    Cube_Clear();
48
    Cube_Clear();
18
    Cube_Overlay_Clear();
49
    Cube_Overlay_Clear();
19
 
50
 
-
 
51
    // Determine the number of players (connected controllers)
-
 
52
    tron_data_p->players = Controller_Get_Connected();
20
    // Draw player 1 light trail
53
    while (tron_data_p->players < 2) {
21
    uint32_t index;
54
        Delay_MS(100);
22
    for (index = 0; index < data_p->length - 1; index++) {
55
        Controller_Poll_Connected();
23
        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);
56
        tron_data_p->players = Controller_Get_Connected();
24
    }
57
    }
-
 
58
    if (tron_data_p->players > TRON_MAX_PLAYERS)
-
 
59
        tron_data_p->players = TRON_MAX_PLAYERS;
25
    Cube_Set_Pixel(data_p->p1_body[data_p->length-1].z, data_p->p1_body[data_p->length-1].x, data_p->p1_body[data_p->length-1].y, TRON_PLAYER_1_HEAD);
60
    tron_data_p->players_alive = tron_data_p->players;
26
 
61
 
27
    // Draw player 2 light trail
62
    // Draw each player's light trail
-
 
63
    uint8_t i;
28
    for (index = 0; index < data_p->length - 1; index++) {
64
    for (i = 0; i < tron_data_p->players; i++) {
-
 
65
        tron_data_p->length[i] = 1;
-
 
66
        tron_data_p->dead[i] = 0;
-
 
67
        // Draw the head
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_COLOR);
68
        Cube_Set_Pixel(tron_data_p->body[i][tron_data_p->length[i] - 1].z,
-
 
69
                tron_data_p->body[i][tron_data_p->length[i] - 1].x,
-
 
70
                tron_data_p->body[i][tron_data_p->length[i] - 1].y,
-
 
71
                tron_data_p->Color_Head_R[i], tron_data_p->Color_Head_G[i],
-
 
72
                tron_data_p->Color_Head_B[i]);
30
    }
73
    }
-
 
74
 
-
 
75
    // Set starting direction for players
-
 
76
    Tron_Update_Direction(0, (CTRL_BTN_STATUS) (uint8_t) 0x08);
-
 
77
    Tron_Update_Direction(1, (CTRL_BTN_STATUS) (uint8_t) 0x10);
-
 
78
    Tron_Update_Direction(2, (CTRL_BTN_STATUS) (uint8_t) 0x04);
31
    Cube_Set_Pixel(data_p->p2_body[data_p->length-1].z, data_p->p2_body[data_p->length-1].x, data_p->p2_body[data_p->length-1].y, TRON_PLAYER_2_HEAD);
79
    Tron_Update_Direction(3, (CTRL_BTN_STATUS) (uint8_t) 0x20);
32
}
80
}
33
 
81
 
34
void Tron_Main(void) {
82
void Tron_Main(void) {
35
    // Main function, loops and delays while updating the frame every x milliseconds
83
    // Main function, loops and delays while updating the frame every x milliseconds
-
 
84
    uint8_t i;
-
 
85
    for (i = 0; i < tron_data_p->players; i++) {
-
 
86
        Controller_Set_Active(i);
-
 
87
    }
-
 
88
    Delay_MS(20);
-
 
89
    // Light up the player indicators
-
 
90
    switch (tron_data_p->players) {
-
 
91
        case 4:
-
 
92
            Controller_Set_Left_Leds(3, 0x02);
-
 
93
        case 3:
36
    Tron_Update_Direction(0x04,0x04);
94
            Controller_Set_Left_Leds(2, 0x04);
-
 
95
        case 2:
-
 
96
        default:
-
 
97
            Controller_Set_Left_Leds(1, 0x08);
37
//    Controller_Set_Leds(0x01,0x01);
98
            Controller_Set_Left_Leds(0, 0x01);
-
 
99
            break;
-
 
100
    }
-
 
101
    TIMER4_Start();
38
    Delay_MS(2000);
102
    Delay_MS(3000);
39
    while (1) {
103
    while (1) {
40
        Tron_Update_Frame();
104
        Tron_Update_Frame();
41
        Delay_MS(data_p->delay);
105
        Delay_MS(tron_data_p->delay);
42
    }
106
    }
43
}
107
}
44
 
108
 
45
void Tron_Update_Direction(uint8_t p1, uint8_t p2) {
109
void Tron_Update_Direction(uint8_t controller, CTRL_BTN_STATUS value) {
46
    // Determine the next direction for the trails based off the last button press
110
    // Determine the next direction for the trails based off the last button press
47
    if (p1) {
111
    if (controller < tron_data_p->players) {
48
        TRON_P1_DIRECTION p1_dir;
112
        // Save the current button value
49
        p1_dir.value = p1;
113
        tron_data_p->last_direction[controller] = value.w;
-
 
114
 
50
        data_p->p1_last_direction = p1_dir.value;
115
        // Grab the location of the head
51
        TRON_POINT p1_point = data_p->p1_body[data_p->length - 1];
116
        TRON_POINT next_pos = tron_data_p->body[controller][tron_data_p->length[controller] - 1];
-
 
117
 
-
 
118
        // Determine which next point to move to
52
        if (p1_dir.up) {
119
        if (value.BTN_L_N || value.BTN_L_E) {   // Up
53
            p1_point.z = (p1_point.z == CUBE_LAYER_COUNT - 1) ? 0 : p1_point.z + 1;
120
            next_pos.z = (next_pos.z == CUBE_LAYER_COUNT - 1) ? 0 : next_pos.z + 1;
54
        } else if (p1_dir.down) {
121
        } else if (value.BTN_L_S || value.BTN_L_W) {    // Down
55
            p1_point.z = (p1_point.z == 0) ? CUBE_LAYER_COUNT - 1 : p1_point.z - 1;
122
            next_pos.z = (next_pos.z == 0) ? CUBE_LAYER_COUNT - 1 : next_pos.z - 1;
56
        } else if (p1_dir.forward) {
123
        } else if (value.BTN_R_N) { // Forward
57
            p1_point.x = (p1_point.x == CUBE_ROW_COUNT - 1) ? 0 : p1_point.x + 1;
124
            next_pos.x = (next_pos.x == CUBE_ROW_COUNT - 1) ? 0 : next_pos.x + 1;
58
        } else if (p1_dir.right) {
125
        } else if (value.BTN_R_W) { // Left
59
            p1_point.y = (p1_point.y == CUBE_COLUMN_COUNT - 1) ? 0 : p1_point.y + 1;
126
            next_pos.y = (next_pos.y == CUBE_COLUMN_COUNT - 1) ? 0 : next_pos.y + 1;
60
        } else if (p1_dir.backward) {
127
        } else if (value.BTN_R_S) { // Backwards
61
            p1_point.x = (p1_point.x == 0) ? CUBE_ROW_COUNT - 1 : p1_point.x - 1;
128
            next_pos.x = (next_pos.x == 0) ? CUBE_ROW_COUNT - 1 : next_pos.x - 1;
-
 
129
//        } else if (value.BTN_R_E) { // Right
62
        } else if (p1_dir.left) {
130
        } else { // Right
63
            p1_point.y = (p1_point.y== 0) ? CUBE_COLUMN_COUNT - 1 : p1_point.y - 1;
131
            next_pos.y = (next_pos.y== 0) ? CUBE_COLUMN_COUNT - 1 : next_pos.y - 1;
64
        }
132
        }
65
        data_p->p1_direction = p1_point;
-
 
66
    }
133
        
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) {
134
        // Save the next point to move to
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;
135
        tron_data_p->direction[controller] = next_pos;
86
    }
136
    }
87
}
137
}
88
 
138
 
89
void Tron_Update_Frame(void) {
139
void Tron_Update_Frame(void) {
-
 
140
    uint8_t player, player2;
-
 
141
 
-
 
142
    // Change the current head color to the body color
-
 
143
    for (player = 0; player < tron_data_p->players; player++) {
-
 
144
        if (tron_data_p->dead[player] == 0) {
-
 
145
            Cube_Set_Pixel(tron_data_p->body[player][tron_data_p->length[player] - 1].z,
-
 
146
                    tron_data_p->body[player][tron_data_p->length[player] - 1].x,
-
 
147
                    tron_data_p->body[player][tron_data_p->length[player] - 1].y,
-
 
148
                    tron_data_p->Color_Body_R[player],
-
 
149
                    tron_data_p->Color_Body_G[player],
-
 
150
                    tron_data_p->Color_Body_B[player]);
-
 
151
        }
-
 
152
    }
-
 
153
 
90
    // Check if there is a head-on collision
154
    // Check if there is a head-on collision between any of the players
-
 
155
    for (player = 0; player < tron_data_p->players; player++) {
-
 
156
        if (tron_data_p->dead[player] == 0) {
-
 
157
            for (player2 = 0; player2 < tron_data_p->players; player2++) {
-
 
158
                if (player2 == player) continue;
-
 
159
                if (tron_data_p->dead[player2] == 0) {
91
    if (data_p->p1_direction.x == data_p->p2_direction.x &&
160
                    if (tron_data_p->direction[player].x == tron_data_p->direction[player2].x &&
92
            data_p->p1_direction.y == data_p->p2_direction.y &&
161
                            tron_data_p->direction[player].y == tron_data_p->direction[player2].y &&
93
            data_p->p1_direction.z == data_p->p2_direction.z) {
162
                            tron_data_p->direction[player].z == tron_data_p->direction[player2].z) {
-
 
163
                        // Indicate the collision point
-
 
164
                        Cube_Set_Pixel(tron_data_p->direction[player].z, tron_data_p->direction[player].x,
94
        Cube_Set_Pixel(data_p->p1_direction.z, data_p->p1_direction.x, data_p->p1_direction.y, TRON_COLLISION);
165
                                tron_data_p->direction[player].y, TRON_COLLISION);
95
        Delay_MS(3000);
166
                        // Mark the players as dead
96
        Animation_Cube_In_Out(200, TRON_COLLISION);
167
                        tron_data_p->dead[player] = 1;
-
 
168
                        tron_data_p->dead[player2] = 1;
97
        Reset_Board(BOARD_MODE_IDLE);
169
                        tron_data_p->players_alive -= 2;
-
 
170
                    }
-
 
171
                }
-
 
172
            }
-
 
173
        }
98
    }
174
    }
99
 
175
 
100
    // Check if the location that we are moving to is overlapping either trails
176
    // Check if the location that we are moving to is overlapping either trails
101
    uint32_t index, p1_collision = 0, p2_collision = 0;
177
    uint8_t index;
102
    for (index = 0; index < data_p->length; index++) {
178
    for (player = 0; player < tron_data_p->players; player++) {
-
 
179
        // Check if the player in question is dead
103
        if (data_p->p1_direction.x == data_p->p1_body[index].x &&
180
        if (tron_data_p->dead[player] == 0) {
104
                data_p->p1_direction.y == data_p->p1_body[index].y &&
181
            // See if the player's next point hits anyone elses trail
105
                data_p->p1_direction.z == data_p->p1_body[index].z) {
182
            for (player2 = 0; player2 < tron_data_p->players; player2++) {
106
            p1_collision = 1;
183
                for (index = 0; index < tron_data_p->length[player2]; index++) {
107
        }
-
 
108
        if (data_p->p1_direction.x == data_p->p2_body[index].x &&
184
                    if (tron_data_p->direction[player].x == tron_data_p->body[player2][index].x &&
109
                data_p->p1_direction.y == data_p->p2_body[index].y &&
185
                            tron_data_p->direction[player].y == tron_data_p->body[player2][index].y &&
110
                data_p->p1_direction.z == data_p->p2_body[index].z) {
186
                            tron_data_p->direction[player].z == tron_data_p->body[player2][index].z) {
111
            p1_collision = 1;
187
                        // Indicate the collision point
112
        }
-
 
113
        if (data_p->p2_direction.x == data_p->p2_body[index].x &&
188
                        Cube_Set_Pixel(tron_data_p->direction[player].z, tron_data_p->direction[player].x,
114
                data_p->p2_direction.y == data_p->p2_body[index].y &&
189
                                tron_data_p->direction[player].y, TRON_COLLISION);
115
                data_p->p2_direction.z == data_p->p2_body[index].z) {
190
                        // Mark the player as dead
116
            p2_collision = 1;
191
                        tron_data_p->dead[player] = 1;
117
        }
-
 
118
        if (data_p->p2_direction.x == data_p->p1_body[index].x &&
192
                        tron_data_p->players_alive -= 1;
119
                data_p->p2_direction.y == data_p->p1_body[index].y &&
193
                    }
120
                data_p->p2_direction.z == data_p->p1_body[index].z) {
194
                }
121
            p2_collision = 1;
195
            }
122
        }
196
        }
123
    }
197
    }
124
 
198
 
125
    // Save the new head location of each trail
199
    // Save the new head location of each trail
-
 
200
    for (player = 0; player < tron_data_p->players; player++) {
-
 
201
        // If player is still alive, extend its body in the next direction
126
    data_p->length++;
202
        if (tron_data_p->dead[player] == 0) {
127
    data_p->p1_body[data_p->length - 1] = data_p->p1_direction;
203
            tron_data_p->length[player]++;
128
    data_p->p2_body[data_p->length - 1] = data_p->p2_direction;
204
            tron_data_p->body[player][tron_data_p->length[player] - 1] = tron_data_p->direction[player];
-
 
205
        }
-
 
206
    }
129
 
207
 
130
    Cube_Clear();
208
    // Update the head to its corresponding color
131
    // Draw player 1 light trail
209
    for (player = 0; player < tron_data_p->players; player++) {
-
 
210
        // If player is still alive, recolor its body to indicate its new head
132
    for (index = 0; index < data_p->length - 1; index++) {
211
        if (tron_data_p->dead[player] == 0) {
-
 
212
            // Set the color of the new head
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_COLOR);
213
            Cube_Set_Pixel(tron_data_p->body[player][tron_data_p->length[player] - 1].z,
134
    }
-
 
135
    Cube_Set_Pixel(data_p->p1_body[data_p->length-1].z, data_p->p1_body[data_p->length-1].x, data_p->p1_body[data_p->length-1].y, TRON_PLAYER_1_HEAD);
214
                    tron_data_p->body[player][tron_data_p->length[player] - 1].x,
136
 
-
 
-
 
215
                    tron_data_p->body[player][tron_data_p->length[player] - 1].y,
137
    // Draw player 2 light trail
216
                    tron_data_p->Color_Head_R[player],
138
    for (index = 0; index < data_p->length - 1; index++) {
217
                    tron_data_p->Color_Head_G[player],
139
        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);
218
                    tron_data_p->Color_Head_B[player]);
-
 
219
        }
140
    }
220
    }
141
    Cube_Set_Pixel(data_p->p2_body[data_p->length-1].z, data_p->p2_body[data_p->length-1].x, data_p->p2_body[data_p->length-1].y, TRON_PLAYER_2_HEAD);
-
 
142
 
221
 
143
    // Indicate the point of collision and reset to idle mode
222
    // End the game if there is one player left alive
144
    if (p1_collision || p2_collision) {
223
    if (tron_data_p->players_alive == 1) {
145
        uint32_t ind = data_p->length - 1;
224
        Delay_MS(5000);
146
        if (p1_collision)
225
        // Determine which player is still alive and flash his color
147
            Cube_Set_Pixel(data_p->p1_body[ind].z, data_p->p1_body[ind].x, data_p->p1_body[ind].y, TRON_COLLISION);
226
        for (player = 0; player < tron_data_p->players; player++) {
148
        if (p2_collision)
227
            if (tron_data_p->dead[player] == 0) {
-
 
228
                Animation_Cube_In_Out(200, tron_data_p->Color_Head_R[player],
149
            Cube_Set_Pixel(data_p->p2_body[ind].z, data_p->p2_body[ind].x, data_p->p2_body[ind].y, TRON_COLLISION);
229
                        tron_data_p->Color_Head_G[player], tron_data_p->Color_Head_B[player]);
150
        Delay_MS(3000);
230
            }
151
        if (p1_collision && p2_collision)
231
        }
152
            Animation_Cube_In_Out(200, TRON_COLLISION);
232
        Reset_Board(BOARD_MODE_IDLE);
153
        else if (p1_collision)
233
    } else if (tron_data_p->players_alive == 0) {
154
            Animation_Cube_In_Out(200, TRON_PLAYER_2_HEAD);
234
        // If no players are alive flash neutral color
155
        else if (p2_collision)
235
        Delay_MS(5000);
156
            Animation_Cube_In_Out(200, TRON_PLAYER_1_HEAD);
236
        Animation_Cube_In_Out(200, TRON_COLLISION_2);
157
        Reset_Board(BOARD_MODE_IDLE);
237
        Reset_Board(BOARD_MODE_IDLE);
158
    }
238
    }
159
 
239
 
160
    // Determine the next direction to take
240
    // Determine the next direction to move to for each player
-
 
241
    for (player = 0; player < tron_data_p->players; player++) {
-
 
242
        if (tron_data_p->dead[player] == 0) {
161
    Tron_Update_Direction(data_p->p1_last_direction, data_p->p2_last_direction);
243
            Tron_Update_Direction(player, (CTRL_BTN_STATUS)tron_data_p->last_direction[player]);
-
 
244
        }
-
 
245
    }
162
 
246
 
163
    // Decrease the delay between frame updates by 5ms
247
    // Decrease the delay between frame updates by 5ms
164
    data_p->delay -= 5; 
248
    tron_data_p->delay -= 5;
165
}
249
}
166
250