Commits

Ivan Vučica  committed 33cf69f

Removed level background. Firing rate. Elementary joystick support.

  • Participants
  • Parent commits b1098a7

Comments (0)

Files changed (4)

File gm_level.cpp

 
 GM_Level::GM_Level() : playership(new PlayerShip("playership.png", -250, 200))
 {
+    /*
     Layer *layer(new Layer("background.png", 0,0));
+    this->layers.push_back(layer);
+     */
+    
     EnemyShip *enemyShip(new EnemyShip("enemyship01.png", 600, 400));
     this->addGameObject(enemyShip);
     
     BlueEnemyShip *blueEnemyShip(new BlueEnemyShip(320, 0));
     this->addGameObject(blueEnemyShip);
     
-    this->layers.push_back(layer);
-    
     for(int i = 0; i < 50; i++)
     {
         YellowEnemyShip *yellowEnemyShip(new YellowEnemyShip(320 + i * 60, 240));
         break;
 
         case SDLK_SPACE:
-        {
-            const double BX = this->playership->getX();
-            const double BY = this->playership->getY();
-            Bullet*bullet(new Bullet(BX,BY));
-            this->addGameObject(bullet);
-        }
+        playership->keyDown(event);
         break;
-            
+        
         case SDLK_c:
         gDrawCollisionRects = !gDrawCollisionRects;
         break;

File playership.cpp

 static const double playerShipAngleV = 180;
 static const double playerShipYV = 380;
 
+extern SDL_Joystick * gJoystick;
+
 PlayerShip::PlayerShip(const std::string& textureFile,
                        const double argX,
-                       const double argY) : texture(new Texture(textureFile)), x(argX), y(argY), angle(0), angleV(0), yv(0)
+                       const double argY) : texture(new Texture(textureFile)), x(argX), y(argY), angle(0), angleV(0), yv(0), timeUntilNextFiring(getFiringRate())
 {
 
 }
 {
     angle += angleV * timeDelta;
     y += yv * timeDelta;
+    timeUntilNextFiring -= timeDelta;
+    
+    static bool prevJoyState = 0;
+    bool currentJoyState = (gJoystick && SDL_JoystickGetButton(gJoystick, 0));
+    
+    if(SDL_GetKeyState(NULL)[SDLK_SPACE] ||
+       currentJoyState)
+    {
+        if(currentJoyState && !prevJoyState)
+            timeUntilNextFiring = 0;
+        fire();
+    }
+    
+    if(gJoystick)
+    {
+        Sint16 axisYInt = SDL_JoystickGetAxis(gJoystick, 1);
+        double axisY = (axisYInt / 32767.);
+        
+        y += axisY * playerShipYV * timeDelta;
+    }
+    
+    if(y < -240) y = -240;
+    if(y > 240) y = 240;
+    prevJoyState = currentJoyState;
 }
 
 void
 PlayerShip::keyDown(const SDL_Event &event)
 {
-    int lasty = y;
-    if(y < 145)
-        y = lasty +5;
-    if(y > 300)
-        y = lasty -5;
+
     switch(event.key.keysym.sym)
     {
         case SDLK_RIGHT:
         case SDLK_DOWN:
         yv =  playerShipYV;
         break;
-
+            
         case SDLK_SPACE:
-        {
-			Bullet * b = new Bullet(this->x, this->y);
-			GM_Level * currentLevel = (GM_Level*)gGame;
-
-			currentLevel->addGameObject(b);
-		}
+        timeUntilNextFiring = 0;
         break;
 
         default:
 
 }
 
+void
+PlayerShip::fire(void)
+{
+    if(this->timeUntilNextFiring > 0)
+        return;
+    
+    Bullet * b = new Bullet(this->x, this->y);
+    GM_Level * currentLevel = (GM_Level*)gGame;
+    
+    currentLevel->addGameObject(b);
+    
+    this->timeUntilNextFiring = this->getFiringRate();
+}
 

File playership.h

                const double y);
     virtual ~PlayerShip();
 
-
     void keyDown(const SDL_Event& event);
-
     void keyUp(const SDL_Event& event);
-
     void mouseDown(const SDL_Event &event);
-
     void mouseUp(const SDL_Event &event);
-
     void mouseMove(const SDL_Event &event);
-
     void paintSelf(void);
-
     void updateSelf(double timeDelta);
 
+    void fire(void);
+    
     const double getX () { return x;};
     const double getY () { return y;};
 
+    const double getFiringRate() { return 0.2; }
+    
     private:
     Texture * texture;
     double x, y;
     double angle;
     double angleV;
     double yv;
+    double timeUntilNextFiring;
+    
 };
 #endif
 

File pucaljka.cpp

 
 SDL_Surface * gScreen = NULL;
 bool gRunning = true;
+SDL_Joystick * gJoystick = NULL;
 
 /*****************************/
 
 main(int argc,
      char ** argv)
 {
-    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
+    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);
+    }
+    
     gGame = new GM_MainMenu();
     uint32_t OldTime = SDL_GetTicks();
-
+    
     glEnable(GL_BLEND);
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);