123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using WebTools.Reward;
- public class ReplyKeeper : MonoBehaviour
- {
- [Header("Elements")]
- [SerializeField]
- private Text replyText;
- [Header("Com controller")]
- [SerializeField]
- private CommentsController comCtrl;
- [Header("Currency elements")]
- [SerializeField]
- private GameObject currencyGO;
- [SerializeField]
- private Image currencyImage;
- [SerializeField]
- private Text currencyText;
- [SerializeField]
- private GameObject prefabReward;
- [Header("Currency sprites")]
- [SerializeField]
- private Sprite currencyDollars;
- [SerializeField]
- private Sprite currencyGlams;
- [SerializeField]
- private Sprite currencyMotivation;
- [Header("Data")]
- [SerializeField]
- DataTools.DataUser dataUser;
- private DataTools.Reply localReply;
- public void SetReply(DataTools.Reply reply, CommentsController cc)
- {
- localReply = reply;
- SetData();
- SetCurrencyData();
- }
- private void SetCurrencyData()
- {
- currencyGO.SetActive(false);
- if (localReply.Cost != null)
- {
- if (localReply.Cost.Motivation != 0)
- {
- currencyImage.sprite = currencyMotivation;
- currencyText.text = Mathf.Abs(localReply.Cost.Motivation).ToString();
- currencyGO.SetActive(true);
- }
- else if (localReply.Cost.Dollars!=0)
- {
- currencyImage.sprite = currencyDollars;
- currencyText.text = Mathf.Abs(localReply.Cost.Dollars).ToString();
- currencyGO.SetActive(true);
- }
- else if (localReply.Cost.Glam != 0)
- {
- currencyImage.sprite = currencyGlams;
- currencyText.text = Mathf.Abs(localReply.Cost.Glam).ToString();
- currencyGO.SetActive(true);
- }
- }
- }
- private void SetData()
- {
- if(localReply != null)
- {
- replyText.text = localReply.Text;
- }
- }
- private bool HasCost()
- {
- return localReply.Cost != null && (localReply.Cost.Dollars != 0 || localReply.Cost.Glam != 0 || localReply.Cost.Motivation != 0);
- }
- /// <summary>
- /// Change user`s currency
- /// </summary>
- /// <returns></returns>
- private void ExecuteCostRewardChanges()
- {
- bool cost = HasCost();
- if (cost)
- {
- if (localReply.Cost.Motivation != 0)
- {
- if(dataUser.UserData.CurrencyData.Motivation < localReply.Cost.Motivation)
- {
- cost = false;
- }
- else
- {
- dataUser.UserData.CurrencyData.Motivation -= localReply.Cost.Motivation;
- cost = true;
- }
- }
- else if (localReply.Cost.Dollars != 0)
- {
- if (dataUser.UserData.CurrencyData.Dollars < localReply.Cost.Dollars)
- {
- cost = false;
- }
- else
- {
- dataUser.UserData.CurrencyData.Dollars -= localReply.Cost.Dollars;
- cost = true;
- }
- }
- else if (localReply.Cost.Glam != 0)
- {
- if (dataUser.UserData.CurrencyData.Glam < localReply.Cost.Glam)
- {
- cost = false;
- }
- else
- {
- dataUser.UserData.CurrencyData.Glam -= localReply.Cost.Glam;
- cost = true;
- }
- }
- }
- else
- cost = true;
- if(cost)
- {
- if (localReply.Reward.Motivation != 0)
- {
- SpawnReward(localReply.Reward.Motivation, RewardType.Motivation);
- }
- if (localReply.Reward.Dollars != 0)
- {
- SpawnReward(localReply.Reward.Dollars, RewardType.Dollar);
- }
- if (localReply.Reward.Glam != 0)
- {
- SpawnReward(localReply.Reward.Glam, RewardType.Glam);
- }
- if (localReply.Reward.Experience != 0)
- {
- SpawnReward(localReply.Reward.Experience, RewardType.Experience);
- }
- }
- }
- private void SpawnReward(int value, RewardType rewardType)
- {
- GameObject item = Instantiate(prefabReward);
- item.transform.position = currencyGO.transform.position;
- item.GetComponent<RewardConversation>().SpawnReward(rewardType, value);
- }
- public void Show(bool show)
- {
- this.gameObject.SetActive(show);
- }
- public void OnClick()
- {
- if(localReply != null)
- {
- ExecuteCostRewardChanges();
- comCtrl.SwitchDialog(localReply.NextDialogIndex);
- }
- }
- }
|