Files
spacerace/Assets/Src/UI/ShipUi.cs
T
2023-07-30 22:42:10 +10:00

77 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UIElements;
public class ShipUi : MonoBehaviour {
UIDocument uiDocument;
[SerializeField]
ShipControls ship;
[SerializeField]
Timing timing;
Label speed;
Label timer;
VisualElement sectors;
VisualElement timeSheet;
void OnEnable() {
Init();
}
void Init() {
uiDocument = GetComponent<UIDocument>();
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() {
if (timer == null || speed == null) {
Debug.LogWarning("no speed or timer");
Init();
return;
}
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");
timeSheet.Clear();
foreach (Lap lap in LapStorageService.GetPlayerLaps(timing.mapName).Laps) {
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);
}
}
}