Implemented getting and setting high scores from server

This commit is contained in:
Max 2025-03-07 09:26:54 +01:00
parent c103ffb339
commit d3e2d425bf
17 changed files with 346 additions and 17 deletions

View file

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using DungeonSelection;
using Unity.VisualScripting;
using UnityEngine;
@ -7,21 +8,25 @@ using UnityEngine.UIElements;
public class DungeonSelectMenuController : MonoBehaviour
{
private const int NUM_HIGH_SCORES = 10;
private Button _next;
private Button _previous;
private Button _start;
private Image _thumbnailImage;
private Label _dungeonName;
private VisualElement _dungeonThumbnail;
private ListView _highScores;
private DungeonData _currentlyShowingDungeon;
private DungeonDisplayData _currentlyShowingDungeon;
private List<string> _highScoreData = new List<string>();
private void OnEnable()
{
VisualElement root = GetComponent<UIDocument>().rootVisualElement;
Button rules = root.Q<Button>("Rules");
rules.clicked += UIEvents.ShowRules;
rules.clicked += RulesClicked;
_dungeonThumbnail = root.Q<VisualElement>("DungeonThumbnail");
_next = root.Q<Button>("Next");
_next.clicked += NextClicked;
@ -30,16 +35,26 @@ public class DungeonSelectMenuController : MonoBehaviour
_start = root.Q<Button>("Start");
_start.clicked += StartClicked;
_dungeonName = root.Q<Label>("DungeonName");
_highScores = root.Q<ListView>("HighScoresListView");
_highScores.makeItem = () => new Label();
_highScores.bindItem = (element, index) => { (element as Label).text = _highScoreData[index]; };
_highScores.itemsSource = _highScoreData;
_highScores.makeNoneElement = () => new Label();
GameEvents.ShowingDungeon += ShowDungeon;
GameEvents.ShowingFirstDugeon += () => SetPreviousButtonEnabled(false);
GameEvents.ShowingLastDungeon += () => SetNextButtonEnabled(false);
}
private void ShowDungeon(DungeonData dungeon)
private void Start()
{
UIEvents.GetScores?.Invoke();
}
private void ShowDungeon(DungeonDisplayData dungeon)
{
_currentlyShowingDungeon = dungeon;
_dungeonName.text = _currentlyShowingDungeon.DungeonName;
_dungeonName.text = _currentlyShowingDungeon.DungeonData.Name;
if (_thumbnailImage == null)
{
@ -48,7 +63,18 @@ public class DungeonSelectMenuController : MonoBehaviour
_dungeonThumbnail.Add(_thumbnailImage);
}
_thumbnailImage.image = _currentlyShowingDungeon.Thumbnail.texture;
_thumbnailImage.image = _currentlyShowingDungeon.DungeonData.Thumbnail.texture;
_highScoreData.Clear();
int rank = 1;
foreach (ScoreDisplay score in dungeon.HighScores)
{
if (rank > NUM_HIGH_SCORES) break;
string formattedScore = $"{rank}. {score.PlayerName} {score.Score}";
_highScoreData.Add(formattedScore);
rank++;
}
_highScores.RefreshItems();
}
private void SetPreviousButtonEnabled(bool enabled)
@ -77,6 +103,11 @@ public class DungeonSelectMenuController : MonoBehaviour
private void StartClicked()
{
UIEvents.EnterDungeonClicked?.Invoke(_currentlyShowingDungeon);
UIEvents.EnterDungeonClicked?.Invoke(_currentlyShowingDungeon.DungeonData);
}
private void RulesClicked()
{
UIEvents.ShowRules?.Invoke();
}
}