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

64 lines
1.8 KiB
C#

using System;
using UnityEngine;
[RequireComponent(typeof(Collider))]
public class LevelStartTrigger : MonoBehaviour
{
[SerializeField] private string playerTag = "Player";
[SerializeField] private int levelIndexToStart = 0; // Level 1 = 0
[Tooltip("Disable the trigger after first use")]
[SerializeField] private bool oneShot = true;
[SerializeField] private Level targetLevel;
private bool _used;
private void Reset()
{
var col = GetComponent<Collider>();
if (col) col.isTrigger = true;
}
private void OnEnable()
{
if (targetLevel != null && targetLevel.config != null && ObjectiveManager.Instance != null)
{
ObjectiveManager.Instance.SetList(
targetLevel.config.objectives,
targetLevel.config.startObjectiveIndex
);
}
}
private void OnTriggerEnter(Collider other)
{
if (_used) return;
if (!other.CompareTag(playerTag)) return;
_used = true;
// Prefer using GameplayManager
if (GameplayManager.Instance != null)
{
GameplayManager.Instance.StartLevel(levelIndexToStart);
}
else
{
// Fallback: find any Level component at index and start manually
// (only used if your manager doesn't have StartLevel yet)
var level = FindObjectOfType<Level1>();
if (level != null)
{
level.gameObject.SetActive(true);
level.OnLevelStart();
}
else
{
Debug.LogWarning("LevelStartTrigger: No GameplayManager and no Level1 found.");
}
}
if (oneShot)
{
gameObject.SetActive(false);
}
}
}