示例#1
0
 /** Is called when the application is resumed */
 @Override
 public void onResume() {
   super.onResume();
   locationListener.startLocationUpdate(); // Start the location listener again
   getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // Keep screen on
   if (isAlive) {
     sensorDataObservable.Resume(); // Resume the sensor observer
     frequencyListener.run();
   }
 }
示例#2
0
 /** Is called when the application is paused */
 @Override
 public void onPause() {
   super.onPause();
   locationListener.stopLocationUpdate(); // Pauses the lcoation listener
   getWindow()
       .clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // Disable screen lock on
   if (isAlive) {
     frequencyListener.pause();
     sensorDataObservable.Pause(); // Pauses the sensor observer
   }
 }
示例#3
0
  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);
  }