138 lines
4.9 KiB
C#
138 lines
4.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Scores;
|
|
using UnityEngine;
|
|
using UnityEngine.Serialization;
|
|
|
|
namespace DungeonSelection
|
|
{
|
|
public class DungeonDisplayData
|
|
{
|
|
public DungeonDisplayData(DungeonData dungeonData, List<ScoreDisplay> highScores)
|
|
{
|
|
DungeonData = dungeonData;
|
|
HighScores = highScores;
|
|
}
|
|
public DungeonData DungeonData;
|
|
public List<ScoreDisplay> HighScores;
|
|
}
|
|
|
|
public class ScoreDisplay
|
|
{
|
|
public ScoreDisplay(string playerName, int score)
|
|
{
|
|
PlayerName = playerName;
|
|
Score = score;
|
|
}
|
|
|
|
public string PlayerName;
|
|
public int Score;
|
|
}
|
|
|
|
public class DungeonSelector : MonoBehaviour
|
|
{
|
|
[SerializeField] private DungeonData[] dungeons;
|
|
[SerializeField] private ScoreManager scoreManager;
|
|
private DungeonData _currentlySelectedDungeonData;
|
|
private int _currentlySelectedDungeonIndex = 0;
|
|
private Dictionary<int, List<ScoreDisplay>> scoresByLevelId = new Dictionary<int, List<ScoreDisplay>>();
|
|
|
|
private bool _scoresReceived = false;
|
|
|
|
private void OnEnable()
|
|
{
|
|
UIEvents.NextDungeonClicked += SelectNextDungeon;
|
|
UIEvents.PreviousDungeonClicked += SelectPreviousDungeon;
|
|
UIEvents.EnterDungeonClicked += LoadDungeon;
|
|
scoreManager.ScoresRecieved += HandleScoresRecieved;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
UIEvents.NextDungeonClicked -= SelectNextDungeon;
|
|
UIEvents.PreviousDungeonClicked -= SelectPreviousDungeon;
|
|
UIEvents.EnterDungeonClicked -= LoadDungeon;
|
|
scoreManager.ScoresRecieved -= HandleScoresRecieved;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
GameEvents.DungeonSelectionStarted?.Invoke();
|
|
|
|
_currentlySelectedDungeonData = dungeons[_currentlySelectedDungeonIndex];
|
|
StartCoroutine(WaitForScoresAndShowDungeon());
|
|
}
|
|
|
|
private IEnumerator WaitForScoresAndShowDungeon()
|
|
{
|
|
while (!_scoresReceived)
|
|
{
|
|
yield return null;
|
|
}
|
|
|
|
scoresByLevelId.TryGetValue(_currentlySelectedDungeonData.Id, out var scoreDisplays);
|
|
GameEvents.ShowingDungeon?.Invoke(new DungeonDisplayData(_currentlySelectedDungeonData, scoreDisplays ?? new List<ScoreDisplay>()));
|
|
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];
|
|
scoresByLevelId.TryGetValue(_currentlySelectedDungeonData.Id, out var scoreDisplays);
|
|
GameEvents.ShowingDungeon?.Invoke(new DungeonDisplayData(_currentlySelectedDungeonData, scoreDisplays ?? new List<ScoreDisplay>()));
|
|
if (_currentlySelectedDungeonIndex == dungeons.Length - 1)
|
|
{
|
|
GameEvents.ShowingLastDungeon?.Invoke();
|
|
}
|
|
}
|
|
|
|
private void SelectPreviousDungeon()
|
|
{
|
|
if (_currentlySelectedDungeonIndex - 1 < 0) return;
|
|
_currentlySelectedDungeonIndex--;
|
|
_currentlySelectedDungeonData = dungeons[_currentlySelectedDungeonIndex];
|
|
scoresByLevelId.TryGetValue(_currentlySelectedDungeonData.Id, out var scoreDisplays);
|
|
GameEvents.ShowingDungeon?.Invoke(new DungeonDisplayData(_currentlySelectedDungeonData, scoreDisplays ?? new List<ScoreDisplay>()));
|
|
if (_currentlySelectedDungeonIndex == 0)
|
|
{
|
|
GameEvents.ShowingFirstDugeon?.Invoke();
|
|
}
|
|
}
|
|
|
|
private void HandleScoresRecieved(ScoreEntry[] scores)
|
|
{
|
|
scoresByLevelId = new Dictionary<int, List<ScoreDisplay>>();
|
|
foreach (ScoreEntry score in scores)
|
|
{
|
|
if (scoresByLevelId.TryGetValue(score.level_id, out var scoresDisplay))
|
|
{
|
|
scoresDisplay.Add(new ScoreDisplay(score.player, score.score));
|
|
}
|
|
else
|
|
{
|
|
scoresByLevelId[score.level_id] = new List<ScoreDisplay>()
|
|
{ new ScoreDisplay(score.player, score.score) };
|
|
}
|
|
}
|
|
|
|
foreach (List<ScoreDisplay> scoresDisplay in scoresByLevelId.Values)
|
|
{
|
|
// Sort highest to lowest
|
|
scoresDisplay.Sort((a, b) => b.Score.CompareTo(a.Score));
|
|
}
|
|
|
|
_scoresReceived = true;
|
|
}
|
|
}
|
|
}
|