QuestMarker.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using UnityEngine;
  2. /// <summary>
  3. /// Individual quest marker component that displays quest information and handles interactions
  4. /// </summary>
  5. public class QuestMarker : MonoBehaviour
  6. {
  7. [Header("Marker Components")]
  8. [SerializeField] private ActiveQuest activeQuest;
  9. [SerializeField] private GameObject completionArea;
  10. [Header("Visual Settings")]
  11. [SerializeField] private bool showFloatingText = true;
  12. [SerializeField] private float bobSpeed = 1f;
  13. [SerializeField] private float bobHeight = 0.5f;
  14. private Vector3 originalPosition;
  15. private Renderer markerRenderer;
  16. private TextMesh floatingText;
  17. private float time = 0f;
  18. public ActiveQuest ActiveQuest => activeQuest;
  19. private void Start()
  20. {
  21. originalPosition = transform.position;
  22. markerRenderer = GetComponent<Renderer>();
  23. if (showFloatingText)
  24. {
  25. CreateFloatingText();
  26. }
  27. }
  28. private void Update()
  29. {
  30. // Gentle bobbing animation
  31. if (bobHeight > 0f)
  32. {
  33. time += Time.deltaTime * bobSpeed;
  34. float newY = originalPosition.y + Mathf.Sin(time) * bobHeight;
  35. transform.position = new Vector3(originalPosition.x, newY, originalPosition.z);
  36. }
  37. // Update floating text if quest exists
  38. if (activeQuest != null && floatingText != null)
  39. {
  40. UpdateFloatingText();
  41. }
  42. }
  43. /// <summary>
  44. /// Initialize the quest marker with quest data
  45. /// </summary>
  46. public void Initialize(ActiveQuest quest, GameObject area = null)
  47. {
  48. activeQuest = quest;
  49. completionArea = area;
  50. if (completionArea != null)
  51. {
  52. completionArea.transform.SetParent(transform);
  53. }
  54. // Update visual representation
  55. UpdateMarkerVisuals();
  56. }
  57. /// <summary>
  58. /// Set the marker color
  59. /// </summary>
  60. public void SetColor(Color color)
  61. {
  62. if (markerRenderer != null)
  63. {
  64. markerRenderer.material.color = color;
  65. }
  66. // Also color the completion area if it exists
  67. if (completionArea != null)
  68. {
  69. var areaRenderer = completionArea.GetComponent<Renderer>();
  70. if (areaRenderer != null)
  71. {
  72. var areaColor = color;
  73. areaColor.a = 0.3f; // Make area semi-transparent
  74. areaRenderer.material.color = areaColor;
  75. }
  76. }
  77. }
  78. private void CreateFloatingText()
  79. {
  80. // Create floating text object
  81. GameObject textObject = new GameObject("QuestText");
  82. textObject.transform.SetParent(transform);
  83. textObject.transform.localPosition = Vector3.up * 1.5f;
  84. // Add TextMesh component
  85. floatingText = textObject.AddComponent<TextMesh>();
  86. floatingText.anchor = TextAnchor.MiddleCenter;
  87. floatingText.alignment = TextAlignment.Center;
  88. floatingText.fontSize = 16;
  89. floatingText.color = Color.white;
  90. // Make text face camera (simple billboard)
  91. textObject.transform.LookAt(Camera.main?.transform ?? Camera.current?.transform);
  92. textObject.transform.Rotate(0, 180, 0); // Flip to face camera correctly
  93. }
  94. private void UpdateFloatingText()
  95. {
  96. if (floatingText == null || activeQuest?.questData == null) return;
  97. // Show quest title and time remaining
  98. string questText = activeQuest.questData.questTitle;
  99. // Add time remaining with color coding
  100. var urgency = activeQuest.GetUrgency();
  101. string timeRemaining = activeQuest.GetTimeRemainingString();
  102. // Color code based on urgency
  103. string timeColor = urgency switch
  104. {
  105. QuestUrgency.Low => "white",
  106. QuestUrgency.Medium => "yellow",
  107. QuestUrgency.High => "orange",
  108. QuestUrgency.Critical => "red",
  109. _ => "white"
  110. };
  111. floatingText.text = $"{questText}\n<color={timeColor}>{timeRemaining}</color>";
  112. // Make text face camera
  113. if (Camera.main != null)
  114. {
  115. Vector3 directionToCamera = Camera.main.transform.position - floatingText.transform.position;
  116. floatingText.transform.rotation = Quaternion.LookRotation(-directionToCamera);
  117. }
  118. }
  119. private void UpdateMarkerVisuals()
  120. {
  121. if (activeQuest?.questData == null) return;
  122. // Scale marker based on quest importance/difficulty
  123. float scale = activeQuest.questData.difficulty switch
  124. {
  125. QuestDifficulty.Easy => 0.8f,
  126. QuestDifficulty.Normal => 1.0f,
  127. QuestDifficulty.Hard => 1.2f,
  128. QuestDifficulty.Legendary => 1.5f,
  129. _ => 1.0f
  130. };
  131. transform.localScale = Vector3.one * scale;
  132. // Adjust bob speed based on urgency
  133. var urgency = activeQuest.GetUrgency();
  134. bobSpeed = urgency switch
  135. {
  136. QuestUrgency.Low => 0.5f,
  137. QuestUrgency.Medium => 1.0f,
  138. QuestUrgency.High => 1.5f,
  139. QuestUrgency.Critical => 2.5f,
  140. _ => 1.0f
  141. };
  142. }
  143. /// <summary>
  144. /// Called when player clicks on the quest marker
  145. /// </summary>
  146. private void OnMouseDown()
  147. {
  148. if (activeQuest?.questData == null) return;
  149. // Show quest info in UI or console
  150. ShowQuestInfo();
  151. }
  152. private void ShowQuestInfo()
  153. {
  154. if (activeQuest?.questData == null) return;
  155. var quest = activeQuest.questData;
  156. string info = $"Quest: {quest.questTitle}\n" +
  157. $"Description: {quest.questDescription}\n" +
  158. $"Time Remaining: {activeQuest.GetTimeRemainingString()}\n" +
  159. $"Progress: {activeQuest.GetOverallProgress() * 100:F0}%\n" +
  160. $"Reward: {quest.goldReward}g, {quest.renownReward} renown";
  161. // TODO: Show in actual UI popup instead of just debug log
  162. }
  163. /// <summary>
  164. /// Check if position is within quest completion area
  165. /// </summary>
  166. public bool IsPositionInCompletionArea(Vector3 position)
  167. {
  168. if (activeQuest?.questData == null) return false;
  169. float distance = Vector3.Distance(transform.position, position);
  170. return distance <= activeQuest.questData.completionRadius;
  171. }
  172. /// <summary>
  173. /// Get distance to this quest marker from given position
  174. /// </summary>
  175. public float GetDistanceFrom(Vector3 position)
  176. {
  177. return Vector3.Distance(transform.position, position);
  178. }
  179. /// <summary>
  180. /// Show completion effect when quest is completed
  181. /// </summary>
  182. public void ShowCompletionEffect()
  183. {
  184. // Simple completion effect - change color and scale
  185. if (markerRenderer != null)
  186. {
  187. markerRenderer.material.color = Color.green;
  188. }
  189. // Animate scale up then destroy
  190. StartCoroutine(CompletionAnimation());
  191. }
  192. private System.Collections.IEnumerator CompletionAnimation()
  193. {
  194. Vector3 originalScale = transform.localScale;
  195. float duration = 1f;
  196. float elapsed = 0f;
  197. while (elapsed < duration)
  198. {
  199. elapsed += Time.deltaTime;
  200. float progress = elapsed / duration;
  201. // Scale up then down
  202. float scaleMultiplier = 1f + Mathf.Sin(progress * Mathf.PI) * 0.5f;
  203. transform.localScale = originalScale * scaleMultiplier;
  204. // Fade out
  205. if (markerRenderer != null)
  206. {
  207. Color color = markerRenderer.material.color;
  208. color.a = 1f - progress;
  209. markerRenderer.material.color = color;
  210. }
  211. yield return null;
  212. }
  213. // Destroy the marker
  214. Destroy(gameObject);
  215. }
  216. #region Editor Gizmos
  217. private void OnDrawGizmos()
  218. {
  219. if (activeQuest?.questData == null) return;
  220. // Draw completion radius
  221. Gizmos.color = Color.yellow;
  222. Gizmos.DrawWireSphere(transform.position, activeQuest.questData.completionRadius);
  223. // Draw quest info
  224. var urgency = activeQuest.GetUrgency();
  225. Gizmos.color = urgency switch
  226. {
  227. QuestUrgency.Low => Color.green,
  228. QuestUrgency.Medium => Color.yellow,
  229. QuestUrgency.High => new Color(1f, 0.5f, 0f), // orange
  230. QuestUrgency.Critical => Color.red,
  231. _ => Color.white
  232. };
  233. Gizmos.DrawWireCube(transform.position, Vector3.one * 0.5f);
  234. }
  235. #endregion
  236. }