-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemy.cpp
173 lines (146 loc) · 3.2 KB
/
Enemy.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
#include "Enemy.h"
#include "Sound.h"
Enemy::Enemy()
{
}
Enemy::~Enemy()
{
}
bool Enemy::Initialize(D3DXVECTOR3 * START)
{
// set active and health, and speed
active = true;
maxHealth = health = 5;
speed = 95;
angle = 0;
// set start position
pos = startPos = *START;
currentState = DOWN;
// set scale
D3DXMatrixScaling(&S, 3.5, 3.5, 3.5);
// set mesh
enemyMesh.Initialize("models/enemy.x");
// initialize bullets
for (int i = 0; i < 10; i++) {
EnemyBullet *temp = new EnemyBullet();
bulletList.push_back(temp);
}
for (int i = 0; i < bulletList.size(); i++)
bulletList[i]->Initialize();
return true;
}
void Enemy::Render(float deltaTime)
{
// set rotation and position and render
D3DXMatrixRotationY(&R, angle);
D3DXMatrixTranslation(&T, pos.x, pos.y, pos.z);
W = S*R*T;
Device->SetTransform(D3DTS_WORLD, &W);
enemyMesh.Render();
// render active bullets
for (int i = 0; i < bulletList.size(); i++) {
if (bulletList[i]->active)
bulletList[i]->Render(deltaTime);
}
// transform the min and max bounding coordinates for collision detection
D3DXVec3TransformCoord(&min, &enemyMesh.min, &(S*T));
D3DXVec3TransformCoord(&max, &enemyMesh.max, &(S*T));
}
void Enemy::Update(float deltaTime, D3DXVECTOR3 *playerPos)
{
// fire rate control
if (fireTime > 1) {
fireTime = 0;
canShoot = true;
}
else {
fireTime += deltaTime;
}
// check to see what state the enemy is in
float x = pos.x - playerPos->x;
float z = pos.z - playerPos->z;
if (sqrt((x*x) + (z*z)) < 200) {
currentState = PURSUE;
}
if (sqrt((x*x) + (z*z)) < 100) {
currentState = FIRE;
}
if (health <= 0)
currentState = DIE;
// state machine
switch (currentState) {
case UP:
if(pos.z <= 975)
pos.z += speed*deltaTime;
else {
nextState = DOWN;
nextAngle = 0;
currentState = TURN;
}
break;
case DOWN:
if(pos.z >= -975)
pos.z -= speed*deltaTime;
else {
nextState = UP;
nextAngle = D3DX_PI;
currentState = TURN;
}
break;
case TURN:
if (sqrt(nextAngle - angle)*(nextAngle - angle) > 0.1) {
if (angle > nextAngle)
angle -= deltaTime;
else if (angle < nextAngle)
angle += deltaTime;
}
else {
angle = nextAngle;
currentState = nextState;
}
break;
case PURSUE:
angle = atan2(x, z);
pos.x -= speed*deltaTime*sin(angle);
pos.z -= speed*deltaTime*cos(angle);
break;
case FIRE:
angle = atan2(x, z);
if (canShoot) {
SoundEngine->PlayFX(Sound::ENEMYSHOT);
CreateBullet();
canShoot = false;
}
break;
case DIE:
active = false;
break;
}
}
void Enemy::CreateBullet()
{
// find the next non-active bullet in the list and spawn it
for (int i = 0; i < bulletList.size(); i++) {
if (!bulletList[i]->active) {
bulletList[i]->Spawn(&(R*T));
break;
}
}
}
bool Enemy::CollisionDetection(D3DXVECTOR3 *MIN, D3DXVECTOR3 *MAX)
{
// check to see if object min and max are inside enemy's min and max
if ((MAX->x < max.x) && (MIN->x > min.x) && (MAX->z < max.z) && (MIN->z > min.z))
return true;
return false;
}
void Enemy::Reset() {
// reset the enemy for next game/level
pos = startPos;
active = true;
health = maxHealth;
angle = 0;
currentState = DOWN;
for (int i = 0; i < bulletList.size(); i++)
bulletList[i]->Reset();
}