Commits

Nikola Cvetko  committed 91b15d8

Added asteroids.

  • Participants
  • Parent commits 33cf69f

Comments (0)

Files changed (6)

File Pucaljka.cbp

 			<Add library="glu32" />
 			<Add library="SDL_image" />
 		</Linker>
+		<Unit filename="asteroid.cpp" />
+		<Unit filename="asteroid.h" />
 		<Unit filename="blueenemyship.cpp" />
 		<Unit filename="blueenemyship.h" />
 		<Unit filename="bullet.cpp" />

File asteroid.cpp

+#include "asteroid.h"
+#include "texture.h"
+
+Asteroid::Asteroid(const double argX,
+                   const double argY) : texture(new Texture("asteroid.png")),  x(argX), y(argY)
+{
+}
+Asteroid::~Asteroid()
+{
+}
+void
+Asteroid::paintSelf()
+{
+    this->texture->bind();
+    glEnable(GL_TEXTURE_2D);
+
+    glMatrixMode(GL_MODELVIEW);
+    glPushMatrix();
+    glTranslatef(this->x, this->y, 0);
+    glScalef(32, 32, 0);
+    glBegin(GL_TRIANGLES);
+
+
+    glTexCoord2f(0, 0);
+    glVertex2f(-0.5, -0.5);
+    glTexCoord2f(0, 1);
+    glVertex2f(-0.5, 0.5);
+    glTexCoord2f(1, 1);
+    glVertex2f(0.5, 0.5);
+
+    glTexCoord2f(1, 1);
+    glVertex2f(0.5, 0.5);
+    glTexCoord2f(1, 0);
+    glVertex2f(0.5, -0.5);
+    glTexCoord2f(0, 0);
+    glVertex2f(-0.5, -0.5);
+
+    glEnd();
+    glPopMatrix();
+
+}
+void
+Asteroid::updateSelf(const double timeDelta)
+{
+    this->x-=350*timeDelta;
+}
+#ifndef PUCALJKA_ASTEROID_H
+#define PUCALJKA_ASTEROID_H
+#include <string>
+class Texture;
+
+class Asteroid
+{
+    public:
+    Asteroid(const double x,
+             const double y);
+    ~Asteroid();
+
+    void paintSelf();
+    void updateSelf(const double timeDelta);
+
+    private:
+    double x, y;
+    Texture * texture;
+};
+
+
+#endif
+

File asteroid.png

Added
New image

File gm_level.cpp

 #include "redenemyship.h"
 #include "blueenemyship.h"
 #include "bullet.h"
+#include "asteroid.h"
 
 
 extern bool gRunning;
 {
 	this->gameObjects.push_back((GameObject*)gameObject);
 }
+void
+GM_Level::spawnAsteroid(double x, double y)
+{
+    Asteroid *asteroid(new Asteroid(x, y));
+    this->asteroids.push_back(asteroid);
+}
 
 GM_Level::~GM_Level()
 {
     Layer *layer(new Layer("background.png", 0,0));
     this->layers.push_back(layer);
      */
-    
+
     EnemyShip *enemyShip(new EnemyShip("enemyship01.png", 600, 400));
     this->addGameObject(enemyShip);
-    
+
     EnemyShip *blueenemyShip(new EnemyShip("enemyship02.png", 800, 400));
     this->addGameObject(blueenemyShip);
-    
+
     RedEnemyShip *redEnemyShip(new RedEnemyShip(320, 0));
     this->addGameObject(redEnemyShip);
-    
+
     BlueEnemyShip *blueEnemyShip(new BlueEnemyShip(320, 0));
     this->addGameObject(blueEnemyShip);
-    
+
     for(int i = 0; i < 50; i++)
     {
         YellowEnemyShip *yellowEnemyShip(new YellowEnemyShip(320 + i * 60, 240));
         case SDLK_SPACE:
         playership->keyDown(event);
         break;
-        
+
         case SDLK_c:
         gDrawCollisionRects = !gDrawCollisionRects;
         break;
     {
         (*it)->paintSelf();
     }
+    for(std::vector<Asteroid*>::iterator it = asteroids.begin(); it != asteroids.end(); it++)
+    {
+        (*it)->paintSelf();
+    }
 
 
     playership->paintSelf();
 }
 void GM_Level::updateSelf(double timeDelta)
 {
+    if(rand() % 10000 < 1000 * timeDelta )
+    {
+        double randomNumberX = ((rand() % 10000) / 10000.) * 320;
+        double randomNumberY = ((rand() % 10000) / 10000.) * 240;
+        spawnAsteroid(randomNumberX, randomNumberY);
+    }
+    for(std::vector<Asteroid*>::iterator it = this->asteroids.begin(); it != this->asteroids.end(); it++)
+    {
+        (*it)->updateSelf(timeDelta);
+    }
     this->playership->updateSelf(timeDelta);
     for(std::vector<GameObject*>::iterator it = this->gameObjects.begin(); it != this->gameObjects.end(); it++)
     {
         (*it)->updateSelf(timeDelta);
     }
-    
+
     for(std::vector<GameObject*>::iterator it = this->gameObjects.begin(); it != this->gameObjects.end();)
     {
         if((*it)->getShouldDie())
             it++;
         }
     }
+
 }
 class Layer;
 class RedEnemyShip;
 class BlueEnemyShip;
+class Asteroid;
 
 class GM_Level : public GameMode
 {
 
     void addGameObject(const GameObject *gameObject);
 
+    void spawnAsteroid(double x, double y);
+
     std::vector<GameObject*>& getGameObjects()
     {
         return this->gameObjects;
     }
-    
+
     private:
     std::vector<GameObject*> gameObjects;
     std::vector<Layer*> layers;
+    std::vector<Asteroid*> asteroids;
     PlayerShip * playership;
 };