From 8ddf73f3bde3c7b43a3f7f408c2baf88d43e8618 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Fri, 5 Jun 2009 12:49:57 +0100 Subject: [PATCH] Replace asteroid array with vector --- src/Asteroid.cpp | 6 +++--- src/Asteroid.hpp | 6 +++--- src/Game.cpp | 32 ++++++++++++++++++++------------ src/Game.hpp | 5 +++-- 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/Asteroid.cpp b/src/Asteroid.cpp index bd06146..81f506f 100644 --- a/src/Asteroid.cpp +++ b/src/Asteroid.cpp @@ -163,7 +163,7 @@ void Asteroid::GenerateDisplayList(int texidx) // // Returns the line segment which defines the top of the given Poly. // -LineSegment Asteroid::GetUpBoundary(int poly) +LineSegment Asteroid::GetUpBoundary(int poly) const { return LineSegment (xpos*OBJ_GRID_SIZE + uppolys[poly].points[1].x, @@ -175,7 +175,7 @@ LineSegment Asteroid::GetUpBoundary(int poly) // // Returns the line segment which defines the bottom of the given Poly. // -LineSegment Asteroid::GetDownBoundary(int poly) +LineSegment Asteroid::GetDownBoundary(int poly) const { return LineSegment (xpos*OBJ_GRID_SIZE + downpolys[poly].points[1].x, @@ -194,7 +194,7 @@ void Asteroid::Draw(int viewadjust_x, int viewadjust_y) const glCallList(display_list); } -bool Asteroid::CheckCollision(Ship& ship) +bool Asteroid::CheckCollision(const Ship& ship) const { // Look at polys for (int k = 0; k < GetWidth(); k++) { diff --git a/src/Asteroid.hpp b/src/Asteroid.hpp index b460499..df43b41 100644 --- a/src/Asteroid.hpp +++ b/src/Asteroid.hpp @@ -31,9 +31,9 @@ public: void ConstructAsteroid(int x, int y, int width, int surftex); void Draw(int viewadjust_x, int viewadjust_y) const; - bool CheckCollision(Ship& ship); - LineSegment GetUpBoundary(int poly); - LineSegment GetDownBoundary(int poly); + bool CheckCollision(const Ship& ship) const; + LineSegment GetUpBoundary(int poly) const; + LineSegment GetDownBoundary(int poly) const; static const int MAX_ASTEROID_WIDTH = 15; diff --git a/src/Game.cpp b/src/Game.cpp index 9b91180..7259f6f 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -234,9 +234,12 @@ void Game::Process() // Check for collisions with asteroids LineSegment l1, l2; - for (int i = 0; i < asteroidcount; i++) { - if (asteroids[i].ObjectInScreen(&viewport)) { - if (asteroids[i].CheckCollision(ship)) { + for (AsteroidListIt it = asteroids_.begin(); + it != asteroids_.end(); ++it) { + const Asteroid& a = *it; + + if (a.ObjectInScreen(&viewport)) { + if (a.CheckCollision(ship)) { // Crashed if (state == gsInGame) { // Destroy the ship @@ -448,12 +451,14 @@ void Game::MakeKeys() void Game::MakeAsteroids(int surftex) { - asteroidcount = 2 + level*2 + rand()%(level+3); - if (asteroidcount > MAX_ASTEROIDS) - asteroidcount = MAX_ASTEROIDS; - cout << " Asteroids: " << asteroidcount << endl; + int asteroidCount = 2 + level*2 + rand()%(level+3); + if (asteroidCount > MAX_ASTEROIDS) + asteroidCount = MAX_ASTEROIDS; + cout << " Asteroids: " << asteroidCount << endl; + + asteroids_.clear(); - for (int i = 0; i < asteroidcount; i++) { + for (int i = 0; i < asteroidCount; i++) { // Allocate space, check for timeout int x, y, width = rand() % (Asteroid::MAX_ASTEROID_WIDTH - 4) + 4; if (!objgrid.AllocFreeSpace(x, y, width, 4)) { @@ -462,7 +467,9 @@ void Game::MakeAsteroids(int surftex) } // Generate the asteroid - asteroids[i].ConstructAsteroid(x, y, width, surftex); + Asteroid a; + a.ConstructAsteroid(x, y, width, surftex); + asteroids_.push_back(a); } } @@ -619,9 +626,10 @@ void Game::Display() surface.Display(); // Draw the asteroids - for (int i = 0; i < asteroidcount; i++) { - if (asteroids[i].ObjectInScreen(&viewport)) - asteroids[i].Draw(viewport.GetXAdjust(), viewport.GetYAdjust()); + for (AsteroidListIt it = asteroids_.begin(); + it != asteroids_.end(); ++it) { + if ((*it).ObjectInScreen(&viewport)) + (*it).Draw(viewport.GetXAdjust(), viewport.GetYAdjust()); } // Draw the keys diff --git a/src/Game.hpp b/src/Game.hpp index edbef7a..6b19cd9 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -157,8 +157,9 @@ private: // Asteroids static const int MAX_ASTEROIDS = 50; - Asteroid asteroids[MAX_ASTEROIDS]; - int asteroidcount; + typedef vector AsteroidList; + typedef AsteroidList::iterator AsteroidListIt; + AsteroidList asteroids_; // Electric gate things static const int MAX_GATEWAYS = 4; -- 2.39.2