From eaba518ce4ef0797419ed7c70128bebf4a5e392d Mon Sep 17 00:00:00 2001 From: Andreas Ortmann Date: Tue, 27 Sep 2016 23:07:11 +0200 Subject: [PATCH] fiexd cleartraces command. --- game/game.cpp | 19 +++++++++++++++++++ game/state/commands.cpp | 9 +++++++-- game/state/state.cpp | 38 +++++++++++++++++++++++++++++--------- game/state/state.hpp | 5 ++++- game/state/trace.cpp | 14 +++++++++++++- game/state/trace.hpp | 11 +++++++++-- 6 files changed, 81 insertions(+), 15 deletions(-) diff --git a/game/game.cpp b/game/game.cpp index a6b96d9..128cdca 100644 --- a/game/game.cpp +++ b/game/game.cpp @@ -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; diff --git a/game/state/commands.cpp b/game/state/commands.cpp index 2b82f27..5dac8e0 100644 --- a/game/state/commands.cpp +++ b/game/state/commands.cpp @@ -2,6 +2,8 @@ #include "trace.hpp" +#include + 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++; } diff --git a/game/state/state.cpp b/game/state/state.cpp index 71572e6..e76e23e 100644 --- a/game/state/state.cpp +++ b/game/state/state.cpp @@ -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<= 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; } } diff --git a/game/state/state.hpp b/game/state/state.hpp index 42d0659..7128966 100644 --- a/game/state/state.hpp +++ b/game/state/state.hpp @@ -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 */ diff --git a/game/state/trace.cpp b/game/state/trace.cpp index 392076e..0b51695 100644 --- a/game/state/trace.cpp +++ b/game/state/trace.cpp @@ -4,6 +4,8 @@ #include "missile.hpp" +#include + 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; } } diff --git a/game/state/trace.hpp b/game/state/trace.hpp index 7af85dc..2b4f4f0 100644 --- a/game/state/trace.hpp +++ b/game/state/trace.hpp @@ -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 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; }; }