ButtonAnswer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. using DataTools;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using WebTools.Currencies.Behaviours;
  5. using WebTools.Reward;
  6. using System.Collections;
  7. using WebTools.Customizes.Behaviours;
  8. public enum AnswerActionType
  9. {
  10. None,
  11. Cost,
  12. Reward,
  13. Repeat,
  14. Customization
  15. }
  16. public class ButtonAnswer : MonoBehaviour
  17. {
  18. [SerializeField]
  19. private DataUser dataUser;
  20. [SerializeField]
  21. private Sprite dollar;
  22. [SerializeField]
  23. private Sprite dollarBackground;
  24. [SerializeField]
  25. private Sprite glam;
  26. [SerializeField]
  27. private Sprite glamBackground;
  28. [SerializeField]
  29. private Sprite motivation;
  30. [SerializeField]
  31. private Sprite motivationBackground;
  32. [SerializeField]
  33. private Sprite costBackground;
  34. [SerializeField]
  35. private Sprite simpleBackground;
  36. [SerializeField]
  37. private Sprite customization;
  38. [SerializeField]
  39. private Sprite repeat;
  40. [SerializeField]
  41. private GameObject currencyContainer;
  42. [SerializeField]
  43. private Image background;
  44. [SerializeField]
  45. private Image icon;
  46. [SerializeField]
  47. private Text value;
  48. [SerializeField]
  49. private Text operatorText;
  50. [SerializeField]
  51. private Text answerText;
  52. [SerializeField]
  53. private int nextAnswerId;
  54. [SerializeField]
  55. private GameObject prefabReward;
  56. [SerializeField]
  57. private GameObject prefabAnim;
  58. [SerializeField]
  59. private GameObject canvasObject;
  60. [SerializeField]
  61. private bool triggerNextDialog = true;
  62. [Header("Changable UI")]
  63. [SerializeField]
  64. private Image backgroundImage;
  65. [SerializeField]
  66. private Image triangleImage;
  67. [SerializeField]
  68. private Sprite basicConvoSprite;
  69. [SerializeField]
  70. private Sprite phoneConvoSprite;
  71. [SerializeField]
  72. private Sprite basicConvoTriSprite;
  73. [SerializeField]
  74. private Sprite phoneConvoTriSprite;
  75. private AnswerActionType action;
  76. private DataTools.Currency currency;
  77. private PanelCurrenciesBehaviour panelCurrencies;
  78. private Sprite imagePrefabAnim;
  79. private string operatorPrefabAnim;
  80. private string animToPlay = "NONE";
  81. private void Start()
  82. {
  83. panelCurrencies = FindObjectOfType<PanelCurrenciesBehaviour>();
  84. }
  85. public void SetBasicConversationSprite()
  86. {
  87. backgroundImage.sprite = basicConvoSprite;
  88. triangleImage.sprite = basicConvoTriSprite;
  89. }
  90. public void SetPhoneConversationSprite()
  91. {
  92. backgroundImage.sprite = phoneConvoSprite;
  93. triangleImage.sprite = phoneConvoTriSprite;
  94. }
  95. public void SetAnimationToPlay(string animName)
  96. {
  97. animToPlay = animName;
  98. }
  99. public bool UseCameraVFX { get; set; }
  100. public void SetAnswer(string newAnswerText, DataTools.Currency cost, DataTools.Currency reward, bool holdOn=false, bool customize=false)
  101. {
  102. currencyContainer.SetActive(false);
  103. answerText.text = newAnswerText;
  104. if(HasValues(cost))
  105. SetCost(cost);
  106. else if(HasValues(reward))
  107. SetReward(reward);
  108. else
  109. {
  110. action = customize? AnswerActionType.Customization : holdOn ? AnswerActionType.Repeat : AnswerActionType.None;
  111. if(holdOn||customize)
  112. SetSpecialBgAndIcon();
  113. }
  114. }
  115. private void SetCost(DataTools.Currency cost)
  116. {
  117. action = AnswerActionType.Cost;
  118. currency = cost;
  119. SetCurrencyData();
  120. }
  121. private void SetReward(DataTools.Currency reward)
  122. {
  123. action = AnswerActionType.Reward;
  124. currency = reward;
  125. SetCurrencyData();
  126. }
  127. private void SetCurrencyData()
  128. {
  129. if(action == AnswerActionType.Cost)
  130. {
  131. currencyContainer.SetActive(true);
  132. background.sprite = costBackground;
  133. operatorText.text = "-";
  134. operatorPrefabAnim = "-";
  135. }
  136. else if(action == AnswerActionType.Reward)
  137. {
  138. operatorText.text = "+";
  139. operatorPrefabAnim = "+";
  140. SetRewardBackground();
  141. }
  142. SetTextValue();
  143. }
  144. private void SetRewardBackground()
  145. {
  146. if (currency.Dollars != 0)
  147. background.sprite = dollarBackground;
  148. else if (currency.Motivation != 0)
  149. background.sprite = motivationBackground;
  150. else if (currency.Glam != 0)
  151. background.sprite = glamBackground;
  152. }
  153. private void SetSpecialBgAndIcon()
  154. {
  155. currencyContainer.SetActive(true);
  156. background.sprite = simpleBackground;
  157. if (action == AnswerActionType.Customization)
  158. {
  159. icon.sprite = customization;
  160. }
  161. else if (action == AnswerActionType.Repeat)
  162. {
  163. icon.sprite = repeat;
  164. }
  165. operatorText.text = "";
  166. operatorPrefabAnim = "";
  167. value.text = "";
  168. }
  169. private void SetTextValue()
  170. {
  171. if(currency.Dollars != 0)
  172. {
  173. imagePrefabAnim = dollar;
  174. icon.sprite = dollar;
  175. value.text = currency.Dollars.ToString();
  176. }else if(currency.Motivation != 0)
  177. {
  178. imagePrefabAnim = motivation;
  179. icon.sprite = motivation;
  180. value.text = currency.Motivation.ToString();
  181. }
  182. else if(currency.Glam != 0)
  183. {
  184. imagePrefabAnim = glam;
  185. icon.sprite = glam;
  186. value.text = currency.Glam.ToString();
  187. }
  188. }
  189. private bool HasValues(DataTools.Currency currency)
  190. {
  191. return currency.Dollars != 0 || currency.Glam != 0 || currency.Motivation != 0 || currency.Experience != 0;
  192. }
  193. public void SetNextAnswerId(int newNextAnswerId)
  194. {
  195. nextAnswerId = newNextAnswerId;
  196. }
  197. public void AnswerAction()
  198. {
  199. if(action == AnswerActionType.Cost)
  200. {
  201. if(currency.Dollars > dataUser.UserData.CurrencyData.Dollars)
  202. {
  203. panelCurrencies.ShowPanelNotDollars((dataUser.UserData.CurrencyData.Dollars - currency.Dollars).ToString(), "Coins");
  204. ConversationManager.instance.CancelConversation();
  205. return;
  206. }
  207. if(currency.Motivation > dataUser.UserData.CurrencyData.Motivation)
  208. {
  209. panelCurrencies.ShowPanelNotMotivation((dataUser.UserData.CurrencyData.Motivation - currency.Motivation).ToString(), "Motivation");
  210. ConversationManager.instance.CancelConversation();
  211. return;
  212. }
  213. if(currency.Glam > dataUser.UserData.CurrencyData.Glam)
  214. {
  215. panelCurrencies.ShowPanelNotGlam((dataUser.UserData.CurrencyData.Glam - currency.Glam).ToString(), "Glam");
  216. ConversationManager.instance.CancelConversation();
  217. return;
  218. }
  219. AddCost();
  220. }
  221. else if(action == AnswerActionType.Reward)
  222. {
  223. AddReward();
  224. }
  225. TriggerNextDialog(action == AnswerActionType.Repeat || action == AnswerActionType.Customization);
  226. if (action == AnswerActionType.Customization)
  227. ConversationManager.instance.OpenCustomization();
  228. TryToPlayAnimation();
  229. TryToPlayVFX();
  230. ApplyHackForIssue1486();
  231. }
  232. private void TryToPlayAnimation()
  233. {
  234. if(animToPlay != "NONE")
  235. {
  236. if(characterAnimations == null)
  237. characterAnimations = FindObjectOfType<CharacterAnimations>();
  238. if(characterAnimations != null)
  239. {
  240. characterAnimations.PlayAnimation(animToPlay);
  241. }
  242. }
  243. }
  244. private void TryToPlayVFX()
  245. {
  246. if(UseCameraVFX)
  247. {
  248. SoundManager.Instance.UIAudioPlay(ConversationManager.instance.CameraSound);
  249. FlashEffect.Flash();
  250. CatWalkFlashEffect.Flash();
  251. }
  252. }
  253. private void TriggerNextDialog(bool repeat=false)
  254. {
  255. if (triggerNextDialog)
  256. {
  257. if (repeat)
  258. {
  259. ConversationManager.instance.ResetConversation();
  260. }
  261. else
  262. ConversationManager.instance.SetNextDialogue(nextAnswerId);
  263. }
  264. }
  265. private void AddCost()
  266. {
  267. dataUser.UserData.CurrencyData.Dollars -= currency.Dollars;
  268. dataUser.UserData.CurrencyData.Motivation -= currency.Motivation;
  269. dataUser.UserData.CurrencyData.Glam -= currency.Glam;
  270. SpawnItemAnim();
  271. PanelCurrenciesBehaviour.Instance.UpdateTimer();
  272. }
  273. private void AddReward()
  274. {
  275. StartCoroutine(SpawnRutine(currency.Dollars, RewardType.Dollar));
  276. StartCoroutine(SpawnRutine(currency.Motivation, RewardType.Motivation));
  277. StartCoroutine(SpawnRutine(currency.Glam, RewardType.Glam));
  278. StartCoroutine(SpawnRutine(currency.Experience, RewardType.Experience));
  279. }
  280. private IEnumerator SpawnRutine(int rewardValue, RewardType rewardType)
  281. {
  282. int divisor = 3;
  283. if(rewardValue >= divisor)
  284. {
  285. int quotient = 0;
  286. int remainder = 0;
  287. quotient = Mathf.FloorToInt(rewardValue / divisor);
  288. remainder = rewardValue % divisor;
  289. SpawnReward(quotient + remainder, rewardType);
  290. for(int i = 1; i < divisor; i++)
  291. {
  292. SpawnReward(quotient, rewardType);
  293. yield return new WaitForSeconds(0.1f);
  294. }
  295. }
  296. else
  297. {
  298. for(int i = 0; i < rewardValue; i++)
  299. {
  300. SpawnReward(1, rewardType);
  301. yield return new WaitForSeconds(0.1f);
  302. }
  303. }
  304. }
  305. private void SpawnReward(int value, RewardType rewardType)
  306. {
  307. GameObject item = Instantiate(prefabReward);
  308. item.transform.position = currencyContainer.transform.position;
  309. item.GetComponent<RewardConversation>().SpawnReward(rewardType, value);
  310. }
  311. public void SpawnItemAnim()
  312. {
  313. GameObject itemAnim;
  314. if(canvasObject != null)
  315. itemAnim = Instantiate(prefabAnim, canvasObject.transform);
  316. else
  317. itemAnim = Instantiate(prefabAnim);
  318. itemAnim.transform.position = currencyContainer.transform.position;
  319. itemAnim.GetComponent<RewardButtonAnim>().SetRewardPrefab(imagePrefabAnim, operatorPrefabAnim);
  320. }
  321. //
  322. // Hack for issue 1486
  323. //
  324. #region Hack1486Start
  325. void ApplyHackForIssue1486()
  326. {
  327. if(dataUser != null && dataUser.UserData != null)
  328. {
  329. var data = dataUser.UserData.ProgressData;
  330. if (data.Chapter == 0 && data.Quest == 1)
  331. {
  332. if(data.QuestId == "Tyra to the Rescue")
  333. {
  334. if(characterAnimations == null)
  335. characterAnimations = FindObjectOfType<CharacterAnimations>();
  336. if(characterAnimations != null)
  337. {
  338. switch(data.CurrentConversationIndex)
  339. {
  340. case 2:
  341. characterAnimations.PlayAnimation("Pose");
  342. break;
  343. case 4:
  344. characterAnimations.PlayAnimation("Smize");
  345. break;
  346. case 5: // elongate neck
  347. //characterAnimations.PlayAnimation("Romaine Shoot");
  348. characterAnimations.PlayAnimation("Picture perfect");
  349. // characterAnimations.PlayAnimation("Picture perfect");
  350. //Not really:characterAnimations.PlayAnimation("Pose");
  351. // NO: characterAnimations.PlayAnimation("Be cool");
  352. //NO: characterAnimations.PlayAnimation("BrightenUp");
  353. break;
  354. case 6:
  355. characterAnimations.PlayAnimation("Hands on hips");
  356. break;
  357. }
  358. }
  359. }
  360. else if(data.QuestId == "The Waiting Game")
  361. {
  362. if(data.CurrentConversationIndex == 1)
  363. {
  364. if(characterAnimations == null)
  365. characterAnimations = FindObjectOfType<CharacterAnimations>();
  366. characterAnimations.PlayAnimation("Hands on hips");
  367. }
  368. }
  369. Debug.Log(string.Format("ApplyHackForIssue1486()"), this);
  370. }
  371. }
  372. }
  373. CharacterAnimations characterAnimations;
  374. #endregion
  375. }