Login + leaderboard
This commit is contained in:
parent
65daf1f7ff
commit
8e6a706c1c
|
|
@ -752,7 +752,7 @@ MonoBehaviour:
|
|||
m_Calls:
|
||||
- m_Target: {fileID: 353788374}
|
||||
m_TargetAssemblyTypeName: GameManager, Assembly-CSharp
|
||||
m_MethodName: Restart
|
||||
m_MethodName: Menu
|
||||
m_Mode: 1
|
||||
m_Arguments:
|
||||
m_ObjectArgument: {fileID: 0}
|
||||
|
|
@ -1868,7 +1868,7 @@ MonoBehaviour:
|
|||
m_Calls:
|
||||
- m_Target: {fileID: 353788374}
|
||||
m_TargetAssemblyTypeName: GameManager, Assembly-CSharp
|
||||
m_MethodName: Restart
|
||||
m_MethodName: Menu
|
||||
m_Mode: 1
|
||||
m_Arguments:
|
||||
m_ObjectArgument: {fileID: 0}
|
||||
|
|
|
|||
1077
Assets/Scenes/Login.unity
Normal file
1077
Assets/Scenes/Login.unity
Normal file
File diff suppressed because it is too large
Load Diff
7
Assets/Scenes/Login.unity.meta
Normal file
7
Assets/Scenes/Login.unity.meta
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 469333b49e5f2ea428467efd468af64e
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
5744
Assets/Scenes/Menu.unity
Normal file
5744
Assets/Scenes/Menu.unity
Normal file
File diff suppressed because it is too large
Load Diff
7
Assets/Scenes/Menu.unity.meta
Normal file
7
Assets/Scenes/Menu.unity.meta
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: d3ae632d33cb1684f843457f14138d90
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
52
Assets/Scripts/DataManager.cs
Normal file
52
Assets/Scripts/DataManager.cs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public static class DataManager{
|
||||
public const string API_Endpoint="https://109-237-27-65.ip.linodeusercontent.com/pogemoon/api/";
|
||||
private static string username;
|
||||
public static string Username{get{
|
||||
if(PlayerPrefs.HasKey("username")){
|
||||
return PlayerPrefs.GetString("username");
|
||||
}else{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
set{
|
||||
PlayerPrefs.SetString("username",value);
|
||||
PlayerPrefs.Save();
|
||||
}}
|
||||
public static bool LoggedIn {get{return PlayerPrefs.HasKey("username");}}
|
||||
|
||||
|
||||
public static int best{
|
||||
get{
|
||||
return (int)PlayerPrefs.GetFloat("best");
|
||||
}
|
||||
set{
|
||||
PlayerPrefs.SetFloat("best", value);
|
||||
PlayerPrefs.Save();
|
||||
}
|
||||
}
|
||||
|
||||
public static bool OnLoginDone(string username,string response){
|
||||
int uid = -1;
|
||||
try{
|
||||
uid = int.Parse(response);
|
||||
}catch{
|
||||
Debug.LogError("Error in registering : " + response);
|
||||
}
|
||||
|
||||
|
||||
if(uid >=0){
|
||||
Username = username +"#"+ uid;
|
||||
PlayerPrefs.SetString("username", Username);
|
||||
PlayerPrefs.Save();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/DataManager.cs.meta
Normal file
11
Assets/Scripts/DataManager.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6179ef5eb99010845b760cb4660ca4ec
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -24,9 +24,11 @@ public class GameManager : MonoBehaviour
|
|||
public void StartGame(){
|
||||
startPanel.SetActive(false);
|
||||
}
|
||||
|
||||
float moneyEarned =0;
|
||||
void Update(){
|
||||
txt_money.text = "$"+(PlayerController.t.position.y*moneyMultiplier).ToString("n4");
|
||||
if(!PlayerController.Started){return;}
|
||||
moneyEarned = (PlayerController.t.position.y*moneyMultiplier);
|
||||
txt_money.text = "$"+moneyEarned.ToString("n4");
|
||||
|
||||
txt_money.rectTransform.position = Camera.main.WorldToScreenPoint(PlayerController.t.position + txtMoneyOffset);
|
||||
|
||||
|
|
@ -64,9 +66,31 @@ public class GameManager : MonoBehaviour
|
|||
|
||||
finishedScoreTxt.text = txt_money.text;
|
||||
PlayerController.instance.GameOver();
|
||||
|
||||
if(!updatedBest){
|
||||
updatedBest=true;
|
||||
|
||||
StartCoroutine(updateBest());
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator updateBest(){
|
||||
WWWForm form = new WWWForm();
|
||||
form.AddField("username", DataManager.Username);
|
||||
form.AddField("best", (int)moneyEarned);
|
||||
DataManager.best = (int)moneyEarned;
|
||||
|
||||
WWW req = new WWW(DataManager.API_Endpoint +"set_best.php",form);
|
||||
yield return req;
|
||||
Debug.Log(req.text);
|
||||
}
|
||||
|
||||
bool updatedBest = false;
|
||||
|
||||
public void Restart(){
|
||||
SceneManager.LoadScene("Game");
|
||||
}
|
||||
public void Menu(){
|
||||
SceneManager.LoadScene("Menu");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
58
Assets/Scripts/Leaderboard.cs
Normal file
58
Assets/Scripts/Leaderboard.cs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class Leaderboard : MonoBehaviour
|
||||
{
|
||||
public GameObject rowPrefab;
|
||||
public Transform itemsParent;
|
||||
|
||||
public void Show(){
|
||||
gameObject.SetActive(true);
|
||||
|
||||
|
||||
StartCoroutine(show());
|
||||
}
|
||||
|
||||
|
||||
IEnumerator show(){
|
||||
WWW req = new WWW(DataManager.API_Endpoint +"get_leaderboard.php");
|
||||
yield return req;
|
||||
|
||||
Debug.Log("Leaderboard: " + req.text);
|
||||
|
||||
if(req.text.Contains("<td>") && req.text.Contains("<tr>")){
|
||||
|
||||
string[] col = {"<td>"};
|
||||
string[] row = {"<tr>"};
|
||||
|
||||
string[] columns = req.text.Split(col,System.StringSplitOptions.RemoveEmptyEntries);
|
||||
//Purge
|
||||
for(int i=0; i < itemsParent.childCount; i++){
|
||||
if(itemsParent.GetChild(i).gameObject != rowPrefab){
|
||||
Destroy(itemsParent.GetChild(i).gameObject);
|
||||
}else{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
rowPrefab.SetActive(true);
|
||||
for (int i =0; i < columns.Length; i++)
|
||||
{
|
||||
string column = columns[i];
|
||||
string[] rows = column.Split(row, System.StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
if(rows.Length < 2){
|
||||
Debug.Log("Error");
|
||||
}
|
||||
|
||||
|
||||
GameObject newRow = Instantiate(rowPrefab, itemsParent);
|
||||
newRow.transform.GetChild(0).GetComponent<Text>().text = $"{i+1}. {rows[0]}";
|
||||
newRow.transform.GetChild(1).GetComponent<Text>().text ="$"+rows[1];
|
||||
}
|
||||
rowPrefab.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Leaderboard.cs.meta
Normal file
11
Assets/Scripts/Leaderboard.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9e1b7dedd9a4c3a48beb8d9c72821cf1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
41
Assets/Scripts/Login.cs
Normal file
41
Assets/Scripts/Login.cs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
public class Login : MonoBehaviour
|
||||
{
|
||||
public InputField loginInput;
|
||||
public Button loginBtn;
|
||||
|
||||
|
||||
void Awake(){
|
||||
if(DataManager.LoggedIn){
|
||||
SceneManager.LoadScene(1);
|
||||
}
|
||||
loginBtn.onClick.AddListener(OnLogin);
|
||||
loginInput.onValueChanged.AddListener(OnInputChanged);
|
||||
|
||||
loginBtn.interactable=false;
|
||||
}
|
||||
public void OnLogin(){
|
||||
StartCoroutine(login(loginInput.text));
|
||||
}
|
||||
|
||||
void OnInputChanged(string newVal){
|
||||
loginBtn.interactable = newVal.Length > 2;
|
||||
}
|
||||
|
||||
IEnumerator login(string username){
|
||||
WWWForm form = new WWWForm();
|
||||
form.AddField("username", username);
|
||||
form.AddField("key","#2CuV1Bit^S!sW1ZcgRv8BhrO");
|
||||
|
||||
WWW req = new WWW(DataManager.API_Endpoint +"register_instant.php", form);
|
||||
yield return req;
|
||||
|
||||
if(DataManager.OnLoginDone(username, req.text)){
|
||||
SceneManager.LoadScene(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Login.cs.meta
Normal file
11
Assets/Scripts/Login.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4298e4b28e5f9e842a01e087801b1a17
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
45
Assets/Scripts/MenuManager.cs
Normal file
45
Assets/Scripts/MenuManager.cs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
public class MenuManager : MonoBehaviour
|
||||
{
|
||||
public Text txtUsername;
|
||||
public Text txtMoney;
|
||||
|
||||
public Button playBtn;
|
||||
public Button leaderboardBtn;
|
||||
public Leaderboard leaderboard;
|
||||
// public Store store;
|
||||
public static MenuManager instance;
|
||||
void Start()
|
||||
{
|
||||
instance = this;
|
||||
if(!DataManager.LoggedIn){SceneManager.LoadScene(0); return;}
|
||||
Refresh();
|
||||
|
||||
playBtn.onClick.AddListener(OnPlay);
|
||||
leaderboardBtn.onClick.AddListener(OnLeaderboardBtn);
|
||||
}
|
||||
|
||||
void OnPlay(){
|
||||
SceneManager.LoadScene(2);
|
||||
}
|
||||
|
||||
void OnLeaderboardBtn(){
|
||||
leaderboard.Show();
|
||||
}
|
||||
|
||||
public static void Refresh(){
|
||||
if(instance != null){
|
||||
instance.refresh();
|
||||
}
|
||||
}
|
||||
|
||||
void refresh(){
|
||||
|
||||
txtUsername.text = txtUsername.text.Replace("{username}", DataManager.Username);
|
||||
txtMoney.text = "Personal Best: $" + DataManager.best.ToString();
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/MenuManager.cs.meta
Normal file
11
Assets/Scripts/MenuManager.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 474b1819f34c1224192a1651fccc4bb7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -5,6 +5,12 @@ EditorBuildSettings:
|
|||
m_ObjectHideFlags: 0
|
||||
serializedVersion: 2
|
||||
m_Scenes:
|
||||
- enabled: 1
|
||||
path: Assets/Scenes/Login.unity
|
||||
guid: 469333b49e5f2ea428467efd468af64e
|
||||
- enabled: 1
|
||||
path: Assets/Scenes/Menu.unity
|
||||
guid: d3ae632d33cb1684f843457f14138d90
|
||||
- enabled: 1
|
||||
path: Assets/Scenes/Game.unity
|
||||
guid: 2cda990e2423bbf4892e6590ba056729
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user