-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchadM.cpp
259 lines (233 loc) · 6.3 KB
/
chadM.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
/**
* Chad Manning's file for Iron Curtain
* @author: Chad Manning
* Course: CMPS 3350
* Instructor: Gordon Griesel
*/
#include <GL/glx.h>
#include <X11/Xlib.h>
#include <stdio.h>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include "core.h"
#include "chadM.h"
#include "fonts.h"
extern Global& gl;
extern double timeDiff(struct timespec *start, struct timespec *end);
extern EnemyShip *headShip;
extern EnemyShip *tailShip;
extern EnemyShip *eShip;
//Global variables
enum MOVETYPE { RUSH,
STRAFE,
CIRCLING,
BANK,
DIAG_RUSH };
double lastSpawnTime = 0.0;
/*
Displays Chads picture and name on the title screen
*/
void displayChad (float x , float y, GLuint textid) {
float wid = 100.0;
glPushMatrix();
glTranslatef(x, y, 0.0);
glBindTexture(GL_TEXTURE_2D, textid);
glColor3ub(255, 255, 255);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex2i(-wid, -wid);
glTexCoord2f(0.0f, 0.0f); glVertex2i(-wid, wid);
glTexCoord2f(1.0f, 0.0f); glVertex2i(wid, wid);
glTexCoord2f(1.0f, 1.0f); glVertex2i(wid, -wid);
glEnd();
glPopMatrix();
unsigned int color = 0xffff0000;
Rect r;
r.width = 250.0;
r.height = 100.0;
r.center = x;
r.left = x - 10;
r.bot = y - 125;
ggprint16(&r, 16, color, "Chad Manning");
}
/*
Constructor for EnemyShip class
If you want to make the ship behave differently call the config class
from nickJA.cpp to modify path.
Ex.
eShip = new EnemyShip(x, y, movType);
tailShip->config$NAME_OF_PATTERN$;
*/
EnemyShip::EnemyShip(int x, int y, int movType) {
pos[0] = x;
pos[1] = y;
pos[2] = 1;
color[0] = color[1] = color[2] = 0.35;
health = 1;
deathScore = 10;
movPattern = movType;
eWpn = new EnemyStd();
if (headShip == NULL) {
headShip = tailShip = this;
prevShip = nextShip = NULL;
} else {
prevShip = tailShip;
prevShip->nextShip = tailShip = this;
}
nextShip = NULL;
}
/*
Destructor for EnemyShip class
Readjusts linked list depending on which ship was destroyed.
*/
EnemyShip::~EnemyShip() {
if (prevShip != NULL) {
prevShip->nextShip = nextShip;
} else {
headShip = nextShip;
}
if (nextShip != NULL) {
nextShip->prevShip = prevShip;
} else {
tailShip = prevShip;
}
}
/*
Returns radius of bounding circle for the ship
*/
float EnemyShip::getRadius() {
return detRadius;
}
int EnemyShip::getHealth() {
return health;
}
/*
Returns deathScore for each this type of ship
*/
int EnemyShip::getDeathScore() {
return deathScore;
}
/*
Destroys the ship when its health reaches zero. KABOOM.
"But Chad all this does is call the destructor." Yeah but destroyShip
makes more sense than delete.
*/
void EnemyShip::destroyShip() {
delete this;
}
/*
Deducts damage from this ship
*/
void EnemyShip::takeDamage(int damageTaken) {
health -= damageTaken;
}
/*
Takes a Ship class as input and renders it to screen.
used for both player ship and all enemy ships.
*/
void renderShip(Ship* ship) {
glColor3fv(ship->color);
glPushMatrix();
glTranslatef(ship->pos[0], ship->pos[1], ship->pos[2]);
glBegin(GL_QUADS);
glVertex2f(-20.0f, -20.0f);
glVertex2f(-20.0f, 20.0f);
glVertex2f(20.0f, 20.0f);
glVertex2f(20.0, -20.0);
glEnd();
glPopMatrix();
}
/*
Altered function for EnemyShip fire rate
*/
double EnemyShip::getTimeSlice(timespec *bt) {
clock_gettime(CLOCK_REALTIME, bt);
return timeDiff(&bulletTimer, bt);
}
/*
Constructor for lowest level of enemies spawned
*/
Grunt::Grunt(int x, int y, int movType) : EnemyShip(x, y, movType) {
deathScore = 10;
}
/*
Constructor for final boss of level, can take multiple hits
*/
Boss::Boss(int x, int y, int movType) : Grunt(x, y, movType) {
deathScore = 1000;
health = 50;
eWpn = new Pinwheel();
}
/*
Helper function for mainLevel, gets x-coord that is within
game bounds
*/
int getRandXSpawn() {
return rand() % 800 + 50;
}
/*
Helper function for mainLevel, gets x-coord that is within
game bounds
*/
int getRandYSpawn() {
return rand() % 900 + 20;
}
void resetSpawnTimer() {
lastSpawnTime = 0.0;
}
/*
Main level for the game, determines which enemy to spawn at certain
times.
@param gameTime total elapsed time in the game
*/
bool mainLevel(double gameTime) {
//gl.xres == 900
//g.yres == 1000
const int ySpawn = 1100;
static bool bossSpawned = false;
bool bossDefeated = false;
if (gameTime - lastSpawnTime > 2) {
lastSpawnTime = gameTime;
// if ((int) gameTime % 5 == 0)
// eShip = new Grunt(getRandXSpawn(), ySpawn, RUSH);
if (gameTime < 10.0) {
eShip = new Grunt(100, ySpawn, DIAG_RUSH);
tailShip->configDiagRush(gl.xres, 0, 1);
eShip = new Grunt(gl.xres, ySpawn+10, DIAG_RUSH);
tailShip->configDiagRush(0, 0, 1);
}
else if ( gameTime > 20 && gameTime < 50) {
eShip = new Grunt(getRandXSpawn(), ySpawn, CIRCLING);
tailShip->configCircle(50, 90, 1, 1, -1);
eShip = new Grunt(getRandXSpawn(), ySpawn, CIRCLING);
tailShip->configCircle(100, 90, 1.5, 1, 1);
}
else if (gameTime > 60 && gameTime < 80) {
eShip = new Grunt(-15, 700, DIAG_RUSH);
tailShip->configDiagRush(1000, -20, 2);
eShip = new Grunt(915, 800, DIAG_RUSH);
tailShip->configDiagRush(-100, -20, 2);
}
else if (gameTime > 90) {
if (!bossSpawned) {
eShip = new Boss(gl.xres / 2, ySpawn, DIAG_RUSH);
tailShip->configDiagRush(gl.xres / 2, 800, -1);
bossSpawned = true;
}
if (tailShip->pos[1] < 750) {
tailShip->configDiagRush(800, 800, 1);
}
else if (tailShip->pos[0] > 750) {
tailShip->configDiagRush(100, 800, 1);
}
else if (tailShip->pos[0] < 100){
tailShip->configDiagRush(600, 800, 1);
}
if (headShip == NULL) {
bossDefeated = true;
bossSpawned = false;
}
}
}
return bossDefeated;
}