217 lines
6.8 KiB
C#
217 lines
6.8 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
using System.Collections.Generic;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class Checklist : MonoBehaviour
|
|
{
|
|
[SerializeField] private GameObject checklistPanel;
|
|
[SerializeField] private GameObject checklistItemPrefab; // Should be a Toggle with TextMeshPro text child
|
|
[SerializeField] private Transform checklistContent; // Container for checklist items
|
|
[SerializeField] private float fadeAlpha = 0.2f; // Alpha value when faded (20% opacity)
|
|
|
|
private Dictionary<string, Toggle> objectiveToggles = new Dictionary<string, Toggle>();
|
|
private Dictionary<string, Text> objectiveTexts = new Dictionary<string, Text>();
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (LevelManager.Instance != null)
|
|
{
|
|
LevelManager.Instance.OnObjectiveCompleted += OnObjectiveCompleted;
|
|
}
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (LevelManager.Instance != null)
|
|
{
|
|
LevelManager.Instance.OnObjectiveCompleted -= OnObjectiveCompleted;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
// Hide panel initially
|
|
if (checklistPanel != null)
|
|
{
|
|
checklistPanel.SetActive(false);
|
|
}
|
|
|
|
BuildChecklistFromObjectives();
|
|
}
|
|
|
|
private void BuildChecklistFromObjectives()
|
|
{
|
|
if (LevelManager.Instance == null)
|
|
{
|
|
Debug.LogError("Checklist: LevelManager.Instance is NULL!");
|
|
return;
|
|
}
|
|
|
|
// Clear existing items
|
|
for (int i = checklistContent.childCount - 1; i >= 0; i--)
|
|
{
|
|
Destroy(checklistContent.GetChild(i).gameObject);
|
|
}
|
|
objectiveToggles.Clear();
|
|
objectiveTexts.Clear();
|
|
|
|
// Get all objectives from LevelManager
|
|
List<Objective> objectives = LevelManager.Instance.GetAllObjectives();
|
|
|
|
Debug.Log($"Checklist: Found {objectives.Count} objectives to display");
|
|
|
|
foreach (var objective in objectives)
|
|
{
|
|
if (checklistItemPrefab == null)
|
|
{
|
|
Debug.LogError("Checklist: Item Prefab is NULL! Assign it in the Inspector.");
|
|
return;
|
|
}
|
|
|
|
// Instantiate the toggle prefab
|
|
GameObject itemGO = Instantiate(checklistItemPrefab, checklistContent);
|
|
itemGO.name = $"Checklist_{objective.name}";
|
|
|
|
Toggle toggle = itemGO.GetComponent<Toggle>();
|
|
|
|
if (toggle == null)
|
|
{
|
|
Debug.LogError($"Checklist: Prefab '{checklistItemPrefab.name}' does not have a Toggle component!");
|
|
Destroy(itemGO);
|
|
continue;
|
|
}
|
|
|
|
// Find the TextMeshProUGUI child component
|
|
Text textComponent = itemGO.GetComponentInChildren<Text>();
|
|
if (textComponent == null)
|
|
{
|
|
Debug.LogError($"Checklist: Prefab does not have a TextMeshProUGUI child component!");
|
|
Destroy(itemGO);
|
|
continue;
|
|
}
|
|
|
|
// Set the text to objective description
|
|
textComponent.text = objective.description;
|
|
Debug.Log($"Checklist: Created toggle for '{objective.description}'");
|
|
|
|
// Set toggle state based on objective completion
|
|
if (objective.isCompleted)
|
|
{
|
|
// Use the same trick for already-completed objectives
|
|
toggle.interactable = true;
|
|
toggle.isOn = true;
|
|
toggle.interactable = false;
|
|
|
|
// Apply fade effect
|
|
ApplyFadeToText(textComponent);
|
|
|
|
Debug.Log($"Checklist: Objective '{objective.description}' was already completed");
|
|
}
|
|
else
|
|
{
|
|
toggle.isOn = false;
|
|
}
|
|
|
|
// Store references
|
|
objectiveToggles[objective.name] = toggle;
|
|
objectiveTexts[objective.name] = textComponent;
|
|
|
|
// Make toggle read-only - prevent player from clicking
|
|
toggle.interactable = false;
|
|
}
|
|
|
|
Debug.Log($"Checklist: Built {objectiveToggles.Count} checklist items");
|
|
}
|
|
|
|
private void OnObjectiveCompleted(string objectiveKey)
|
|
{
|
|
// Update toggle state
|
|
if (objectiveToggles.ContainsKey(objectiveKey))
|
|
{
|
|
Toggle toggle = objectiveToggles[objectiveKey];
|
|
|
|
// Temporarily enable the toggle so it can update visually
|
|
toggle.interactable = true;
|
|
toggle.isOn = true;
|
|
toggle.interactable = false;
|
|
|
|
// Force layout rebuild
|
|
LayoutRebuilder.ForceRebuildLayoutImmediate(toggle.GetComponent<RectTransform>());
|
|
|
|
// Fade the text
|
|
if (objectiveTexts.ContainsKey(objectiveKey))
|
|
{
|
|
Text textComponent = objectiveTexts[objectiveKey];
|
|
ApplyFadeToText(textComponent);
|
|
}
|
|
|
|
Debug.Log($"Checklist: Objective '{objectiveKey}' marked complete");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"Checklist: Objective '{objectiveKey}' not found in toggles dictionary!");
|
|
}
|
|
}
|
|
|
|
private void ApplyFadeToText(Text textComponent)
|
|
{
|
|
if (textComponent == null)
|
|
{
|
|
Debug.LogError("Checklist: Text component is NULL!");
|
|
return;
|
|
}
|
|
|
|
Color textColor = textComponent.color;
|
|
textColor.a = fadeAlpha;
|
|
textComponent.color = textColor;
|
|
|
|
Debug.Log($"Checklist: Faded text '{textComponent.text}' to alpha {fadeAlpha}");
|
|
}
|
|
|
|
public void ToggleChecklist()
|
|
{
|
|
if (checklistPanel != null)
|
|
{
|
|
bool isActive = checklistPanel.activeSelf;
|
|
checklistPanel.SetActive(!isActive);
|
|
|
|
// Unlock cursor when checklist is open
|
|
if (!isActive)
|
|
{
|
|
Cursor.lockState = CursorLockMode.None;
|
|
Cursor.visible = true;
|
|
Time.timeScale = 0f; // Pause game
|
|
}
|
|
else
|
|
{
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
Cursor.visible = false;
|
|
Time.timeScale = 1f; // Resume game
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ShowChecklist()
|
|
{
|
|
if (checklistPanel != null)
|
|
{
|
|
checklistPanel.SetActive(true);
|
|
Cursor.lockState = CursorLockMode.None;
|
|
Cursor.visible = true;
|
|
Time.timeScale = 0f; // Pause game
|
|
}
|
|
}
|
|
|
|
public void HideChecklist()
|
|
{
|
|
if (checklistPanel != null)
|
|
{
|
|
checklistPanel.SetActive(false);
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
Cursor.visible = false;
|
|
Time.timeScale = 1f; // Resume game
|
|
}
|
|
}
|
|
} |