UPF/Assets/Game/Scripts/CollectBtn.cs
2023-02-24 22:14:55 +05:30

129 lines
4.8 KiB
C#
Executable File

using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class CollectBtn : MonoBehaviour
{
public static CollectBtn instance;
public DateTime lastCollected;
public string buildingId;
public float productionRate;
public double collectableAmount;
public Button btn;
public TMP_Text txt;
public CollectablesData.ResourceType resourceType;
void Awake(){
instance = this;
}
void Start(){
btn.onClick.AddListener(OnClick);
}
void Update()
{
// collectableAmount=((DateTime.UtcNow - lastCollected).TotalSeconds * ((productionRate/60f)/60f));
collectableAmount = GetCollectableGold();
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;
}
public async void OnClick(){
// lastCollected = await DBmanager.GetNetworkTime();
DBmanager.CollectBuilding(buildingId);
switch (resourceType){
case CollectablesData.ResourceType.Metal:
collectableAmount= GetCollectableEnergy();
if((int)collectableAmount <= 0){return;}
DBmanager.SetMetal(DBmanager.Metal + (int)collectableAmount);
CollectEffect.instance.Show(resourceType,(int)collectableAmount);
AudioManager.instnace.CollectGems();
break;
case CollectablesData.ResourceType.Gold:
collectableAmount= GetCollectableGold();
if((int)collectableAmount <= 0){return;}
DBmanager.SetCoins(DBmanager.Coins + (int)collectableAmount);
CollectEffect.instance.Show(resourceType,(int)collectableAmount);
// AudioManager.instnace.CollectGems();
break;
// case CollectablesData.ResourceType.Oxygen:
// DBmanager.SetOxygen(DBmanager.Trophies + (int)collectableAmount);
// break;
}
Selector.selectBuilding(null);
}
double GetCollectableGold(){
if(TutorialManager.showing){
return 50;
}
Building selectedBuilding = BuildingManager.instance.GetBuildingWithId(buildingId);
int coinsCap = 0;
foreach(BuildingStat stat in selectedBuilding.buildingData.levels[selectedBuilding.curLevel].stats){
if(stat.name == "Capacity"){
coinsCap = int.Parse(stat.value);
break;
}
}
return Mathf.Clamp((float)((DateTime.UtcNow - lastCollected).TotalSeconds * ((productionRate/60f)/60f)),0, coinsCap);
}
public bool isGoldCapped(string bid){
if(TutorialManager.showing){
return false;
}
Building selectedBuilding = BuildingManager.instance.GetBuildingWithId(bid);
int coinsCap = 0;
foreach(BuildingStat stat in selectedBuilding.buildingData.levels[selectedBuilding.curLevel].stats){
if(stat.name == "Capacity"){
coinsCap = int.Parse(stat.value);
break;
}
}
float collectable=(float)((DateTime.UtcNow - lastCollected).TotalSeconds * ((productionRate/60f)/60f));
// return Mathf.Clamp(,0, coinsCap);
return collectable >= coinsCap;
}
double GetCollectableEnergy(){
Building selectedBuilding = BuildingManager.instance.GetBuildingWithId(buildingId);
int coinsCap = 0;
foreach(BuildingStat stat in selectedBuilding.buildingData.levels[selectedBuilding.curLevel].stats){
if(stat.name == "Capacity"){
coinsCap = int.Parse(stat.value);
break;
}
}
return Mathf.Clamp((float)((DateTime.UtcNow - lastCollected).TotalSeconds * ((productionRate/60f)/60f)),0, coinsCap);
}
public static bool isEnergyCapped(string bid){
Building selectedBuilding = BuildingManager.instance.GetBuildingWithId(bid);
int coinsCap = 0;
foreach(BuildingStat stat in selectedBuilding.buildingData.levels[selectedBuilding.curLevel].stats){
if(stat.name == "Capacity"){
coinsCap = int.Parse(stat.value);
break;
}
}
float collectable = (float)((DateTime.UtcNow - selectedBuilding.lastCollected).TotalSeconds * ((selectedBuilding.buildingData.productinoRates[selectedBuilding.curLevel]/60f)/60f));
return collectable >= coinsCap;
}
}