157 lines
4.2 KiB
C#
157 lines
4.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
using UnityEngine.UI;
|
|
using Unity.VisualScripting;
|
|
|
|
public class GameManager : MonoBehaviour
|
|
{
|
|
public static GameManager Instance { get; private set; }
|
|
|
|
public float initialGameSpeed = 5f;
|
|
public float gameSpeedIncrease = 0.1f;
|
|
public static float timeElapsed = 0;
|
|
public float gameSpeed { get; private set; }
|
|
|
|
public TextMeshProUGUI scoreText;
|
|
public TextMeshProUGUI hiscoreText;
|
|
public GameObject startScreen;
|
|
public GameObject practiceGameOver;
|
|
public GameObject duelGameOver;
|
|
public GameObject processingObject, closeButton;
|
|
|
|
private Player player;
|
|
private Spawner spawner;
|
|
|
|
private float score;
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
}
|
|
else
|
|
{
|
|
DestroyImmediate(gameObject);
|
|
}
|
|
}
|
|
private void OnDestroy()
|
|
{
|
|
if (Instance == this)
|
|
{
|
|
Instance = null;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
player = FindObjectOfType<Player>();
|
|
spawner = FindObjectOfType<Spawner>();
|
|
|
|
// NewGame();
|
|
startScreen.SetActive(true);
|
|
spawner.gameObject.SetActive(false);
|
|
player.gameObject.SetActive(false);
|
|
ShowGameOver(false);
|
|
UpdateHiscore();
|
|
}
|
|
public void NewGame()
|
|
{
|
|
Obstacle[] obstacles = FindObjectsOfType<Obstacle>();
|
|
|
|
foreach (var obstacle in obstacles)
|
|
{
|
|
Destroy(obstacle.gameObject);
|
|
}
|
|
|
|
score = 0f;
|
|
timeElapsed = 0f;
|
|
gameSpeed = initialGameSpeed;
|
|
enabled = true;
|
|
|
|
player.gameObject.SetActive(true);
|
|
spawner.gameObject.SetActive(true);
|
|
ShowGameOver(false);
|
|
startScreen.SetActive(false);
|
|
}
|
|
|
|
public void GameOver()
|
|
{
|
|
gameSpeed = 0f;
|
|
enabled = false;
|
|
|
|
player.gameObject.SetActive(false);
|
|
spawner.gameObject.SetActive(false);
|
|
ShowGameOver(true);
|
|
GameOverHighscore();
|
|
UpdateHiscore();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if(player.gameObject.activeSelf){
|
|
gameSpeed += gameSpeedIncrease * Time.deltaTime;
|
|
score += gameSpeed * Time.deltaTime;
|
|
scoreText.text = Mathf.FloorToInt(score).ToString("D5");
|
|
timeElapsed += Time.deltaTime;
|
|
}
|
|
}
|
|
private void UpdateHiscore()
|
|
{
|
|
if(!HighscoreModeManager.isPraticeMode){
|
|
hiscoreText.text = Mathf.FloorToInt(HighscoreModeManager.highscore).ToString("D5");
|
|
return;
|
|
}
|
|
float hiscore = PlayerPrefs.GetFloat("hiscore", 0);
|
|
if (score > hiscore)
|
|
{
|
|
hiscore = score;
|
|
PlayerPrefs.SetFloat("hiscore", hiscore);
|
|
}
|
|
hiscoreText.text = Mathf.FloorToInt(hiscore).ToString("D5");
|
|
}
|
|
|
|
public void SetHighscore(int highscore){
|
|
PlayerPrefs.SetFloat("highscore", highscore);
|
|
hiscoreText.text = Mathf.FloorToInt(highscore).ToString("D5");
|
|
}
|
|
|
|
public void ShowGameOver(bool show){
|
|
if(!show){
|
|
practiceGameOver.SetActive(false);
|
|
duelGameOver.SetActive(false);
|
|
return;
|
|
}
|
|
if(HighscoreModeManager.isPraticeMode){
|
|
practiceGameOver.SetActive(show);
|
|
}else{
|
|
duelGameOver.SetActive(show);
|
|
}
|
|
}
|
|
|
|
|
|
public void GameOverHighscore(){
|
|
if(HighscoreModeManager.isPraticeMode){
|
|
}else{
|
|
StartCoroutine(UpdateLeaderboard());
|
|
}
|
|
}
|
|
|
|
IEnumerator UpdateLeaderboard(){
|
|
yield return new WaitForSeconds(1f);
|
|
closeButton.SetActive(false);
|
|
processingObject.SetActive(true);
|
|
string url = $"{DuelFi.VALIDATOR_URL}/updateLeaderboardScore?leaderboard_id={HighscoreModeManager.leaderboarId}&player_key={HighscoreModeManager.playerKey}&score={Mathf.FloorToInt(score)}";
|
|
WWW www = new WWW(url);
|
|
yield return www;
|
|
if(www.error == null){
|
|
Debug.Log("Leaderboard updated successfully");
|
|
}else{
|
|
Debug.Log("Error updating leaderboard: " + www.error);
|
|
}
|
|
closeButton.SetActive(true);
|
|
processingObject.SetActive(false);
|
|
}
|
|
} |