ReplyKeeper.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using WebTools.Reward;
  6. public class ReplyKeeper : MonoBehaviour
  7. {
  8. [Header("Elements")]
  9. [SerializeField]
  10. private Text replyText;
  11. [Header("Com controller")]
  12. [SerializeField]
  13. private CommentsController comCtrl;
  14. [Header("Currency elements")]
  15. [SerializeField]
  16. private GameObject currencyGO;
  17. [SerializeField]
  18. private Image currencyImage;
  19. [SerializeField]
  20. private Text currencyText;
  21. [SerializeField]
  22. private GameObject prefabReward;
  23. [Header("Currency sprites")]
  24. [SerializeField]
  25. private Sprite currencyDollars;
  26. [SerializeField]
  27. private Sprite currencyGlams;
  28. [SerializeField]
  29. private Sprite currencyMotivation;
  30. [Header("Data")]
  31. [SerializeField]
  32. DataTools.DataUser dataUser;
  33. private DataTools.Reply localReply;
  34. public void SetReply(DataTools.Reply reply, CommentsController cc)
  35. {
  36. localReply = reply;
  37. SetData();
  38. SetCurrencyData();
  39. }
  40. private void SetCurrencyData()
  41. {
  42. currencyGO.SetActive(false);
  43. if (localReply.Cost != null)
  44. {
  45. if (localReply.Cost.Motivation != 0)
  46. {
  47. currencyImage.sprite = currencyMotivation;
  48. currencyText.text = Mathf.Abs(localReply.Cost.Motivation).ToString();
  49. currencyGO.SetActive(true);
  50. }
  51. else if (localReply.Cost.Dollars!=0)
  52. {
  53. currencyImage.sprite = currencyDollars;
  54. currencyText.text = Mathf.Abs(localReply.Cost.Dollars).ToString();
  55. currencyGO.SetActive(true);
  56. }
  57. else if (localReply.Cost.Glam != 0)
  58. {
  59. currencyImage.sprite = currencyGlams;
  60. currencyText.text = Mathf.Abs(localReply.Cost.Glam).ToString();
  61. currencyGO.SetActive(true);
  62. }
  63. }
  64. }
  65. private void SetData()
  66. {
  67. if(localReply != null)
  68. {
  69. replyText.text = localReply.Text;
  70. }
  71. }
  72. private bool HasCost()
  73. {
  74. return localReply.Cost != null && (localReply.Cost.Dollars != 0 || localReply.Cost.Glam != 0 || localReply.Cost.Motivation != 0);
  75. }
  76. /// <summary>
  77. /// Change user`s currency
  78. /// </summary>
  79. /// <returns></returns>
  80. private void ExecuteCostRewardChanges()
  81. {
  82. bool cost = HasCost();
  83. if (cost)
  84. {
  85. if (localReply.Cost.Motivation != 0)
  86. {
  87. if(dataUser.UserData.CurrencyData.Motivation < localReply.Cost.Motivation)
  88. {
  89. cost = false;
  90. }
  91. else
  92. {
  93. dataUser.UserData.CurrencyData.Motivation -= localReply.Cost.Motivation;
  94. cost = true;
  95. }
  96. }
  97. else if (localReply.Cost.Dollars != 0)
  98. {
  99. if (dataUser.UserData.CurrencyData.Dollars < localReply.Cost.Dollars)
  100. {
  101. cost = false;
  102. }
  103. else
  104. {
  105. dataUser.UserData.CurrencyData.Dollars -= localReply.Cost.Dollars;
  106. cost = true;
  107. }
  108. }
  109. else if (localReply.Cost.Glam != 0)
  110. {
  111. if (dataUser.UserData.CurrencyData.Glam < localReply.Cost.Glam)
  112. {
  113. cost = false;
  114. }
  115. else
  116. {
  117. dataUser.UserData.CurrencyData.Glam -= localReply.Cost.Glam;
  118. cost = true;
  119. }
  120. }
  121. }
  122. else
  123. cost = true;
  124. if(cost)
  125. {
  126. if (localReply.Reward.Motivation != 0)
  127. {
  128. SpawnReward(localReply.Reward.Motivation, RewardType.Motivation);
  129. }
  130. if (localReply.Reward.Dollars != 0)
  131. {
  132. SpawnReward(localReply.Reward.Dollars, RewardType.Dollar);
  133. }
  134. if (localReply.Reward.Glam != 0)
  135. {
  136. SpawnReward(localReply.Reward.Glam, RewardType.Glam);
  137. }
  138. if (localReply.Reward.Experience != 0)
  139. {
  140. SpawnReward(localReply.Reward.Experience, RewardType.Experience);
  141. }
  142. }
  143. }
  144. private void SpawnReward(int value, RewardType rewardType)
  145. {
  146. GameObject item = Instantiate(prefabReward);
  147. item.transform.position = currencyGO.transform.position;
  148. item.GetComponent<RewardConversation>().SpawnReward(rewardType, value);
  149. }
  150. public void Show(bool show)
  151. {
  152. this.gameObject.SetActive(show);
  153. }
  154. public void OnClick()
  155. {
  156. if(localReply != null)
  157. {
  158. ExecuteCostRewardChanges();
  159. comCtrl.SwitchDialog(localReply.NextDialogIndex);
  160. }
  161. }
  162. }