Golf2D/Assets/Scripts/MainMenu.cs
2023-05-20 13:49:50 +05:30

184 lines
7.0 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
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 Image totalScoreBG, scoreBG;
public Text txtUserId;
public Text txtSeasonTimer;
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;
}
DateTime now = DateTime.Now;
DateTime nextSeason = new DateTime(now.Year, (now.Month < 12) ? now.Month+1 : 1,1);
txtSeasonTimer.text = "Season Ends In : <size=80>" +(nextSeason - now).Days + "</size> Days";
}
public void ShowInfoSeason(){
MessageBox.ShowMessage("When the new season begins your Score and Top Score will reset\n By the end of this season, Current scores will be preserved and can be seen on this season's leaderboard", "Season info");
}
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();
ToggleTotalScoreLeaderboardAsync(false);
}
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(){
if(DataManager.Leaderboard == null){return;}
if(DataManager.Leaderboard.Count <= 0){return;}
for(int i =0; i < DataManager.Leaderboard.Count; i++){
LeaderboardItemsParent.GetChild(i).GetChild(0).GetComponent<Text>().text = $"{DataManager.Leaderboard[i].position}." + 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].Score.ToString();
LeaderboardItemsParent.GetChild(i).GetChild(2).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);
}
}
public async void ToggleTotalScoreLeaderboardAsync(bool total){
totalScoreBG.color = new Color(totalScoreBG.color.r, totalScoreBG.color.g, totalScoreBG.color.b, total? 1 : 0.1f);
scoreBG.color = new Color(scoreBG.color.r, scoreBG.color.g, scoreBG.color.b, total? 0.1f : 1f);
await DataManager.RefreshLeaderboard(total);
UpdateLeaderboardUI();
}
void SignOut(){
DataManager.Signout();
SceneManager.LoadScene(0);
}
}