// Update is called once per frame private void Update() { //todo change stuff later to an observer pattern PlayerController playerController = m_player.GetComponent <PlayerController>(); CameraController cameraController = m_camera.GetComponent <CameraController>(); //check if the back button has been pressed if (Input.GetKeyDown(KeyCode.Escape)) { MemoryCard.LoadMenu(); } //has the player beaten the level / fallen outside the level / still playing if (playerController.isFinished()) { //remove player control because he has beaten the level p_levelControl.GetComponent <LevelTilt>().m_playerHasControl = false; //update the highscore if the player was faster if (p_timeInSeconds < p_bestTimeInSeconds || p_bestTimeInSeconds == 0) { MemoryCard.SaveHighscore(new Highscore(MemoryCard.GetScene(), p_timeInSeconds)); m_bestTime.text = p_timeInSeconds.ToString("f2"); } //start cameranimation once player is out of bounds if (playerController.isOutOfBounds()) { p_lookAtPosition.transform.position = new Vector3(m_endCamera, 0, 0); m_camera.GetComponent <CameraController>().m_lookAt = p_lookAtPosition.transform; m_camera.GetComponent <CameraController>().m_smoothTime = p_cameraSmoothTime * 4; if (m_camera.transform.position.x > m_endCamera / 2) { MemoryCard.LoadNextLevel(); } } } else if (playerController.isOutOfBounds()) { ResetLevel(); playerController.setOutOfBounds(false); } else { //start the timer once the player has control if (p_levelControl.GetComponent <LevelTilt>().m_playerHasControl) { p_timeInSeconds += Time.deltaTime; m_currentTime.text = p_timeInSeconds.ToString("f2"); } } }
/// <summary> /// Loads a new map, updates the gui and initialises animation. /// </summary> /// <param name="index">Which map to load.</param> private void LoadPreview(int index) { float x = 0; //unload current map if (p_currentMapPreview != null) { x = p_currentMapPreview.transform.position.x; //save the map position before we instantiate another map Destroy(p_currentMapPreview); } //load new map and initialise string sceneName = MemoryCard.GetScene(index); p_currentMapPreview = (GameObject)Instantiate(Resources.Load("prefab_" + sceneName)); p_currentMapPreview.transform.localScale = new Vector3(1, 1, 1) * 0.5f; p_currentMapPreview.transform.Rotate(new Vector3(1, 0, 0), -50, Space.World); //spawn the new map on the left or right side of the screen dependent on last mapanimation if (x > 0) { p_currentMapPreview.transform.position = p_mapLeftPosition; p_currentMapPreview.transform.Translate(2, 0, 0, Space.World); //translate it a little bit so the if statement for loading a new map wont be triggered } else { p_currentMapPreview.transform.position = p_mapRightPosition; p_currentMapPreview.transform.Translate(-2, 0, 0, Space.World); } p_mapGoToThisPosition = p_mapMiddlePosition; //new destination for the map is the center //update the gui, highscore float bestTime = MemoryCard.LoadHighScore().timeInSeconds; if (bestTime != 0) { p_textString = "level " + (index + 1) + " / " + MemoryCard.levelSize + "\nbest time " + bestTime.ToString("f2") + "s"; } else { p_textString = "level " + (index + 1) + " / " + MemoryCard.levelSize + "\nbest time --.--s"; } }