コード例 #1
0
ファイル: GameActivity.java プロジェクト: TronGame/CWA115
  /**
   * Show the winner in a dialog box. (is called when the host has calculated the winner) When the
   * user presses ok the game ends
   */
  public void showWinner() {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(getString(R.string.game_over_text));

    Player winningPlayer = (Player) map.getItemById(winner);
    String winnerName = "";
    if (winningPlayer != null) winnerName = winningPlayer.getName();

    // Show the winner
    builder.setMessage(getString(R.string.game_winner_text).replaceAll("%winner", winnerName));

    builder.setPositiveButton(
        "OK",
        new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
            // End the game
            quitGame();
          }
        });

    AlertDialog dialog = builder.create();
    try {
      dialog.show();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
コード例 #2
0
ファイル: GameActivity.java プロジェクト: TronGame/CWA115
  /**
   * Most of the game functionality happens in this function. It is called when a location update is
   * snapped to the road by the snappedPointHandler
   *
   * @param result The snapped location
   * @return does not matter
   */
  @Override
  public boolean handleApiResult(ArrayList<LatLng> result) {
    // Get the snapped location from the result (result is a list with one item)
    LatLng newSnappedGpsLoc = result.get(0);

    // Calculate the distance between the snapped location and the actual location
    // So this is basically the distance to the road
    double snappedDistance = LatLngConversion.getDistancePoints(gpsLoc, newSnappedGpsLoc);

    // Log.d("VALUE", "Snapped Distance "+String.valueOf(snappedDistance));

    if (isAlive && mapCenter != null && GameSettings.getMaxDistance() > 0) {
      if (LatLngConversion.getAccurateDistancePoints(mapCenter, newSnappedGpsLoc)
          > GameSettings.getMaxDistanceInMeters()) {
        showNotification(getString(R.string.crossed_border), Toast.LENGTH_SHORT);
        onDeath("//", "Map Border");
      }

      if (LatLngConversion.getAccurateDistancePoints(mapCenter, newSnappedGpsLoc)
          > GameSettings.getMaxDistanceInMeters() - WARNING_DISTANCE_TO_WALL) {
        showNotification(getString(R.string.close_to_border), Toast.LENGTH_SHORT);
      }
    }

    // Check is the player is (almost) on the road
    if (snappedDistance < MAX_ROAD_DISTANCE) {
      // Update the player marker and the camera
      map.updatePlayer(GameSettings.getPlayerId(), newSnappedGpsLoc);
      map.updateCamera(newSnappedGpsLoc);

      // Send the player location
      gameUpdateHandler.sendMyLocation(newSnappedGpsLoc);

      // If the player is alive the interactions with the wall can be checked
      //      Add the travelled distance to the score
      //      Check if the player is to close to or has crossed a wall
      //      Update the wall
      if (isAlive) {
        // Add the travelled distance to the score
        // ---------------------------------------
        double distance = 0.0;
        if (!(snappedGpsLoc.longitude == 0 && snappedGpsLoc.latitude == 0)) {
          // This checks if there has been a previous location update
          distance = LatLngConversion.getDistancePoints(snappedGpsLoc, newSnappedGpsLoc);
        }

        addScore(LatLngConversion.latLngDistanceToMeter(distance));
        if (!creatingWall && travelledDistance >= WALL_DELAY_DISTANCE) createWall();

        // Check if the player is to close to or has crossed a wall
        // ---------------------------------------------------------
        ArrayList<Wall> walls = map.getWalls();
        for (Wall wall : walls) {
          // Check if the player hasn't crossed a wall
          if (wall.hasCrossed(
              snappedGpsLoc,
              newSnappedGpsLoc,
              MIN_WALL_DISTANCE,
              IGNORE_WALL_DISTANCE,
              GameSettings.getPlayerId())) {
            // The player has crossed the wall and has therefore died
            showNotification(getString(R.string.wall_crossed), Toast.LENGTH_SHORT);
            Player killer = (Player) map.getItemById(wall.getOwnerId());
            String killerName = "";
            if (killer != null) killerName = killer.getName();

            onDeath(wall.getOwnerId(), killerName);
          } else {
            // Check if the player isn't to close to a wall
            if (wall.getDistanceTo(
                    newSnappedGpsLoc, MIN_WALL_WARNING_DISTANCE, GameSettings.getPlayerId())
                < MIN_WALL_WARNING_DISTANCE) {
              // Show the "player to close to wall" notification
              showNotification(getString(R.string.wall_too_close), Toast.LENGTH_SHORT);
            }
          }
        }

        // Update the wall
        // ---------------
        Wall wall = (Wall) map.getItemById(wallId);
        // Update the wall (this must happen after the "player to close to wall" check
        if (creatingWall) {
          wall.addPoint(newSnappedGpsLoc); // Add the new point to the wall
          gameUpdateHandler.sendUpdateWall(newSnappedGpsLoc, wallId);
          map.redraw(wall.getId()); // Redraw the wall on the map
        }
      }

      snappedGpsLoc = newSnappedGpsLoc; // update the snapped location

    } else {
      // Player is to far from the road
      map.updatePlayer(
          GameSettings.getPlayerId(), gpsLoc); // Draw the player on the actual location instead
      map.updateCamera(gpsLoc); // Zoom to there as well

      // Send the player location
      gameUpdateHandler.sendMyLocation(gpsLoc);

      if (isAlive) {
        // Show the "player to far from road" notification
        showNotification(getString(R.string.road_too_far), Toast.LENGTH_SHORT);
        onDeath("", "");
      }
    }

    return false; // This is used by the snappedPointHandler
  }