2025-08-22 20:43:18 +05:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class GameplayManager : MonoBehaviour
|
|
|
|
{
|
|
|
|
public static GameplayManager Instance = null;
|
|
|
|
|
|
|
|
[Header("Assign levels in order (Level 1 at index 0)")]
|
|
|
|
public List<Level> levels;
|
|
|
|
|
|
|
|
private int currentLevelIndex = -1;
|
|
|
|
private Level currentLevel;
|
2025-08-27 17:41:31 +05:00
|
|
|
[SerializeField] private List<GameObject> levelStartTriggers;
|
2025-08-22 20:43:18 +05:00
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
if (Instance == null)
|
|
|
|
Instance = this;
|
|
|
|
else if (Instance != this)
|
|
|
|
{
|
|
|
|
DestroyImmediate(gameObject);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DontDestroyOnLoad(gameObject);
|
|
|
|
|
|
|
|
// Ensure all levels are disabled initially
|
|
|
|
foreach (var lvl in levels)
|
|
|
|
{
|
|
|
|
if (lvl != null) lvl.gameObject.SetActive(false);
|
|
|
|
}
|
2025-08-27 15:14:17 +05:00
|
|
|
var upcomingLevel = levels[0];
|
|
|
|
ObjectiveManager.Instance.SetList(upcomingLevel.config.objectives);
|
2025-08-27 17:41:31 +05:00
|
|
|
EnableOnlyTrigger(0);
|
2025-08-22 20:43:18 +05:00
|
|
|
}
|
|
|
|
|
2025-08-27 17:41:31 +05:00
|
|
|
private void EnableOnlyTrigger(int index)
|
|
|
|
{
|
|
|
|
if (levelStartTriggers == null) return;
|
|
|
|
for (int i = 0; i < levelStartTriggers.Count; i++)
|
|
|
|
if (levelStartTriggers[i] != null)
|
|
|
|
levelStartTriggers[i].SetActive(i == index);
|
|
|
|
}
|
|
|
|
|
2025-08-22 20:43:18 +05:00
|
|
|
public void StartLevel(int index)
|
|
|
|
{
|
|
|
|
if (index < 0 || index >= levels.Count)
|
|
|
|
{
|
|
|
|
Debug.LogError($"GameplayManager: Invalid level index {index}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// End previous level if any
|
|
|
|
if (currentLevel != null)
|
|
|
|
{
|
|
|
|
currentLevel.OnLevelEnd();
|
|
|
|
currentLevel.gameObject.SetActive(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
currentLevelIndex = index;
|
|
|
|
currentLevel = levels[currentLevelIndex];
|
|
|
|
|
|
|
|
currentLevel.gameObject.SetActive(true);
|
|
|
|
currentLevel.OnLevelStart();
|
|
|
|
Debug.Log($"GameplayManager: Started Level {currentLevelIndex + 1}");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void CompleteCurrentLevel()
|
|
|
|
{
|
|
|
|
if (currentLevel == null) return;
|
|
|
|
|
|
|
|
Debug.Log($"GameplayManager: Level {currentLevelIndex + 1} completed.");
|
|
|
|
|
|
|
|
currentLevel.OnLevelEnd();
|
|
|
|
currentLevel.gameObject.SetActive(false);
|
|
|
|
currentLevel = null;
|
2025-08-27 15:14:17 +05:00
|
|
|
|
2025-08-22 20:43:18 +05:00
|
|
|
// If you want to auto-progress:
|
|
|
|
int next = currentLevelIndex + 1;
|
2025-08-27 17:41:31 +05:00
|
|
|
// var upcomingLevel = levels[currentLevelIndex];
|
|
|
|
// ObjectiveManager.Instance.SetList(upcomingLevel.config.objectives);
|
2025-08-22 20:43:18 +05:00
|
|
|
if (next < levels.Count)
|
|
|
|
{
|
2025-08-27 17:41:31 +05:00
|
|
|
var upcomingLevel = levels[next];
|
|
|
|
ObjectiveManager.Instance.SetList(
|
|
|
|
upcomingLevel.config.objectives,
|
|
|
|
upcomingLevel.config.startObjectiveIndex
|
|
|
|
);
|
2025-08-22 20:43:18 +05:00
|
|
|
}
|
2025-08-27 17:41:31 +05:00
|
|
|
EnableOnlyTrigger(next);
|
|
|
|
// if (next < levels.Count)
|
|
|
|
// {
|
|
|
|
// StartLevel(next);
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// Debug.Log("GameplayManager: All levels completed!");
|
|
|
|
// }
|
2025-08-22 20:43:18 +05:00
|
|
|
}
|
|
|
|
|
2025-08-27 17:41:31 +05:00
|
|
|
|
|
|
|
|
2025-08-27 15:14:17 +05:00
|
|
|
private void LoadUpNextLevel()
|
|
|
|
{
|
|
|
|
int next = currentLevelIndex + 1;
|
|
|
|
var upcomingLevel = levels[currentLevelIndex];
|
|
|
|
ObjectiveManager.Instance.SetList(upcomingLevel.config.objectives);
|
|
|
|
}
|
2025-08-22 20:43:18 +05:00
|
|
|
// Optional: callable by a trigger to start Level 1 specifically
|
|
|
|
public void StartLevelOne()
|
|
|
|
{
|
|
|
|
StartLevel(0);
|
|
|
|
}
|
|
|
|
}
|