Files
SimUL/Assets/Scripts/DialogueSystem/ChoiceController.cs
T

62 lines
1.9 KiB
C#

using Assets.Scripts.Actions;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
[System.Serializable]
public class ConversationChangeEvent : UnityEvent<BaseAction> { }
public class ChoiceController : MonoBehaviour
{
public PlayerController playerController;
public KeyValuePair<string, BaseAction> _option;
public ConversationChangeEvent conversationChangeEvent;
// Update is called once per frame
public static ChoiceController AddChoiceButton(Button choiceButtonTemplate, KeyValuePair<string, BaseAction> option, int index)
{
int buttonSpacing = -50;
Button button = Instantiate(choiceButtonTemplate);
button.transform.SetParent(choiceButtonTemplate.transform.parent);
button.transform.localScale = Vector3.one;
button.transform.localPosition = choiceButtonTemplate.transform.localPosition + new Vector3(0, index * buttonSpacing, 0);
button.name = option.Key;
button.gameObject.SetActive(true);
ChoiceController choiceController = button.GetComponent<ChoiceController>();
choiceController._option = option;
return choiceController;
}
private void Start()
{
if (conversationChangeEvent == null)
conversationChangeEvent = new ConversationChangeEvent();
var btn = GetComponent<Button>();
var txt = btn.GetComponentInChildren<TextMeshProUGUI>();
txt.text = _option.Key;
}
public void MakeChoice()
{
if (playerController.TryBuyAction(_option.Value))
{
conversationChangeEvent.Invoke(_option.Value);
}
else
{
YesNoDialogUI.Instance.ShowQuestion("not enouth money",
() =>
{
},
() =>
{
//do nothing
});
}
}
}