| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections.Generic;
- public class GuestInfoUI : MonoBehaviour
- {
- public static GuestInfoUI Instance { get; private set; }
- [Header("UI Components")]
- [SerializeField] private GameObject guestInfoPanel;
- [SerializeField] private Text guestNameText;
- [SerializeField] private Text guestTypeText;
- [SerializeField] private Text guestStateText;
- [SerializeField] private Image moodImage;
- [SerializeField] private Text satisfactionText;
- [SerializeField] private Text patienceText;
- [SerializeField] private Transform wishesContainer;
- [SerializeField] private Transform moodReasonsContainer;
- [SerializeField] private Button closeButton;
- [Header("Prefabs")]
- [SerializeField] private GameObject wishItemPrefab;
- [SerializeField] private GameObject moodReasonPrefab;
- [Header("Mood Sprites")]
- [SerializeField] private Sprite[] moodSprites; // Happy, Neutral, Annoyed, Angry
- private Guest currentGuest;
- private void Awake()
- {
- if (Instance != null && Instance != this)
- {
- Destroy(this.gameObject);
- return;
- }
- Instance = this;
- }
- private void Start()
- {
- SetupUI();
- }
- private void SetupUI()
- {
- if (guestInfoPanel != null)
- guestInfoPanel.SetActive(false);
- if (closeButton != null)
- closeButton.onClick.AddListener(CloseGuestInfo);
- // Create UI elements if they don't exist
- CreateUIIfMissing();
- }
- private void CreateUIIfMissing()
- {
- if (guestInfoPanel == null)
- {
- CreateGuestInfoPanel();
- }
- }
- private void CreateGuestInfoPanel()
- {
- // Create a simple guest info panel programmatically
- GameObject canvas = GameObject.Find("Canvas");
- if (canvas == null)
- {
- GameObject canvasObj = new GameObject("Canvas");
- canvas = canvasObj;
- canvas.AddComponent<Canvas>();
- canvas.AddComponent<CanvasScaler>();
- canvas.AddComponent<GraphicRaycaster>();
- canvas.GetComponent<Canvas>().renderMode = RenderMode.ScreenSpaceOverlay;
- }
- guestInfoPanel = new GameObject("Guest Info Panel");
- guestInfoPanel.transform.SetParent(canvas.transform, false);
- RectTransform panelRect = guestInfoPanel.AddComponent<RectTransform>();
- panelRect.anchorMin = new Vector2(0.1f, 0.1f);
- panelRect.anchorMax = new Vector2(0.9f, 0.9f);
- panelRect.offsetMin = Vector2.zero;
- panelRect.offsetMax = Vector2.zero;
- Image panelImage = guestInfoPanel.AddComponent<Image>();
- panelImage.color = new Color(0, 0, 0, 0.8f);
- guestInfoPanel.SetActive(false);
- }
- public void ShowGuestInfo(Guest guest)
- {
- if (guest == null || guestInfoPanel == null) return;
- currentGuest = guest;
- guestInfoPanel.SetActive(true);
- UpdateGuestInfo();
- }
- public void CloseGuestInfo()
- {
- if (guestInfoPanel != null)
- guestInfoPanel.SetActive(false);
- currentGuest = null;
- }
- private void Update()
- {
- // Update info if panel is open and we have a guest
- if (currentGuest != null && guestInfoPanel != null && guestInfoPanel.activeSelf)
- {
- UpdateGuestInfo();
- }
- }
- private void UpdateGuestInfo()
- {
- if (currentGuest == null) return;
- // Update basic info
- if (guestNameText != null)
- guestNameText.text = currentGuest.GuestName;
- if (guestTypeText != null)
- guestTypeText.text = $"Type: {currentGuest.Type}";
- if (guestStateText != null)
- guestStateText.text = $"State: {currentGuest.CurrentState}";
- // Update mood
- if (moodImage != null && moodSprites != null && moodSprites.Length > (int)currentGuest.CurrentMood)
- {
- moodImage.sprite = moodSprites[(int)currentGuest.CurrentMood];
- }
- // Update satisfaction
- if (satisfactionText != null)
- {
- float satisfaction = currentGuest.SatisfactionLevel;
- satisfactionText.text = $"Satisfaction: {satisfaction:P0}";
- // Color code satisfaction
- if (satisfaction > 0.7f)
- satisfactionText.color = Color.green;
- else if (satisfaction < 0.4f)
- satisfactionText.color = Color.red;
- else
- satisfactionText.color = Color.yellow;
- }
- // Update patience
- if (patienceText != null)
- {
- float patience = currentGuest.Patience;
- patienceText.text = $"Patience: {patience:F0}%";
- // Color code patience
- if (patience > 70f)
- patienceText.color = Color.green;
- else if (patience < 30f)
- patienceText.color = Color.red;
- else
- patienceText.color = Color.yellow;
- }
- // Update wishes
- UpdateWishesList();
- // Update mood reasons
- UpdateMoodReasonsList();
- }
- private void UpdateWishesList()
- {
- if (wishesContainer == null || currentGuest == null) return;
- // Clear existing wishes
- foreach (Transform child in wishesContainer)
- {
- Destroy(child.gameObject);
- }
- // Add current wishes
- foreach (var wish in currentGuest.Wishes)
- {
- CreateWishItem(wish);
- }
- }
- private void CreateWishItem(GuestWish wish)
- {
- GameObject wishItem;
- if (wishItemPrefab != null)
- {
- wishItem = Instantiate(wishItemPrefab, wishesContainer);
- }
- else
- {
- // Create simple wish item
- wishItem = new GameObject("Wish Item");
- wishItem.transform.SetParent(wishesContainer);
- Text wishText = wishItem.AddComponent<Text>();
- wishText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
- wishText.fontSize = 14;
- }
- Text textComponent = wishItem.GetComponent<Text>();
- if (textComponent != null)
- {
- string fulfillmentStatus = wish.isFulfilled ? "✓" : "✗";
- textComponent.text = $"{fulfillmentStatus} {wish.wishType} (Importance: {wish.importance:P0})";
- textComponent.color = wish.isFulfilled ? Color.green : Color.white;
- }
- }
- private void UpdateMoodReasonsList()
- {
- if (moodReasonsContainer == null || currentGuest == null) return;
- // Clear existing reasons
- foreach (Transform child in moodReasonsContainer)
- {
- Destroy(child.gameObject);
- }
- // Add recent mood reasons
- foreach (string reason in currentGuest.MoodReasons)
- {
- CreateMoodReasonItem(reason);
- }
- }
- private void CreateMoodReasonItem(string reason)
- {
- GameObject reasonItem;
- if (moodReasonPrefab != null)
- {
- reasonItem = Instantiate(moodReasonPrefab, moodReasonsContainer);
- }
- else
- {
- // Create simple reason item
- reasonItem = new GameObject("Mood Reason Item");
- reasonItem.transform.SetParent(moodReasonsContainer);
- Text reasonText = reasonItem.AddComponent<Text>();
- reasonText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
- reasonText.fontSize = 12;
- reasonText.color = Color.gray;
- }
- Text textComponent = reasonItem.GetComponent<Text>();
- if (textComponent != null)
- {
- textComponent.text = reason;
- }
- }
- #region Static Helper Methods
- public static void ShowGuestInfoStatic(Guest guest)
- {
- if (Instance != null)
- {
- Instance.ShowGuestInfo(guest);
- }
- }
- public static void CloseGuestInfoStatic()
- {
- if (Instance != null)
- {
- Instance.CloseGuestInfo();
- }
- }
- #endregion
- }
|