-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathogro.cpp
273 lines (244 loc) · 8.31 KB
/
ogro.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
// -----------------------------------------------------------------
// Learning Team B
// Members:
// Adam LeMmon
// Faith Satterthwaite
// Tom Fletcher
// Justin Ball
// CS 4280 – 11:30 am
// Final Project
// Dr. Rague
// Due: 12/06/12
// Version: 2.4
// -----------------------------------------------------------------
// We made five major improvements to this game
// 1) New controls
// 2) Enemy attack
// 3) HUD (heads up display)
// 4) Enemy health bars
// 5) New Weapon
// -----------------------------------------------------------------
#include "ogro.h"
// Phase 15 - Comment out the following
// Phase 16 - Uncomment
#include "rocket.h"
// Use the following temporarily
#include "player.h"
/*
OGRO.CPP
The COgroEnemy class implementation
OpenGL Game Programming
Author: Kevin Hawkins
Date: 3/30/2001
Description:
*/
void COgroEnemy::OnCollision(CObject *collisionObject)
{
// as long as the model is not dying, it can collide with other objects
if (modelState != MODEL_DIE)
{
// if this enemy collides with another enemy
if ((typeid(*collisionObject) == typeid(CSodEnemy)) ||
(typeid(*collisionObject) == typeid(COgroEnemy)))
{
aiState = AI_UNCARING;
}
/*********************************Faith Satterthwaite 12/1/2012*********************************/
// if this enemy collides with the player
else if (typeid(*collisionObject) == typeid(CPlayer))
{
direction = (dirToPlayer + 90) + ((rand()%30)-15); // set the direction of the enemy
attackPlayer = false;
}
/***********************************************************************************************/
// if this enemy collides with the terrain (always)
else if (typeid(*collisionObject) == typeid(CTerrain))
{
position.y = ((CTerrain*)collisionObject)->GetHeight(position.x, position.z) + size;
if (position.x <= ((CTerrain*)collisionObject)->GetScanDepth())
position.x = ((CTerrain*)collisionObject)->GetScanDepth();
if (position.x >= ((CTerrain*)collisionObject)->GetWidth()*((CTerrain*)collisionObject)->GetMul() - ((CTerrain*)collisionObject)->GetScanDepth())
position.x = ((CTerrain*)collisionObject)->GetWidth()*((CTerrain*)collisionObject)->GetMul() - ((CTerrain*)collisionObject)->GetScanDepth();
if (position.z <= ((CTerrain*)collisionObject)->GetScanDepth())
position.z = ((CTerrain*)collisionObject)->GetScanDepth();
if (position.z >= ((CTerrain*)collisionObject)->GetWidth()*((CTerrain*)collisionObject)->GetMul() - ((CTerrain*)collisionObject)->GetScanDepth())
position.z = ((CTerrain*)collisionObject)->GetWidth()*((CTerrain*)collisionObject)->GetMul() - ((CTerrain*)collisionObject)->GetScanDepth();
}
// Phase 15 - Comment out the following
// Phase 16 - Uncomment
else if (typeid(*collisionObject) == typeid(CRocket))
{
///// -- Adam
if (!static_cast<CRocket*>(collisionObject)->hitTarget)
{//If the rocket hits the enemy, make it so it only takes damage once
if(hitPoints > 0)
{
beenShot = true;//Tell the enemy they've been shot
if(static_cast<CRocket*>(collisionObject)->isRocket == true)
{
hitPoints -= 50;//minus 10 health
}
else
{
hitPoints -= 1;
}
}
if (hitPoints <= 0)
{
///// -- Adam
// kill the ogre
aiState = AI_DEAD;
velocity = CVector(0.0, 0.0, 0.0);
///// -- Adam
player->killOgro(this);
}
static_cast<CRocket*>(collisionObject)->hitTarget = true;
}
///// -- Adam
}
}
}
void COgroEnemy::OnPrepare()
{
///// -- Adam
//float dirToPlayer; // the angle of the enemy-player vector
///// -- Adam
CVector diff; // the vector from the enemy to the player
diff.x = position.x - player->position.x;
diff.z = position.z - player->position.z;
diff.Normalize();
// find the angle in the world of the vector from the enemy to the player
// in relation the negative z-axis
dirToPlayer = RAD2DEG(diff.Angle(CVector(0,0,-1)));
///// -- Adam
if (player->position.x > position.x)
dirToPlayer *= -1;
///// -- Adam
// seed random generator
srand((unsigned int)time(NULL));
ProcessAI();
// now do Ogro prep
// set modelState based on AIstate
switch (aiState)
{
case AI_SCARED:
direction = (dirToPlayer - 90) + ((rand()%90)-45); // set the direction of the enemy
// -90 to 90 degrees
modelState = MODEL_RUN;
velocity = CVector(0.0, 0.0, 15.0);
break;
/****************************Faith Satterthwaite 12/1/2012****************************/
case AI_BACKUP:
modelState = MODEL_WALK;
velocity = CVector(0.0, 0.0, -10.0);
break;
/****************************Faith Satterthwaite 11/27/2012****************************/
case AI_ATTACK:
direction = (dirToPlayer + 90); // set the direction of the enemy
modelState = MODEL_RUN;
velocity = CVector(0.0, 0.0, 15.0);
break;
/**************************************************************************************/
case AI_UNCARING:
direction = float(rand() % 360);
if ((rand() % 4) != 0)
{
modelState = MODEL_IDLE;
velocity = CVector(0.0, 0.0, 0.0);
}
else
{
velocity = CVector(0.0, 0.0, 15.0);
modelState = MODEL_RUN;
}
break;
case AI_DEAD:
modelState = MODEL_DIE;
velocity = CVector(0.0, 0.0, 0.0);
if (nextFrame == stateStart)
{
// time to kill the monster
isDead = true;
}
break;
default:
break;
}
// do prep for MD2 Model states
CEntity::OnPrepare();
}
void COgroEnemy::Load()
{
// load model
CMD2Model::Load("models\\ogro\\tris.md2", "models\\ogro\\ogrobase.pcx");
}
void COgroEnemy::OnProcessAI()
{
// calculate distance from player
CVector diff = player->position - position;
if (aiState != AI_DEAD)
{
// if the player is close enough, the enemy should become scared
distFromPlayer = sqrt(diff.x*diff.x + diff.y*diff.y + diff.z*diff.z);
if (distFromPlayer < 100.0)
/****************************Faith Satterthwaite 11/27/2012****************************/
{
///// -- Adam
beenShot = true; //If they see you, they'll now never stop chasing you
///// -- Adam
aiState = AI_ATTACK;
if(attackPlayer == false) //Added if statement 12/1/2012
{
//If player collides (attackPlayer = false) move enemy backwards
aiState = AI_BACKUP;
if(distFromPlayer > 25)
{
//Now come at player again
attackPlayer = true;
aiState = AI_ATTACK;
}
}
}
else
{
aiState = AI_UNCARING;
///// -- Adam
if (beenShot == true)//if you've shot an enemy, they will charge you no matter how far away they are
aiState = AI_ATTACK;
///// -- Adam
}
/**************************************************************************************/
}
}
///// -- Adam
void COgroEnemy::OnDraw(CCamera *camera)
{
CEntity::OnDraw(camera);
glPushMatrix();
//x is front and back on the model
//y is left to right on the model
//z is up and down on the model
glTranslatef(0.0, 0.0, 35.0);//move it over their head
glRotatef(direction - dirToPlayer, 0.0, 0.0, 1.0);
glColor4f(1.0, 1.0, 1.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex3f(-14, 0, 1);
glVertex3f(14, 0, 1);
glVertex3f(14, 0, 4);
glVertex3f(-14, 0, 4);
glEnd();
if (hitPoints >= 50)
glColor4f(0.0, 1.0, 0.0, 1.0); // color -- green
else if (hitPoints >= 25)
glColor4f(1.0, 1.0, 0.0, 1.0); // color -- yellow
else
glColor4f(1.0, 0.0, 0.0, 1.0); // color -- red
glBegin(GL_QUADS);
glVertex3f(-14, 0, 1);
glVertex3f((GLfloat)hitPoints / (GLfloat)maxHP * 28 - 14, 0, 1);
glVertex3f((GLfloat)hitPoints / (GLfloat)maxHP * 28 - 14, 0, 4);
glVertex3f(-14, 0, 4);
glEnd();
glPopMatrix();
}
///// -- Adam