2022-01-29 00:32:48 +00:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.UI;
|
|
|
|
using TMPro;
|
|
|
|
public class UiController : MonoBehaviour
|
|
|
|
{
|
|
|
|
public GameObject player = null;
|
2022-01-29 18:13:21 +00:00
|
|
|
private float startPosition;
|
2022-01-29 00:32:48 +00:00
|
|
|
public TextMeshProUGUI uiDistance;
|
2022-01-29 22:48:27 +00:00
|
|
|
public TextMeshProUGUI uiHighScore;
|
|
|
|
|
2022-01-30 10:43:57 +00:00
|
|
|
public GameObject menuCamera;
|
|
|
|
public GameObject playerCamera;
|
|
|
|
|
2022-01-30 11:48:10 +00:00
|
|
|
public static bool isInMenu = true;
|
|
|
|
|
2022-01-29 22:48:27 +00:00
|
|
|
public static float distance = 0;
|
|
|
|
public static int highScore = 0;
|
2022-01-29 10:54:13 +00:00
|
|
|
private float oldDistance = 0.0f;
|
2022-01-29 00:32:48 +00:00
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
2022-01-29 18:13:21 +00:00
|
|
|
startPosition = this.player.transform.position.z;
|
2022-01-29 11:21:42 +00:00
|
|
|
Cursor.lockState = CursorLockMode.None;
|
|
|
|
Cursor.visible = true;
|
2022-01-29 21:45:27 +00:00
|
|
|
|
2022-01-29 22:48:27 +00:00
|
|
|
UiController.LoadGame();
|
2022-01-29 21:45:27 +00:00
|
|
|
}
|
2022-01-30 11:48:10 +00:00
|
|
|
public void OnNewGameBtnClick()
|
2022-01-30 10:43:57 +00:00
|
|
|
{
|
2022-01-30 11:48:10 +00:00
|
|
|
UiController.isInMenu = false;
|
2022-01-30 10:43:57 +00:00
|
|
|
menuCamera.SetActive(false);
|
|
|
|
playerCamera.SetActive(true);
|
|
|
|
}
|
2022-01-29 22:48:27 +00:00
|
|
|
public static void SaveGame()
|
2022-01-29 21:45:27 +00:00
|
|
|
{
|
2022-01-29 23:05:42 +00:00
|
|
|
if (UiController.distance > UiController.highScore)
|
|
|
|
{
|
|
|
|
PlayerPrefs.SetInt("HighestScore", (int)UiController.distance);
|
|
|
|
PlayerPrefs.Save();
|
|
|
|
}
|
2022-01-29 21:45:27 +00:00
|
|
|
Debug.Log("Game data saved!");
|
|
|
|
}
|
2022-01-29 22:48:27 +00:00
|
|
|
static void LoadGame()
|
2022-01-29 21:45:27 +00:00
|
|
|
{
|
|
|
|
if (PlayerPrefs.HasKey("HighestScore"))
|
|
|
|
{
|
2022-01-29 22:48:27 +00:00
|
|
|
UiController.highScore = PlayerPrefs.GetInt("HighestScore");
|
|
|
|
Debug.Log(UiController.highScore);
|
2022-01-29 21:45:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-29 22:48:27 +00:00
|
|
|
UiController.highScore = 0;
|
2022-01-29 21:45:27 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 00:32:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
2022-01-29 22:48:27 +00:00
|
|
|
UiController.distance = this.player.transform.position.z - startPosition;
|
2022-01-29 10:54:13 +00:00
|
|
|
if (oldDistance < distance)
|
|
|
|
{
|
2022-01-29 11:21:42 +00:00
|
|
|
uiDistance.text = "Distance : " + distance.ToString("0");
|
2022-01-29 22:48:27 +00:00
|
|
|
uiHighScore.text = "HighScore : " + UiController.highScore.ToString("0");
|
2022-01-29 10:54:13 +00:00
|
|
|
oldDistance = distance;
|
|
|
|
}
|
2022-01-29 00:32:48 +00:00
|
|
|
}
|
2022-01-30 11:29:47 +00:00
|
|
|
public void FixedUpdate(){
|
|
|
|
this.menuCamera.transform.position = this.menuCamera.transform.position + Vector3.forward * 0.05f;
|
|
|
|
}
|
2022-01-29 11:21:42 +00:00
|
|
|
|
|
|
|
//MENU
|
|
|
|
public void exitGame()
|
|
|
|
{
|
|
|
|
Application.Quit();
|
|
|
|
}
|
2022-01-29 00:32:48 +00:00
|
|
|
}
|