Files
PuzzleColorBall/Assets/Scripts/jatekmanager.cs

226 lines
5.6 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Sockets;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class jatekmanager : MonoBehaviour
{
//gamestate-s cuccok
public static jatekmanager Instance;
public GameState State;
public static event Action<GameState> OnGameStateChanged;
//gameobjectek,gombok,scriptek
2023-05-13 16:51:40 +02:00
public GameObject playGomb;
public GameObject garazs;
public GameObject homeGomb;
public GameObject settingsGomb;
public GameObject shopGomb;
2023-03-21 15:36:23 +01:00
public GameObject goLeftButton;
public GameObject jumpButton;
public GameObject goRightButton;
public TMP_Text scoreText;
public TMP_Text timerText;
2023-03-20 19:56:27 +01:00
private DatabaseData db;
private UsernameHandler usernameHandler;
private Score score;
private Timer timer;
2023-05-02 13:46:38 +02:00
private CoinCounter cc;
2023-03-20 19:56:27 +01:00
private void Awake()
{
2023-03-20 19:56:27 +01:00
Application.targetFrameRate = 60;
2023-03-23 19:11:36 +01:00
Instance = this;
2023-03-20 19:56:27 +01:00
db = FindObjectOfType<DatabaseData>();
usernameHandler = FindObjectOfType<UsernameHandler>();
score = FindObjectOfType<Score>();
timer = FindObjectOfType<Timer>();
2023-05-02 13:46:38 +02:00
cc = FindObjectOfType<CoinCounter>();
}
private void Start()
{
UpdateGameState(GameState.Home);
}
public void UpdateGameState(GameState newState)
{
State = newState;
switch (newState)
{
case GameState.Home:
HandleHome();
break;
case GameState.Settings:
HandleSettings();
break;
case GameState.Shop:
HandleShop();
break;
case GameState.Game:
HandleGame();
break;
case GameState.Meghaltal:
HandleMeghaltal();
break;
default:
throw new ArgumentOutOfRangeException(nameof(newState), newState, null);
}
OnGameStateChanged?.Invoke(newState);
}
public enum GameState
{
Home,
Settings,
Shop,
Game,
Meghaltal
}
public void ChangeToHome()
{
UpdateGameState(GameState.Home);
homeGomb.SetActive(true);
settingsGomb.SetActive(true);
shopGomb.SetActive(true);
2023-05-13 16:51:40 +02:00
2023-03-21 15:36:23 +01:00
goLeftButton.SetActive(false);
jumpButton.SetActive(false);
goRightButton.SetActive(false);
scoreText.gameObject.SetActive(false);
timerText.gameObject.SetActive(false);
}
private async void HandleHome()
2023-03-21 15:36:23 +01:00
{
//deactivate buttons
StartCoroutine(TimerHome());
2023-05-13 16:51:40 +02:00
playGomb.SetActive(true);
GetComponent<GroundController>().enabled = false;
GetComponent<PlayerController>().enabled = false;
2023-03-21 15:36:23 +01:00
goLeftButton.SetActive(false);
jumpButton.SetActive(false);
goRightButton.SetActive(false);
scoreText.gameObject.SetActive(false);
timerText.gameObject.SetActive(false);
garazs.SetActive(true);
}
IEnumerator TimerHome()
{
yield return new WaitForSecondsRealtime(2);
2023-05-13 16:51:40 +02:00
}
public void ChangeToSettings()
{
UpdateGameState(GameState.Settings);
2023-05-13 16:51:40 +02:00
playGomb.SetActive(false);
}
private async void HandleSettings()
{
2023-05-13 16:51:40 +02:00
playGomb.SetActive(false);
2023-03-21 15:36:23 +01:00
goLeftButton.SetActive(false);
jumpButton.SetActive(false);
goRightButton.SetActive(false);
scoreText.gameObject.SetActive(false);
timerText.gameObject.SetActive(false);
2023-05-13 16:51:40 +02:00
playGomb.SetActive(false);
}
public void ChangeToShop()
{
UpdateGameState(GameState.Shop);
2023-05-13 16:51:40 +02:00
}
private async void HandleShop()
{
2023-05-13 16:51:40 +02:00
playGomb.SetActive(false);
2023-03-21 15:36:23 +01:00
goLeftButton.SetActive(false);
jumpButton.SetActive(false);
goRightButton.SetActive(false);
scoreText.gameObject.SetActive(false);
timerText.gameObject.SetActive(false);
}
public void ChangeToGame()
{
homeGomb.SetActive(false);
settingsGomb.SetActive(false);
shopGomb.SetActive(false);
2023-05-13 16:51:40 +02:00
playGomb.SetActive(false);
2023-03-20 19:56:27 +01:00
UpdateGameState(GameState.Game);
2023-05-13 16:51:40 +02:00
}
IEnumerator TimerGame()
{
yield return new WaitForSecondsRealtime(1);
garazs.SetActive(false);
2023-05-13 16:51:40 +02:00
}
private async void HandleGame()
{
StartCoroutine(TimerGame());
2023-05-13 16:51:40 +02:00
GetComponent <GroundController> ().enabled = true;
GetComponent <PlayerController>().enabled = true;
2023-05-02 13:46:38 +02:00
2023-05-13 16:51:40 +02:00
2023-03-21 15:36:23 +01:00
goLeftButton.SetActive(true);
jumpButton.SetActive(true);
goRightButton.SetActive(true);
scoreText.gameObject.SetActive(true);
2023-05-13 16:51:40 +02:00
timerText.gameObject.SetActive(true);;
2023-05-02 13:46:38 +02:00
2023-03-20 19:56:27 +01:00
timer.playTime.Start();
2023-05-13 16:51:40 +02:00
}
public void ChangeToMeghaltal()
{
UpdateGameState(GameState.Meghaltal);
}
private async void HandleMeghaltal()
{
2023-03-20 19:56:27 +01:00
//ora leallitasa
timer.playTime.Stop();
//Valtson at high score tabla scenebe utana ha megnyomott egy gombot ugy menjen vissza a menube
//toltse fel az adatokat a run-rol
db.PostNewScoreData(usernameHandler.userid, score.score, timer.convertTimeToString());
2023-05-02 13:46:38 +02:00
//coin feltoltes
db.PostNewCoinData(cc.coin, usernameHandler.userid);
2023-03-20 19:56:27 +01:00
SceneUIManager.LoadScene(1); //HighScore scene
2023-05-13 16:51:40 +02:00
/*
homeGomb.SetActive(true);
GetComponent<GroundController>().enabled = false;
GetComponent<PlayerController>().enabled = false;
2023-03-20 19:56:27 +01:00
garazs.SetActive(true);*/
}
2023-03-20 19:56:27 +01:00
}