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); glUseProgram(0);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.0, 0.0, 1.0);
for (const game::Trace *trace : m_state->traces) { 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); glBegin(GL_LINE_STRIP);
for (const game::Trace::TracePoint &tp : trace->points) { for (const game::Trace::TracePoint &tp : trace->points) {
glVertex2f(tp.position.x, tp.position.y); glVertex2f(tp.position.x, tp.position.y);

View file

@ -262,6 +262,8 @@ namespace game {
advanceExplosions(dt); advanceExplosions(dt);
advanceTraceAges(dt);
advancePlayerCommands(dt); advancePlayerCommands(dt);
advancePlayerMissiles(dt); advancePlayerMissiles(dt);
@ -356,4 +358,22 @@ namespace game {
explosions.push_back(new Explosion(evt->position, evt->hit)); 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 addExplosionFromHit(const Missile::Event *evt);
void advanceTraceAges(float dt);
void advanceExplosions(float dt); void advanceExplosions(float dt);
void advancePlayerShipSpawns(float dt); void advancePlayerShipSpawns(float dt);
void advancePlayerCommands(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() Trace::~Trace()
{ {
//std::cout<<"~Trace()" << std::endl; //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) void Trace::addPointFromMissile(bool forceAdd)
{ {
fidelityCounter++; fidelityCounter++;
if (forceAdd || fidelityCounter >= 10) { if (forceAdd || fidelityCounter >= 20) {
fidelityCounter = 0; fidelityCounter = 0;
points.push_back(TracePoint(missile)); points.push_back(TracePoint(missile));
} }

View file

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