GigButton.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using DataTools;
  2. using System;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using WebTools.Customizes.Behaviours;
  6. using WebTools.Currencies.Behaviours;
  7. using System.Collections.Generic;
  8. using WebTools.Reward;
  9. public class GigButton : MonoBehaviour
  10. {
  11. public event Action<GigButton> OnDestroyButton;
  12. [Header("DATAUSER")]
  13. [SerializeField] private DataUser dataUser;
  14. [SerializeField] private Text actionName;
  15. [SerializeField] private Text motivation;
  16. [SerializeField] private Image slider;
  17. [SerializeField] private GigRewardButton rewardButton;
  18. [SerializeField] private AudioClip clickSound;
  19. [SerializeField] private AudioClip cameraSound;
  20. [SerializeField] private int rewardsCount = 5;
  21. private DataTools.Gig currentGig;
  22. private DataTools.GigAction myAction;
  23. private CharacterAnimations characterAnimations;
  24. private Transform locationInWorld;
  25. private Vector3 position;
  26. private float sliderFragment;
  27. public DataTools.GigAction MyAction
  28. {
  29. get
  30. {
  31. return myAction;
  32. }
  33. }
  34. private void Start()
  35. {
  36. characterAnimations = FindObjectOfType<CharacterAnimations>();
  37. position = Vector3.zero;
  38. }
  39. public void Setup(DataTools.Gig currentGig, DataTools.GigAction action)
  40. {
  41. this.currentGig = currentGig;
  42. myAction = new DataTools.GigAction(action.EnergyCost, action.Stars, action.Tag);
  43. myAction.Id = action.Id;
  44. actionName.text = action.Id;
  45. Debug.Log("Action ID " + action.Id);
  46. motivation.text = myAction.EnergyCost.ToString();
  47. sliderFragment = 1f / myAction.EnergyCost;
  48. Debug.Log("sliderFragment " + sliderFragment);
  49. }
  50. public void SetupLocation(Location location)
  51. {
  52. Interior interior = location.GetComponent<Interior>();
  53. locationInWorld = interior.ActionsBtn.GetComponentInChildren<Transform>().Find(myAction.Tag);
  54. if(locationInWorld == null)
  55. {
  56. Debug.Log(myAction.Tag + " ActionBtn position not found in Interior, assigning random position.");
  57. SetRandomLocation(interior);
  58. }
  59. }
  60. public void SetupLocation(Transform location)
  61. {
  62. locationInWorld = location;
  63. }
  64. private void SetRandomLocation(Interior interior)
  65. {
  66. List<Transform> actionBtnTransforms = new List<Transform>(interior.ActionsBtn.GetComponentsInChildren<Transform>());
  67. actionBtnTransforms.Remove(interior.ActionsBtn.transform);
  68. if (actionBtnTransforms.Count > 0)
  69. {
  70. locationInWorld = actionBtnTransforms[UnityEngine.Random.Range(0, actionBtnTransforms.Count)];
  71. locationInWorld.gameObject.SetActive(false);
  72. }
  73. else
  74. {
  75. Debug.LogWarning("There's no action btn in this location.");
  76. }
  77. }
  78. private void LateUpdate()
  79. {
  80. if(locationInWorld != null)
  81. {
  82. position.x = locationInWorld.position.x;
  83. position.y = locationInWorld.position.y;
  84. position.z = -5;
  85. transform.position = position;
  86. }
  87. }
  88. public void Clicked()
  89. {
  90. if(dataUser.UserData.CurrencyData.Motivation > 0)
  91. {
  92. if((currentGig == null) || ((currentGig.Type == DataTools.GigType.Photoshoot) || (currentGig.Type == DataTools.GigType.FashionShow)))
  93. {
  94. if (currentGig != null)
  95. {
  96. float flashChance = 0.2f;
  97. float random = UnityEngine.Random.value;
  98. if (random < flashChance)
  99. {
  100. SoundManager.Instance.UIAudioPlay(cameraSound);
  101. FlashEffect.Flash();
  102. CatWalkFlashEffect.Flash();
  103. }
  104. }
  105. else
  106. {
  107. SoundManager.Instance.UIAudioPlay(clickSound);
  108. }
  109. }
  110. else
  111. {
  112. SoundManager.Instance.UIAudioPlay(clickSound);
  113. }
  114. PlayAnimation();
  115. dataUser.UserData.StarsEarned += 1;
  116. dataUser.UserData.CurrencyData.Motivation--;
  117. myAction.EnergyCost--;
  118. motivation.text = myAction.EnergyCost.ToString();
  119. slider.fillAmount -= sliderFragment;
  120. dataUser.UpdateUserStars();
  121. dataUser.UpdateUserCurrencyData();
  122. PanelCurrenciesBehaviour.Instance.UpdateTimer();
  123. if (WebTools.Phone.PanelPhoneBehaviour.Instance.isActiveAndEnabled)
  124. {
  125. WebTools.Phone.PanelPhoneBehaviour.Instance.TogglePhone();
  126. }
  127. GameGlobal.Instance.PannigLocked = false;
  128. CommentsManager.Instance.TryTriggerComment();
  129. bool t = GameGlobal.Instance.PannigLocked;
  130. if(myAction.EnergyCost == 0)
  131. {
  132. CreateRewards();
  133. if (OnDestroyButton != null)
  134. {
  135. OnDestroyButton(this);
  136. }
  137. Destroy(gameObject);
  138. }
  139. }
  140. else
  141. {
  142. PanelCurrenciesBehaviour.Instance.ShowPanelNotMotivation("1", "Motivation");
  143. PanelCurrenciesBehaviour.Instance.UpdateTimer();
  144. }
  145. }
  146. private void PlayAnimation()
  147. {
  148. if (characterAnimations == null)
  149. {
  150. characterAnimations = FindObjectOfType<CharacterAnimations>();
  151. }
  152. if (characterAnimations != null)
  153. {
  154. characterAnimations.PlayAnimation(myAction.Id);
  155. }
  156. }
  157. private void CreateRewards()
  158. {
  159. for(int i = 0; i < rewardsCount; i++)
  160. {
  161. GigRewardButton gigReward = Instantiate(rewardButton);
  162. gigReward.transform.position = transform.position;
  163. }
  164. }
  165. }