Subversion Repositories Code-Repo

Rev

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
243 Kevin 21
    uint32_t index;
22
    for (index = 0; index < data_p->length - 1; index++) {
241 Kevin 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);
24
    }
243 Kevin 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);
241 Kevin 26
 
27
    // Draw player 2 light trail
243 Kevin 28
    for (index = 0; index < data_p->length - 1; index++) {
241 Kevin 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);
30
    }
243 Kevin 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);
241 Kevin 32
}
33
 
34
void Tron_Main(void) {
35
    // Main function, loops and delays while updating the frame every x milliseconds
242 Kevin 36
    Tron_Update_Direction(0x04,0x04);
255 Kevin 37
    Controller_Set_Leds(0x01,0x01);
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
243 Kevin 132
    for (index = 0; index < data_p->length - 1; index++) {
241 Kevin 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);
134
    }
243 Kevin 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);
241 Kevin 136
 
137
    // Draw player 2 light trail
243 Kevin 138
    for (index = 0; index < data_p->length - 1; index++) {
241 Kevin 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);
140
    }
243 Kevin 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);
241 Kevin 142
 
143
    // Indicate the point of collision and reset to idle mode
144
    if (p1_collision || p2_collision) {
145
        uint32_t ind = data_p->length - 1;
146
        if (p1_collision)
147
            Cube_Set_Pixel(data_p->p1_body[ind].z, data_p->p1_body[ind].x, data_p->p1_body[ind].y, TRON_COLLISION);
148
        if (p2_collision)
149
            Cube_Set_Pixel(data_p->p2_body[ind].z, data_p->p2_body[ind].x, data_p->p2_body[ind].y, TRON_COLLISION);
150
        Delay_MS(3000);
242 Kevin 151
        if (p1_collision && p2_collision)
152
            Animation_Cube_In_Out(200, TRON_COLLISION);
153
        else if (p1_collision)
154
            Animation_Cube_In_Out(200, TRON_PLAYER_2_HEAD);
155
        else if (p2_collision)
156
            Animation_Cube_In_Out(200, TRON_PLAYER_1_HEAD);
241 Kevin 157
        Reset_Board(BOARD_MODE_IDLE);
158
    }
159
 
160
    // Determine the next direction to take
161
    Tron_Update_Direction(data_p->p1_last_direction, data_p->p2_last_direction);
162
 
163
    // Decrease the delay between frame updates by 5ms
164
    data_p->delay -= 5; 
165
}