66 lines
1.8 KiB
C#
66 lines
1.8 KiB
C#
using UnityEngine;
|
|
using TMPro;
|
|
using System.Collections;
|
|
|
|
public class TimeAliveMoney : MonoBehaviour
|
|
{
|
|
public float moneyPerSecond = 0.1f;
|
|
public TMP_Text moneyText;
|
|
public TMP_Text totalMoneyText;
|
|
public Player player;
|
|
private float timeAlive = 0f;
|
|
private float totalMoneyEarned = 0f;
|
|
|
|
private void Start()
|
|
{
|
|
totalMoneyEarned = PlayerPrefs.GetFloat("Money", 0f);
|
|
totalMoneyText.text = ((int)totalMoneyEarned).ToString() + "$";
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (player.gameStarted && player.isAlive == true) {
|
|
timeAlive += Time.deltaTime;
|
|
float earnedMoney = timeAlive * moneyPerSecond;
|
|
if (moneyText != null)
|
|
moneyText.SetText(((int)earnedMoney).ToString() + "$");
|
|
}
|
|
}
|
|
|
|
float moneyEarned => timeAlive * moneyPerSecond;
|
|
public void OnPlayerDeath()
|
|
{
|
|
totalMoneyEarned += timeAlive * moneyPerSecond;
|
|
SaveMoney();
|
|
StartCoroutine(SaveDataToServer((int)moneyEarned));
|
|
|
|
timeAlive = 0f;
|
|
totalMoneyText.text = ((int)totalMoneyEarned).ToString() + "$";
|
|
}
|
|
|
|
|
|
private void SaveMoney()
|
|
{
|
|
PlayerPrefs.SetFloat("Money", totalMoneyEarned);
|
|
PlayerPrefs.Save();
|
|
|
|
}
|
|
|
|
IEnumerator SaveDataToServer(int _moneyEarned){
|
|
WWWForm f1 = new WWWForm();
|
|
f1.AddField("username", DataManager.Username);
|
|
f1.AddField("total",(int)totalMoneyEarned);
|
|
|
|
WWW reqTotal = new WWW(DataManager.API_Endpoint + "set_total.php",f1);
|
|
yield return reqTotal;
|
|
|
|
WWWForm f2 = new WWWForm();
|
|
f2.AddField("username", DataManager.Username);
|
|
f2.AddField("best", (int)_moneyEarned);
|
|
|
|
WWW reqBest = new WWW(DataManager.API_Endpoint + "set_best.php",f2);
|
|
yield return reqBest;
|
|
|
|
Debug.Log(reqBest.text);
|
|
}
|
|
} |