-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplosion.cpp
165 lines (132 loc) · 6.09 KB
/
explosion.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
// -----------------------------------------------------------------
// 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
// -----------------------------------------------------------------
/****************************************************************************
Particles.h Particle and particle system base classes.
Author : Dave Astle
Date : 2/1/2001
Written for OpenGL Game Programming
*****************************************************************************/
/********************************* Includes *********************************/
#include "explosion.h"
#include <time.h>
/*****************************************************************************
CExplosion::Constructor
Nothing to do
*****************************************************************************/
CExplosion::CExplosion(int numParticles, CVector origin, float spread, GLuint texture)
: m_texture(texture), m_spread(spread), CParticleSystem(numParticles, origin)
{
srand((unsigned)time(NULL));
CParticleSystem::InitializeSystem();
Emit(numParticles);
} // end CExplosion::Constructor
/*****************************************************************************
CExplosion::InitializeParticle()
Sets the initial particle properties for the explosion
*****************************************************************************/
void CExplosion::InitializeParticle(int index)
{
// start the particle at the sky at a random location in the emission zone
m_particleList[index].m_pos.x = m_origin.x + FRAND * m_spread;
m_particleList[index].m_pos.y = m_origin.y + FRAND * m_spread;
m_particleList[index].m_pos.z = m_origin.z + FRAND * m_spread;
// set the size of the particle
m_particleList[index].m_size = PARTICLE_SIZE + FRAND * SIZE_VARIATION;
// give the particle a random velocity
m_particleList[index].m_velocity.x = PARTICLE_VELOCITY.x + FRAND * VELOCITY_VARIATION.x;
m_particleList[index].m_velocity.y = PARTICLE_VELOCITY.y + FRAND * VELOCITY_VARIATION.y;
m_particleList[index].m_velocity.z = PARTICLE_VELOCITY.z + FRAND * VELOCITY_VARIATION.z;
m_particleList[index].m_acceleration = PARTICLE_ACCELERATION;
m_particleList[index].m_color[0] = 1.0;
m_particleList[index].m_color[1] = 1.0;
m_particleList[index].m_color[2] = 0.4f;
m_particleList[index].m_color[3] = 1.0;
m_particleList[index].m_energy = static_cast<float>(0.5 + FRAND/2.0);
m_particleList[index].m_colorDelta[0] = 0.0;
m_particleList[index].m_colorDelta[1] = 0.0;
m_particleList[index].m_colorDelta[2] = 0.0;
m_particleList[index].m_colorDelta[3] = static_cast<float>(-1.0/m_particleList[index].m_energy);
// m_particleList[index].m_sizeDelta = -m_particleList[index].m_size/m_particleList[index].m_energy;
m_particleList[index].m_sizeDelta = 8.0;
} // end CExplosion::InitializeParticle
/*****************************************************************************
CExplosion::Update
Update the existing particles, killing them and creating new ones as needed
*****************************************************************************/
void CExplosion::Update(float elapsedTime)
{
for (int i = 0; i < m_numParticles; )
{
// update the particle's position based on the elapsed time and velocity
m_particleList[i].m_pos = m_particleList[i].m_pos + m_particleList[i].m_velocity * elapsedTime;
m_particleList[i].m_velocity = m_particleList[i].m_velocity + m_particleList[i].m_acceleration * elapsedTime;
m_particleList[i].m_energy -= elapsedTime;
m_particleList[i].m_size += m_particleList[i].m_sizeDelta * elapsedTime;
m_particleList[i].m_color[3] += m_particleList[i].m_colorDelta[3] * elapsedTime;
m_particleList[i].m_color[1] += m_particleList[i].m_colorDelta[1] * elapsedTime;
// if the particle has hit the ground plane, kill it
if (m_particleList[i].m_energy <= 0.0)
{
// move the last particle to the current positon, and decrease the count
m_particleList[i] = m_particleList[--m_numParticles];
}
else
{
++i;
}
}
} // end CExplosion::Update()
/*****************************************************************************
CExplosion::Render()
Draw the snowflake particles as textured quads
*****************************************************************************/
void CExplosion::Render()
{
float viewMatrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, viewMatrix);
CVector right(viewMatrix[0], viewMatrix[4], viewMatrix[8]);
CVector up(viewMatrix[1], viewMatrix[5], viewMatrix[9]);
// glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
GLfloat size;
CVector pos;
glBegin(GL_QUADS);
for (int i = 0; i < m_numParticles; ++i)
{
size = m_particleList[i].m_size/2;
pos = m_particleList[i].m_pos;
glColor4fv(m_particleList[i].m_color);
glTexCoord2f(0.0, 0.0); glVertex3fv((pos + (right + up) * - size).v);
glTexCoord2f(1.0, 0.0); glVertex3fv((pos + (right - up) * size).v);
glTexCoord2f(1.0, 1.0); glVertex3fv((pos + (right + up) * size).v);
glTexCoord2f(0.0, 1.0); glVertex3fv((pos + (up - right) * size).v);
}
glEnd();
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glDepthMask(GL_TRUE);
// glEnable(GL_DEPTH_TEST);
} // end CExplosion::Update