136 lines
3.7 KiB
C#
136 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
public class TimingUI : MonoBehaviour {
|
|
[SerializeField]
|
|
ShipControls ship;
|
|
|
|
Timing timing;
|
|
UIDocument uiDocument;
|
|
|
|
VisualElement timer;
|
|
VisualElement timerSectors;
|
|
Label timerTime;
|
|
Label timerPb;
|
|
Label timerPosition;
|
|
|
|
VisualElement notice;
|
|
VisualElement noticeSectors;
|
|
VisualElement noticeDiff;
|
|
Label noticeMsg;
|
|
Label noticeTime;
|
|
Label noticePosition;
|
|
|
|
static string[] completionMsgs = {
|
|
"smooth",
|
|
"very fast",
|
|
"well done",
|
|
"ナイス",
|
|
"(⌐▨_▨)",
|
|
};
|
|
|
|
void OnEnable() {
|
|
Init();
|
|
}
|
|
|
|
void Init() {
|
|
uiDocument = GetComponent<UIDocument>();
|
|
timing = GetComponent<Timing>();
|
|
|
|
notice = uiDocument.rootVisualElement.Q<VisualElement>("notice");
|
|
noticeSectors = uiDocument.rootVisualElement.Q<VisualElement>("noticeSectors");
|
|
|
|
timer = uiDocument.rootVisualElement.Q<VisualElement>("timer");
|
|
timerSectors = uiDocument.rootVisualElement.Q<VisualElement>("timerSectors");
|
|
|
|
noticeMsg = uiDocument.rootVisualElement.Q<Label>("noticeMsg");
|
|
noticeTime = uiDocument.rootVisualElement.Q<Label>("noticeTime");
|
|
noticeDiff = uiDocument.rootVisualElement.Q<VisualElement>("noticeDiff");
|
|
|
|
timerTime = uiDocument.rootVisualElement.Q<Label>("timerTime");
|
|
timerPb = uiDocument.rootVisualElement.Q<Label>("timerPb");
|
|
|
|
noticePosition = uiDocument.rootVisualElement.Q<Label>("noticePosition");
|
|
timerPosition = uiDocument.rootVisualElement.Q<Label>("timerPosition");
|
|
|
|
timerPb.text = "-";
|
|
noticePosition.text = "- / -";
|
|
timerPosition.text = "- / -";
|
|
|
|
notice.AddToClassList("hidden");
|
|
}
|
|
|
|
void FixedUpdate() {
|
|
if (timer == null || timing == null) {
|
|
Debug.LogWarning("no timer");
|
|
Init();
|
|
return;
|
|
}
|
|
|
|
timerTime.text = $"{TimeSpan.FromSeconds(timing.lapTime):m\\:ss\\.f}";
|
|
|
|
// clearly this is not ideal but idk atm
|
|
UpdateUI();
|
|
}
|
|
|
|
public void UpdateUI() {
|
|
if (timerSectors == null) {
|
|
Debug.LogWarning("null timerSectors");
|
|
return;
|
|
}
|
|
|
|
if (timing == null) {
|
|
Debug.LogWarning("null timing");
|
|
return;
|
|
}
|
|
|
|
Lap timerPbLap = timing.Pb;
|
|
if (timerPbLap != null)
|
|
timerPb.text = $"{timerPbLap.ToString()}";
|
|
|
|
timerSectors.Clear();
|
|
|
|
foreach (Sector sector in timing.sectors) {
|
|
Label l = timing.sectorTimes
|
|
.Where(s => s.Sector == sector.sector)
|
|
.Select(s => timing.CompareSector(s).ToLabel())
|
|
.DefaultIfEmpty(new Label("-"))
|
|
.Single();
|
|
|
|
timerSectors.Add(l);
|
|
}
|
|
|
|
noticePosition.text = timing.Position;
|
|
timerPosition.text = timing.Position;
|
|
}
|
|
|
|
public void ShowRecentLap(Lap lap) {
|
|
Init();
|
|
timer.AddToClassList("hidden");
|
|
notice.RemoveFromClassList("hidden");
|
|
|
|
noticeTime.text = lap.ToString();
|
|
noticeDiff.Clear();
|
|
noticeDiff.Add(timing.CompareLap(lap).ToLabel());
|
|
|
|
int msgIndex = UnityEngine.Random.Range(0, completionMsgs.Length);
|
|
noticeMsg.text = completionMsgs[msgIndex];
|
|
|
|
noticeSectors.Clear();
|
|
foreach (SectorTime sector in lap.Sectors) {
|
|
Label l = timing.CompareSector(sector).ToLabel();
|
|
noticeSectors.Add(l);
|
|
}
|
|
}
|
|
|
|
public void HideRecentLap() {
|
|
Init();
|
|
timer.RemoveFromClassList("hidden");
|
|
notice.AddToClassList("hidden");
|
|
}
|
|
}
|