UPF/Assets/Game/Scripts/CollectBtn.cs
2022-10-06 23:45:04 +05:30

57 lines
1.8 KiB
C#

using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class CollectBtn : MonoBehaviour
{
public DateTime lastCollected;
public string buildingId;
public float productionRate;
public double collectableAmount;
public Button btn;
public TMP_Text txt;
public CollectablesData.ResourceType resourceType;
void Start(){
btn.onClick.AddListener(OnClick);
}
void Update()
{
collectableAmount=((DateTime.UtcNow - lastCollected).TotalSeconds * ((productionRate/60f)/60f));
txt.text = ((int)collectableAmount).ToString();
btn.interactable = collectableAmount > 1;
}
public void Set(string id,DateTime _lastCollected, float _productionRate, CollectablesData.ResourceType _resourceType){
buildingId = id;
lastCollected = _lastCollected;
productionRate=_productionRate;
resourceType = _resourceType;
}
async void OnClick(){
collectableAmount=((DateTime.UtcNow - lastCollected).TotalSeconds * ((productionRate/60f)/60f));
lastCollected = await DBmanager.GetNetworkTime();
switch (resourceType){
case CollectablesData.ResourceType.Metal:
DBmanager.SetMetal(DBmanager.Metal + (int)collectableAmount);
break;
case CollectablesData.ResourceType.Gold:
DBmanager.SetCoins(DBmanager.Coins + (int)collectableAmount);
AudioManager.instnace.CollectGold();
break;
// case CollectablesData.ResourceType.Oxygen:
// DBmanager.SetOxygen(DBmanager.Trophies + (int)collectableAmount);
// break;
}
DBmanager.CollectBuilding(buildingId);
}
}