Golf2D/Assets/Scripts/MainMenu.cs
2023-03-21 21:19:40 +05:30

156 lines
5.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Google;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class MainMenu : MonoBehaviour
{
public GameObject MainMenuPanel;
public GameObject SettingsPanel;
public GameObject LeaderboarPanel;
public GameObject LinkAccountPanel;
public InputField fhid_input;
public Button btnLink;
public Transform LeaderboardItemsParent;
public float transitionTime;
public LeanTweenType transitionEffect;
private Vector2 defaultCenter;
public Button copyBtn, muteBtn;
public Button signoutBtn;
public Sprite muteIcon, unmuteIcon;
public Text txtUserId;
void Awake(){
if(!DataManager.isLogged){
SceneManager.LoadScene(0);
return;
}
defaultCenter = MainMenuPanel.transform.position;
SettingsPanel.transform.position = MainMenuPanel.transform.position - new Vector3(10000,0);
SettingsPanel.SetActive(true);
LeaderboarPanel.SetActive(true);
LeaderboarPanel.transform.position = MainMenuPanel.transform.position + new Vector3(10000,0);
muteBtn.onClick.AddListener(ToggleMute);
copyBtn.onClick.AddListener(CopyId);
signoutBtn.onClick.AddListener(SignOut);
btnLink.onClick.AddListener(OnLinkClicked);
DataManager.RefreshLeaderboard();
}
public void Leave(){
Application.Quit();
}
void Start(){
Refresh();
// MessageBox.ShowMessage("Welcome to Infinite Golf 2D.\nThis is a slow paced 2d endless golf game, All the levels are proceduraly generated and you will be rewarded for putting the ball in every and each hole.\n\nGood Luck","Welcome");
}
void Refresh(){
txtUserId.text = Encryptor.intToString(DataManager.userData.id);
if(AudioManager.isMute){
muteBtn.transform.GetChild(0).GetComponent<Image>().sprite = muteIcon;
}else{
muteBtn.transform.GetChild(0).GetComponent<Image>().sprite = unmuteIcon;
}
}
public void SettingsPage(){
LeanTween.moveX(MainMenuPanel, 10000, transitionTime).setEase(transitionEffect);
LeanTween.moveX(SettingsPanel, defaultCenter.x, transitionTime).setEase(transitionEffect);
}
public void LeaderboarPage(){
LeanTween.moveX(MainMenuPanel, -10000, transitionTime).setEase(transitionEffect);
LeanTween.moveX(LeaderboarPanel, defaultCenter.x, transitionTime).setEase(transitionEffect);
RefreshLeaderboard();
}
public void MainPage(){
LeanTween.moveX(MainMenuPanel, defaultCenter.x, transitionTime).setEase(transitionEffect);
LeanTween.moveX(SettingsPanel, -10000, transitionTime).setEase(transitionEffect);
LeanTween.moveX(LeaderboarPanel, 10000, transitionTime).setEase(transitionEffect);
}
public void OpenLinkPanel(){
if(DataManager.userData.faucetId > 0){
MessageBox.ShowMessage("You've already linked this player account to Faucet Hub.", "Already Linked");
return;
}
LinkAccountPanel.transform.localScale = Vector3.zero;
LinkAccountPanel.SetActive(true);
LeanTween.scale(LinkAccountPanel, Vector3.one, transitionTime/2f).setEase(LeanTweenType.easeInCirc);
}
public async void OnLinkClicked(){
btnLink.interactable=false;
if(fhid_input.text.Length <= 0){
Debug.Log("Empty FHID was entered");
MessageBox.ShowMessage("Please enter a valid faucet hub ID","Invalid FH ID");
return;
}
int result = await DataManager.LinkFH(fhid_input.text);
if(result == 0){
MessageBox.ShowMessage("Congrats! You've successfully linked this player account with Faucet Hub. Enjoy!", "Success");
LinkAccountPanel.SetActive(false);
}else{
MessageBox.ShowMessage("Something went wrong trying to link this play account to FH ID of " + fhid_input.text, "Link failed");
}
btnLink.interactable=true;
}
public void CopyId(){
GUIUtility.systemCopyBuffer = txtUserId.text;
copyBtn.transform.Find("lbl").GetComponent<Text>().text = "Copied";
}
public void ToggleMute(){
AudioManager.ToggleMute();
Refresh();
}
public void Info(){
MessageBox.ShowMessage(@"This is a game where you can relax while playing,
Listen to a podcast or to music while playing this to get the best out of it!
Any feedback or suggestion can be directed to us via this email
sewmina7@gmail.com","About");
}
async void RefreshLeaderboard(){
UpdateLeaderboardUI();
await DataManager.RefreshLeaderboard();
UpdateLeaderboardUI();
}
void UpdateLeaderboardUI(){
for(int i =0; i < DataManager.Leaderboard.Count; i++){
LeaderboardItemsParent.GetChild(i).GetChild(0).GetComponent<Text>().text = $"{i+1}." + DataManager.Leaderboard[i].DisplayName;
// LeaderboardItemsParent.GetChild(i).GetChild(0).GetComponent<Text>().text = $"{i+1}." + DataManager.Leaderboard[i].name;
LeaderboardItemsParent.GetChild(i).GetChild(1).GetComponent<Text>().text = DataManager.Leaderboard[i].topScore.ToString();
LeaderboardItemsParent.GetChild(i).GetComponent<Image>().CrossFadeAlpha((DataManager.Leaderboard[i].name == DataManager.userData.username) ? 0.9f : 0,0.5f,true);
}
}
void SignOut(){
DataManager.Signout();
SceneManager.LoadScene(0);
}
}