Commits

Filip Mravunac  committed a269ba4 Merge

Merge

  • Participants
  • Parent commits c6c67d3, 5a4d389

Comments (0)

Files changed (8)

File Pucaljka.cbp

 		<Unit filename="button.h" />
 		<Unit filename="enemyship.cpp" />
 		<Unit filename="enemyship.h" />
+		<Unit filename="explosion.cpp" />
+		<Unit filename="explosion.h" />
 		<Unit filename="gamemode.cpp" />
 		<Unit filename="gamemode.h" />
 		<Unit filename="gameobject.cpp" />
 		<Unit filename="texture.h" />
 		<Unit filename="texture_manager.cpp" />
 		<Unit filename="texture_manager.h" />
+		<Unit filename="types.h" />
 		<Unit filename="yellowenemyship.cpp" />
 		<Unit filename="yellowenemyship.h" />
 		<Extensions>

File explosion.cpp

+#include "explosion.h"
+#include "texture_manager.h"
+
+Explosion::Explosion(const std::string& textureFile,
+                     const double argX,
+                     const double argY) : texture(TextureManager::getInstance()->getTexture(textureFile)),
+                                            x(argX), y(argY), t(0)
+{
+    this->texture->retain();
+}
+Explosion::~Explosion()
+{
+    this->texture->release();
+}
+void
+Explosion::calcTexCoords(int spriteOffX, int spriteOffY,
+                         float wImg, float hImg,
+                         int wSprite, int hSprite,
+                         int animation, int frame,
+                         float *txTop, float *txBottom, float *txLeft, float *txRight)
+{
+    int animX = animation % 3, animY = animation / 3;
+    int pxAnimX = animX * wSprite * 4, pxAnimY = animY * hSprite * 4;
+
+    int frameX = frame % 4, frameY = frame / 4;
+    int pxFrameX = frameX * wSprite, pxFrameY = frameY * hSprite;
+
+    int pxLeft = spriteOffX + pxAnimX + pxFrameX;
+    int pxRight = pxLeft + wSprite;
+    int pxTop = spriteOffY + pxAnimY + pxFrameY;
+    int pxBottom = pxTop + hSprite;
+
+    //printf("PX L: %d R: %d T: %d B: %d\n", pxLeft, pxRight, pxTop, pxBottom);
+
+    float left = pxLeft / (float)wImg;
+    float right = pxRight / (float)wImg;
+    float top = pxTop / (float)hImg;
+    float bottom = pxBottom / (float)hImg;
+
+    //printf("TX L: %g R: %g T: %g B: %g\n", left, right, top, bottom);
+
+    *txLeft = left;
+    *txRight = right;
+    *txTop = top;
+    *txBottom = bottom;
+}
+
+void
+Explosion::paintSelf()
+{
+    if(t >= 1)
+        return;
+
+    ///////////
+
+    float txTop = 0, txBottom = 0, txLeft = 0, txRight = 0;
+
+    calcTexCoords(0, 0, 128, 128, 32, 32, 0, t * 16, &txLeft, &txRight, &txTop, &txBottom);
+
+    this->texture->bind();
+    glEnable(GL_TEXTURE_2D);
+
+    glMatrixMode(GL_MODELVIEW);
+    glPushMatrix();
+    glTranslatef(this->x, this->y, 0);
+    glScalef(50, 50, 1);
+    glBegin(GL_TRIANGLES);
+
+
+    glTexCoord2f(txLeft, txBottom);
+    glVertex2f(-0.5, -0.5);
+    glTexCoord2f(txLeft, txTop);
+    glVertex2f(-0.5, 0.5);
+    glTexCoord2f(txRight, txTop);
+    glVertex2f(0.5, 0.5);
+
+    glTexCoord2f(txRight, txTop);
+    glVertex2f(0.5, 0.5);
+    glTexCoord2f(txRight, txBottom);
+    glVertex2f(0.5, -0.5);
+    glTexCoord2f(txLeft, txBottom);
+    glVertex2f(-0.5, -0.5);
+
+    glEnd();
+    glPopMatrix();
+
+}
+void
+Explosion::updateSelf(const double timeDelta)
+{
+    GameObject::updateSelf(timeDelta);
+    t += timeDelta;
+    if(t >= 1)
+        die();
+}
+
+
+#ifndef EXPLOSION_H
+#define EXPLOSION_H
+
+#include <SDL/SDL_events.h>
+#include <string>
+#include "texture.h"
+#include "gameobject.h"
+
+class Explosion : public GameObject
+{
+    public:
+    Explosion(const std::string& textureFile,
+               const double x,
+               const double y);
+    ~Explosion();
+    void calcTexCoords(int spriteOffX, int spriteOffY,
+                       float wImg, float hImg,
+                       int wSprite, int hSprite,
+                       int animation, int frame,
+                       float *txTop, float *txBottom, float *txLeft, float *txRight);
+   virtual void paintSelf();
+   virtual void updateSelf(const double timeDelta);
+
+    private:
+    Texture * texture;
+    double x, y;
+    double t;
+};
+
+
+#endif

File gm_level.cpp

 #include <OpenGL/gl.h>
 #include <OpenGL/glu.h>
 #endif
+#include "gm_mainmenu.h"
 #include "gameobject.h"
 #include "layer.h"
 #include "enemyship.h"
 #include "bullet.h"
 #include "asteroid.h"
 #include "levelbackground.h"
+#include "explosion.h"
 
 
 extern bool gRunning;
         YellowEnemyShip *yellowEnemyShip(new YellowEnemyShip(320 + i * 60, -120));
         this->addGameObject(yellowEnemyShip);
     }
+
+
+    Explosion *explosion(new Explosion("text.png", 50, 50));
+    this->addGameObject(explosion);
+
 }
 
 void
     switch(event.key.keysym.sym)
     {
         case SDLK_ESCAPE:
-        gRunning = false;
+        gGame->switchGameMode(new GM_MainMenu());
         break;
 
         case SDLK_LEFT:
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
 
+    for(std::vector<Layer*>::iterator it = layers.begin(); it != layers.end(); it++)
+    {
+        (*it)->paintSelf();
+    }
+    for(std::vector<Asteroid*>::iterator it = asteroids.begin(); it != asteroids.end(); it++)
+    {
+        (*it)->paintSelf();
+    }
+
     playership->paintSelf();
     for(std::vector<GameObject*>::iterator it = gameObjects.begin(); it != gameObjects.end(); it++)
     {
         (*it)->paintSelf();
     }
-    for(std::vector<Layer*>::iterator it = layers.begin(); it != layers.end(); it++)
-    {
-        (*it)->paintSelf();
-    }
 
-    for(std::vector<Asteroid*>::iterator it = asteroids.begin(); it != asteroids.end(); it++)
-    {
-        (*it)->paintSelf();
-    }
+
 
 
 
             it++;
         }
     }
-
+    if(playership->isDead())
+    {
+        gGame->switchGameMode(new GM_MainMenu());
+        printf("Game Over");
+    }
 }

File playership.cpp

 #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;
+}
 

File playership.h

 #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
 

File pucaljka.cpp

 #include "gm_mainmenu.h"
 #include "gm_level.h"
 #include "audiomgr.h"
+#include <time.h>
 
 SDL_Surface * gScreen = NULL;
 bool gRunning = true;
 main(int argc,
      char ** argv)
 {
+    srand(time(NULL));
+
     SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK);
     SDL_WM_SetCaption("Pucaljka", "Pucaljka");
     gScreen = SDL_SetVideoMode(1024, 768, 0, SDL_OPENGL);
-    
+
     if(SDL_NumJoysticks() > 0)
     {
         printf("Opening joystick %s\n", SDL_JoystickName(0));
         gJoystick = SDL_JoystickOpen(0);
     }
-    
+
     AudioMgr::getInstance()->initAudio();
-    
+
     gGame = new GM_MainMenu();
     uint32_t OldTime = SDL_GetTicks();
-    
+
     glEnable(GL_BLEND);
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 

File text.png

Added
New image