86 lines
2.3 KiB
C#
86 lines
2.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
public class TradingPost : MonoBehaviour
|
|
{
|
|
public Building building;
|
|
|
|
public TMP_Text txtMetalAmount;
|
|
public TMP_Text txtGoldAmount;
|
|
public Slider metalSlider;
|
|
// public Slider metalSlider;
|
|
public TMP_Text warningTxt;
|
|
public Button tradeButton;
|
|
void Start()
|
|
{
|
|
metalSlider.onValueChanged.AddListener(OnGoldChanged);
|
|
tradeButton.onClick.AddListener(OnTrade);
|
|
Refresh();
|
|
}
|
|
|
|
public void Show(){
|
|
gameObject.SetActive(true);
|
|
Refresh();
|
|
}
|
|
|
|
public void Hide(){
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
|
|
int metalCount = 10;
|
|
public int goldCount => goldPerMetal * metalCount /10;
|
|
public int goldPerMetal => (int)(float.Parse(building.buildingData.levels[building.curLevel].stats[0].value) * 10);
|
|
|
|
void OnGoldChanged(float value){
|
|
Refresh();
|
|
}
|
|
|
|
void OnTrade(){
|
|
MessageDialog.instance.ShowQuestion("Are you sure?", $"You are about to trade <b>{metalCount} Energy</b> for <b>{goldCount} Coins</b>", OnYes:OnTradeConfirmed, OnNo:()=>{});
|
|
}
|
|
|
|
void OnTradeConfirmed(){
|
|
if(Refresh()==1){
|
|
MessageDialog.instance.ShowMessage("Error", "You need to have atleast 10 golds to trade.");
|
|
return;
|
|
}
|
|
Hide();
|
|
DBmanager.SetCoins(DBmanager.Coins + goldCount);
|
|
DBmanager.SetMetal(DBmanager.Metal - metalCount);
|
|
}
|
|
|
|
public int Refresh(){
|
|
if(DBmanager.Coins < 10){
|
|
warningTxt.text = "You need atleast 10 golds to trade";
|
|
tradeButton.interactable = false;
|
|
metalSlider.interactable = false;
|
|
txtGoldAmount.text = "0";
|
|
txtMetalAmount.text = "0";
|
|
return 1;
|
|
}
|
|
tradeButton.interactable = true;
|
|
metalSlider.interactable = true;
|
|
warningTxt.text = "";
|
|
|
|
float selectedPart = (metalSlider.value/metalSlider.maxValue) * DBmanager.Metal;
|
|
metalCount = ((int)selectedPart).RoundOff();
|
|
|
|
|
|
if(metalCount > DBmanager.Coins){
|
|
metalCount -= 10;
|
|
}
|
|
if(metalCount <= 0){
|
|
metalCount =10;
|
|
}
|
|
txtMetalAmount.text = metalCount.ToString();
|
|
|
|
txtGoldAmount.text = (goldCount).ToString();
|
|
|
|
return 0;
|
|
}
|
|
|
|
}
|