// Update method to update the game play public void update(float dt, float fps) { FPS = fps; switch (GameState) { case 0: { // Only if game is not paused if (!GamePaused) { bgX -= ScrollSpeed * dt; // Speed of background scrolling // Reset once reaches 0 if (bgX < -ScreenWidth) { bgX = 0; } // Only when game is active we update the following if (GameActive) { SpawnTimer += dt; timer += dt; if (timer > 5.f) { ScrollSpeed += 100; timer = 0; } if (SpawnTimer > SpawnRate) { FetchObstacle(); SpawnTimer = 0.f; } stickman_anim.update(System.currentTimeMillis()); // Adventure mode if (GameMode == 1) { Progress_bar.setPosX(Progress_bar.getPosX() + BarSpeed * 0.015f); if (Progress_bar.getPosX() > DestinationPoint) { Win = true; GameActive = false; } } } // Feedback for game over if (GameActive == false) { if (UpdateHighscore && GameMode == 0) { Vector<Integer> highscores = appPrefs.getHighscore(); Vector<Integer> updatedscores = new Vector<Integer>(); boolean scoreentered = false; int index = 0; for (int i = 0; i < highscores.size(); ++i) { if (score > highscores.get(index) && !scoreentered) { updatedscores.addElement(score); scoreentered = true; } else { updatedscores.addElement(highscores.get(index)); ++index; } } for (int i = 0; i < updatedscores.size(); ++i) { appPrefs.setHighscore(i, updatedscores.get(i)); } UpdateHighscore = false; } // Vibration feedback vibrateTime += dt; if (vibrateTime > MaxVibrateTime) { stopVibrate(); } else { startVibrate(); } } if (nearestObstacle.isActive()) { // Detecting user tap for tapping obstacle if (nearestObstacle.getType() == Obstacle.TYPE.T_TAP && Tapped == true) { score += 10; nearestObstacle.setActive(false); DirectionVector.SetZero(); Tapped = false; } // Detecting user swipe direction for direction obstacle else if (DirectionVector.IsZero() == false && Obstacle.fromInteger(ProcessSwipe(DirectionVector)) == nearestObstacle.getType()) { score += 10; nearestObstacle.setActive(false); DirectionVector.SetZero(); } DirectionVector.SetZero(); } // Updating game elements for (int i = 0; i < obstacleList.length; ++i) { if (obstacleList[i].isActive()) { obstacleList[i].setPosX(obstacleList[i].getPosX() - ScrollSpeed * dt); // if out of screen if (obstacleList[i].getPosX() < 0) { obstacleList[i].setActive(false); } // Only if game is active we check these collisions if (GameActive == true) { // Player collision against obstacles if (CheckCollision( stickman_anim.getX(), stickman_anim.getY(), stickman_anim.getSpriteWidth(), stickman_anim.getSpriteHeight(), (int) obstacleList[i].getPosX(), (int) obstacleList[i].getPosY(), obstacleList[i].getImgWidth(), obstacleList[i].getImgHeight())) { obstacleList[i].setActive(false); GameActive = false; // Game status set to false // Enable buttons Restart_button.setActive(true); Mainmenu_button.setActive(true); } // Get nearest obstacle if (nearestObstacle.isActive() == false) { nearestObstacle = obstacleList[i]; } else if (obstacleList[i].getPosX() < nearestObstacle.getPosX()) { nearestObstacle = obstacleList[i]; } } } } } } break; } }