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
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);
241 Kevin 37
    Delay_MS(2000);
38
    while (1) {
39
        Tron_Update_Frame();
40
        Delay_MS(data_p->delay);
41
    }
42
}
43
 
44
void Tron_Update_Direction(uint8_t p1, uint8_t p2) {
45
    // Determine the next direction for the trails based off the last button press
46
    if (p1) {
47
        TRON_P1_DIRECTION p1_dir;
48
        p1_dir.value = p1;
49
        data_p->p1_last_direction = p1_dir.value;
50
        TRON_POINT p1_point = data_p->p1_body[data_p->length - 1];
51
        if (p1_dir.up) {
52
            p1_point.z = (p1_point.z == CUBE_LAYER_COUNT - 1) ? 0 : p1_point.z + 1;
53
        } else if (p1_dir.down) {
54
            p1_point.z = (p1_point.z == 0) ? CUBE_LAYER_COUNT - 1 : p1_point.z - 1;
55
        } else if (p1_dir.forward) {
56
            p1_point.x = (p1_point.x == CUBE_ROW_COUNT - 1) ? 0 : p1_point.x + 1;
57
        } else if (p1_dir.right) {
58
            p1_point.y = (p1_point.y == CUBE_COLUMN_COUNT - 1) ? 0 : p1_point.y + 1;
59
        } else if (p1_dir.backward) {
60
            p1_point.x = (p1_point.x == 0) ? CUBE_ROW_COUNT - 1 : p1_point.x - 1;
61
        } else if (p1_dir.left) {
62
            p1_point.y = (p1_point.y== 0) ? CUBE_COLUMN_COUNT - 1 : p1_point.y - 1;
63
        }
64
        data_p->p1_direction = p1_point;
65
    }
66
    if (p2) {
67
        TRON_P2_DIRECTION p2_dir;
68
        p2_dir.value = p2;
69
        data_p->p2_last_direction = p2_dir.value;
70
        TRON_POINT p2_point = data_p->p2_body[data_p->length - 1];
71
        if (p2_dir.up) {
72
            p2_point.z = (p2_point.z == CUBE_LAYER_COUNT - 1) ? 0 : p2_point.z + 1;
73
        } else if (p2_dir.down) {
74
            p2_point.z = (p2_point.z == 0) ? CUBE_LAYER_COUNT - 1 : p2_point.z - 1;
75
        } else if (p2_dir.forward) {
76
            p2_point.x = (p2_point.x == CUBE_ROW_COUNT - 1) ? 0 : p2_point.x + 1;
77
        } else if (p2_dir.right) {
78
            p2_point.y = (p2_point.y == CUBE_COLUMN_COUNT - 1) ? 0 : p2_point.y + 1;
79
        } else if (p2_dir.backward) {
80
            p2_point.x = (p2_point.x == 0) ? CUBE_ROW_COUNT - 1 : p2_point.x - 1;
81
        } else if (p2_dir.left) {
82
            p2_point.y = (p2_point.y== 0) ? CUBE_COLUMN_COUNT - 1 : p2_point.y - 1;
83
        }
84
        data_p->p2_direction = p2_point;
85
    }
86
}
87
 
88
void Tron_Update_Frame(void) {
89
    // Check if there is a head-on collision
90
    if (data_p->p1_direction.x == data_p->p2_direction.x &&
91
            data_p->p1_direction.y == data_p->p2_direction.y &&
92
            data_p->p1_direction.z == data_p->p2_direction.z) {
93
        Cube_Set_Pixel(data_p->p1_direction.z, data_p->p1_direction.x, data_p->p1_direction.y, TRON_COLLISION);
94
        Delay_MS(3000);
242 Kevin 95
        Animation_Cube_In_Out(200, TRON_COLLISION);
241 Kevin 96
        Reset_Board(BOARD_MODE_IDLE);
97
    }
98
 
99
    // Check if the location that we are moving to is overlapping either trails
100
    uint32_t index, p1_collision = 0, p2_collision = 0;
101
    for (index = 0; index < data_p->length; index++) {
102
        if (data_p->p1_direction.x == data_p->p1_body[index].x &&
103
                data_p->p1_direction.y == data_p->p1_body[index].y &&
104
                data_p->p1_direction.z == data_p->p1_body[index].z) {
105
            p1_collision = 1;
106
        }
107
        if (data_p->p1_direction.x == data_p->p2_body[index].x &&
108
                data_p->p1_direction.y == data_p->p2_body[index].y &&
109
                data_p->p1_direction.z == data_p->p2_body[index].z) {
110
            p1_collision = 1;
111
        }
112
        if (data_p->p2_direction.x == data_p->p2_body[index].x &&
113
                data_p->p2_direction.y == data_p->p2_body[index].y &&
114
                data_p->p2_direction.z == data_p->p2_body[index].z) {
115
            p2_collision = 1;
116
        }
117
        if (data_p->p2_direction.x == data_p->p1_body[index].x &&
118
                data_p->p2_direction.y == data_p->p1_body[index].y &&
119
                data_p->p2_direction.z == data_p->p1_body[index].z) {
120
            p2_collision = 1;
121
        }
122
    }
123
 
124
    // Save the new head location of each trail
125
    data_p->length++;
126
    data_p->p1_body[data_p->length - 1] = data_p->p1_direction;
127
    data_p->p2_body[data_p->length - 1] = data_p->p2_direction;
128
 
129
    Cube_Clear();
130
    // Draw player 1 light trail
243 Kevin 131
    for (index = 0; index < data_p->length - 1; index++) {
241 Kevin 132
        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);
133
    }
243 Kevin 134
    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 135
 
136
    // Draw player 2 light trail
243 Kevin 137
    for (index = 0; index < data_p->length - 1; index++) {
241 Kevin 138
        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);
139
    }
243 Kevin 140
    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 141
 
142
    // Indicate the point of collision and reset to idle mode
143
    if (p1_collision || p2_collision) {
144
        uint32_t ind = data_p->length - 1;
145
        if (p1_collision)
146
            Cube_Set_Pixel(data_p->p1_body[ind].z, data_p->p1_body[ind].x, data_p->p1_body[ind].y, TRON_COLLISION);
147
        if (p2_collision)
148
            Cube_Set_Pixel(data_p->p2_body[ind].z, data_p->p2_body[ind].x, data_p->p2_body[ind].y, TRON_COLLISION);
149
        Delay_MS(3000);
242 Kevin 150
        if (p1_collision && p2_collision)
151
            Animation_Cube_In_Out(200, TRON_COLLISION);
152
        else if (p1_collision)
153
            Animation_Cube_In_Out(200, TRON_PLAYER_2_HEAD);
154
        else if (p2_collision)
155
            Animation_Cube_In_Out(200, TRON_PLAYER_1_HEAD);
241 Kevin 156
        Reset_Board(BOARD_MODE_IDLE);
157
    }
158
 
159
    // Determine the next direction to take
160
    Tron_Update_Direction(data_p->p1_last_direction, data_p->p2_last_direction);
161
 
162
    // Decrease the delay between frame updates by 5ms
163
    data_p->delay -= 5; 
164
}