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 speed = 0.005;
m_state->players[0]->addCommand(new game::SetSpeedCommand(speed));
m_state->players[0]->addCommand(new game::ShootCommand(angle));
m_state->players.back()->addCommand(new game::SetSpeedCommand(speed));
m_state->players.back()->addCommand(new game::ShootCommand(angle));
//static bool done = false;
//if (total >= 10.0 && !done) {

View file

@ -58,16 +58,17 @@ namespace game {
{
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->player == player) {
state->deleteTrace(i);
std::vector<Trace*> rm;
} else {
i++;
for (Trace *trace : state->traces) {
if (trace->missile == nullptr && trace->player == player) {
rm.push_back(trace);
}
}
for (Trace *trace : rm) {
state->traces.remove(trace);
}
}
#if 0

View file

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

View file

@ -2,7 +2,7 @@
#include <string>
#include <deque>
#include <vector>
#include <list>
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
@ -21,7 +21,7 @@ namespace game {
float deadTimeCounter;
Ship *ship;
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>")
{

View file

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

View file

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