Filip Mravunac avatar Filip Mravunac committed 67436b4 Merge

Merge

Comments (0)

Files changed (6)

 #include "playership.h"
 #include "gm_level.h"
 #include "texture_manager.h"
+#include "explosion.h"
 
 Bullet::Bullet( double argX, double argY) : texture(TextureManager::getInstance()->getTexture("bullet.png")),
                                             x(argX), y(argY)
             if(this->x >= r.x && this->x <= r.x + r.w &&
                this->y >= r.y && this->y <= r.y + r.h)
             {
+                Explosion *explosion(new Explosion("text.png", currentEnemyShip->x, currentEnemyShip->y));
+                ownerLevel->addGameObject(explosion, true);
+
                 currentEnemyShip->die();
                 this->die();
                 break;
 void
 EnemyShip::paintSelf()
 {
-    if(this->getShouldDie())
+    if(this->isDead())
         glColor4f(1, 0, 0, 1);
 
     this->texture->bind();
 
     double getShipW() const { return 64; }
     double getShipH() const { return 64; }
-    
+
     PUCRect getRect() const
     {
         PUCRect r;
         r.y = y - this->getShipH() / 2;
         r.w = this->getShipW();
         r.h = this->getShipH();
-        
+
         return r;
     }
-    
+    double x,y;
     private:
     Texture*texture;
+    protected:
 
-    protected:
-    double x,y;
     float angle;
 };
 
     virtual void updateSelf(double timeDelta)
     {
     }
-    
+
     virtual void die(void)
     {
         this->shouldDie = true;
     }
-    bool getShouldDie(void) const
+    bool isDead(void) const
     {
         return this->shouldDie;
     }
 
     GM_Level * getOwnerLevel();
-    
+
     private:
     bool shouldDie;
 };
 extern SDL_Surface * gScreen;
 
 void
-GM_Level::addGameObject(const GameObject *gameObject)
+GM_Level::addGameObject(const GameObject *gameObject, bool toFront)
 {
-	this->gameObjects.push_back((GameObject*)gameObject);
+    if(!toFront)
+        this->gameObjectsToInsertToBack.push_back((GameObject*)gameObject);
+    else
+        this->gameObjectsToInsertToFront.push_back((GameObject*)gameObject);
 }
 void
 GM_Level::spawnAsteroid(double x, double y)
         this->addGameObject(yellowEnemyShip);
     }
 
-
-    Explosion *explosion(new Explosion("text.png", 50, 50));
-    this->addGameObject(explosion);
-
+ /*   if(enemyShip->getShouldDie()== true)
+    {
+        Explosion *explosion(new Explosion("text.png", 50, 50));
+        this->addGameObject(explosion);
+    }*/
 }
 
 void
     {
         (*it)->updateSelf(timeDelta);
     }
+    this->playership->updateSelf(timeDelta);
     for(std::vector<GameObject*>::iterator it = this->gameObjects.begin(); it != this->gameObjects.end(); it++)
     {
         (*it)->updateSelf(timeDelta);
     }
+    this->flushInsertedAndDeadObjects();
+    if(playership->isDead())
+    {
+        gGame->switchGameMode(new GM_MainMenu());
+        printf("Game Over");
+    }
+}
 
+void GM_Level::flushInsertedAndDeadObjects()
+{
     for(std::vector<GameObject*>::iterator it = this->gameObjects.begin(); it != this->gameObjects.end();)
     {
-        if((*it)->getShouldDie())
+        if((*it)->isDead())
         {
             delete *it;
             this->gameObjects.erase(it);
             it++;
         }
     }
-    if(playership->isDead())
-    {
-        gGame->switchGameMode(new GM_MainMenu());
-        printf("Game Over\n");
-    }
+
+    this->gameObjects.insert(this->gameObjects.begin(), this->gameObjectsToInsertToFront.rbegin(), this->gameObjectsToInsertToFront.rend());
+    this->gameObjects.insert(this->gameObjects.end(), this->gameObjectsToInsertToBack.begin(), this->gameObjectsToInsertToBack.end());
+
+    this->gameObjectsToInsertToFront.clear();
+    this->gameObjectsToInsertToBack.clear();
 }
     void mouseMove(const SDL_Event &event);
     void updateSelf(double timeDelta);
 
-    void addGameObject(const GameObject *gameObject);
+    void addGameObject(const GameObject *gameObject, bool toFront = false);
 
     void spawnAsteroid(double x, double y);
 
     std::vector<Asteroid*> asteroids;
     PlayerShip * playership;
     LevelBackground * levelbackground;
+
+    std::vector<GameObject*> gameObjectsToInsertToFront;
+    std::vector<GameObject*> gameObjectsToInsertToBack;
+    void flushInsertedAndDeadObjects();
+
 };
 
 #endif
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.