164 lines
4.4 KiB
C#
164 lines
4.4 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 timerMsg;
|
|
Label timerTime;
|
|
Label timerPb;
|
|
Label timerPosition;
|
|
|
|
VisualElement notice;
|
|
VisualElement noticeSectors;
|
|
VisualElement noticeDiff;
|
|
Label noticeMsg;
|
|
Label noticeTime;
|
|
Label noticePosition;
|
|
|
|
bool ShouldHide => timing?.sectors.Count() == 0;
|
|
public bool TimerEnabled => timing.timerEnabled;
|
|
|
|
static string[] completionMsgs = {
|
|
"(⌐▨_▨)",
|
|
"(⌐■_■)",
|
|
"(⌐▼_▼)"
|
|
// "(^_^)",
|
|
};
|
|
|
|
void OnEnable() {
|
|
Init();
|
|
}
|
|
|
|
void Start() {
|
|
Init();
|
|
}
|
|
|
|
public void Init() {
|
|
if (ShouldHide) {
|
|
Debug.Log("No sectors found, hiding timing.");
|
|
uiDocument.enabled = false;
|
|
return;
|
|
}
|
|
|
|
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");
|
|
|
|
timerMsg = uiDocument.rootVisualElement.Q<Label>("msg");
|
|
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");
|
|
|
|
TimingNotice lapNotice = timing.CompareLap(lap);
|
|
|
|
noticeTime.text = lap.ToString();
|
|
noticeDiff.Clear();
|
|
noticeDiff.Add(lapNotice.ToLabel());
|
|
|
|
if (lapNotice.Category == NoticeCategory.Faster) ShowMessage("New PB!");
|
|
if (lapNotice.Category == NoticeCategory.Fastest) {
|
|
int msgIndex = UnityEngine.Random.Range(0, completionMsgs.Length);
|
|
ShowMessage(completionMsgs[msgIndex]);
|
|
}
|
|
|
|
noticeSectors.Clear();
|
|
foreach (SectorTime sector in lap.Sectors) {
|
|
Label l = timing.CompareSector(sector).ToLabel();
|
|
noticeSectors.Add(l);
|
|
}
|
|
}
|
|
|
|
public void HideRecentLap() {
|
|
Init();
|
|
HideMessage();
|
|
timer.RemoveFromClassList("hidden");
|
|
notice.AddToClassList("hidden");
|
|
}
|
|
|
|
public void ShowMessage(string msg) {
|
|
timerMsg.text = msg;
|
|
}
|
|
|
|
public void HideMessage() {
|
|
timerMsg.text = "";
|
|
}
|
|
}
|