adding nice trace-fadeout.

This commit is contained in:
Andreas Ortmann 2016-09-29 09:32:56 +02:00
parent 1a3fd63250
commit b15cc1304d
5 changed files with 42 additions and 13 deletions

View file

@ -263,9 +263,13 @@ namespace endofthejedi {
glUseProgram(0);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.0, 0.0, 1.0);
for (const game::Trace *trace : m_state->traces) {
float fade_out = 1.0;
if (trace->missile == nullptr) {
fade_out = 1.0 - (trace->age / trace->maxAge);
}
glColor3f(0.0, 0.3/fade_out, 1.0/fade_out);
glBegin(GL_LINE_STRIP);
for (const game::Trace::TracePoint &tp : trace->points) {
glVertex2f(tp.position.x, tp.position.y);

View file

@ -262,6 +262,8 @@ namespace game {
advanceExplosions(dt);
advanceTraceAges(dt);
advancePlayerCommands(dt);
advancePlayerMissiles(dt);
@ -356,4 +358,22 @@ namespace game {
explosions.push_back(new Explosion(evt->position, evt->hit));
}
void State::advanceTraceAges(float dt)
{
std::vector<Trace*> rm;
for (Trace *trace : traces) {
if (trace->missile == nullptr) {
trace->age += dt;
if (trace->age >= trace->maxAge) {
rm.push_back(trace);
}
}
}
for (Trace *trace : rm) {
traces.remove(trace);
delete(trace);
}
}
}

View file

@ -101,6 +101,7 @@ namespace game {
void addExplosionFromHit(const Missile::Event *evt);
void advanceTraceAges(float dt);
void advanceExplosions(float dt);
void advancePlayerShipSpawns(float dt);
void advancePlayerCommands(float dt);

View file

@ -13,24 +13,25 @@ namespace game {
{
}
Trace::Trace(Missile *missile, float maxAge)
: missile(missile)
, player(missile->player)
, fidelityCounter(0)
, age(0.0)
, maxAge(maxAge)
{
points.push_back(TracePoint(missile));
}
Trace::~Trace()
{
//std::cout<<"~Trace()" << std::endl;
}
Trace::Trace(Missile *missile)
: missile(missile)
, player(missile->player)
, fidelityCounter(0)
, age(0.0)
{
points.push_back(TracePoint(missile));
}
void Trace::addPointFromMissile(bool forceAdd)
{
fidelityCounter++;
if (forceAdd || fidelityCounter >= 10) {
if (forceAdd || fidelityCounter >= 20) {
fidelityCounter = 0;
points.push_back(TracePoint(missile));
}

View file

@ -13,7 +13,7 @@ namespace game {
*/
class Trace {
public:
Trace(Missile *missile);
Trace(Missile *missile, float maxAge=5.0);
~Trace();
// Add the current position of the missile as a new point on the
@ -51,6 +51,9 @@ namespace game {
// age of the trace. if too old, it can be removed to save
// space/power
// ageing starts when the trace is finished.
float age;
float maxAge;
};
}