fiexd cleartraces command.

This commit is contained in:
Andreas Ortmann 2016-09-27 23:07:11 +02:00
parent 67f43de22c
commit eaba518ce4
6 changed files with 81 additions and 15 deletions

View file

@ -24,8 +24,20 @@ Game::Game()
bool Game::cycle(float dt) bool Game::cycle(float dt)
{ {
static float total = 0.0;
static float acc = 0.0; static float acc = 0.0;
//if (total == 0.0) {
// float speed = 0.005;
// m_state->players[0]->addCommand(new game::SetSpeedCommand(speed));
// for (int i=0; i<100; i++) {
// float a = 2.0 * M_PI * util::randf_0_1();
// m_state->players[0]->addCommand(new game::ShootCommand(a));
// }
//}
acc += dt; acc += dt;
total += dt;
float spawnInterval = 0.1; float spawnInterval = 0.1;
while(acc > spawnInterval) { while(acc > spawnInterval) {
@ -36,6 +48,13 @@ bool Game::cycle(float dt)
m_state->players[0]->addCommand(new game::SetSpeedCommand(speed)); m_state->players[0]->addCommand(new game::SetSpeedCommand(speed));
m_state->players[0]->addCommand(new game::ShootCommand(a)); m_state->players[0]->addCommand(new game::ShootCommand(a));
//static bool done = false;
//if (total >= 10.0 && !done) {
// done = true;
// m_state->players[0]->addCommand(new game::ClearTracesCommand());
//}
} }
//std::cout<<"adding dt: " << dt << std::endl; //std::cout<<"adding dt: " << dt << std::endl;

View file

@ -2,6 +2,8 @@
#include "trace.hpp" #include "trace.hpp"
#include <iostream>
namespace game { namespace game {
void ShootCommand::apply(Player *player, State *state) const void ShootCommand::apply(Player *player, State *state) const
{ {
@ -51,11 +53,14 @@ namespace game {
void ClearTracesCommand::apply(Player *player, State *state) const void ClearTracesCommand::apply(Player *player, State *state) const
{ {
std::cout<<"clearing traces!!!" << std::endl;
size_t i = 0; size_t i = 0;
while(i < state->traces.size()) { while(i < state->traces.size()) {
Trace *trace = state->traces[i]; Trace *trace = state->traces[i];
if (trace->missile == nullptr && trace->missile->player == player) { if (trace->missile == nullptr && trace->player == player) {
state->deleteTrace(trace); state->deleteTrace(i);
} else { } else {
i++; i++;
} }

View file

@ -145,12 +145,9 @@ namespace game {
// TODO // TODO
// add points // add points
//
// TODO // TODO
// message // message
//
// TODO
// respawn timer
} }
void State::advancePlayerMissiles(float dt) void State::advancePlayerMissiles(float dt)
@ -166,7 +163,9 @@ namespace game {
const bool isHit = (evt.hit != Hit::Nothing); const bool isHit = (evt.hit != Hit::Nothing);
if (missile->trace != nullptr) {
missile->trace->addPointFromMissile(isHit); // force point if missile gets destroyed a missile->trace->addPointFromMissile(isHit); // force point if missile gets destroyed a
}
if (!isHit) { if (!isHit) {
i++; i++;
@ -192,6 +191,10 @@ namespace game {
addExplosionFromHit(&evt); addExplosionFromHit(&evt);
if (missile->trace != nullptr) {
missile->trace->finish();
}
player->missiles.erase(player->missiles.begin() + i); player->missiles.erase(player->missiles.begin() + i);
delete(missile); delete(missile);
//std::cout<<std::endl; //std::cout<<std::endl;
@ -290,15 +293,32 @@ namespace game {
traces.push_back(trace); traces.push_back(trace);
} }
void State::deleteTrace(size_t index)
{
if (index >= traces.size()) {
std::cerr << "can't find trace with invalid index " << index << std::endl;
return;
}
Trace *trace = traces[index];
//std::cout<<"removing a trace" << std::endl;
if (trace->missile != nullptr) {
// delete backlink.
// XXX: there's a missile without a trace now.
trace->missile->trace = nullptr;
}
traces.erase(traces.begin() + index);
delete(trace);
}
void State::deleteTrace(Trace *trace) void State::deleteTrace(Trace *trace)
{ {
// TODO: if traces of running missile is deleted it is dangerous
size_t i = 0; size_t i = 0;
while(i < traces.size()) { while(i < traces.size()) {
if (traces[i] == trace) { if (traces[i] == trace) {
delete(trace); deleteTrace(i);
traces.erase(traces.begin()+i);
break; break;
} }
} }

View file

@ -68,7 +68,10 @@ namespace game {
// add a trace to the list of traces. // add a trace to the list of traces.
void addTrace(Trace *trace); void addTrace(Trace *trace);
void deleteTrace(Trace *trace);
// delete traces with this command
void deleteTrace(Trace *trace); // using a pointer
void deleteTrace(size_t index); // using an index
/*************************************************************************/ /*************************************************************************/
/* Rendering */ /* Rendering */

View file

@ -4,6 +4,8 @@
#include "missile.hpp" #include "missile.hpp"
#include <iostream>
namespace game { namespace game {
Trace::TracePoint::TracePoint(const Missile *missile) Trace::TracePoint::TracePoint(const Missile *missile)
: position(missile->position) : position(missile->position)
@ -11,7 +13,16 @@ namespace game {
{ {
} }
Trace::Trace(const Missile *missile) : missile(missile), fidelityCounter(0) 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)); points.push_back(TracePoint(missile));
} }
@ -27,6 +38,7 @@ namespace game {
void Trace::finish() void Trace::finish()
{ {
//std::cout<<"trace finished now!" << std::endl;
missile = nullptr; missile = nullptr;
} }
} }

View file

@ -5,6 +5,7 @@
namespace game { namespace game {
class Missile; class Missile;
class Player;
/* /*
* Trace of a missile through the space. * Trace of a missile through the space.
@ -12,7 +13,8 @@ namespace game {
*/ */
class Trace { class Trace {
public: public:
Trace(const Missile *missile); Trace(Missile *missile);
~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
// trace. // trace.
@ -39,11 +41,16 @@ namespace game {
}; };
std::vector<TracePoint> points; std::vector<TracePoint> points;
const Missile *missile; // missile which creates this path. Missile *missile; // missile which creates this path.
Player *player;
// counter which is incremented each time addPointFromMissile() is called. // counter which is incremented each time addPointFromMissile() is called.
// when reaching a certain value the point is saved for // when reaching a certain value the point is saved for
// optimization. // optimization.
int fidelityCounter; int fidelityCounter;
// age of the trace. if too old, it can be removed to save
// space/power
float age;
}; };
} }