123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814 |
- using UnityEngine;
- using UnityEngine.UI;
- using System;
- using DataTools;
- using WebTools.Customizes.Behaviours;
- [Serializable]
- public class ConversationAudioData
- {
- public string audioId;
- public AudioClip voice;
- }
- public class ConversationManager : GameplayBase
- {
- public static ConversationManager instance;
- public event Action<DataTools.Dialog> onChangeDialog;
- [Header("DATA")]
- [SerializeField] protected DataUser dataUser;
- [Header("ANIMATIONS")]
- [SerializeField] private Animator dialogAnimation;
- [Header("GAME OBJECTS")]
- [SerializeField] private GameObject Dialog;
- [SerializeField] private GameObject Notification;
- [SerializeField] private GameObject nextButton;
- [SerializeField] private GameObject answersContainer;
- [SerializeField] private GameObject textGlobeConversation;
- [SerializeField] private GameObject textGlobePhone;
- [Header("TEXT")]
- [SerializeField] private Text currenConversationText;
- [SerializeField] private Text currentSpeakerText;
- [Header("TEXT MESSAGE")]
- [SerializeField] private PhoneUIMessages texMessage;
- [SerializeField] private PhoneUIMessages mailMessage;
- [Header("BUTTONS")]
- [SerializeField] private ButtonAnswer[] buttonsAnswer;
- [Header("SOUNDS")]
- [SerializeField] private ConversationAudioData[] conversationAudioData;
- [SerializeField] private AudioSource audioSource;
- [Header("CHARACTER")]
- [SerializeField] private CustomizeCharacterBehaviour currentCharacter;
- [Header("StyleCheck")]
- [SerializeField] private FillBar stylebar;
- [SerializeField] private Image styleImage;
- [SerializeField] private Sprite styleCasual;
- [SerializeField] private Sprite styleCouture;
- [SerializeField] private Sprite styleActivewear;
- [SerializeField] private Sprite styleSwimwear;
- [Header("Next dialog marks")]
- [SerializeField] private Image nextMark;
- [SerializeField] private Sprite basicMark;
- [SerializeField] private Sprite phoneMark;
- [Header("Sound for answers VFX")]
- [SerializeField] private AudioClip cameraSound;
- public AudioClip CameraSound
- {
- get
- {
- return cameraSound;
- }
- }
- private Characters characters;
- private int nextDialogIndex = 0;
- private string newLocationId = "";
- private float conversationDelay = 0;
- private bool dialogIsEnabled = true;
- private bool needToRestartConversation = false;
- private void Awake()
- {
- instance = this;
- }
- protected override void Start()
- {
- WebTools.UserInput.OnNameInput += ResetConversation;
- WebTools.UserInput.OnGenderInput += NextDialogue;
- DisableDialogItmes();
- characters = FindObjectOfType<Characters>();
- base.Start();
- }
- private void OnDestroy()
- {
- WebTools.UserInput.OnNameInput -= ResetConversation;
- WebTools.UserInput.OnGenderInput -= NextDialogue;
- }
- public void ClearText()
- {
- currenConversationText.text = "";
- currentSpeakerText.text = "";
- }
- private void SetNextMark()
- {
- if(currentConversation != null)
- {
- switch(currentConversation.ConvType)
- {
- case DataTools.ConversationType.PhoneCall:
- {
- nextMark.sprite = phoneMark;
- break;
- }
- default:
- {
- nextMark.sprite = basicMark;
- break;
- }
- }
- }
- }
- private bool LocationMatch
- {
- get
- {
- bool locationFlag = currentConversation.Location.ToUpper() == newLocationId.ToUpper();
- bool exteriorFlag = (currentConversation.Location.ToUpper() == "EXTERIOR") && GameGlobal.Instance.GetIsExterior();
- bool anyFlag = (currentConversation.Location.ToUpper() == "ANY");
- return (locationFlag || exteriorFlag || anyFlag);
- }
- }
- public void StartDialogue()
- {
- UnblockGame();
- if (!LocationMatch)
- {
- return;
- }
- ClearText();
- PanelQuestBehaviour.Instance.HideMainQuest();
- Notification.SetActive(false);
- Dialog.SetActive(true);
- SetupInitialDiaogue();
- EnableNextDialogue(0f, currentConversation.Dialog[dataUser.UserData.ProgressData.CurrentConversationIndex]);
- }
- public void FinishTask(bool resetDialog = true)
- {
- ContacsBehaviour.Instance.ShowContac();
- if (resetDialog)
- {
- dataUser.UserData.ProgressData.CurrentConversationIndex = 0;
- }
- UnblockGame();
- OnFinishConversation();
- }
- private void OnFinishConversation(bool mainStoryConvo = true)
- {
- currentConversation = null;
- if (mainStoryConvo)
- {
- GameplayManager.instance.NextChapterAction();
- }
- }
- private void DisableDialogItmes()
- {
- ClearText();
- Debug.Log("Disable dialog items");
- Dialog.SetActive(false);
- Notification.SetActive(false);
- }
- private void EnableNextDialogue(float animDelay, DataTools.Dialog dialog)
- {
- if(onChangeDialog != null)
- {
- onChangeDialog(dialog);
- }
- string prevNpcId = GameGlobal.Instance.CurrentNpc;
- GameGlobal.Instance.CurrentNpc = dialog.NpcId;
- if (prevNpcId != GameGlobal.Instance.CurrentNpc)
- {
- ClearText();
- dialogAnimation.StopPlayback();
- Dialog.SetActive(false);
- Invoke("PlayDialogue", 0.2f);
- }
- else
- {
- Invoke("PlayDialogue", animDelay);
- }
- }
- private bool isNoProblemWithNextDialog
- {
- get
- {
- return ((currentConversation != null) && (currentConversation.Dialog != null) && (currentConversation.Dialog.Count != 0));
- }
- }
- private void NextDialogue()
- {
- SetActiveButtons(false);
- if (nextDialogIndex == -1)
- {
- int nextIndex = isNoProblemWithNextDialog ? currentConversation.Dialog[dataUser.UserData.ProgressData.CurrentConversationIndex].NextDialogIndex : -1;
- if (nextIndex != -1)
- {
- dataUser.UserData.ProgressData.CurrentConversationIndex = nextIndex;
- }
- else
- {
- dataUser.UserData.ProgressData.CurrentConversationIndex = currentConversation.Dialog.Count;
- }
- }
- else
- {
- if (nextDialogIndex == dataUser.UserData.ProgressData.CurrentConversationIndex || nextDialogIndex == 0)
- {
- int nextIndex = isNoProblemWithNextDialog ? currentConversation.Dialog[dataUser.UserData.ProgressData.CurrentConversationIndex].NextDialogIndex : -1;
- if (nextIndex != -1)
- {
- dataUser.UserData.ProgressData.CurrentConversationIndex = nextIndex;
- }
- else
- {
- dataUser.UserData.ProgressData.CurrentConversationIndex = currentConversation.Dialog.Count;
- }
- }
- else
- {
- dataUser.UserData.ProgressData.CurrentConversationIndex = nextDialogIndex;
- }
- }
- if (currentConversation != null)
- {
- if (GameGlobal.Instance.GetCurrentQuest != null /*&& currentConversation.ConvType!=DataTools.ConversationType.Optional*/)
- {
- GameGlobal.Instance.SetCurrentDialogIndex(dataUser.UserData.ProgressData.CurrentConversationIndex);
- GameGlobal.Instance.SetActionID(GameGlobal.Instance.GetCurrentQuest.Tasks[GameGlobal.Instance.GetQuestIndex()].Action.PropertyId, currentConversation.Id);
- }
- if (dataUser.UserData.ProgressData.CurrentConversationIndex < currentConversation.Dialog.Count)
- {
- EnableNextDialogue(0.5f, currentConversation.Dialog[dataUser.UserData.ProgressData.CurrentConversationIndex]);
- }
- else
- {
- dialogIsEnabled = false;
- if (currentConversation.ConvType == DataTools.ConversationType.PhoneCall)
- {
- WebTools.Phone.PanelPhoneBehaviour.Instance.EndCall();
- }
- FinishTask();
- }
- }
- dataUser.UpdateUserProgress();
- }
- private void PlayAudio()
- {
- conversationDelay = 0;
- if (currentConversation != null)
- {
- if (currentConversation.Dialog[dataUser.UserData.ProgressData.CurrentConversationIndex].SoundId != "")
- {
- foreach (ConversationAudioData audioData in conversationAudioData)
- {
- if (audioData.audioId == currentConversation.Dialog[dataUser.UserData.ProgressData.CurrentConversationIndex].SoundId)
- {
- audioSource.clip = audioData.voice;
- audioSource.Play();
- conversationDelay = audioSource.clip.length;
- }
- }
- }
- }
- }
-
- public void SetNextDialogue(int _nextDialogIndex)
- {
- if (currentConversation != null)
- {
- if (currentConversation.ConvType == DataTools.ConversationType.Auto ||
- currentConversation.ConvType == DataTools.ConversationType.Basic ||
- currentConversation.ConvType == DataTools.ConversationType.PhoneCall ||
- currentConversation.ConvType == DataTools.ConversationType.Optional)
- {
- if (needToRestartConversation)
- {
- needToRestartConversation = false;
- ResetConversation();
- return;
- }
- nextDialogIndex = _nextDialogIndex;
- PlayAudio();
- dialogAnimation.Play("EndDialogue");
- Invoke("NextDialogue", conversationDelay);
- }
- }
- }
- public void GoToNextDialog()
- {
- int dialog = dataUser.UserData.ProgressData.CurrentConversationIndex;
- int next = currentConversation.Dialog[dialog].NextDialogIndex;
- SetNextDialogue(next);
- }
- public void ResetConversation()
- {
- if (GameplayManager.instance.QuestIsActive)
- {
- GameGlobal.Instance.DecreaseQuestIndex();
- }
- else
- {
- GameGlobal.Instance.DecreaseChapElementIndex();
- }
- FinishTask(false);
- }
- private void UnblockGame()
- {
- GameGlobal.Instance.PannigLocked = false;
- if (currentConversation.ConvType != DataTools.ConversationType.TextMessage)
- {
- if (currentConversation.ConvType != DataTools.ConversationType.Email)
- {
- Navigation.Instance.FinishCam();
- }
- }
-
- BlockerManager.Instance.UnblockAllButtons(GameplayManager.instance.isFirstGigPassed);
- }
- public void CancelConversation()
- {
- UnblockGame();
- DisableDialogItmes();
- SetConversation(currentConversation);
- }
- private void SetActiveButtons(bool setActive)
- {
- Button[] buttons = this.gameObject.GetComponentsInChildren<Button>();
- foreach (var button in buttons)
- {
- button.interactable = setActive;
- }
- }
- private void PlayDialogue()
- {
- Dialog.SetActive(true);
- string dialogFixed = "";
- if (!isNoProblemWithNextDialog)
- {
- return;
- }
- DataTools.Dialog currentDialog = currentConversation.Dialog[dataUser.UserData.ProgressData.CurrentConversationIndex];
- bool styleCheckRequred = currentDialog.StyleCheck;
- bool styleCheckPassed = false;
- if(styleCheckRequred)
- {
- styleCheckPassed = GetStyleBonusValue(currentDialog.StyleType) >= currentDialog.StyleValue;
- stylebar.CurrentValue = GetStyleBonusValue(currentDialog.StyleType) / 100.0f;
- stylebar.RequiredValue = currentDialog.StyleValue / 100.0f;
- stylebar.ShowRequired = true;
- }
- stylebar.gameObject.SetActive(styleCheckRequred);
- styleImage.gameObject.SetActive(styleCheckRequred);
- needToRestartConversation = (styleCheckRequred && !styleCheckPassed);
-
- dialogFixed = ChangeStrings.ChangeText(needToRestartConversation ? currentDialog.AltText : currentDialog.Text);
- currenConversationText.text = dialogFixed;
- currentSpeakerText.text = currentConversation.Dialog[dataUser.UserData.ProgressData.CurrentConversationIndex].NpcId;
- if (styleCheckRequred && !styleCheckPassed)
- {
- SetbuttonDataForStyleCheck();
- }
- else
- {
- SetAnswerButtons();
- }
- dialogAnimation.Play("StartDialogue");
- SetActiveButtons(true);
- GameObject npc = characters.NpcInstances.Find(n => n.GetComponent<NPC>().Name.ToUpper() == currentConversation.Dialog[dataUser.UserData.ProgressData.CurrentConversationIndex].NpcId.ToUpper());
- if (npc != null)
- {
- npc.GetComponent<NPC>().PlayAnimation(currentConversation.Dialog[dataUser.UserData.ProgressData.CurrentConversationIndex].AnimationIndex);
- }
- }
- private void DissableAllAnswerButtons()
- {
- for (int i = 0; i > buttonsAnswer.Length; i++)
- {
- buttonsAnswer[i].gameObject.SetActive(false);
- }
- }
- private void SetButtonData(int index)
- {
- DataTools.Reply reply = currentConversation.Dialog[dataUser.UserData.ProgressData.CurrentConversationIndex].Replies[index];
- string answerFixed = ChangeStrings.ChangeText(reply.Text);
- buttonsAnswer[index].SetAnswer(answerFixed, reply.Cost, reply.Reward, reply.StartAction==DataTools.StartAction.Repeat);
- buttonsAnswer[index].SetNextAnswerId(reply.NextDialogIndex);
- buttonsAnswer[index].UseCameraVFX = reply.UseCameraVFX;
- buttonsAnswer[index].SetAnimationToPlay(reply.AnimationId);
- }
- private void SetbuttonDataForStyleCheck()
- {
- nextButton.SetActive(false);
- answersContainer.SetActive(true);
- foreach (ButtonAnswer b in buttonsAnswer)
- {
- b.gameObject.SetActive(false);
- }
- buttonsAnswer[0].SetAnswer("(Change Clothes)", new DataTools.Currency(), new DataTools.Currency(),true,true);
- buttonsAnswer[1].SetAnswer("(Come back later)", new DataTools.Currency(), new DataTools.Currency(), true);
- buttonsAnswer[0].gameObject.SetActive(true);
- buttonsAnswer[1].gameObject.SetActive(true);
- buttonsAnswer[2].gameObject.SetActive(false);
- }
- public void OpenCustomization()
- {
- MenuOld.Instance.ChangePanel(MenuOld.Instance.GetMenuScenes()[1]);
- }
- private void SetAllButtons()
- {
- for (int i = 0; i < buttonsAnswer.Length; i++)
- {
- if (i < currentConversation.Dialog[dataUser.UserData.ProgressData.CurrentConversationIndex].Replies.Count)
- {
- buttonsAnswer[i].gameObject.SetActive(true);
- SetButtonData(i);
- }
- else
- {
- buttonsAnswer[i].gameObject.SetActive(false);
- }
- }
- }
- private void SetAnswerButtons()
- {
- DissableAllAnswerButtons();
-
- if (currentConversation.Dialog[dataUser.UserData.ProgressData.CurrentConversationIndex].Replies.Count == 0)
- {
- nextButton.SetActive(true);
- answersContainer.SetActive(false);
- }
- else
- {
- nextButton.SetActive(false);
- answersContainer.SetActive(true);
- SetAllButtons();
- }
- }
- private void SetupInitialDiaogue()
- {
- GameGlobal.Instance.PannigLocked = true;
- GameGlobal.Instance.SetCurrentDialogIndex(dataUser.UserData.ProgressData.CurrentConversationIndex);
- BlockerManager.Instance.BlockAllButtons("TextMessageButton");
- }
-
- protected override void OnLocationChanged(Location nLocation)
- {
- newLocationId = nLocation.ID;
- TryEnableDialogue(nLocation.ID);
- }
- protected override void SetConversation(DataTools.Conversation conversation)
- {
- dialogIsEnabled = true;
- base.SetConversation(conversation);
- GameGlobal.Instance.CurrentNpc = conversation.Dialog[0].NpcId;
- ConversationIndexCheck();
- if (!LocationMatch)
- {
- return;
- }
- TryEnableDialogue(newLocationId);
- }
- /// <summary>
- /// Prevents game from stuck if conversation index in UserData is incorrect
- /// </summary>
- private void ConversationIndexCheck()
- {
- if (dataUser.UserData.ProgressData.CurrentConversationIndex >= currentConversation.Dialog.Count)
- {
- dataUser.UserData.ProgressData.CurrentConversationIndex = 0;
- }
- }
- private void SetupPhoneMessage(PhoneUIMessages phoneMessage)
- {
- SetupInitialDiaogue();
- string dialogFixed = ChangeStrings.ChangeText(currentConversation.Dialog[0].Text);
- string replyFixed = "...";
- DataTools.Currency newCost = new DataTools.Currency(0,0,0,0,0);
- DataTools.Currency newReward = new DataTools.Currency(0, 0, 0, 0, 0);
- if (currentConversation.Dialog[0].Replies.Count > 0)
- {
- replyFixed = ChangeStrings.ChangeText(currentConversation.Dialog[0].Replies[0].Text);
- newCost = currentConversation.Dialog[0].Replies[0].Cost;
- newReward = currentConversation.Dialog[0].Replies[0].Reward;
- }
- phoneMessage.SetupPhoneMessage(currentConversation.Dialog[0].NpcId,
- currentConversation.Dialog[0].NpcId,
- dialogFixed,
- replyFixed,
- newCost,
- newReward);
- }
- public bool IsDialogEnabled
- {
- get
- {
- return dialogIsEnabled;
- }
- }
-
- private void SetButtonsBackgrounds(DataTools.ConversationType type)
- {
- switch(type)
- {
- case DataTools.ConversationType.PhoneCall:
- {
- foreach (ButtonAnswer b in buttonsAnswer)
- {
- b.SetPhoneConversationSprite();
- }
- break;
- }
- default:
- {
- foreach (ButtonAnswer b in buttonsAnswer)
- {
- b.SetBasicConversationSprite();
- }
- break;
- }
- }
- }
- private void EnableDialogue()
- {
- if (dialogIsEnabled)
- {
- SetButtonsBackgrounds(currentConversation.ConvType);
- SetNextMark();
- switch (currentConversation.ConvType)
- {
- case DataTools.ConversationType.PhoneCall:
- {
- Debug.LogError("PhoneCall");
- textGlobeConversation.SetActive(false);
- textGlobePhone.SetActive(true);
- DisableDialogItmes();
- BlockerManager.Instance.BlockAllButtons("AnswerCallButton");
- WebTools.Phone.PanelPhoneBehaviour.Instance.ActivateCall();
- break;
- }
- case DataTools.ConversationType.TextMessage:
- {
- Debug.LogError("TextMessage");
- AnimationPanelMain.Instance.ActiveRightBottomButtonsUIAnimation(true);
- WebTools.Phone.PanelPhoneBehaviour.Instance.ActivateTextMessage();
- SetupPhoneMessage(texMessage);
- break;
- }
- case DataTools.ConversationType.Email:
- {
- Debug.LogError("Email");
- AnimationPanelMain.Instance.ActiveRightBottomButtonsUIAnimation(true);
- WebTools.Phone.PanelPhoneBehaviour.Instance.ActivateEmailMessage();
- SetupPhoneMessage(mailMessage);
- break;
- }
- default:
- {
- textGlobeConversation.SetActive(true);
- textGlobePhone.SetActive(false);
- if (currentConversation.ConvType == DataTools.ConversationType.Auto /*|| dataUser.UserData.ProgressData.CurrentConversationIndex > 0*/)
- {
- StartDialogue();
- }
- else
- {
- Notification.SetActive(true);
- answersContainer.SetActive(false);
- dialogAnimation.Play("DialogueActive");
- }
- TalkIndicator.Instance.SetPositionObj(currentConversation.Dialog[dataUser.UserData.ProgressData.CurrentConversationIndex].NpcId);
- break;
- }
- }
- }
- else
- {
- DisableDialogItmes();
- }
- }
- private bool NoNotificationInQueue()
- {
- bool result = false;
- result = !GameNotificationManager.Instance.IsNotificationInQueue();
- return result;
- }
- private int GetStyleBonusValue(CustomizeType type)
- {
- int value = 0;
- currentCharacter.CheckStyle();
- CustomizeStats stats = currentCharacter.GetCustomizeStats();
- switch(type)
- {
- case CustomizeType.ACTIVEWEAR:
- {
- value = stats.ActiveWear;
- styleImage.sprite = styleActivewear;
- break;
- }
- case CustomizeType.CASUAL:
- {
- value = stats.Casual;
- styleImage.sprite = styleCasual;
- break;
- }
- case CustomizeType.COUTURE:
- {
- value = stats.Couture;
- styleImage.sprite = styleCouture;
- break;
- }
- case CustomizeType.MAKEUP:
- {
- value = stats.MakeUp;
- break;
- }
- case CustomizeType.SWIMWEAR:
- {
- value = stats.SwimWear;
- styleImage.sprite = styleSwimwear;
- break;
- }
- case CustomizeType.NONE:
- {
- value = currentCharacter.GetMaxCustomizeType();
- break;
- }
- default:
- {
- break;
- }
- }
- return value;
- }
- public void TryDialogIfPosssible()
- {
- if (!needToRestartConversation)
- {
- TryEnableDialogue(newLocationId);
- }
- else
- {
- Debug.Log("needToRestart is " + needToRestartConversation.ToString() + " - conversation will not be activated");
- }
- }
- public bool NeedToRestart
- {
- get
- {
- return needToRestartConversation;
- }
- }
- private void TryEnableDialogue(string newLocation)
- {
- if (currentConversation != null && NoNotificationInQueue())
- {
- bool locationFlag = currentConversation.Location.ToUpper() == newLocation.ToUpper();
- bool exteriorFlag = (currentConversation.Location.ToUpper() == "EXTERIOR") && GameGlobal.Instance.GetIsExterior();
- bool anyFlag = (currentConversation.Location.ToUpper() == "ANY");
- if (locationFlag || exteriorFlag || anyFlag)
- {
- switch(currentConversation.ConvType)
- {
- case DataTools.ConversationType.Basic:
- case DataTools.ConversationType.Auto:
- case DataTools.ConversationType.Optional:
- case DataTools.ConversationType.Email:
- case DataTools.ConversationType.PhoneCall:
- case DataTools.ConversationType.TextMessage:
- {
- EnableDialogue();
- break;
- }
- default:
- {
- DisableDialogItmes();
- break;
- }
- }
- }
- else
- {
- DisableDialogItmes();
- }
- }
- else
- {
- DisableDialogItmes();
- }
- }
- }
|