123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- using DataTools;
- using UnityEngine;
- using UnityEngine.UI;
- using WebTools.Currencies.Behaviours;
- using WebTools.Reward;
- using System.Collections;
- using WebTools.Customizes.Behaviours;
- public enum AnswerActionType
- {
- None,
- Cost,
- Reward,
- Repeat,
- Customization
- }
- public class ButtonAnswer : MonoBehaviour
- {
- [SerializeField]
- private DataUser dataUser;
- [SerializeField]
- private Sprite dollar;
- [SerializeField]
- private Sprite dollarBackground;
- [SerializeField]
- private Sprite glam;
- [SerializeField]
- private Sprite glamBackground;
- [SerializeField]
- private Sprite motivation;
- [SerializeField]
- private Sprite motivationBackground;
- [SerializeField]
- private Sprite costBackground;
- [SerializeField]
- private Sprite simpleBackground;
- [SerializeField]
- private Sprite customization;
- [SerializeField]
- private Sprite repeat;
- [SerializeField]
- private GameObject currencyContainer;
- [SerializeField]
- private Image background;
- [SerializeField]
- private Image icon;
- [SerializeField]
- private Text value;
- [SerializeField]
- private Text operatorText;
- [SerializeField]
- private Text answerText;
- [SerializeField]
- private int nextAnswerId;
- [SerializeField]
- private GameObject prefabReward;
- [SerializeField]
- private GameObject prefabAnim;
- [SerializeField]
- private GameObject canvasObject;
- [SerializeField]
- private bool triggerNextDialog = true;
- [Header("Changable UI")]
- [SerializeField]
- private Image backgroundImage;
- [SerializeField]
- private Image triangleImage;
- [SerializeField]
- private Sprite basicConvoSprite;
- [SerializeField]
- private Sprite phoneConvoSprite;
- [SerializeField]
- private Sprite basicConvoTriSprite;
- [SerializeField]
- private Sprite phoneConvoTriSprite;
- private AnswerActionType action;
- private DataTools.Currency currency;
- private PanelCurrenciesBehaviour panelCurrencies;
- private Sprite imagePrefabAnim;
- private string operatorPrefabAnim;
- private string animToPlay = "NONE";
- private void Start()
- {
- panelCurrencies = FindObjectOfType<PanelCurrenciesBehaviour>();
- }
- public void SetBasicConversationSprite()
- {
- backgroundImage.sprite = basicConvoSprite;
- triangleImage.sprite = basicConvoTriSprite;
- }
- public void SetPhoneConversationSprite()
- {
- backgroundImage.sprite = phoneConvoSprite;
- triangleImage.sprite = phoneConvoTriSprite;
- }
- public void SetAnimationToPlay(string animName)
- {
- animToPlay = animName;
- }
- public bool UseCameraVFX { get; set; }
- public void SetAnswer(string newAnswerText, DataTools.Currency cost, DataTools.Currency reward, bool holdOn=false, bool customize=false)
- {
- currencyContainer.SetActive(false);
- answerText.text = newAnswerText;
- if(HasValues(cost))
- SetCost(cost);
- else if(HasValues(reward))
- SetReward(reward);
- else
- {
- action = customize? AnswerActionType.Customization : holdOn ? AnswerActionType.Repeat : AnswerActionType.None;
- if(holdOn||customize)
- SetSpecialBgAndIcon();
- }
-
- }
- private void SetCost(DataTools.Currency cost)
- {
- action = AnswerActionType.Cost;
- currency = cost;
- SetCurrencyData();
- }
- private void SetReward(DataTools.Currency reward)
- {
- action = AnswerActionType.Reward;
- currency = reward;
- SetCurrencyData();
- }
- private void SetCurrencyData()
- {
- if(action == AnswerActionType.Cost)
- {
- currencyContainer.SetActive(true);
- background.sprite = costBackground;
- operatorText.text = "-";
- operatorPrefabAnim = "-";
- }
- else if(action == AnswerActionType.Reward)
- {
- operatorText.text = "+";
- operatorPrefabAnim = "+";
- SetRewardBackground();
- }
- SetTextValue();
- }
- private void SetRewardBackground()
- {
- if (currency.Dollars != 0)
- background.sprite = dollarBackground;
- else if (currency.Motivation != 0)
- background.sprite = motivationBackground;
- else if (currency.Glam != 0)
- background.sprite = glamBackground;
- }
- private void SetSpecialBgAndIcon()
- {
- currencyContainer.SetActive(true);
- background.sprite = simpleBackground;
- if (action == AnswerActionType.Customization)
- {
- icon.sprite = customization;
- }
- else if (action == AnswerActionType.Repeat)
- {
- icon.sprite = repeat;
- }
- operatorText.text = "";
- operatorPrefabAnim = "";
- value.text = "";
- }
- private void SetTextValue()
- {
- if(currency.Dollars != 0)
- {
- imagePrefabAnim = dollar;
- icon.sprite = dollar;
- value.text = currency.Dollars.ToString();
- }else if(currency.Motivation != 0)
- {
- imagePrefabAnim = motivation;
- icon.sprite = motivation;
- value.text = currency.Motivation.ToString();
- }
- else if(currency.Glam != 0)
- {
- imagePrefabAnim = glam;
- icon.sprite = glam;
- value.text = currency.Glam.ToString();
- }
- }
- private bool HasValues(DataTools.Currency currency)
- {
- return currency.Dollars != 0 || currency.Glam != 0 || currency.Motivation != 0 || currency.Experience != 0;
- }
- public void SetNextAnswerId(int newNextAnswerId)
- {
- nextAnswerId = newNextAnswerId;
- }
- public void AnswerAction()
- {
- if(action == AnswerActionType.Cost)
- {
- if(currency.Dollars > dataUser.UserData.CurrencyData.Dollars)
- {
- panelCurrencies.ShowPanelNotDollars((dataUser.UserData.CurrencyData.Dollars - currency.Dollars).ToString(), "Coins");
- ConversationManager.instance.CancelConversation();
- return;
- }
- if(currency.Motivation > dataUser.UserData.CurrencyData.Motivation)
- {
- panelCurrencies.ShowPanelNotMotivation((dataUser.UserData.CurrencyData.Motivation - currency.Motivation).ToString(), "Motivation");
- ConversationManager.instance.CancelConversation();
- return;
- }
- if(currency.Glam > dataUser.UserData.CurrencyData.Glam)
- {
- panelCurrencies.ShowPanelNotGlam((dataUser.UserData.CurrencyData.Glam - currency.Glam).ToString(), "Glam");
- ConversationManager.instance.CancelConversation();
- return;
- }
- AddCost();
- }
- else if(action == AnswerActionType.Reward)
- {
- AddReward();
- }
- TriggerNextDialog(action == AnswerActionType.Repeat || action == AnswerActionType.Customization);
- if (action == AnswerActionType.Customization)
- ConversationManager.instance.OpenCustomization();
- TryToPlayAnimation();
- TryToPlayVFX();
- ApplyHackForIssue1486();
- }
- private void TryToPlayAnimation()
- {
- if(animToPlay != "NONE")
- {
- if(characterAnimations == null)
- characterAnimations = FindObjectOfType<CharacterAnimations>();
- if(characterAnimations != null)
- {
- characterAnimations.PlayAnimation(animToPlay);
- }
- }
- }
- private void TryToPlayVFX()
- {
- if(UseCameraVFX)
- {
- SoundManager.Instance.UIAudioPlay(ConversationManager.instance.CameraSound);
- FlashEffect.Flash();
- CatWalkFlashEffect.Flash();
- }
- }
- private void TriggerNextDialog(bool repeat=false)
- {
- if (triggerNextDialog)
- {
- if (repeat)
- {
- ConversationManager.instance.ResetConversation();
- }
- else
- ConversationManager.instance.SetNextDialogue(nextAnswerId);
- }
- }
- private void AddCost()
- {
- dataUser.UserData.CurrencyData.Dollars -= currency.Dollars;
- dataUser.UserData.CurrencyData.Motivation -= currency.Motivation;
- dataUser.UserData.CurrencyData.Glam -= currency.Glam;
- SpawnItemAnim();
- PanelCurrenciesBehaviour.Instance.UpdateTimer();
- }
- private void AddReward()
- {
- StartCoroutine(SpawnRutine(currency.Dollars, RewardType.Dollar));
- StartCoroutine(SpawnRutine(currency.Motivation, RewardType.Motivation));
- StartCoroutine(SpawnRutine(currency.Glam, RewardType.Glam));
- StartCoroutine(SpawnRutine(currency.Experience, RewardType.Experience));
- }
- private IEnumerator SpawnRutine(int rewardValue, RewardType rewardType)
- {
- int divisor = 3;
- if(rewardValue >= divisor)
- {
- int quotient = 0;
- int remainder = 0;
- quotient = Mathf.FloorToInt(rewardValue / divisor);
- remainder = rewardValue % divisor;
- SpawnReward(quotient + remainder, rewardType);
- for(int i = 1; i < divisor; i++)
- {
- SpawnReward(quotient, rewardType);
- yield return new WaitForSeconds(0.1f);
- }
- }
- else
- {
- for(int i = 0; i < rewardValue; i++)
- {
- SpawnReward(1, rewardType);
- yield return new WaitForSeconds(0.1f);
- }
- }
- }
- private void SpawnReward(int value, RewardType rewardType)
- {
- GameObject item = Instantiate(prefabReward);
- item.transform.position = currencyContainer.transform.position;
- item.GetComponent<RewardConversation>().SpawnReward(rewardType, value);
- }
- public void SpawnItemAnim()
- {
- GameObject itemAnim;
- if(canvasObject != null)
- itemAnim = Instantiate(prefabAnim, canvasObject.transform);
- else
- itemAnim = Instantiate(prefabAnim);
- itemAnim.transform.position = currencyContainer.transform.position;
- itemAnim.GetComponent<RewardButtonAnim>().SetRewardPrefab(imagePrefabAnim, operatorPrefabAnim);
- }
- //
- // Hack for issue 1486
- //
- #region Hack1486Start
- void ApplyHackForIssue1486()
- {
- if(dataUser != null && dataUser.UserData != null)
- {
- var data = dataUser.UserData.ProgressData;
- if (data.Chapter == 0 && data.Quest == 1)
- {
- if(data.QuestId == "Tyra to the Rescue")
- {
- if(characterAnimations == null)
- characterAnimations = FindObjectOfType<CharacterAnimations>();
- if(characterAnimations != null)
- {
- switch(data.CurrentConversationIndex)
- {
- case 2:
- characterAnimations.PlayAnimation("Pose");
- break;
- case 4:
- characterAnimations.PlayAnimation("Smize");
- break;
- case 5: // elongate neck
- //characterAnimations.PlayAnimation("Romaine Shoot");
- characterAnimations.PlayAnimation("Picture perfect");
- // characterAnimations.PlayAnimation("Picture perfect");
- //Not really:characterAnimations.PlayAnimation("Pose");
- // NO: characterAnimations.PlayAnimation("Be cool");
- //NO: characterAnimations.PlayAnimation("BrightenUp");
- break;
- case 6:
- characterAnimations.PlayAnimation("Hands on hips");
- break;
- }
- }
- }
- else if(data.QuestId == "The Waiting Game")
- {
- if(data.CurrentConversationIndex == 1)
- {
- if(characterAnimations == null)
- characterAnimations = FindObjectOfType<CharacterAnimations>();
- characterAnimations.PlayAnimation("Hands on hips");
- }
- }
- Debug.Log(string.Format("ApplyHackForIssue1486()"), this);
- }
- }
- }
- CharacterAnimations characterAnimations;
- #endregion
- }
|