Пример #1
0
  /**
   * Called when the start/stop wall button is pressed or when the proximity sensor detects
   * something
   */
  public void createWall() {
    if (!isAlive) return;

    // Is the player currently creating a wall
    if (!creatingWall) {
      // Start creating a wall
      creatingWall = true; // The player is now creating a wall
      // Create the wall object
      Wall wall =
          new Wall(
              "W" + GameSettings.generateUniqueId(),
              GameSettings.getPlayerId(),
              GameSettings.getWallColor(),
              context);
      wallId = wall.getId(); // Store the wall id
      map.addMapItem(wall); // Add the wall to the map

      // Tell the other devices that a new wall has been created
      gameUpdateHandler.sendCreateWall(
          GameSettings.getPlayerId(), wallId, new ArrayList<LatLng>(), GameSettings.getWallColor());

      // Show the "creating wall" notification
      showNotification(getString(R.string.wall_on_notification), Toast.LENGTH_SHORT);
    }
    // The wall is never turned off
    // else {
    //     // Stop creating the wall
    //     creatingWall = false;                       // The player is no longer creating a wall
    //     wallId = null;                              // Forget about the current wall
    //
    //     // Show the "stopped creating wall" notification
    //     showNotification(getString(R.string.wall_off_notification), Toast.LENGTH_SHORT);
    // }
  }
Пример #2
0
  public void onEndGame() {
    if (!GameSettings.isOwner()) {
      if (hasEnded) return;

      hasEnded = true;
    }

    // The host updates the high scores
    if (travelledDistance > GameSettings.getProfile().getHighscore()) {
      dataServer.sendRequest(
          ServerCommand.SET_HIGHSCORE,
          ImmutableMap.of(
              "id", GameSettings.getPlayerId(),
              "token", GameSettings.getPlayerToken(),
              "highscore", String.valueOf(Math.round(travelledDistance))),
          new HttpConnector.Callback() {
            @Override
            public void handleResult(String data) {
              showWinner();
            }
          });
    } else {
      showWinner();
    }
  }
Пример #3
0
  /**
   * Is called when another player has died
   *
   * @param playerName The name of the player that died
   * @param killerId The id of the killer
   * @param killerName The name of the killer
   */
  public void playerDied(String playerName, String killerId, String killerName) {
    if (killerId.equals("")) {
      // The player died because he went to far from the road
      showNotification(
          getString(R.string.player_died_text).replaceAll("%name", playerName), Toast.LENGTH_SHORT);
    } else {
      // The player crossed a wall
      // The killer gets a bonus score
      if (GameSettings.getPlayerId().equals(killerId)) {
        addScore(KILL_SCORE);
        showNotification(
            getString(R.string.player_killed_text)
                    .replaceAll("%name", playerName)
                    .replaceAll("%killer", "you")
                + " "
                + getString(R.string.score_received_text)
                    .replaceAll("%score", String.valueOf(KILL_SCORE)),
            Toast.LENGTH_SHORT);
      } else {
        showNotification(
            getString(R.string.player_killed_text)
                .replaceAll("%name", playerName)
                .replaceAll("%killer", killerName),
            Toast.LENGTH_SHORT);
      }
    }

    if (GameSettings.isOwner()) {
      // There is one less player alive now
      playersAliveCount -= 1;
      // If there is only one player left : end the game
      if (!IMMORTAL && playersAliveCount <= 1) endGame();
    }
  }
Пример #4
0
  public void handleBellDetected() {
    // TODO: avoid calling this when the map is not yet ready
    if (hasEnded) return;

    if (isBellRinging) return; // Wait for the bell to stop ringing
    isBellRinging = true;
    gameUpdateHandler.sendBellSound(GameSettings.getPlayerId());
    bellCount += 1;

    if (currentEvent != null && currentEvent instanceof BellEvent) {
      ((TextView) findViewById(R.id.eventValue))
          .setText(getString(R.string.bell_event_text).replaceAll("%value", "" + bellCount));
    }

    // After 3 seconds, disable the bell.
    new Handler()
        .postDelayed(
            new Runnable() {
              @Override
              public void run() {
                isBellRinging = false;
              }
            },
            3000);
  }
Пример #5
0
  private void joinGame(LobbyListItem item) {
    if (!checkBoxView.isChecked() && item.getPlayerCount() >= item.getMaxPlayersAsInteger()) {
      Toast.makeText(this, getString(R.string.too_many_players), Toast.LENGTH_SHORT).show();
      return;
    }

    String gameName = item.getGamename();

    GameSettings.setGameName(gameName);
    GameSettings.setGameId(roomIds.get(gameName));
    GameSettings.setOwnerId(item.getHostId());
    GameSettings.setCanBreakWall(item.getCanBreakWall());
    GameSettings.setTimeLimit(item.getTimeLimit());
    GameSettings.setMaxDistance(item.getMaxDist());
    GameSettings.setMaxPlayers(item.getMaxPlayersAsInteger());
    GameSettings.setSpectate(checkBoxView.isChecked());
    showToast(R.string.joinging_message, gameName);

    if (!GameSettings.getSpectate()) {
      Map<String, String> query =
          ImmutableMap.of(
              "gameId", String.valueOf(roomIds.get(gameName)),
              "id", String.valueOf(GameSettings.getPlayerId()),
              "token", GameSettings.getPlayerToken());

      dataServer.sendRequest(
          ServerCommand.JOIN_GAME,
          query,
          new HttpConnector.Callback() {
            @Override
            public void handleResult(String data) {
              try {
                JSONObject result = new JSONObject(data);
                // TODO check for errors
                showRoomActivity();

              } catch (JSONException e) {
                e.printStackTrace();
              }
            }
          });
    } else {
      showRoomActivity();
    }
  }
Пример #6
0
  /**
   * 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
  }
Пример #7
0
  // region Initialization
  // ---------------------------------------------------------------------------------------------
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
      // If the application closed accidentally the player is set to spectator mode
      // At this point the game is corrupt and a lot of problems will arise
      // If this happens to the host it is extremely problematic
      // TODO make the host end the game if his game is corrupt
      GameSettings.loadFromBundle(savedInstanceState);
      GameSettings.setSpectate(true);
      Toast.makeText(this, getString(R.string.game_activity_closed), Toast.LENGTH_SHORT).show();
    }

    // Content of Activity
    // -----------------------------------------------------------------------------------------
    setContentView(R.layout.activity_game);

    // Sensor tracking
    // -----------------------------------------------------------------------------------------
    // Initialize sensorDataObservable and proximityObserver
    acceleration = 0;
    accelerationCount = 0;
    turns = 0;
    sensorDataObservable = new SensorDataObservable(this);
    isBellRinging = false;

    // Location tracking
    // -----------------------------------------------------------------------------------------
    // Initialize the stored locations to 0,0
    snappedGpsLoc = new LatLng(0, 0);
    gpsLoc = new LatLng(0, 0);
    if (savedInstanceState != null)
      travelledDistance = savedInstanceState.getDouble("travelledDistance", 0.0);
    else travelledDistance = 0.0;

    // Initialize location listener
    locationListener =
        new CustomLocationListener(
            this, // The activity that builds the google api
            this, // The LocationObserver
            1500, // Normal update frequency of the location
            500, // Fastest update frequency of the location
            LocationRequest.PRIORITY_HIGH_ACCURACY // Accuracy of the location
            );

    // Create the context used by the google api (just stores the google key)
    context = new GeoApiContext().setApiKey(getString(R.string.google_maps_key_server));

    // Permissions
    // -----------------------------------------------------------------------------------------
    // Request permissions before doing anything else (after initializing the location listener!)
    requestPermissions();

    // Map Object
    // -----------------------------------------------------------------------------------------
    // Create the map object
    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    map = new Map(mapFragment);

    // Player objects
    // -----------------------------------------------------------------------------------------
    // Create the player
    map.addMapItem(
        new Player(GameSettings.getPlayerId(), GameSettings.getPlayerName(), new LatLng(0.0, 0.0)));

    // Networking
    // -----------------------------------------------------------------------------------------
    // Create the gameUpdateHandler object
    connection = new SocketIoConnection("testA1", String.valueOf(GameSettings.getGameId()));

    gameUpdateHandler = new GameUpdateHandler(this, connection, map, context);
    gameEventHandler = new GameEventHandler(connection, this);

    if (savedInstanceState != null)
      // When the game is corrupt, you are considered dead.
      gameUpdateHandler.sendDeathMessage("//", "A corrupted game");

    // Create the data server
    dataServer = new HttpConnector(getString(R.string.dataserver_url));

    // Setup the game ui
    if (!GameSettings.isOwner()) {
      Button startButton = (Button) findViewById(R.id.start_game_button);
      startButton.setVisibility(View.GONE);
    }

    Button wallBreaker = (Button) findViewById(R.id.breakWallButton);
    wallBreaker.setVisibility(View.GONE);

    LinearLayout travelledDistanceContainer =
        (LinearLayout) findViewById(R.id.travelledDistanceContainer);
    travelledDistanceContainer.setVisibility(View.GONE);

    TextView travelledDistance = (TextView) findViewById(R.id.travelledDistance);
    travelledDistance.setVisibility(View.GONE);

    TextView travelledDistanceHead = (TextView) findViewById(R.id.travelledDistanceHead);
    travelledDistanceHead.setVisibility(View.GONE);

    findViewById(R.id.eventContainer).setVisibility(View.GONE);
    findViewById(R.id.countdown).setVisibility(View.GONE);
  }