1. ZRS Razvoj igara
  2. Untitled project
  3. Pucaljka

Commits

Ivan Vučica  committed b1098a7

Bullet collision detection. Dying objects. Abstracted shipW and shipH. Timedeltas are doubles. Collision rect debug mode.

  • Participants
  • Parent commits c96a3c9
  • Branches default

Comments (0)

Files changed (12)

File Pucaljka.xcodeproj/project.pbxproj

View file
 		7F74A6E01747BDEC00CE1680 /* backbutton.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = backbutton.png; sourceTree = "<group>"; };
 		7F74A6E11747BDEC00CE1680 /* bullet.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = bullet.png; sourceTree = "<group>"; };
 		7F74A6E21747BDEC00CE1680 /* nextbutton.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = nextbutton.png; sourceTree = "<group>"; };
+		7F74A6E61747C8F000CE1680 /* types.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = types.h; sourceTree = SOURCE_ROOT; };
 		7F9B66081719986B004DC616 /* enemyship.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = enemyship.cpp; sourceTree = SOURCE_ROOT; };
 		7F9B66091719986B004DC616 /* enemyship.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = enemyship.h; sourceTree = SOURCE_ROOT; };
 		7F9B660A1719986C004DC616 /* playership.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = playership.cpp; sourceTree = SOURCE_ROOT; };
 				7FFE2EB71722D53E0059ECD6 /* yellowenemyship.h */,
 				AF1D1EB21477E73E00C95550 /* SDLMain.m */,
 				AF1D1E951477E6D400C95550 /* Supporting Files */,
+				7F74A6E61747C8F000CE1680 /* types.h */,
 			);
 			path = Pucaljka;
 			sourceTree = "<group>";

File bullet.cpp

View file
 }
 
 void
-Bullet::updateSelf(const float timeDelta)
+Bullet::updateSelf(const double timeDelta)
 {
-	this->x+=100 * timeDelta;
+	this->x+=320 * timeDelta;
 
     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->die();
+                break;
+            }
+        }
+    }
+
 }
 
 
 
     glEnd();
     glPopMatrix();
-
+    
 }

File bullet.h

View file
     virtual void paintSelf(void);
 
 
-    virtual void updateSelf(const float timeDelta);
+    virtual void updateSelf(const double timeDelta);
 
     private:
     Texture*texture;

File button.cpp

View file
     glColor3f(1.0, 1.0, 1.0);
 }
 
-void Button::updateSelf(const float timeDelta)
+void Button::updateSelf(const double timeDelta)
 {
 }
 

File button.h

View file
     }
 
     void paintSelf();
-    void updateSelf(const float timeDelta);
+    void updateSelf(const double timeDelta);
 
     bool mouseUp(const SDL_Event &event);
     void mouseDown(const SDL_Event &event);

File enemyship.cpp

View file
 #include "texture.h"
 #include <math.h>
 
+bool gDrawCollisionRects;
+
 EnemyShip::EnemyShip(const std::string& textureFile,
                const double argX,
                const double argY) : texture(new Texture(textureFile)), x(argX), y(argY), angle(0)
 void
 EnemyShip::paintSelf()
 {
+    if(this->getShouldDie())
+        glColor4f(1, 0, 0, 1);
+    
     this->texture->bind();
     glEnable(GL_TEXTURE_2D);
 
     glMatrixMode(GL_MODELVIEW);
     glPushMatrix();
-    glTranslatef(this->x, -this->y, 0);
-    glScalef(64, 64, 0);
+    glTranslatef(this->x, this->y, 0);
+    glScalef(this->getShipW(), this->getShipH(), 0);
     glRotatef(-90, 0, 0, 1);
     glRotatef(this->angle * 180 / M_PI, 0, 0, 1);
     glBegin(GL_TRIANGLES);
     glPopMatrix();
 
 
+    glColor4f(1, 1, 1, 1);
+    
+    if(gDrawCollisionRects)
+    {
+        glDisable(GL_TEXTURE_2D);
+        glBegin(GL_LINE_LOOP);
+        glVertex2f(getRect().x, getRect().y);
+        glVertex2f(getRect().x + getRect().w, getRect().y);
+        glVertex2f(getRect().x + getRect().w, getRect().y + getRect().h);
+        glVertex2f(getRect().x, getRect().y + getRect().h);
+        glEnd();
+    }
 }
 
 void

File enemyship.h

View file
 #include <string>
 #include "texture.h"
 #include "gameobject.h"
+#include "types.h"
 
 class EnemyShip : public GameObject
 {
                const double x,
                const double y);
 
-
     virtual ~EnemyShip();
 
-
     virtual void paintSelf(void);
-
-
     virtual void updateSelf(const double timeDelta);
 
+    double getShipW() const { return 64; }
+    double getShipH() const { return 64; }
+    
+    PUCRect getRect() const
+    {
+        PUCRect r;
+        r.x = x - this->getShipW() / 2;
+        r.y = y - this->getShipH() / 2;
+        r.w = this->getShipW();
+        r.h = this->getShipH();
+        
+        return r;
+    }
+    
     private:
     Texture*texture;
 
     float angle;
 };
 
-
-
+extern bool gDrawCollisionRects;
 
 #endif
 

File gameobject.h

View file
 class GameObject
 {
     public:
-    GameObject()
+    GameObject() : shouldDie(false)
     {
     }
     virtual ~GameObject()
     virtual void updateSelf(double timeDelta)
     {
     }
+    
+    virtual void die(void)
+    {
+        this->shouldDie = true;
+    }
+    bool getShouldDie(void) const
+    {
+        return this->shouldDie;
+    }
 
     GM_Level * getOwnerLevel();
     
     private:
+    bool shouldDie;
 };
 
 #endif

File gm_level.cpp

View file
     delete playership;
 }
 
-GM_Level::GM_Level() : playership(new PlayerShip("playership.png", 150, 200))
+GM_Level::GM_Level() : playership(new PlayerShip("playership.png", -250, 200))
 {
     Layer *layer(new Layer("background.png", 0,0));
     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->gameObjects.push_back(enemyShip);
-    this->gameObjects.push_back(blueenemyShip);
+    this->addGameObject(blueEnemyShip);
+    
     this->layers.push_back(layer);
-    this->gameObjects.push_back(redEnemyShip);
-    this->gameObjects.push_back(blueEnemyShip);
-
+    
     for(int i = 0; i < 50; i++)
     {
         YellowEnemyShip *yellowEnemyShip(new YellowEnemyShip(320 + i * 60, 240));
-        this->gameObjects.push_back(yellowEnemyShip);
+        this->addGameObject(yellowEnemyShip);
     }
 
     for(int i = 0; i < 50; i++)
     {
         YellowEnemyShip *yellowEnemyShip(new YellowEnemyShip(320 + i * 60, -240));
-        this->gameObjects.push_back(yellowEnemyShip);
+        this->addGameObject(yellowEnemyShip);
     }
 
 
     for(int i = 0; i < 50; i++)
     {
         YellowEnemyShip *yellowEnemyShip(new YellowEnemyShip(320 + i * 60, 120));
-        this->gameObjects.push_back(yellowEnemyShip);
+        this->addGameObject(yellowEnemyShip);
     }
 
     for(int i = 0; i < 50; i++)
     {
         YellowEnemyShip *yellowEnemyShip(new YellowEnemyShip(320 + i * 60, -120));
-        this->gameObjects.push_back(yellowEnemyShip);
+        this->addGameObject(yellowEnemyShip);
     }
 }
 
             const double BX = this->playership->getX();
             const double BY = this->playership->getY();
             Bullet*bullet(new Bullet(BX,BY));
-            this->gameObjects.push_back(bullet);
+            this->addGameObject(bullet);
         }
         break;
+            
+        case SDLK_c:
+        gDrawCollisionRects = !gDrawCollisionRects;
+        break;
 
         default:
         GameMode::keyDown(event);
     {
         (*it)->updateSelf(timeDelta);
     }
+    
+    for(std::vector<GameObject*>::iterator it = this->gameObjects.begin(); it != this->gameObjects.end();)
+    {
+        if((*it)->getShouldDie())
+        {
+            delete *it;
+            this->gameObjects.erase(it);
+            it = this->gameObjects.begin();
+        }
+        else
+        {
+            it++;
+        }
+    }
 }

File gm_level.h

View file
 
     void addGameObject(const GameObject *gameObject);
 
+    std::vector<GameObject*>& getGameObjects()
+    {
+        return this->gameObjects;
+    }
+    
     private:
     std::vector<GameObject*> gameObjects;
     std::vector<Layer*> layers;

File playership.cpp

View file
 #include "gamemode.h"
 
 static const double playerShipAngleV = 180;
-static const double playerShipYV = 150;
+static const double playerShipYV = 380;
 
 PlayerShip::PlayerShip(const std::string& textureFile,
                        const double argX,

File types.h

View file
+#ifndef PUCALJKA_TYPES_H
+#define PUCALJKA_TYPES_H
+
+typedef struct _PUCRect
+{
+    double x, y;
+    double w, h;
+} PUCRect;
+
+#endif