replaced vectors with lists for easier managment.

This commit is contained in:
Andreas Ortmann 2016-09-28 17:38:32 +02:00
parent b71cb5cc52
commit a5ff1bc20d
6 changed files with 72 additions and 91 deletions

View file

@ -51,8 +51,8 @@ bool Game::cycle(float dt)
float angle = 360.0 * util::randf_0_1(); float angle = 360.0 * util::randf_0_1();
float speed = 0.005; float speed = 0.005;
m_state->players[0]->addCommand(new game::SetSpeedCommand(speed)); m_state->players.back()->addCommand(new game::SetSpeedCommand(speed));
m_state->players[0]->addCommand(new game::ShootCommand(angle)); m_state->players.back()->addCommand(new game::ShootCommand(angle));
//static bool done = false; //static bool done = false;
//if (total >= 10.0 && !done) { //if (total >= 10.0 && !done) {

View file

@ -58,16 +58,17 @@ namespace game {
{ {
std::cout<<"clearing traces!!!" << std::endl; std::cout<<"clearing traces!!!" << std::endl;
size_t i = 0; std::vector<Trace*> rm;
while(i < state->traces.size()) {
Trace *trace = state->traces[i];
if (trace->missile == nullptr && trace->player == player) {
state->deleteTrace(i);
} else { for (Trace *trace : state->traces) {
i++; if (trace->missile == nullptr && trace->player == player) {
rm.push_back(trace);
} }
} }
for (Trace *trace : rm) {
state->traces.remove(trace);
}
} }
#if 0 #if 0

View file

@ -23,7 +23,7 @@ namespace game {
Missile::~Missile() Missile::~Missile()
{ {
//std::cout<<"destroyed missile" << std::endl; //std::cout<<"~Missile" << std::endl;
} }
Missile::Event Missile::advance(const game::State *state, float dt) Missile::Event Missile::advance(const game::State *state, float dt)

View file

@ -2,7 +2,7 @@
#include <string> #include <string>
#include <deque> #include <deque>
#include <vector> #include <list>
#include <glm/vec2.hpp> #include <glm/vec2.hpp>
#include <glm/vec3.hpp> #include <glm/vec3.hpp>
@ -21,7 +21,7 @@ namespace game {
float deadTimeCounter; float deadTimeCounter;
Ship *ship; Ship *ship;
std::string name; std::string name;
std::vector<Missile*> missiles; std::list<Missile*> missiles;
Player(int id) : id(id), alive(true), speed(0.01), energy(0.0), ship(nullptr), name("<unnamed>") Player(int id) : id(id), alive(true), speed(0.01), energy(0.0), ship(nullptr), name("<unnamed>")
{ {

View file

@ -18,8 +18,6 @@
#include "util.hpp" #include "util.hpp"
using namespace std;
namespace game { namespace game {
void State::init() void State::init()
{ {
@ -77,13 +75,21 @@ namespace game {
void State::quitPlayer(int playerId) void State::quitPlayer(int playerId)
{ {
(void) playerId; std::cout << playerId << " quit" << std::endl;
cout << playerId << " quit" << endl;
Player *player = playerForId(playerId);
if (player != nullptr) {
for (Missile *missile : player->missiles) {
missile->player = nullptr;
}
players.remove(player);
}
} }
void State::clear(int playerId) void State::clear(int playerId)
{ {
cout << playerId << " clear" << endl; std::cout << playerId << " clear" << std::endl;
} }
void State::setName(int playerId, std::string name) void State::setName(int playerId, std::string name)
@ -158,13 +164,7 @@ namespace game {
std::cout<<"player " << killer->id << " killed " << victim->id << std::endl; std::cout<<"player " << killer->id << " killed " << victim->id << std::endl;
// destroy ship // destroy ship
size_t i; ships.remove(victim->ship);
for (i=0; i<ships.size(); i++) {
if (ships[i] == victim->ship) {
ships.erase(ships.begin() + i);
break;
}
}
delete(victim->ship); delete(victim->ship);
victim->ship = nullptr; victim->ship = nullptr;
@ -181,11 +181,11 @@ namespace game {
void State::advancePlayerMissiles(float dt) void State::advancePlayerMissiles(float dt)
{ {
std::vector<Missile*> rm;
// advance missiles // advance missiles
for (Player *player : players) { for (Player *player : players) {
size_t i = 0; for (Missile *missile : player->missiles) {
while (i<player->missiles.size()) {
Missile *missile = player->missiles[i];
//std::cout<<"missile: " << (long unsigned int) missile << std::endl; //std::cout<<"missile: " << (long unsigned int) missile << std::endl;
const Missile::Event evt = missile->advance(this, dt); const Missile::Event evt = missile->advance(this, dt);
@ -197,9 +197,9 @@ namespace game {
} }
if (!isHit) { if (!isHit) {
i++; continue;
}
} else {
switch(evt.hit) { switch(evt.hit) {
case Hit::Planet: case Hit::Planet:
//std::cout<<"hit Planet" << std::endl; //std::cout<<"hit Planet" << std::endl;
@ -224,30 +224,30 @@ namespace game {
missile->trace->finish(); missile->trace->finish();
} }
player->missiles.erase(player->missiles.begin() + i); rm.push_back(missile);
delete(missile);
//std::cout<<std::endl;
} }
// TODO for (Missile *missile : rm) {
// add trace to the missile player->missiles.remove(missile);
delete(missile);
} }
} }
} }
void State::advanceExplosions(float dt) void State::advanceExplosions(float dt)
{ {
size_t i=0; std::vector<Explosion*> rm;
while(i < explosions.size()) {
Explosion *explosion = explosions[i]; for (Explosion *explosion : explosions) {
explosion->age += dt; explosion->age += dt;
if (explosion->age >= explosion->maxAge) { if (explosion->age >= explosion->maxAge) {
delete(explosion); rm.push_back(explosion);
explosions.erase(explosions.begin()+i); }
continue;
} }
i++; for (Explosion *explosion : rm) {
explosions.remove(explosion);
delete(explosion);
} }
} }
@ -260,7 +260,6 @@ namespace game {
advancePlayerCommands(dt); advancePlayerCommands(dt);
advancePlayerMissiles(dt); advancePlayerMissiles(dt);
} }
Player *State::playerForId(int playerId) Player *State::playerForId(int playerId)
@ -288,7 +287,6 @@ namespace game {
pos = glm::vec2(0.0, 0.0); pos = glm::vec2(0.0, 0.0);
} else { } else {
pos = glm::vec2(util::randf_0_1(), util::randf_m1_1()); pos = glm::vec2(util::randf_0_1(), util::randf_m1_1());
//glm::linearRand(-1.0, 1.0), glm::linearRand(-1.0, 1.0));
} }
for (Planet *p : planets) { for (Planet *p : planets) {
@ -332,37 +330,19 @@ namespace game {
traces.push_back(trace); traces.push_back(trace);
} }
void State::deleteTrace(size_t index) void State::deleteTrace(Trace *trace)
{ {
if (index >= traces.size()) { traces.remove(trace);
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) { if (trace->missile != nullptr) {
// delete backlink. // delete backlink.
// XXX: there's a missile without a trace now. // XXX: there's a missile without a trace now.
trace->missile->trace = nullptr; trace->missile->trace = nullptr;
} }
traces.erase(traces.begin() + index);
delete(trace); delete(trace);
} }
void State::deleteTrace(Trace *trace)
{
size_t i = 0;
while(i < traces.size()) {
if (traces[i] == trace) {
deleteTrace(i);
break;
}
}
}
void State::addExplosionFromHit(const Missile::Event *evt) void State::addExplosionFromHit(const Missile::Event *evt)
{ {
if (evt->hit == Hit::Nothing || evt->hit == Hit::BorderOfUniverse) { if (evt->hit == Hit::Nothing || evt->hit == Hit::BorderOfUniverse) {

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <vector> #include <vector>
#include <list>
#include <queue> #include <queue>
#include <string> #include <string>
@ -75,7 +76,6 @@ namespace game {
// delete traces with this command // delete traces with this command
void deleteTrace(Trace *trace); // using a pointer void deleteTrace(Trace *trace); // using a pointer
void deleteTrace(size_t index); // using an index
/*************************************************************************/ /*************************************************************************/
/* Rendering */ /* Rendering */
@ -84,10 +84,10 @@ namespace game {
// Game items which should be rendered are here: // Game items which should be rendered are here:
// (access missiles by iterating over player's missiles attribute) // (access missiles by iterating over player's missiles attribute)
std::vector<Planet*> planets; std::vector<Planet*> planets;
std::vector<Ship*> ships; std::list<Ship*> ships;
std::vector<Player*> players; std::list<Player*> players;
std::vector<Trace*> traces; std::list<Trace*> traces;
std::vector<Explosion*> explosions; std::list<Explosion*> explosions;
private: private:
/*************************************************************************/ /*************************************************************************/