added results
This commit is contained in:
1 parent
586c476851
commit
bae0e2218e
11 files changed
+617
-5
No files matched your search
@@ -1,5 +1,6 @@
|
||||
using Code.DI;
|
||||
using Code.Input;
|
||||
using Code.Records;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
@@ -112,13 +113,24 @@ namespace Code.Gameplay
|
||||
_toMenuButton.onClick.AddListener(LoadMainMenu);
|
||||
}
|
||||
|
||||
private void SaveResult()
|
||||
{
|
||||
var totalMs = (long)(_elapsedTime * 1000f);
|
||||
var player = _nameField?.text?.Trim() ?? "Player";
|
||||
if (string.IsNullOrWhiteSpace(player)) player = "Player";
|
||||
|
||||
RecordsService.AddRecord(player, totalMs);
|
||||
}
|
||||
|
||||
private void RestartRace()
|
||||
{
|
||||
SaveResult();
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
|
||||
}
|
||||
|
||||
private void LoadMainMenu()
|
||||
{
|
||||
SaveResult();
|
||||
SceneManager.LoadScene("MainMenu");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 149512b826944d56852239a378b204b7
|
||||
timeCreated: 1769530513
|
||||
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
|
||||
namespace Code.Records
|
||||
{
|
||||
[Serializable]
|
||||
public class Record
|
||||
{
|
||||
public string name;
|
||||
public long totalMilliseconds;
|
||||
|
||||
public Record(string name, long ms)
|
||||
{
|
||||
this.name = name;
|
||||
totalMilliseconds = ms;
|
||||
}
|
||||
|
||||
public string formattedTime => FormatTime(totalMilliseconds);
|
||||
|
||||
private string FormatTime(long ms)
|
||||
{
|
||||
var minutes = ms / 60000;
|
||||
var seconds = ms % 60000 / 1000;
|
||||
var milliseconds = ms % 1000;
|
||||
return $"{minutes:D2}:{seconds:D2}:{milliseconds:D2}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8f08515aa1b1499d99937107933f7a9b
|
||||
timeCreated: 1769530713
|
||||
@@ -0,0 +1,41 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Records
|
||||
{
|
||||
public class RecordsService
|
||||
{
|
||||
private const string Key = "RaceRecords";
|
||||
|
||||
public static List<Record> Load()
|
||||
{
|
||||
var json = PlayerPrefs.GetString(Key, "[]");
|
||||
try
|
||||
{
|
||||
return JsonConvert.DeserializeObject<List<Record>>(json) ?? new List<Record>();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new List<Record>();
|
||||
}
|
||||
}
|
||||
|
||||
public static void Save(List<Record> records)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(records);
|
||||
PlayerPrefs.SetString(Key, json);
|
||||
PlayerPrefs.Save();
|
||||
}
|
||||
|
||||
public static void AddRecord(string name, long totalMs)
|
||||
{
|
||||
var records = Load();
|
||||
records.Add(new Record(name, totalMs));
|
||||
records = records.OrderBy(r => r.totalMilliseconds).ToList();
|
||||
records = records.Take(5).ToList();
|
||||
Save(records);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c8fc6666dae743c5abb11a5fba8b495b
|
||||
timeCreated: 1769530671
|
||||
@@ -14,7 +14,7 @@ namespace Code.UI
|
||||
private void Start()
|
||||
{
|
||||
var seed = Random.Range(0, 10000);
|
||||
_currentTrack = TrackGenerator.Generate(seed, 2000, segmentCount: 30);
|
||||
_currentTrack = TrackGenerator.Generate(seed, 500, segmentCount: 30);
|
||||
|
||||
trackBuilder.Build(_currentTrack);
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Code.DI;
|
||||
using Code.Input;
|
||||
using Code.Records;
|
||||
using DG.Tweening;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
@@ -19,6 +20,10 @@ namespace Code.UI
|
||||
[SerializeField] private Image fader;
|
||||
[SerializeField] private Image backgroundImage;
|
||||
|
||||
[SerializeField] private Transform resultsWrapper;
|
||||
[SerializeField] private TMP_Text resultsText;
|
||||
[SerializeField] private Button closeResultsButton;
|
||||
|
||||
[SerializeField] private float parallaxStrength = 15f;
|
||||
[SerializeField] private float parallaxSmoothTime = 0.1f;
|
||||
|
||||
@@ -26,14 +31,16 @@ namespace Code.UI
|
||||
private Vector3 _currentParallaxOffset;
|
||||
private IInputService _inputService;
|
||||
private AudioSource _audioSource;
|
||||
|
||||
|
||||
private void Start()
|
||||
{
|
||||
var container = FindFirstObjectByType<DIContainer>();
|
||||
_inputService = container.Resolve<IInputService>();
|
||||
_inputService.EnableMap(ActionMapping.MainMenu);
|
||||
_audioSource = FindFirstObjectByType<AudioSource>();
|
||||
|
||||
|
||||
resultsWrapper.gameObject.SetActive(false);
|
||||
|
||||
SetupButtons();
|
||||
AnimateLogo();
|
||||
AnimateBackground();
|
||||
@@ -100,7 +107,7 @@ namespace Code.UI
|
||||
|
||||
private void HandleParallax()
|
||||
{
|
||||
if (parallaxRoot == null) return;
|
||||
if (!parallaxRoot) return;
|
||||
|
||||
var mousePos = _inputService.MainMenuParalax;
|
||||
var center = new Vector2(Screen.width / 2f, Screen.height / 2f);
|
||||
@@ -124,7 +131,20 @@ namespace Code.UI
|
||||
}
|
||||
|
||||
private void OnStartGameClicked() => FadeOutAndLoad("Gameplay");
|
||||
private void OnShowResultsClicked() => Debug.Log("Show results");
|
||||
|
||||
private void OnShowResultsClicked()
|
||||
{
|
||||
resultsWrapper.gameObject.SetActive(true);
|
||||
closeResultsButton.onClick.AddListener(HideResults);
|
||||
UpdateResultsDisplay();
|
||||
}
|
||||
|
||||
private void HideResults()
|
||||
{
|
||||
closeResultsButton.onClick.RemoveAllListeners();
|
||||
resultsWrapper.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
private void OnExitButtonClicked() => FadeOutAndQuit();
|
||||
|
||||
private void FadeOutAndLoad(string sceneName)
|
||||
@@ -159,5 +179,24 @@ namespace Code.UI
|
||||
showResultsButton?.onClick.RemoveListener(OnShowResultsClicked);
|
||||
exitButton?.onClick.RemoveListener(OnExitButtonClicked);
|
||||
}
|
||||
|
||||
private void UpdateResultsDisplay()
|
||||
{
|
||||
var records = RecordsService.Load();
|
||||
if (records.Count == 0)
|
||||
{
|
||||
resultsText.text = "RESULTS\n\nNo records yet.";
|
||||
return;
|
||||
}
|
||||
|
||||
var lines = new System.Text.StringBuilder();
|
||||
lines.AppendLine("RESULTS");
|
||||
for (var i = 0; i < records.Count; i++)
|
||||
{
|
||||
var r = records[i];
|
||||
lines.AppendLine($"{i + 1}. {r.name} - {r.formattedTime}");
|
||||
}
|
||||
resultsText.text = lines.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user