Files
spacerace/Assets/Src/UI/ShipUi.cs
T
2023-07-26 21:51:12 +10:00

71 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UIElements;
public class ShipUi : MonoBehaviour {
[SerializeField]
UIDocument uiDocument = null;
[SerializeField]
ShipControls ship;
[SerializeField]
Timing timing;
Label speed;
Label timer;
VisualElement sectors;
VisualElement timeSheet;
void OnEnable() {
speed = uiDocument.rootVisualElement.Q<Label>("speed");
timer = uiDocument.rootVisualElement.Q<Label>("timer");
sectors = uiDocument.rootVisualElement.Q<VisualElement>("sectors");
timeSheet = uiDocument.rootVisualElement.Q<VisualElement>("timeSheet");
}
void FixedUpdate() {
timer.text = $"{TimeSpan.FromSeconds(timing.lapTime):m\\:ss\\.fff}";
speed.text = $"{ship.Speed():0} u/s";
// clearly this is not ideal but idk atm
UpdateTimesheet();
}
public void UpdateTimesheet() {
if (sectors == null)
Debug.LogWarning("null sectors");
// timing not ready yet or smth?
if (timing.lapTimes == null)
return;
timeSheet.Clear();
foreach (Lap lap in timing.lapTimes) {
Label l = new Label();
l.text = $"{lap}";
timeSheet.Add(l);
}
sectors.Clear();
foreach (Sector sector in timing.sectors) {
Label l = timing.sectorTimes
.Where(s => s.Sector == sector.sector)
.Select(s => {
Label l = new Label($"{s}");
string colourClass = timing.GetSectorColour(s).ToString().ToLowerInvariant();
l.AddToClassList(colourClass);
return l;
})
.DefaultIfEmpty(new Label("-"))
.Single();
sectors.Add(l);
}
}
}