- Boss after rift clearance - job completed event - job rewards awarded on completion
78 lines
1.7 KiB
C#
78 lines
1.7 KiB
C#
using Photon.Pun;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
public class CoinBag : MonoBehaviour
|
|
{
|
|
[SerializeField] private TMP_Text coinText;
|
|
[SerializeField] private GameEventListener_Int onCoinDrop;
|
|
[SerializeField] private GameEventListener onJoinedRoom;
|
|
[SerializeField] private GameEventListener_JobInstance onJobCompleted;
|
|
|
|
public int currentCoin;
|
|
|
|
CoinData coinData = new CoinData();
|
|
|
|
private void Awake()
|
|
{
|
|
onCoinDrop.Response.AddListener(ChangeAmount);
|
|
|
|
onJoinedRoom.Response.AddListener(LoadCoins);
|
|
|
|
onJobCompleted.Response.AddListener(job => ChangeAmount(job.coinReward));
|
|
|
|
UpdateCoinText();
|
|
}
|
|
|
|
|
|
public void ChangeAmount(int amount)
|
|
{
|
|
currentCoin += amount;
|
|
|
|
SaveCoinData();
|
|
|
|
UpdateCoinText();
|
|
}
|
|
|
|
public bool SpendCoin(int amount)
|
|
{
|
|
if (currentCoin - amount < 0)
|
|
return false;
|
|
|
|
currentCoin -= amount;
|
|
|
|
SaveCoinData();
|
|
|
|
UpdateCoinText();
|
|
return true;
|
|
}
|
|
|
|
private void UpdateCoinText()
|
|
{
|
|
coinText.text = currentCoin.ToString();
|
|
}
|
|
|
|
private void SaveCoinData()
|
|
{
|
|
coinData.totalCoin = currentCoin;
|
|
|
|
PlayerDataHandler.Instance.SavePlayerCoinData(PhotonNetwork.NickName, coinData);
|
|
|
|
}
|
|
|
|
private void LoadCoins()
|
|
{
|
|
coinData = PlayerDataHandler.Instance.LoadPlayerCoinData(PhotonNetwork.NickName);
|
|
if (coinData != null)
|
|
{
|
|
Debug.Log("Success Loading CoinData");
|
|
currentCoin = coinData.totalCoin;
|
|
UpdateCoinText();
|
|
}
|
|
else
|
|
coinData = new CoinData();
|
|
}
|
|
}
|