/
ShockParticle.cpp
119 lines (94 loc) · 2.95 KB
/
ShockParticle.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
#include "ShockParticle.h"
#include "GlobalConstants.h"
#include "Engine.h"
ShockParticle::ShockParticle(const Vector &worldPosition,
const Vector &position) {
Engine::RandomNumberGenerator *rng =
Engine::Core::getInstance()->getRandomNumberGenerator();
this->worldPosition = worldPosition;
this->pos = position;
pos.z += rng->randomReal(-0.005, 0.005);
int direction = rng->randomInt(0, 7);
this->vel =
Vector(-std::sin(direction * M_PI / 4.0),
std::cos(direction * M_PI / 4.0),
0.0);
this->vel = this->vel.mult(6.0 / 1000.0);
this->acc = Vector(0.0, 0.0, 0.0);
this->size = 0.0;
this->texture = 3;
this->rotation = 0.0;
this->lifetime = 0.0;
this->duration = 1000.0;
this->turnCounter = 0.0;
this->turnInterval = 250.0;
}
ShockParticle::~ShockParticle() {
}
double ShockParticle::getOpacity() {
double alpha = 1.0 - (lifetime / duration);
return alpha;
}
Vector ShockParticle::getWorldPosition() {
return worldPosition;
}
Vector ShockParticle::getPosition(double edt) {
Vector eVel = vel.translate(acc.mult(edt));
Vector ePos = pos.translate(vel.mult(edt));
return ePos;
}
double ShockParticle::getSize() {
double currentSize = std::sin(lifetime * SHAKE_SPEED);
if (currentSize > 0.0) {
currentSize = 2.0;
} else {
currentSize = 1.0;
}
return currentSize;
}
double ShockParticle::getRotation() {
return rotation;
}
int ShockParticle::getTexture() {
return texture;
}
bool ShockParticle::isAlive() {
bool alive = (lifetime < duration);
return alive;
}
void ShockParticle::update(double dt) {
if (isAlive()) {
turnCounter += dt;
if (turnCounter > turnInterval) {
turnCounter -= turnInterval;
Engine::RandomNumberGenerator *rng =
Engine::Core::getInstance()->getRandomNumberGenerator();
int direction = rng->randomInt(0, 1);
if (direction == 0) {
vel =
Vector(std::cos(M_PI / 4.0) * vel.x -
std::sin(M_PI / 4.0) * vel.y,
std::sin(M_PI / 4.0) * vel.x +
std::cos(M_PI / 4.0) * vel.y,
vel.z);
} else if (direction == 1) {
vel =
Vector(std::cos(-M_PI / 4.0) * vel.x -
std::sin(-M_PI / 4.0) * vel.y,
std::sin(-M_PI / 4.0) * vel.x +
std::cos(-M_PI / 4.0) * vel.y,
vel.z);
}
vel = vel.mult(0.5);
}
vel = vel.translate(acc.mult(dt));
pos = pos.translate(vel.mult(dt));
lifetime += dt;
if (lifetime > duration) {
lifetime = duration;
}
}
}
void ShockParticle::setBlendMode() {
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
}