Ejemplo n.º 1
0
  public void update() {
    if (player.getPlaying()) {
      if (botborder.isEmpty()) {
        player.setPlaying(false);
        return;
      }
      if (topborder.isEmpty()) {
        player.setPlaying(false);
        return;
      }

      bg.update();
      player.update();

      // calculate the threshold of height the border can have based on the score
      // max and min border heart are updated, and the border switched direction when either max or
      // min is met

      maxBorderHeight = 30 + player.getScore() / progressDenom;
      // cap max border height so that borders can only take up a total of 1/2 the screen
      if (maxBorderHeight > HEIGHT / 4) maxBorderHeight = HEIGHT / 4;
      minBorderHeight = 5 + player.getScore() / progressDenom;

      // check bottom border collision
      for (int i = 0; i < botborder.size(); i++) {
        if (collision(botborder.get(i), player)) player.setPlaying(false);
      }

      // check top border collision
      for (int i = 0; i < topborder.size(); i++) {
        if (collision(topborder.get(i), player)) player.setPlaying(false);
      }

      // update top border
      this.updateTopBorder();

      // udpate bottom border
      this.updateBottomBorder();

      // add missiles on timer
      long missileElapsed = (System.nanoTime() - missileStartTime) / 1000000;
      if (missileElapsed > (2000 - player.getScore() / 4)) {
        // first missile always goes down the middle
        if (missiles.size() == 0) {
          missiles.add(
              new Missile(
                  BitmapFactory.decodeResource(getResources(), R.drawable.missile),
                  WIDTH + 10,
                  HEIGHT / 2,
                  45,
                  15,
                  player.getScore(),
                  13));
        } else {

          missiles.add(
              new Missile(
                  BitmapFactory.decodeResource(getResources(), R.drawable.missile),
                  WIDTH + 10,
                  (int) (rand.nextDouble() * (HEIGHT - (maxBorderHeight * 2)) + maxBorderHeight),
                  45,
                  15,
                  player.getScore(),
                  13));
        }

        // reset timer
        missileStartTime = System.nanoTime();
      }
      // loop through every missile and check collision and remove
      for (int i = 0; i < missiles.size(); i++) {
        // update missile
        missiles.get(i).update();

        if (collision(missiles.get(i), player)) {
          missiles.remove(i);
          player.setPlaying(false);
          smoke.clear();
          break;
        }
        // remove missile if it is way off the screen
        if (missiles.get(i).getX() < -100) {
          missiles.remove(i);
          break;
        }
      }

      // add smoke puffs on timer
      long elapsed = (System.nanoTime() - smokeStartTime) / 1000000;
      if (elapsed > 120) {
        smoke.add(new Smokepuff(player.getX(), player.getY() + 10));
        smokeStartTime = System.nanoTime();
      }

      for (int i = 0; i < smoke.size(); i++) {
        smoke.get(i).update();
        if (smoke.get(i).getX() < -10) {
          smoke.remove(i);
        }
      }
    } else {
      player.resetDY();
      if (!reset) {
        newGameCreated = false;
        startReset = System.nanoTime();
        reset = true;
        dissapear = true;
        explosion =
            new Explosion(
                BitmapFactory.decodeResource(getResources(), R.drawable.explosion),
                player.getX(),
                player.getY() - 30,
                100,
                100,
                25);
      }

      explosion.update();
      long resetElapsed = (System.nanoTime() - startReset) / 1000000;

      if (resetElapsed > 2500 && !newGameCreated) {
        newGame();
      }
    }
  }
 /**
  * This is the game update method. It iterates through all the objects and calls their update
  * method if they have one or calls specific engine's update method.
  */
 public void update(float interpolatedTime) {
   // update explosions
   if (explosion != null && explosion.isAlive()) {
     explosion.update(interpolatedTime);
   }
 }