Commits

Nikola Cvetko committed 2adbc46

PlayerShip dies if he's hit by EnemyShip.

Comments (0)

Files changed (3)

 #include <OpenGL/gl.h>
 #include <OpenGL/glu.h>
 #endif
+#include "gm_mainmenu.h"
 #include "gameobject.h"
 #include "layer.h"
 #include "enemyship.h"
             it++;
         }
     }
-
+    if(playership->isDead())
+    {
+        gGame->switchGameMode(new GM_MainMenu());
+        printf("Game Over");
+    }
 }
 #include <SDL/SDL_events.h>
 #include "bullet.h"
 #include "gm_level.h"
+#include "gameobject.h"
 #include "gamemode.h"
 #include "audiomgr.h"
 #include "texture_manager.h"
 
 static const double playerShipAngleV = 180;
 static const double playerShipYV = 380;
+static double playerShipLives = 3;
 
 extern SDL_Joystick * gJoystick;
 
 PlayerShip::PlayerShip(const std::string& textureFile,
                        const double argX,
                        const double argY) : texture(TextureManager::getInstance()->getTexture(textureFile)),
-                        x(argX), y(argY), angle(0), angleV(0), yv(0), timeUntilNextFiring(getFiringRate())
+                        x(argX), y(argY), angle(0), angleV(0), yv(0), timeUntilNextFiring(getFiringRate()), died(0)
 {
     this->texture->retain();
     shootSound = AudioMgr::getInstance()->loadSound("gun.wav");
     if(y < -240) y = -240;
     if(y > 240) y = 240;
     prevJoyState = currentJoyState;
+
+
+    GM_Level * ownerLevel = this->getOwnerLevel();
+    std::vector<GameObject*>& gameObjects(ownerLevel->getGameObjects());
+    for(std::vector<GameObject*>::iterator it = gameObjects.begin(); it != gameObjects.end(); it++)
+    {
+        GameObject * currentObject = *it;
+
+        if(this == currentObject)
+            continue;
+
+        EnemyShip * currentEnemyShip = dynamic_cast<EnemyShip*>(currentObject);
+        if(currentEnemyShip) // if(currentEnemyShip != NULL)
+        {
+            PUCRect r(currentEnemyShip->getRect());
+            if(this->x >= r.x && this->x <= r.x + r.w &&
+               this->y >= r.y && this->y <= r.y + r.h)
+            {
+                currentEnemyShip->die();
+                this->takenDamage();
+                break;
+            }
+        }
+    }
 }
 
 void
 
     this->timeUntilNextFiring = this->getFiringRate();
 }
+void
+PlayerShip::takenDamage()
+{
+    playerShipLives -= 1;
+    if(playerShipLives <= 0)
+        died = true;
+}
 
 #include <SDL/SDL_events.h>
 #include <string>
 #include "texture.h"
+#include "gameobject.h"
 
-class PlayerShip
+class PlayerShip : public GameObject
 {
     public:
     PlayerShip(const std::string& textureFile,
     void mouseMove(const SDL_Event &event);
     void paintSelf(void);
     void updateSelf(double timeDelta);
+    void takenDamage();
 
     void fire(void);
-    
+
     const double getX () { return x;};
     const double getY () { return y;};
 
+    const bool isDead () {return died; };
     const double getFiringRate() { return 0.2; }
-    
+
     private:
     Texture * texture;
     double x, y;
     double yv;
     double timeUntilNextFiring;
     int shootSound;
+    bool died;
 };
 #endif