Refactor the electric gates
authornick <nick@a97b1542-0b21-0410-a459-e47997c36f34>
Sat, 15 Mar 2008 23:35:47 +0000 (23:35 +0000)
committernick <nick@a97b1542-0b21-0410-a459-e47997c36f34>
Sat, 15 Mar 2008 23:35:47 +0000 (23:35 +0000)
git-svn-id: http://svn.nickg.me.uk/work/lander/trunk@311 a97b1542-0b21-0410-a459-e47997c36f34

src/ElectricGate.cpp [new file with mode: 0644]
src/ElectricGate.hpp [new file with mode: 0644]
src/Game.cpp
src/Lander.hpp
src/Makefile.am

diff --git a/src/ElectricGate.cpp b/src/ElectricGate.cpp
new file mode 100644 (file)
index 0000000..012ad8d
--- /dev/null
@@ -0,0 +1,145 @@
+/*  ElectricGate.cpp -- Electric gateway thingys.
+ *  Copyright (C) 2008  Nick Gasson
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "ElectricGate.hpp"
+#include "DataFile.hpp"
+
+extern DataFile *g_pData;
+
+GLuint ElectricGate::uGatewayTexture = 0;
+
+ElectricGate::ElectricGate(Viewport *v, int length, bool vertical, int x, int y)
+   : length(length), vertical(vertical), viewport(v)
+{
+   xpos = x;
+   ypos = y;
+   
+   icon.width = OBJ_GRID_SIZE;
+   icon.height = OBJ_GRID_SIZE;
+   icon.uTexture = uGatewayTexture;
+   timer = rand() % 70 + 10;
+}
+
+void ElectricGate::Load()
+{
+   OpenGL &opengl = OpenGL::GetInstance();
+   uGatewayTexture = opengl.LoadTextureAlpha(g_pData, "Gateway.bmp");
+}
+
+bool ElectricGate::CheckCollision(Ship &ship)
+{
+   int dx = vertical ? 0 : length;
+   int dy = vertical ? length : 0;
+   if (timer > GATEWAY_ACTIVE) {
+      bool collide1 = ship.BoxCollision
+         (xpos*OBJ_GRID_SIZE,
+          ypos*OBJ_GRID_SIZE + OBJ_GRID_TOP,
+          OBJ_GRID_SIZE,
+          OBJ_GRID_SIZE);
+                       
+      bool collide2 = ship.BoxCollision
+         ((xpos + dx)*OBJ_GRID_SIZE,
+          (ypos + dy)*OBJ_GRID_SIZE + OBJ_GRID_TOP,
+          OBJ_GRID_SIZE,
+          OBJ_GRID_SIZE);
+               
+      return collide1 || collide2;
+   }
+   else {
+      return ship.BoxCollision
+         (xpos*OBJ_GRID_SIZE, 
+          ypos*OBJ_GRID_SIZE + OBJ_GRID_TOP,
+          (dx + 1)*OBJ_GRID_SIZE,
+          (dy + 1)*OBJ_GRID_SIZE); 
+   }
+}
+
+void ElectricGate::Draw()
+{
+   OpenGL &opengl = OpenGL::GetInstance();
+   
+   // Draw first sphere
+   icon.x = xpos*OBJ_GRID_SIZE - viewport->GetXAdjust();
+   icon.y = ypos*OBJ_GRID_SIZE + OBJ_GRID_TOP - viewport->GetYAdjust();
+   opengl.Draw(&icon);
+
+   // Draw second sphere
+   if (vertical) {
+      icon.x = xpos*OBJ_GRID_SIZE - viewport->GetXAdjust();
+      icon.y = (ypos+length)*OBJ_GRID_SIZE + OBJ_GRID_TOP - viewport->GetYAdjust();
+   }
+   else {
+      icon.x = (xpos+length)*OBJ_GRID_SIZE - viewport->GetXAdjust();
+      icon.y = ypos*OBJ_GRID_SIZE + OBJ_GRID_TOP - viewport->GetYAdjust();
+   }
+   opengl.Draw(&icon);
+
+   // Draw the electricity stuff
+   if (--timer < GATEWAY_ACTIVE) {
+      float r, g, b; 
+      int x, y, deviation;
+      
+      opengl.DisableBlending();
+      opengl.DisableTexture();
+      
+      for (int j = 0; j < 10; j++) {
+         deviation = 0;
+         for (int k = 0; k < length; k++) {
+            glLoadIdentity();
+            glBegin(GL_LINE_STRIP);
+            r = 0.0f + (float)(rand()%5)/10.0f;
+            g = 0.0f + (float)(rand()%5)/10.0f;
+            b = 1.0f - (float)(rand()%5)/10.0f;
+            
+            glColor3f(r, g, b);
+            if (vertical) {
+               x = xpos*OBJ_GRID_SIZE + 16 + deviation - viewport->GetXAdjust();
+               y = (ypos+k)*OBJ_GRID_SIZE + OBJ_GRID_TOP + 16 - viewport->GetYAdjust();
+               glVertex2i(x, y);
+               if (k == length-1)
+                  deviation = 0;
+               else
+                  deviation += rand()%20 - 10;
+               x = xpos*OBJ_GRID_SIZE + 16 + deviation - viewport->GetXAdjust();
+               y += OBJ_GRID_SIZE;
+               glVertex2i(x, y);
+            }
+            else {
+               x = (xpos+k)*OBJ_GRID_SIZE + 16 - viewport->GetXAdjust();
+               y = ypos*OBJ_GRID_SIZE + OBJ_GRID_TOP + 16 + deviation
+                  - viewport->GetYAdjust();
+               glVertex2i(x, y);
+               if (k == length-1)
+                  deviation = 0;
+               else
+                  deviation += rand()%20 - 10;
+               y = ypos*OBJ_GRID_SIZE + OBJ_GRID_TOP + 16 + deviation
+                  - viewport->GetYAdjust();
+               x += OBJ_GRID_SIZE;
+               glVertex2i(x, y);
+            }
+            glEnd();
+         }
+      }
+      
+      // Reset timer 
+      if (timer < 0)
+         timer = 100;
+   }
+}
+
+
diff --git a/src/ElectricGate.hpp b/src/ElectricGate.hpp
new file mode 100644 (file)
index 0000000..bf365a7
--- /dev/null
@@ -0,0 +1,46 @@
+/*  ElectricGate.hpp -- Electric gateway thingys.
+ *  Copyright (C) 2008  Nick Gasson
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef INC_ELECTRICGATE_HPP
+#define INC_ELECTRICGATE_HPP
+
+#include "ObjectGrid.hpp"
+#include "OpenGL.hpp"
+#include "Ship.hpp"
+#include "Viewport.hpp"
+
+class ElectricGate : public StaticObject {
+public:
+   ElectricGate(Viewport *v, int length, bool vertical, int x, int y);
+
+   static void Load();
+
+   bool CheckCollision(Ship &ship);
+   void Draw();
+   
+private:
+   static const int GATEWAY_ACTIVE = 30;
+   
+   int length, timer;
+   bool vertical;
+   TextureQuad icon;
+   Viewport *viewport;
+
+   static GLuint uGatewayTexture;
+};
+
+#endif
index 222f4f3284fd4c895583013a7e2b27f13963f4f4..17394929109f2574aa325aab6fb5ee46672d659a 100644 (file)
@@ -83,7 +83,6 @@ void Game::Load()
       uGreenArrow = opengl.LoadTextureAlpha(g_pData, "GreenArrow.bmp");
       uYellowArrow = opengl.LoadTextureAlpha(g_pData, "YellowArrow.bmp");
       uPinkArrow = opengl.LoadTextureAlpha(g_pData, "PinkArrow.bmp");
-      uGatewayTexture = opengl.LoadTextureAlpha(g_pData, "Gateway.bmp");
       uFuelMeterTexture = opengl.LoadTextureAlpha(g_pData, "FuelMeter.bmp");
       uFuelBarTexture = opengl.LoadTextureAlpha(g_pData, "FuelBar.bmp");
       uShipSmallTexture = opengl.LoadTextureAlpha(g_pData, "ShipSmall.bmp");
@@ -112,6 +111,7 @@ void Game::Load()
       LandingPad::Load();
       Surface::Load();
       Mine::Load();
+      ElectricGate::Load();
       
       hasloaded = true;
    }
@@ -196,7 +196,6 @@ void Game::NewGame()
    StartLevel(level);
 }
 
-/* Processes events */
 void Game::Process()
 {
    Input &input = Input::GetInstance();
@@ -348,74 +347,20 @@ void Game::Process()
       }
 
    // Check for collision with gateways
-   for (int i = 0; i < gatewaycount; i++)
-      {
-         int dx = gateways[i].vertical ? 0 : gateways[i].length;
-         int dy = gateways[i].vertical ? gateways[i].length : 0;
-         if (gateways[i].timer > GATEWAY_ACTIVE)
-            {
-               bool collide1 = ship.BoxCollision
-                  (
-                   gateways[i].xpos*ObjectGrid::OBJ_GRID_SIZE,
-                   gateways[i].ypos*ObjectGrid::OBJ_GRID_SIZE + ObjectGrid::OBJ_GRID_TOP,
-                   ObjectGrid::OBJ_GRID_SIZE,
-                   ObjectGrid::OBJ_GRID_SIZE);
-                       
-               bool collide2 = ship.BoxCollision
-                  (
-                   (gateways[i].xpos + dx)*ObjectGrid::OBJ_GRID_SIZE,
-                   (gateways[i].ypos + dy)*ObjectGrid::OBJ_GRID_SIZE + ObjectGrid::OBJ_GRID_TOP,
-                   ObjectGrid::OBJ_GRID_SIZE,
-                   ObjectGrid::OBJ_GRID_SIZE  );
-               
-               if (collide1 || collide2)
-                  {
-                     if (state == gsInGame)
-                        {
-                           // Destroy the ship
-                           ExplodeShip();
-                           ship.Bounce();
-                        }
-                     else if (state == gsExplode)
-                        {
-                           ship.Bounce();
-
-                           // See if we need to stop the madness
-                           if (state == gsExplode && -ship.GetYSpeed() < 0.05f)
-                              {
-                                 state = gsDeathWait; 
-                                 death_timeout = DEATH_TIMEOUT;
-                              }
-                        }
-                  }
-            }
-         else
-            {
-               bool collide = ship.BoxCollision
-                  (
-                   gateways[i].xpos*ObjectGrid::OBJ_GRID_SIZE, 
-                   gateways[i].ypos*ObjectGrid::OBJ_GRID_SIZE + ObjectGrid::OBJ_GRID_TOP,
-                   (dx + 1)*ObjectGrid::OBJ_GRID_SIZE,
-                   (dy + 1)*ObjectGrid::OBJ_GRID_SIZE
-                   ); 
-               
-               if (collide)
-                  {
-                     if (state == gsInGame)
-                        {
-                           // Destroy the ship
-                           ExplodeShip();
-                           ship.Bounce();
-                        }
-                     else if (state == gsExplode)
-                        {
-                           // Destroy the ship anyway
-                           state = gsDeathWait; 
-                           death_timeout = DEATH_TIMEOUT;
-                        }
-                  }
-            }
+   for (ElectricGateListIt it = gateways.begin(); it != gateways.end(); ++it) {
+      if ((*it).CheckCollision(ship)) {
+         if (state == gsInGame) {
+            // Destroy the ship
+            ExplodeShip();
+            ship.Bounce();
+         }
+         else if (state == gsExplode) {
+            // Destroy the ship anyway
+            state = gsDeathWait; 
+            death_timeout = DEATH_TIMEOUT;
+         }
       }
+   }
 
    // Check for collisions with mines
    for (MineListIt it = mines.begin(); it != mines.end(); ++it)
@@ -642,7 +587,8 @@ void Game::StartLevel(int level)
    int surftex = rand() % Surface::NUM_SURF_TEX;
    surface.Generate(surftex, pads);
     
-   // Create the keys (must be created first because no success check is made on AllocFreeSpace call)
+   // Create the keys (must be created first because no success check
+   // is made on AllocFreeSpace call)
    nKeys = (level / 2) + (level % 2);
    if (nKeys > MAX_KEYS)
       nKeys = MAX_KEYS;
@@ -715,7 +661,6 @@ void Game::StartLevel(int level)
          if (!objgrid.AllocFreeSpace(x, y, width, 4))
             {
                // Failed to allocate space so don't make any more asteroids
-               asteroidcount = i + 1;
                break;
             }
 
@@ -724,36 +669,33 @@ void Game::StartLevel(int level)
       }
 
    // Create gateways
-   gatewaycount = level/2 + rand()%(level);
+   int gatewaycount = level/2 + rand()%(level);
    if (gatewaycount > MAX_GATEWAYS)
       gatewaycount = MAX_GATEWAYS;
    for (i = 0; i < gatewaycount; i++)
       {
          // Allocate space for gateway
-         gateways[i].length = rand()%(MAX_GATEWAY_LENGTH-3) + 3;
+         int length = rand()%(MAX_GATEWAY_LENGTH-3) + 3;
+         bool vertical;
          switch(rand() % 2)
             {
-            case 0: gateways[i].vertical = true; break;
-            case 1: gateways[i].vertical = false; break;
+            case 0: vertical = true; break;
+            case 1: vertical = false; break;
             }
                
          bool result;
-         if (gateways[i].vertical)
-            result = objgrid.AllocFreeSpace(gateways[i].xpos, gateways[i].ypos, 1, gateways[i].length+1);
+         int xpos, ypos;
+         if (vertical)
+            result = objgrid.AllocFreeSpace(xpos, ypos, 1, length+1);
          else
-            result = objgrid.AllocFreeSpace(gateways[i].xpos, gateways[i].ypos, gateways[i].length+1, 1);
+            result = objgrid.AllocFreeSpace(xpos, ypos, length+1, 1);
          if (!result)
             {
                // Failed to allocate space so don't make any more gateways
-               gatewaycount = i + 1;
                break;
             }
 
-         // Set texture, etc.
-         gateways[i].icon.width = ObjectGrid::OBJ_GRID_SIZE;
-         gateways[i].icon.height = ObjectGrid::OBJ_GRID_SIZE;
-         gateways[i].icon.uTexture = uGatewayTexture;
-         gateways[i].timer = rand() % 70 + 10;
+         gateways.push_back(ElectricGate(&viewport, length, vertical, xpos, ypos));
       }
 
    // Create mines (MUST BE CREATED LAST)
@@ -863,83 +805,9 @@ void Game::Display()
       }
 
    // Draw gateways
-   for (i = 0; i < gatewaycount; i++)
-      {
-         // Draw first sphere
-         gateways[i].icon.x = gateways[i].xpos*ObjectGrid::OBJ_GRID_SIZE - viewport.GetXAdjust();
-         gateways[i].icon.y = gateways[i].ypos*ObjectGrid::OBJ_GRID_SIZE + ObjectGrid::OBJ_GRID_TOP - viewport.GetYAdjust();
-         opengl.Draw(&gateways[i].icon);
-
-         // Draw second sphere
-         if (gateways[i].vertical)
-            {
-               gateways[i].icon.x = gateways[i].xpos*ObjectGrid::OBJ_GRID_SIZE - viewport.GetXAdjust();
-               gateways[i].icon.y = (gateways[i].ypos+gateways[i].length)*ObjectGrid::OBJ_GRID_SIZE + ObjectGrid::OBJ_GRID_TOP - viewport.GetYAdjust();
-            }
-         else
-            {
-               gateways[i].icon.x = (gateways[i].xpos+gateways[i].length)*ObjectGrid::OBJ_GRID_SIZE - viewport.GetXAdjust();
-               gateways[i].icon.y = gateways[i].ypos*ObjectGrid::OBJ_GRID_SIZE + ObjectGrid::OBJ_GRID_TOP - viewport.GetYAdjust();
-            }
-         opengl.Draw(&gateways[i].icon);
-
-         // Draw the electricity stuff
-         if (--gateways[i].timer < GATEWAY_ACTIVE)
-            {
-               float r, g, b; 
-               int x, y, deviation;
-
-               opengl.DisableBlending();
-               opengl.DisableTexture();
-
-               for (j = 0; j < 10; j++)
-                  {
-                     deviation = 0;
-                     for (k = 0; k < gateways[i].length; k++)
-                        {
-                           glLoadIdentity();
-                           glBegin(GL_LINE_STRIP);
-                           r = 0.0f + (float)(rand()%5)/10.0f;
-                           g = 0.0f + (float)(rand()%5)/10.0f;
-                           b = 1.0f - (float)(rand()%5)/10.0f;
-
-                           glColor3f(r, g, b);
-                           if (gateways[i].vertical)
-                              {
-                                 x = gateways[i].xpos*ObjectGrid::OBJ_GRID_SIZE + 16 + deviation - viewport.GetXAdjust();
-                                 y = (gateways[i].ypos+k)*ObjectGrid::OBJ_GRID_SIZE + ObjectGrid::OBJ_GRID_TOP + 16 - viewport.GetYAdjust();
-                                 glVertex2i(x, y);
-                                 if (k == gateways[i].length-1)
-                                    deviation = 0;
-                                 else
-                                    deviation += rand()%20 - 10;
-                                 x = gateways[i].xpos*ObjectGrid::OBJ_GRID_SIZE + 16 + deviation - viewport.GetXAdjust();
-                                 y += ObjectGrid::OBJ_GRID_SIZE;
-                                 glVertex2i(x, y);
-                              }
-                           else
-                              {
-                                 x = (gateways[i].xpos+k)*ObjectGrid::OBJ_GRID_SIZE + 16 - viewport.GetXAdjust();
-                                 y = gateways[i].ypos*ObjectGrid::OBJ_GRID_SIZE + ObjectGrid::OBJ_GRID_TOP + 16 + deviation - viewport.GetYAdjust();
-                                 glVertex2i(x, y);
-                                 if (k == gateways[i].length-1)
-                                    deviation = 0;
-                                 else
-                                    deviation += rand()%20 - 10;
-                                 y = gateways[i].ypos*ObjectGrid::OBJ_GRID_SIZE + ObjectGrid::OBJ_GRID_TOP + 16 + deviation - viewport.GetYAdjust();
-                                 x += ObjectGrid::OBJ_GRID_SIZE;
-                                 glVertex2i(x, y);
-                              }
-                           glEnd();
-                        }
-                  }
-
-               // Reset timer 
-               if (gateways[i].timer < 0)
-                  gateways[i].timer = 100;
-            }
-      }
-
+   for (ElectricGateListIt it = gateways.begin(); it != gateways.end(); ++it)
+      (*it).Draw();
+   
    // Draw mines
    for (MineListIt it = mines.begin(); it != mines.end(); ++it)
       (*it).Draw();
index 7226b2ee50480fbec1e8016d9c9fba6dd4a8307e..7347bdee0c4bd3c2db9598822d96f2ea3a97eef5 100644 (file)
@@ -38,6 +38,7 @@
 #include "LandingPad.hpp"
 #include "Surface.hpp"
 #include "Mine.hpp"
+#include "ElectricGate.hpp"
 
 // Different fonts to be loaded
 enum FontType { ftNormal, ftBig, ftScore, ftHollow, ftScoreName, ftLarge };
@@ -85,7 +86,7 @@ private:
    GLuint uStarTexture, uSurf2Texture[Surface::NUM_SURF_TEX], uFadeTexture;
    GLuint uLevComTexture, uSpeedTexture, uBlueKey[18], uRedKey[18], uGreenKey[18], uPinkKey[18], uYellowKey[18];
    GLuint uBlueArrow, uPinkArrow, uRedArrow, uYellowArrow, uGreenArrow;
-   GLuint uGatewayTexture, uFuelMeterTexture, uFuelBarTexture,  uShipSmallTexture;
+   GLuint uFuelMeterTexture, uFuelBarTexture,  uShipSmallTexture;
    GLuint uGameOver, uPausedTexture;
     
     
@@ -123,13 +124,9 @@ private:
    // Electric gate things
    static const int MAX_GATEWAYS = 4;
    static const int MAX_GATEWAY_LENGTH = 10;
-   struct Gateway 
-   {
-      int xpos, ypos, length, timer;
-      bool vertical;
-      TextureQuad icon;
-   } gateways[MAX_GATEWAYS];
-   int gatewaycount;
+   typedef vector<ElectricGate> ElectricGateList;
+   typedef ElectricGateList::iterator ElectricGateListIt;
+   ElectricGateList gateways;
 
    // Space mines
    static const int MAX_MINES = 5;
index 7b701c050dd23b7fc7a00ad9673ba336c5296573..edffb5cd1cf438a053da92e2d58f41db01f5286d 100644 (file)
@@ -11,6 +11,7 @@ lander_SOURCES = Main.cpp Lander.hpp Game.cpp \
        Emitter.hpp Screens.cpp Screens.hpp Strings.hpp \
        ObjectGrid.hpp ObjectGrid.cpp Asteroid.hpp Asteroid.cpp \
        Ship.hpp Ship.cpp Viewport.hpp Viewport.cpp LandingPad.hpp \
-       LandingPad.cpp Surface.hpp Surface.cpp Mine.hpp Mine.cpp
+       LandingPad.cpp Surface.hpp Surface.cpp Mine.hpp Mine.cpp \
+       ElectricGate.hpp ElectricGate.cpp
 
 dist_pkgdata_DATA = ../lander.dat ../Default_Font.ttf ../Hollow_Font.ttf