-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
194 lines (172 loc) · 5.24 KB
/
main.c
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <pic32mx.h>
#include <stdint.h>
#include "helpers.h"
int main()
{
init();
init_player();
init_npcs();
bsl_init();
int start = TICKS;
init_splash();
inputs i;
// Initiate states
GAME_STATE = MENU;
ARROW_STATE = PLAY;
// Keeping track of frame transitions
int transition_timer;
// Button pressing delay
int button_delay = TICKS;
int menu_led_timer = TICKS;
UBYTE led_counter;
while (1)
{
// FIXED TIME UPDATE ROUTINE
if (TICKS - start > 33)
{
start = TICKS;
clear_buf();
switch(GAME_STATE) {
case MENU:
draw_menu();
draw_arrow();
break;
case GAME:
draw(car.pos, car.texture);
draw_npcs();
update_road();
draw_side();
draw_road();
display_points();
break;
case SCOREBOARD:
display_scoreboard();
break;
case TRANSITION:
// Transitioning screen
if(TICKS - transition_timer <= 2500)
{
death_transition();
UNDERSCORE_POS = UNDERSCORE_DEFAULT;
} else {
GAME_STATE = DEATH;
}
break;
case DEATH:
display_game_over();
break;
}
oled_put_buffer();
}
i = get_inputs();
switch(GAME_STATE) {
case MENU:
// Start game
srand(TICKS);
if (i.b2 && ARROW_STATE == PLAY)
{
GAME_STATE = GAME;
init_player();
init_road();
init_npcs();
init_scenary();
}
// Show scoreboard
if (i.b2 && ARROW_STATE == SCORE)
{
GAME_STATE = SCOREBOARD;
}
if (TICKS - menu_led_timer > LED_DELAY)
{
leds l;
l._all = (0x1 << led_counter) | (0x80 >> led_counter);
set_leds(l);
led_counter = (led_counter + 1) % 8;
menu_led_timer = TICKS;
}
// Move arrow
if (i.b3 || i.b4)
{
// Delay
if((TICKS-button_delay)<BUTTON_DELAY)
{
continue;
}
// Reset delay
button_delay = TICKS;
if(ARROW_STATE == SCORE)
{
ARROW_STATE = PLAY;
}
else
{
ARROW_STATE = SCORE;
}
}
break;
case GAME:
update_npc();
update_player(i);
update_side();
if (car.pos._1 < 4.0 || car.pos._1 > 110.0
|| update_npc() || OVERFLOW)
{
transition_timer = TICKS;
GAME_STATE = TRANSITION;
}
break;
case SCOREBOARD:
if (i.b1)
GAME_STATE = MENU;
break;
case DEATH:
if(i.b1) {
if(OVERFLOW) {
points = INT_MAX;
}
// Overflow counts as a "win"
add_score(name, (int) points, OVERFLOW);
reset_game_variables();
GAME_STATE = SCOREBOARD;
}
if(i.b2)
{
// Delay
if((TICKS-button_delay)<BUTTON_DELAY)
{
continue;
}
// Reset delay
button_delay = TICKS;
// Switch between letters
UNDERSCORE_POS = UNDERSCORE_DEFAULT + (UNDERSCORE_POS - UNDERSCORE_DEFAULT + UNDERSCORE_STEP) % (3*UNDERSCORE_STEP);
}
if(i.b3)
{
// Delay
if((TICKS-button_delay) < BUTTON_DELAY)
{
continue;
}
// Reset delay
button_delay = TICKS;
next_letter(1);
}
if(i.b4)
{
// Delay
if((TICKS-button_delay) < BUTTON_DELAY)
{
continue;
}
// Reset delay
button_delay = TICKS;
next_letter(-1);
}
break;
}
}
for (;;)
;
return 0;
}