GuestInfoUI.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections.Generic;
  4. public class GuestInfoUI : MonoBehaviour
  5. {
  6. public static GuestInfoUI Instance { get; private set; }
  7. [Header("UI Components")]
  8. [SerializeField] private GameObject guestInfoPanel;
  9. [SerializeField] private Text guestNameText;
  10. [SerializeField] private Text guestTypeText;
  11. [SerializeField] private Text guestStateText;
  12. [SerializeField] private Image moodImage;
  13. [SerializeField] private Text satisfactionText;
  14. [SerializeField] private Text patienceText;
  15. [SerializeField] private Transform wishesContainer;
  16. [SerializeField] private Transform moodReasonsContainer;
  17. [SerializeField] private Button closeButton;
  18. [Header("Prefabs")]
  19. [SerializeField] private GameObject wishItemPrefab;
  20. [SerializeField] private GameObject moodReasonPrefab;
  21. [Header("Mood Sprites")]
  22. [SerializeField] private Sprite[] moodSprites; // Happy, Neutral, Annoyed, Angry
  23. private Guest currentGuest;
  24. private void Awake()
  25. {
  26. if (Instance != null && Instance != this)
  27. {
  28. Destroy(this.gameObject);
  29. return;
  30. }
  31. Instance = this;
  32. }
  33. private void Start()
  34. {
  35. SetupUI();
  36. }
  37. private void SetupUI()
  38. {
  39. if (guestInfoPanel != null)
  40. guestInfoPanel.SetActive(false);
  41. if (closeButton != null)
  42. closeButton.onClick.AddListener(CloseGuestInfo);
  43. // Create UI elements if they don't exist
  44. CreateUIIfMissing();
  45. }
  46. private void CreateUIIfMissing()
  47. {
  48. if (guestInfoPanel == null)
  49. {
  50. CreateGuestInfoPanel();
  51. }
  52. }
  53. private void CreateGuestInfoPanel()
  54. {
  55. // Create a simple guest info panel programmatically
  56. GameObject canvas = GameObject.Find("Canvas");
  57. if (canvas == null)
  58. {
  59. GameObject canvasObj = new GameObject("Canvas");
  60. canvas = canvasObj;
  61. canvas.AddComponent<Canvas>();
  62. canvas.AddComponent<CanvasScaler>();
  63. canvas.AddComponent<GraphicRaycaster>();
  64. canvas.GetComponent<Canvas>().renderMode = RenderMode.ScreenSpaceOverlay;
  65. }
  66. guestInfoPanel = new GameObject("Guest Info Panel");
  67. guestInfoPanel.transform.SetParent(canvas.transform, false);
  68. RectTransform panelRect = guestInfoPanel.AddComponent<RectTransform>();
  69. panelRect.anchorMin = new Vector2(0.1f, 0.1f);
  70. panelRect.anchorMax = new Vector2(0.9f, 0.9f);
  71. panelRect.offsetMin = Vector2.zero;
  72. panelRect.offsetMax = Vector2.zero;
  73. Image panelImage = guestInfoPanel.AddComponent<Image>();
  74. panelImage.color = new Color(0, 0, 0, 0.8f);
  75. guestInfoPanel.SetActive(false);
  76. }
  77. public void ShowGuestInfo(Guest guest)
  78. {
  79. if (guest == null || guestInfoPanel == null) return;
  80. currentGuest = guest;
  81. guestInfoPanel.SetActive(true);
  82. UpdateGuestInfo();
  83. }
  84. public void CloseGuestInfo()
  85. {
  86. if (guestInfoPanel != null)
  87. guestInfoPanel.SetActive(false);
  88. currentGuest = null;
  89. }
  90. private void Update()
  91. {
  92. // Update info if panel is open and we have a guest
  93. if (currentGuest != null && guestInfoPanel != null && guestInfoPanel.activeSelf)
  94. {
  95. UpdateGuestInfo();
  96. }
  97. }
  98. private void UpdateGuestInfo()
  99. {
  100. if (currentGuest == null) return;
  101. // Update basic info
  102. if (guestNameText != null)
  103. guestNameText.text = currentGuest.GuestName;
  104. if (guestTypeText != null)
  105. guestTypeText.text = $"Type: {currentGuest.Type}";
  106. if (guestStateText != null)
  107. guestStateText.text = $"State: {currentGuest.CurrentState}";
  108. // Update mood
  109. if (moodImage != null && moodSprites != null && moodSprites.Length > (int)currentGuest.CurrentMood)
  110. {
  111. moodImage.sprite = moodSprites[(int)currentGuest.CurrentMood];
  112. }
  113. // Update satisfaction
  114. if (satisfactionText != null)
  115. {
  116. float satisfaction = currentGuest.SatisfactionLevel;
  117. satisfactionText.text = $"Satisfaction: {satisfaction:P0}";
  118. // Color code satisfaction
  119. if (satisfaction > 0.7f)
  120. satisfactionText.color = Color.green;
  121. else if (satisfaction < 0.4f)
  122. satisfactionText.color = Color.red;
  123. else
  124. satisfactionText.color = Color.yellow;
  125. }
  126. // Update patience
  127. if (patienceText != null)
  128. {
  129. float patience = currentGuest.Patience;
  130. patienceText.text = $"Patience: {patience:F0}%";
  131. // Color code patience
  132. if (patience > 70f)
  133. patienceText.color = Color.green;
  134. else if (patience < 30f)
  135. patienceText.color = Color.red;
  136. else
  137. patienceText.color = Color.yellow;
  138. }
  139. // Update wishes
  140. UpdateWishesList();
  141. // Update mood reasons
  142. UpdateMoodReasonsList();
  143. }
  144. private void UpdateWishesList()
  145. {
  146. if (wishesContainer == null || currentGuest == null) return;
  147. // Clear existing wishes
  148. foreach (Transform child in wishesContainer)
  149. {
  150. Destroy(child.gameObject);
  151. }
  152. // Add current wishes
  153. foreach (var wish in currentGuest.Wishes)
  154. {
  155. CreateWishItem(wish);
  156. }
  157. }
  158. private void CreateWishItem(GuestWish wish)
  159. {
  160. GameObject wishItem;
  161. if (wishItemPrefab != null)
  162. {
  163. wishItem = Instantiate(wishItemPrefab, wishesContainer);
  164. }
  165. else
  166. {
  167. // Create simple wish item
  168. wishItem = new GameObject("Wish Item");
  169. wishItem.transform.SetParent(wishesContainer);
  170. Text wishText = wishItem.AddComponent<Text>();
  171. wishText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
  172. wishText.fontSize = 14;
  173. }
  174. Text textComponent = wishItem.GetComponent<Text>();
  175. if (textComponent != null)
  176. {
  177. string fulfillmentStatus = wish.isFulfilled ? "✓" : "✗";
  178. textComponent.text = $"{fulfillmentStatus} {wish.wishType} (Importance: {wish.importance:P0})";
  179. textComponent.color = wish.isFulfilled ? Color.green : Color.white;
  180. }
  181. }
  182. private void UpdateMoodReasonsList()
  183. {
  184. if (moodReasonsContainer == null || currentGuest == null) return;
  185. // Clear existing reasons
  186. foreach (Transform child in moodReasonsContainer)
  187. {
  188. Destroy(child.gameObject);
  189. }
  190. // Add recent mood reasons
  191. foreach (string reason in currentGuest.MoodReasons)
  192. {
  193. CreateMoodReasonItem(reason);
  194. }
  195. }
  196. private void CreateMoodReasonItem(string reason)
  197. {
  198. GameObject reasonItem;
  199. if (moodReasonPrefab != null)
  200. {
  201. reasonItem = Instantiate(moodReasonPrefab, moodReasonsContainer);
  202. }
  203. else
  204. {
  205. // Create simple reason item
  206. reasonItem = new GameObject("Mood Reason Item");
  207. reasonItem.transform.SetParent(moodReasonsContainer);
  208. Text reasonText = reasonItem.AddComponent<Text>();
  209. reasonText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
  210. reasonText.fontSize = 12;
  211. reasonText.color = Color.gray;
  212. }
  213. Text textComponent = reasonItem.GetComponent<Text>();
  214. if (textComponent != null)
  215. {
  216. textComponent.text = reason;
  217. }
  218. }
  219. #region Static Helper Methods
  220. public static void ShowGuestInfoStatic(Guest guest)
  221. {
  222. if (Instance != null)
  223. {
  224. Instance.ShowGuestInfo(guest);
  225. }
  226. }
  227. public static void CloseGuestInfoStatic()
  228. {
  229. if (Instance != null)
  230. {
  231. Instance.CloseGuestInfo();
  232. }
  233. }
  234. #endregion
  235. }