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)
{
static float total = 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;
total += dt;
float spawnInterval = 0.1;
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::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;

View file

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

View file

@ -145,12 +145,9 @@ namespace game {
// TODO
// add points
//
// TODO
// message
//
// TODO
// respawn timer
}
void State::advancePlayerMissiles(float dt)
@ -166,7 +163,9 @@ namespace game {
const bool isHit = (evt.hit != Hit::Nothing);
missile->trace->addPointFromMissile(isHit); // force point if missile gets destroyed a
if (missile->trace != nullptr) {
missile->trace->addPointFromMissile(isHit); // force point if missile gets destroyed a
}
if (!isHit) {
i++;
@ -192,6 +191,10 @@ namespace game {
addExplosionFromHit(&evt);
if (missile->trace != nullptr) {
missile->trace->finish();
}
player->missiles.erase(player->missiles.begin() + i);
delete(missile);
//std::cout<<std::endl;
@ -290,15 +293,32 @@ namespace game {
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)
{
// TODO: if traces of running missile is deleted it is dangerous
size_t i = 0;
while(i < traces.size()) {
if (traces[i] == trace) {
delete(trace);
traces.erase(traces.begin()+i);
deleteTrace(i);
break;
}
}

View file

@ -68,7 +68,10 @@ namespace game {
// add a trace to the list of traces.
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 */

View file

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

View file

@ -5,6 +5,7 @@
namespace game {
class Missile;
class Player;
/*
* Trace of a missile through the space.
@ -12,7 +13,8 @@ namespace game {
*/
class Trace {
public:
Trace(const Missile *missile);
Trace(Missile *missile);
~Trace();
// Add the current position of the missile as a new point on the
// trace.
@ -39,11 +41,16 @@ namespace game {
};
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.
// when reaching a certain value the point is saved for
// optimization.
int fidelityCounter;
// age of the trace. if too old, it can be removed to save
// space/power
float age;
};
}