using DataTools; using System; using UnityEngine; using UnityEngine.UI; using WebTools.Customizes.Behaviours; using WebTools.Currencies.Behaviours; using System.Collections.Generic; using WebTools.Reward; public class GigButton : MonoBehaviour { public event Action OnDestroyButton; [Header("DATAUSER")] [SerializeField] private DataUser dataUser; [SerializeField] private Text actionName; [SerializeField] private Text motivation; [SerializeField] private Image slider; [SerializeField] private GigRewardButton rewardButton; [SerializeField] private AudioClip clickSound; [SerializeField] private AudioClip cameraSound; [SerializeField] private int rewardsCount = 5; private DataTools.Gig currentGig; private DataTools.GigAction myAction; private CharacterAnimations characterAnimations; private Transform locationInWorld; private Vector3 position; private float sliderFragment; public DataTools.GigAction MyAction { get { return myAction; } } private void Start() { characterAnimations = FindObjectOfType(); position = Vector3.zero; } public void Setup(DataTools.Gig currentGig, DataTools.GigAction action) { this.currentGig = currentGig; myAction = new DataTools.GigAction(action.EnergyCost, action.Stars, action.Tag); myAction.Id = action.Id; actionName.text = action.Id; Debug.Log("Action ID " + action.Id); motivation.text = myAction.EnergyCost.ToString(); sliderFragment = 1f / myAction.EnergyCost; Debug.Log("sliderFragment " + sliderFragment); } public void SetupLocation(Location location) { Interior interior = location.GetComponent(); locationInWorld = interior.ActionsBtn.GetComponentInChildren().Find(myAction.Tag); if(locationInWorld == null) { Debug.Log(myAction.Tag + " ActionBtn position not found in Interior, assigning random position."); SetRandomLocation(interior); } } public void SetupLocation(Transform location) { locationInWorld = location; } private void SetRandomLocation(Interior interior) { List actionBtnTransforms = new List(interior.ActionsBtn.GetComponentsInChildren()); actionBtnTransforms.Remove(interior.ActionsBtn.transform); if (actionBtnTransforms.Count > 0) { locationInWorld = actionBtnTransforms[UnityEngine.Random.Range(0, actionBtnTransforms.Count)]; locationInWorld.gameObject.SetActive(false); } else { Debug.LogWarning("There's no action btn in this location."); } } private void LateUpdate() { if(locationInWorld != null) { position.x = locationInWorld.position.x; position.y = locationInWorld.position.y; position.z = -5; transform.position = position; } } public void Clicked() { if(dataUser.UserData.CurrencyData.Motivation > 0) { if((currentGig == null) || ((currentGig.Type == DataTools.GigType.Photoshoot) || (currentGig.Type == DataTools.GigType.FashionShow))) { if (currentGig != null) { float flashChance = 0.2f; float random = UnityEngine.Random.value; if (random < flashChance) { SoundManager.Instance.UIAudioPlay(cameraSound); FlashEffect.Flash(); CatWalkFlashEffect.Flash(); } } else { SoundManager.Instance.UIAudioPlay(clickSound); } } else { SoundManager.Instance.UIAudioPlay(clickSound); } PlayAnimation(); dataUser.UserData.StarsEarned += 1; dataUser.UserData.CurrencyData.Motivation--; myAction.EnergyCost--; motivation.text = myAction.EnergyCost.ToString(); slider.fillAmount -= sliderFragment; dataUser.UpdateUserStars(); dataUser.UpdateUserCurrencyData(); PanelCurrenciesBehaviour.Instance.UpdateTimer(); if (WebTools.Phone.PanelPhoneBehaviour.Instance.isActiveAndEnabled) { WebTools.Phone.PanelPhoneBehaviour.Instance.TogglePhone(); } GameGlobal.Instance.PannigLocked = false; CommentsManager.Instance.TryTriggerComment(); bool t = GameGlobal.Instance.PannigLocked; if(myAction.EnergyCost == 0) { CreateRewards(); if (OnDestroyButton != null) { OnDestroyButton(this); } Destroy(gameObject); } } else { PanelCurrenciesBehaviour.Instance.ShowPanelNotMotivation("1", "Motivation"); PanelCurrenciesBehaviour.Instance.UpdateTimer(); } } private void PlayAnimation() { if (characterAnimations == null) { characterAnimations = FindObjectOfType(); } if (characterAnimations != null) { characterAnimations.PlayAnimation(myAction.Id); } } private void CreateRewards() { for(int i = 0; i < rewardsCount; i++) { GigRewardButton gigReward = Instantiate(rewardButton); gigReward.transform.position = transform.position; } } }