PuzzleGame/PuzzleGameProject/Assets/Scripts/DungeonSelection/DungeonSelector.cs
2025-03-03 16:22:19 +01:00

69 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
namespace DungeonSelection
{
public class DungeonSelector : MonoBehaviour
{
[SerializeField] private DungeonData[] dungeons;
private DungeonData _currentlySelectedDungeonData;
private int _currentlySelectedDungeonIndex = 0;
private void OnEnable()
{
UIEvents.NextDungeonClicked += SelectNextDungeon;
UIEvents.PreviousDungeonClicked += SelectPreviousDungeon;
UIEvents.EnterDungeonClicked += LoadDungeon;
}
private void OnDisable()
{
UIEvents.NextDungeonClicked -= SelectNextDungeon;
UIEvents.PreviousDungeonClicked -= SelectPreviousDungeon;
UIEvents.EnterDungeonClicked -= LoadDungeon;
}
private void Start()
{
GameEvents.DungeonSelectionStarted?.Invoke();
_currentlySelectedDungeonData = dungeons[_currentlySelectedDungeonIndex];
GameEvents.ShowingDungeon?.Invoke(_currentlySelectedDungeonData);
if (_currentlySelectedDungeonIndex == 0)
{
GameEvents.ShowingFirstDugeon?.Invoke();
}
}
private void LoadDungeon(DungeonData dungeon)
{
GameEvents.LoadDungeon?.Invoke(dungeon);
}
private void SelectNextDungeon()
{
if (_currentlySelectedDungeonIndex + 1 >= dungeons.Length) return;
_currentlySelectedDungeonIndex++;
_currentlySelectedDungeonData = dungeons[_currentlySelectedDungeonIndex];
GameEvents.ShowingDungeon?.Invoke(_currentlySelectedDungeonData);
if (_currentlySelectedDungeonIndex == dungeons.Length - 1)
{
GameEvents.ShowingLastDungeon?.Invoke();
}
}
private void SelectPreviousDungeon()
{
if (_currentlySelectedDungeonIndex - 1 < 0) return;
_currentlySelectedDungeonIndex--;
_currentlySelectedDungeonData = dungeons[_currentlySelectedDungeonIndex];
GameEvents.ShowingDungeon?.Invoke(_currentlySelectedDungeonData);
if (_currentlySelectedDungeonIndex == 0)
{
GameEvents.ShowingFirstDugeon?.Invoke();
}
}
}
}