Files
SimUL/Assets/Scripts/DialogueSystem/ConversationController.cs
T
Vladimir Koshevarov c8047bd170 Fix shop System
add food indicator
2022-11-17 17:24:00 +02:00

86 lines
2.1 KiB
C#

using Assets.Scripts.Actions;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class ConversationController : MonoBehaviour
{
[SerializeField]
public TextMeshProUGUI _title;
[SerializeField]
public Button _choiceButton;
[SerializeField]
private Button _closeBtn;
[SerializeField]
PlayerManager _playerManager;
public ConversationChangeEvent conversationChangeEvent;
private List<ChoiceController> choiceControllers = new();
private void Awake()
{
Hide();
}
public void Change(string title, Dictionary<string, BaseAction> options)
{
_closeBtn.onClick.AddListener(() =>
{
Hide();
});
_playerManager.allowMovement = false;
RemoveChoices();
_title.text = title;
gameObject.SetActive(true);
for (var count = 0; count < options.Count; count++)
{
ChoiceController c = ChoiceController.AddChoiceButton(_choiceButton, options.ElementAt(count), count);
c.conversationChangeEvent.AddListener(ApplyActionOnPlayer);
choiceControllers.Add(c);
}
StopAllCoroutines();
Time.timeScale = 0;
_choiceButton.gameObject.SetActive(false);
}
private void ApplyActionOnPlayer(BaseAction action)
{
if (!_playerManager.TryBuyAction(action))
{
YesNoDialogUI.Instance.ShowQuestion("not enouth money",
() =>
{
},
() =>
{
//do nothing
});
}
}
private void RemoveChoices()
{
foreach (ChoiceController c in choiceControllers)
{
c.conversationChangeEvent.RemoveAllListeners();
Destroy(c.gameObject);
}
choiceControllers.Clear();
}
public void Hide()
{
RemoveChoices();
gameObject.SetActive(false);
Time.timeScale = 1;
_playerManager.allowMovement = true;
}
}