/** * 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 }
public void startGame(LatLng startPos) { // Tell the other players that the game has started if (GameSettings.isOwner()) gameUpdateHandler.sendStartGame(startPos); // Start sensor tracking // Listen to proximity updates sensorDataObservable.startSensorTracking( SensorFlag.PROXIMITY, new SensorDataObserver() { /** * Is called when the proximity sensor detects something * * @param observable The observable that has changed * @param data Extra data attached by observable */ @Override public void updateSensor(SensorDataObservable observable, Object data) { // Check whether it was the proximity sensor that detected something if (observable != sensorDataObservable) return; sensorDataObservable.resetSensorData( SensorFlag.PROXIMITY); // This was moved from within SensorDataObservable if (GameSettings.getCanBreakWall()) breakWall(null); // Activiate break wall button } /** * The amount of times the sensor can detect useful information before updateSensor is * called */ @Override public int getCountLimit() { return 1; } }); // Listen to accelerometer updates sensorDataObservable.startSensorTracking( SensorFlag.ACCELEROMETER, new SensorDataObserver() { @Override /** * Is called when the accelerometer sensor detects something * * @param observable The observable that has changed * @param data Extra data attached by observable */ public void updateSensor(SensorDataObservable observable, Object data) { // Check whether it was the proximity sensor that detected something if (observable != sensorDataObservable) return; if (data instanceof HorizontalAccelerationDataHolder) { HorizontalAccelerationDataHolder holder = (HorizontalAccelerationDataHolder) data; acceleration += holder.getAccelerationMagnitude(); accelerationCount += 1; } if (currentEvent != null && currentEvent instanceof ShowOffEvent) { ((TextView) findViewById(R.id.eventValue)) .setText( getString(R.string.show_off_event_text) .replaceAll("%value", "" + Math.floor(acceleration))); } } /** * The amount of times the sensor can detect useful information before updateSensor is * called */ @Override public int getCountLimit() { return -1; } }); // Listen to gyroscope sensorDataObservable.startSensorTracking( SensorFlag.GYROSCOPE, new SensorDataObserver() { @Override public void updateSensor(SensorDataObservable observable, Object data) { // Check whether it was the proximity sensor that detected something if (observable != sensorDataObservable) return; if (data instanceof GyroscopeDataHolder) { GyroscopeDataHolder holder = (GyroscopeDataHolder) data; turns = holder.getCount(); } if (currentEvent != null && currentEvent instanceof TurnEvent) { ((TextView) findViewById(R.id.eventValue)) .setText( getString(R.string.turn_event_text) .replaceAll("%value", "" + Math.floor(turns))); } } @Override public int getCountLimit() { return 1; } }); // Listen to microphone frequencyListener = new FrequencyListener( new Handler( new Handler.Callback() { @Override public boolean handleMessage(Message msg) { handleBellDetected(); return false; } }), 44100, 1650, 2450, 16384); frequencyListener.run(); // Start the FrequencyListener // Set the correct buttons to visible if (!GameSettings.getSpectate()) { if (GameSettings.getCanBreakWall()) { // TODO hide the breakWall button when there is a proximity sensor Button wallBreaker = (Button) findViewById(R.id.breakWallButton); wallBreaker.setVisibility(View.VISIBLE); } LinearLayout travelledDistanceContainer = (LinearLayout) findViewById(R.id.travelledDistanceContainer); travelledDistanceContainer.setVisibility(View.VISIBLE); TextView travelledDistance = (TextView) findViewById(R.id.travelledDistance); travelledDistance.setVisibility(View.VISIBLE); TextView travelledDistanceHead = (TextView) findViewById(R.id.travelledDistanceHead); travelledDistanceHead.setVisibility(View.VISIBLE); } // Set start time (in order to measure total playtime) startTime = System.currentTimeMillis(); if (GameSettings.isOwner()) // Set the amount of players that are alive playersAliveCount = GameSettings.getPlayersInGame().size(); // Set the initial location mapCenter = startPos; if (GameSettings.getMaxDistance() > 0) // Draw the border on the map // The distance is GameSettings.getMaxDistance() - WARNING_DISTANCE_TO_WALL so that the // player won't die right away after crossing the wall on the map. map.drawBorder(mapCenter, GameSettings.getMaxDistanceInMeters()); // Activate end game timer if (GameSettings.getTimeLimit() >= 0) { if (GameSettings.isOwner()) { // End the game in FINAL_SCORE_TIMEOUT seconds new Handler() .postDelayed( new Runnable() { @Override public void run() { endGame(); } }, 1000 * GameSettings.getTimeLimit() * 60); } // Update countdown endTime = startTime + 1000 * GameSettings.getTimeLimit() * 60; countdown = new Handler(); countdownRunnable = new Runnable() { @Override public void run() { TextView countdownText = (TextView) findViewById(R.id.countdown); if (endTime > System.currentTimeMillis()) { countdownText.setText(formatTime(endTime - System.currentTimeMillis())); countdown.postDelayed(this, 1000); } else { countdownText.setText(formatTime(0)); countdownText.setVisibility(View.GONE); } } }; countdown.postDelayed(countdownRunnable, 1000); findViewById(R.id.countdown).setVisibility(View.VISIBLE); } // Start the events if (GameSettings.isOwner() && HAS_EVENTS) gameEventHandler.start(); // Set the player to alive if (!GameSettings.getSpectate()) isAlive = true; else isAlive = false; hasEnded = false; showNotification(getString(R.string.game_started), Toast.LENGTH_SHORT); }