Пример #1
0
  @Override
  public void render() {
    delta = Gdx.graphics.getDeltaTime() * 60;
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    camera.update();
    player.update(delta);
    if (player.atLimit()) backgroundPos -= 20 * delta;
    else backgroundPos -= 10 * delta;

    if (backgroundPos <= -background.getHeight()) backgroundPos = 0;

    spriteBatch.setProjectionMatrix(camera.combined);
    spriteBatch.begin();
    spriteBatch.draw(background, -1080 / 2, +background.getHeight() / 2 + backgroundPos);
    spriteBatch.draw(background, -1080 / 2, -background.getHeight() / 2 + backgroundPos);
    player.draw(spriteBatch);
    enemies.update(spriteBatch, delta);
    tweenManager.update(delta / 60);
    spriteBatch.end();

    rayHandler.setCombinedMatrix(camera.combined);
    rayHandler.updateAndRender();

    controls.handleInput(camera);
  }
  // The SurfaceView class implements onTouchListener
  // So we can override this method and detect screen touches.
  @Override
  public boolean onTouchEvent(MotionEvent motionEvent) {

    switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {

        // Player has touched the screen
      case MotionEvent.ACTION_DOWN:
        paused = false;

        if (motionEvent.getY() > screenY - screenY / 8) {
          if (motionEvent.getX() > screenX / 2) {
            playerShip.setMovementState(playerShip.RIGHT);
            if (playerShip.getX() <= 1116.6749) {
              canvas.drawBitmap(playerShip.getBitmap(), playerShip.getX(), screenY - 110, paint);
            }
          } else {
            playerShip.setMovementState(playerShip.LEFT);
            //                        if (playerShip.getX() < -26.160135){
            //                            playerShip.setMovementState(playerShip.RIGHT);
            //                        }
          }
        }

        if (motionEvent.getY() < screenY - screenY / 8) {
          // Shots fired
          if (bullet.shoot(playerShip.getX() + playerShip.getLength() / 2, screenY, bullet.UP)) {
            soundPool.play(shootID, 1, 1, 0, 0, 1);
          }
        }

        break;

        // Player has removed finger from screen
      case MotionEvent.ACTION_UP:
        if (motionEvent.getY() > screenY - screenY / 10) {
          playerShip.setMovementState(playerShip.STOPPED);
        }
        break;
    }
    return true;
  }
Пример #3
0
  void createShips() {

    Global.log("Creating ships...");
    int n = numPlayers();

    for (int i = 0; i < n; ++i) {

      Global.log("Creating ship for Player " + (i + 1) + " [" + playerNames[i] + "]...");

      final int I = i;

      if (i == playerID) {
        final PlayerShip ship = new PlayerShip(name, nextColor(), this, new Controls());
        ship.getControls().enabled = false;
        timers.add(
            new FixedTimer(
                new FixedTask() {
                  public boolean fixedRate() {
                    return true;
                  }

                  public float FPS() {
                    return Global.SendFPS;
                  }

                  public void run() {
                    try {
                      sendMessage(ship.getControlBytes());
                    } catch (IOException ex) {
                      System.err.println(
                          "An exception occured via the ship of Player "
                              + (I + 1)
                              + " ["
                              + playerNames[I]
                              + "]: "
                              + ex.toString());
                      Global.onException();
                      stop();
                    }
                  }
                }));
        timers.add(
            new FixedTimer(
                new FixedTask() {
                  public boolean fixedRate() {
                    return true;
                  }

                  public float FPS() {
                    return Global.ReceiveFPS;
                  }

                  public void run() {
                    byte[] data = null;
                    try {
                      data = playerByteStreams[I].read();
                    } catch (IOException ex) {
                      System.err.println("Error reading ship from server: " + ex);
                      Global.onException();
                      stop();
                      return;
                    }
                    if (data == null) return;
                    ship.fromBytes(data);
                  }
                }));
        addShip(ship);
      } else {
        final Ship ship = new Ship(playerNames[i], nextColor());
        timers.add(
            new FixedTimer(
                new FixedTask() {
                  public boolean fixedRate() {
                    return true;
                  }

                  public float FPS() {
                    return Global.ReceiveFPS;
                  }

                  public void run() {
                    byte[] data = null;
                    try {
                      data = playerByteStreams[I].read();
                    } catch (IOException ex) {
                      System.err.println("Error reading ship from server: " + ex);
                      Global.onException();
                      stop();
                      return;
                    }
                    if (data == null) return;
                    ship.fromBytes(data);
                  }
                }));
        addShip(ship);
      }

      Global.log("Done!");
    }

    Global.log("Done creating ships!");
  }
  private void draw() {
    // Make sure our drawing surface is valid or we crash
    if (ourHolder.getSurface().isValid()) {
      // Lock the canvas ready to draw
      canvas = ourHolder.lockCanvas();

      // Draw the background color
      canvas.drawColor(Color.argb(255, 26, 128, 182));
      //            Resources res = getResources();
      //            Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.background);
      //            canvas.drawBitmap(bitmap, 0, 0, paint);

      // Choose the brush color for drawing
      paint.setColor(Color.argb(255, 255, 255, 255));

      // Now draw the player spaceship
      canvas.drawBitmap(playerShip.getBitmap(), playerShip.getX(), screenY - 110, paint);

      // Draw the invaders
      for (int i = 0; i < numInvaders; i++) {
        if (invaders[i].getVisibility()) {
          if (uhOrOh) {
            canvas.drawBitmap(
                invaders[i].getBitmap(), invaders[i].getX(), invaders[i].getY(), paint);
          } else {
            canvas.drawBitmap(
                invaders[i].getBitmap2(), invaders[i].getX(), invaders[i].getY(), paint);
          }
        }
      }

      // Draw the bricks if visible
      for (int i = 0; i < numBricks; i++) {
        if (bricks[i].getVisibility()) {
          canvas.drawRect(bricks[i].getRect(), paint);
        }
      }

      // Draw the players bullet if active
      if (bullet.getStatus()) {
        canvas.drawRect(bullet.getRect(), paint);
      }

      // Draw the invaders bullets

      // Update all the invader's bullets if active
      for (int i = 0; i < invadersBullets.length; i++) {
        if (invadersBullets[i].getStatus()) {
          canvas.drawRect(invadersBullets[i].getRect(), paint);
        }
      }

      // Draw the score and remaining lives
      // Change the brush color
      paint.setColor(Color.argb(255, 249, 129, 0));
      paint.setTextSize(40);
      canvas.drawText("Score: " + score + "   Lives: " + lives, 10, 50, paint);

      // Draw everything to the screen
      ourHolder.unlockCanvasAndPost(canvas);
    }
  }
  private void update() {

    // Did an invader bump into the side of the screen
    boolean bumped = false;

    // Has the player lost
    boolean lost = false;

    // Move the player's ship
    playerShip.update(fps);

    // Update the invaders if visible

    // Update the players bullet
    if (bullet.getStatus()) {
      bullet.update(fps);
    }

    // Update all the invaders bullets if active
    for (int i = 0; i < invadersBullets.length; i++) {
      if (invadersBullets[i].getStatus()) {
        invadersBullets[i].update(fps);
      }
    }

    // Update all the invaders if visible
    for (int i = 0; i < numInvaders; i++) {
      if (invaders[i].getVisibility()) {

        // Move the next invader
        invaders[i].update(fps);

        // Does he want to take a shot?
        if (invaders[i].takeAim(playerShip.getX(), playerShip.getLength())) {

          // If so try and spawn a bullet
          if (invadersBullets[nextBullet].shoot(
              invaders[i].getX() + invaders[i].getLength() / 2, invaders[i].getY(), bullet.DOWN)) {

            // Shot fired
            // Prepare for the next shot
            nextBullet++;

            // Loop back to the first one if we have reached the last
            if (nextBullet == maxInvaderBullets) {
              // This stops the firing of another bullet until one completes its journey
              // Because if bullet 0 is still active shoot returns false.
              nextBullet = 0;
            }
          }
        }

        // If that move caused them to bump the screen change bumped to true
        if (invaders[i].getX() > screenX - invaders[i].getLength() || invaders[i].getX() < 0) {

          bumped = true;
        }
      }
    }

    // Did an invader bump into the edge of the screen
    if (bumped) {
      // Move all the invaders down and change direction
      for (int i = 0; i < numInvaders; i++) {
        invaders[i].dropDownAndReverse();

        // Have the invaders landed
        if (invaders[i].getY() > screenY - screenY / 10) {
          lost = true;
        }
      }

      // Increase the menace level
      // By making the sounds more frequent
      menaceInterval = menaceInterval - 80;
    }

    if (lost) {
      prepareLevel();
    }

    // Has the player's bullet hit the top of the screen
    if (bullet.getImpactPointY() < 0) {
      bullet.setInactive();
    }

    // Has an invaders bullet hit the bottom of the screen
    for (int i = 0; i < invadersBullets.length; i++) {

      if (invadersBullets[i].getImpactPointY() > screenY) {
        invadersBullets[i].setInactive();
      }
    }

    // Has the player's bullet hit an invader
    if (bullet.getStatus()) {
      for (int i = 0; i < numInvaders; i++) {
        if (invaders[i].getVisibility()) {
          if (RectF.intersects(bullet.getRect(), invaders[i].getRect())) {
            invaders[i].setInvisible();
            soundPool.play(invaderExplodeID, 1, 1, 0, 0, 1);
            bullet.setInactive();
            score = score + 10;

            // Has the player won

            if (score == numInvaders * 10) {
              paused = true;
              String mScore = score + "";
              Log.d("Score: ", mScore);
              Context context = getContext();
              Intent intent = new Intent(context, ScoreActivity.class);
              intent.putExtra("score", mScore);
              context.startActivity(intent);
            }
          }
        }
      }
    }

    // Has an alien bullet hit a shelter brick
    for (int i = 0; i < invadersBullets.length; i++) {
      if (invadersBullets[i].getStatus()) {
        for (int j = 0; j < numBricks; j++) {
          if (bricks[j].getVisibility()) {
            if (RectF.intersects(invadersBullets[i].getRect(), bricks[j].getRect())) {
              // A collision has occurred
              invadersBullets[i].setInactive();
              bricks[j].setInvisible();
              soundPool.play(damageShelterID, 1, 1, 0, 0, 1);
            }
          }
        }
      }
    }

    // Has a player bullet hit a shelter brick
    if (bullet.getStatus()) {
      for (int i = 0; i < numBricks; i++) {
        if (bricks[i].getVisibility()) {
          if (RectF.intersects(bullet.getRect(), bricks[i].getRect())) {
            // A collision has occurred
            bullet.setInactive();
            bricks[i].setInvisible();
            soundPool.play(damageShelterID, 1, 1, 0, 0, 1);
          }
        }
      }
    }

    // Has an invader bullet hit the player ship
    for (int i = 0; i < invadersBullets.length; i++) {
      if (invadersBullets[i].getStatus()) {
        if (RectF.intersects(playerShip.getRect(), invadersBullets[i].getRect())) {
          invadersBullets[i].setInactive();
          lives--;
          soundPool.play(playerExplodeID, 1, 1, 0, 0, 1);

          // Is it game over?
          if (lives == 0) {
            paused = true;
            //                        lives = 3;
            //                        score = 0;
            String mScore = score + "";
            Context context = getContext();
            Intent intent = new Intent(context, ScoreActivity.class);
            intent.putExtra("score", mScore);
            context.startActivity(intent);

            //                        prepareLevel();

          }
        }
      }
    }
  }