-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
305 lines (252 loc) · 10.4 KB
/
main.cpp
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "EngineUtils.h"
#include "ThisThread.h"
#include "Thread.h"
#include "mbed.h"
// Include libraries for LCD
#include "N5110.h"
#include <cstdio>
// Include library for Joystick
#include "Joystick.h"
// Include Game Engine
#include "Engine.h"
#include <cstdlib>
#include <vector>
void GameOver();
void PlayLevel();
void level2();
void level3();
void Win();
void startMenu();
const int SPRITE[23][56]= {
{ 0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1 },
{ 0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0 },
{ 0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }
};
// Pin assignment format: lcd(IO, Ser_TX, Ser_RX, MOSI, SCLK, PWM)
N5110 screen(PC_7, PA_9, PB_10, PB_5, PB_3, PA_10);
// y x
Joystick joystick(PC_0, PC_1); // Attach and create joystick object
DigitalIn fireMissile(PA_8);
/* ---------------------
GAME OBJECTS FOR LEVELS
------------------------ */
Camera mainCamera({0,0,10});
char buffer[14] = {0};
// Create cubes
Cube test_cube({10,0,10}, 10, 10, 10);
Circle cube_core(Vector3ToPoint2D(test_cube.getPosition()), 2);
int test_cube_speed = 3;
Vector3 axis_1 = {4,1,3};
Cube test_cube2({30,0,10}, 10, 20, 15);
Circle cube2_core(Vector3ToPoint2D(test_cube.getPosition()), 4);
int test_cube2_speed = 5;
Vector3 axis_2 = {1,0,1};
Cube test_cube3({63,15,20}, 25, 8, 10);
Circle cube3_core(Vector3ToPoint2D(test_cube.getPosition()), 3);
int test_cube3_speed = 3;
Vector3 axis_3 = {5,0,2};
// Create the player
Player spaceship({60, 38});
int shots_left = 2;
Circle missile({0,0}, 2);
int current_level = 1;
// Initialise everything in int main()
int main() {
initialiseEngine(); // Does nothing atm
// Initialise the LCD
screen.init(LPH7366_1);
screen.setContrast(1); // Set contrast to 55%
screen.setBrightness(0); // Set brightness to 50% (utilises the PWM)
// Initialise the Joystick
joystick.init();
fireMissile.mode(PullUp);
startMenu();
}
void startMenu() {
// Create a main menu here for the game
screen.clear();
screen.drawSprite(10, 0, 23, 56, (int *)SPRITE);
screen.printString("Press to Play!", 0, 4);
screen.refresh();
ThisThread::sleep_for(1000ms);
while (fireMissile) {}
PlayLevel();
}
void PlayLevel() {
// Create a camera - TODO fix camera projection, does not take into account camera Z position (Use for perspective projection)
//spaceship.rotatePlayer(95);
ThisThread::sleep_for(500ms);
bool fired = false;
Point2D missilePos(0,0);
while (true) {
screen.clear();
sprintf(buffer, "Shots:%i", shots_left);
screen.printString(buffer, 40, 0);
// Draws screen border
screen.drawRect(0,0,84,48,FILL_TRANSPARENT);
Vector2 player_pos = spaceship.getPosition();
// Continue motion of projectile if fired
if (fired) {
missilePos.y-=2;
if (missilePos.y <= 2){
fired = false;
}
if (fired){
missile.centre = missilePos;
missile.draw(screen);
}
}
// Joystick handling for movement
if(joystick.get_direction() == E){
player_pos.x +=3;
spaceship.setPosition(player_pos);
printf(" Direction: E\n");
}else if(joystick.get_direction() == W){
player_pos.x -=3;
spaceship.setPosition(player_pos);
printf(" Direction: W\n");
}
if (!fireMissile && !fired) {
fired = true;
shots_left-=1;
missilePos = Vector2ToPoint2D(player_pos);
missile.centre = missilePos;
missile.draw(screen);
printf("Fire missile!\n");
}
// Draw player
spaceship.drawPlayer(screen);
// If cube core has collided with missile, stop rendering cube
if (fired && cube_core.isCollidingWith(missile)) { test_cube.destroyCube(true); }
if (!test_cube.isDestroyed()){
cube_core.centre = Vector3ToPoint2D(test_cube.getPosition());
cube_core.draw(screen);
test_cube.drawCube(mainCamera, screen);
}
if (fired && cube2_core.isCollidingWith(missile)) { test_cube2.destroyCube(true); }
if (!test_cube2.isDestroyed()){
cube2_core.centre = Vector3ToPoint2D(test_cube2.getPosition());
cube2_core.draw(screen);
test_cube2.drawCube(mainCamera, screen);
}
if (fired && cube3_core.isCollidingWith(missile)) { test_cube3.destroyCube(true); }
if (!test_cube3.isDestroyed()){
cube3_core.centre = Vector3ToPoint2D(test_cube3.getPosition());
cube3_core.draw(screen);
test_cube3.drawCube(mainCamera, screen);
}
// End Game if there are no shots left and all junk has not been destroyed
if(shots_left <=0 && !fired && (!test_cube.isDestroyed() || !test_cube2.isDestroyed() || !test_cube3.isDestroyed())){
GameOver();
}
if (test_cube.isDestroyed() && test_cube2.isDestroyed() && test_cube3.isDestroyed()) {
switch (current_level) {
case 1:
level2();
case 2:
level3();
case 3:
Win();
}
}
screen.refresh();
// Spin the cubes around a weird axis to make them appear floating
test_cube.rotateCube(test_cube_speed, axis_1);
test_cube2.rotateCube(test_cube2_speed, axis_2);
test_cube3.rotateCube(test_cube3_speed, axis_3);
ThisThread::sleep_for(100ms);
}
}
void level2(){
current_level = 2;
screen.clear();
// Draws screen border
screen.drawRect(0,0,84,48,FILL_TRANSPARENT);
screen.printString("LEVEL 2", 20, 3);
screen.refresh();
shots_left = 1;
// CONFIGURE CUBE 1
test_cube.destroyCube(false); // reset cube
int test_cube_speed = 10; // faster movement
Vector3 axis_1 = {10,5,6};
test_cube.setPosition({30, 10, 20});
test_cube.setWidthDepthHeight(20, 15, 12);
cube_core.centre = Vector3ToPoint2D(test_cube.getPosition());
cube_core.radius = 1; // smaller target
missile.radius = 1; // smaller projectile
ThisThread::sleep_for(3000ms);
PlayLevel();
}
void level3() {
current_level = 3;
screen.clear();
// Draws screen border
screen.drawRect(0,0,84,48,FILL_TRANSPARENT);
screen.printString("LEVEL 3", 20, 3);
screen.refresh();
shots_left = 3;
// CONFIGURE CUBE 1
test_cube.destroyCube(false);
int test_cube_speed = 15;
Vector3 axis_1 = {1,5,6};
test_cube.setPosition({40, 5, 10});
test_cube.setWidthDepthHeight(10, 15, 12);
cube_core.centre = Vector3ToPoint2D(test_cube.getPosition());
cube_core.radius = 3;
missile.radius = 1;
// CONFIGURE CUBE 2
test_cube2.destroyCube(false); // reset cube
int test_cube2_speed = 10;
Vector3 axis_2 = {2,3,1};
test_cube.setPosition({5, 30, 20});
test_cube.setWidthDepthHeight(15, 5, 25);
cube_core.centre = Vector3ToPoint2D(test_cube.getPosition());
cube_core.radius = 2; // smaller target
test_cube3.destroyCube(false); // reset cube
int test_cube3_speed = 10;
Vector3 axis_3 = {1,3,4};
test_cube.setPosition({15, 10, 20});
test_cube.setWidthDepthHeight(20, 5, 15);
cube_core.centre = Vector3ToPoint2D(test_cube.getPosition());
cube_core.radius = 1; // smaller target
ThisThread::sleep_for(3000ms);
PlayLevel();
}
void GameOver() {
screen.clear();
// Draws screen border
screen.drawRect(0,0,84,48,FILL_TRANSPARENT);
screen.printString("GAME OVER", 15, 3);
screen.refresh();
ThisThread::sleep_for(2000ms);
exit(0);
}
void Win() {
screen.clear();
// Draws screen border
screen.drawRect(0,0,84,48,FILL_TRANSPARENT);
screen.printString("YOU WIN!!", 15, 3);
screen.refresh();
ThisThread::sleep_for(2000ms);
exit(0);
}