-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemy.c
236 lines (184 loc) · 4.3 KB
/
Enemy.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "Enemy.h"
// enemy screen in pixels. 0 = off, 1 = on
uint16_t current_enemy_screen[3][7] = {
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0}
};
// enemy figures
uint16_t enemy_3d[7][3][2] = {
{
{0,0},
{0,0},
{1,0}
},
{
{0,0},
{1,0},
{1,0}
},
{
{0,0},
{1,0},
{1,0}
},
{
{0,0},
{1,1},
{0,0}
},
{
{0,0},
{1,0},
{0,0}
},
{
{1,0},
{1,0},
{0,0}
},
{
{0,0},
{0,0},
{1,0}
}
};
// delay for the next enemy generation
uint16_t delay = 3;
uint16_t delay_tick = 0;
// speed of the game
const uint32_t game_speed_const = 4500000; // 4500000
uint32_t game_speed = 4500000;
const uint32_t game_speed_min = 2750000; // 2750000 = 114ms || 3500000 = 146ms
const uint32_t game_speed_step = 5000;
// light up a specific LED
void EnemyControlLEDDirect(uint16_t row, uint16_t col, uint16_t value){
// if 1 turn on, if 0 turn off
if(value == 1)
GPIOA->PCOR = (1u << enemy_screen_definition[row][col]);
else
GPIOA->PSOR = (1u << enemy_screen_definition[row][col]);
}
// clear the enemy screen
// does not turn off leds unless EnemyAnimate is ran!
void EnemyClearScreen(void){
// clear current enemy screen
for(int i = 0; i < 3; i++){
for(int b = 0;b < 7; b++){
current_enemy_screen[i][b] = 0;
}
}
}
// initialize or re-initialize
void EnemyInit(uint32_t seed){
// pass in the seed
srand(seed);
// clear enemy screen
EnemyClearScreen();
// game_speed reset
game_speed = game_speed_const;
// reset the delay tick
delay_tick = 0;
}
void EnemyScreenRedraw(void){
// turn on/off leds
for(int i = 0; i < 3; i++){
for(int b = 0; b < 5; b++){
// turn off the led if 0 turn on if 1
if(current_enemy_screen[i][b] == 0)
GPIOA->PSOR = (1u << enemy_screen_definition[i][b]);
else
GPIOA->PCOR = (1u << enemy_screen_definition[i][b]);
}
}
}
// animates and renders the pixels
void EnemyAnimate(){
// translate the enemy screen left by 1 column
for(int i = 0; i < 6; i++){
// translate entire colum
for(int b = 0; b < 3; b++)
current_enemy_screen[b][i] = current_enemy_screen[b][i+1];
}
// clear the last 3
for(int i = 0; i < 3; i++)
current_enemy_screen[i][6] = 0;
// redraw the screen
EnemyScreenRedraw();
}
void GenerateEnemy(){
// generate a random number
int num = rand(); // 0 to 0x7FFFFFFF
num /= 306783378;
// just in case
if(num >= 7)
num = 6;
// set up an enemy
for(int i = 0; i < 3; i++){
for(int b = 0; b < 2; b++){
current_enemy_screen[i][b + 5] = enemy_3d[num][i][b];
}
}
}
// adjust the enemy speed as game progresses
void EnemySpeedAdjust(void){
// adjust the game speed at different rates through out
if(game_speed > (game_speed_min + 300000)){
game_speed -= game_speed_step;
}
else if(game_speed > game_speed_min){
game_speed -= game_speed_step/5;
}
}
// PIT 1 (PWM) interrupt
// main loop for enemy
// takes care of animating enemy
// and checking if jumper has been hit
void PIT_CH1_IRQHandler(void){
// animate the screen
EnemyAnimate();
/* uncomment if you want the jumper to be automated
if(current_enemy_screen[2][3] == 1)
// run the jumper!
JumperSetEnergy(4);
else if(current_enemy_screen[2][2] == 1)
// run the jumper!
JumperSetEnergy(4);
*/
// check if jumper hit an enemy
for(int i = 0; i < 3; i++){
// check if the enemy screen is a 1
if(current_enemy_screen[i][0] == 1){
if(jumper_pos == 2 - i){
// jumper hit!
// end the game
GameStop();
// reset the frequency of the PIT timer -- might not need this
PIT->CHANNEL[1].LDVAL = game_speed_const;
PIT->CHANNEL[1].TFLG = 0x1; // clear the irq
NVIC_ClearPendingIRQ(PIT_CH1_IRQn); // clear ARM irq
return;
}
}
}
// generate a new enemy if delay allows
if( delay_tick >= delay){
GenerateEnemy();
delay_tick = 0;
// establish the next delay 3 to 8
int num = rand(); // 0 to 0x7fffff
num /= 429496729;
num += 3;
if(num >= 9)
num = 8;
delay = num;
}
else
delay_tick++;
// make the game faster
EnemySpeedAdjust();
// reset the frequency of the PIT timer with new game speed
PIT->CHANNEL[1].LDVAL = game_speed;
PIT->CHANNEL[1].TFLG = 0x1; // clear the irq
NVIC_ClearPendingIRQ(PIT_CH1_IRQn); // clear ARM irq
}