FantasyAsset/Assets/Scripts/GameplayManager.cs
2025-08-27 15:14:17 +05:00

95 lines
2.5 KiB
C#

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;
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);
}
var upcomingLevel = levels[0];
ObjectiveManager.Instance.SetList(upcomingLevel.config.objectives);
}
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;
// If you want to auto-progress:
int next = currentLevelIndex + 1;
var upcomingLevel = levels[currentLevelIndex];
ObjectiveManager.Instance.SetList(upcomingLevel.config.objectives);
if (next < levels.Count)
{
StartLevel(next);
}
else
{
Debug.Log("GameplayManager: All levels completed!");
}
}
private void LoadUpNextLevel()
{
int next = currentLevelIndex + 1;
var upcomingLevel = levels[currentLevelIndex];
ObjectiveManager.Instance.SetList(upcomingLevel.config.objectives);
}
// Optional: callable by a trigger to start Level 1 specifically
public void StartLevelOne()
{
StartLevel(0);
}
}