fix all kinds of timer problems

This commit is contained in:
ntr committed 2023-07-18 16:51:59 +10:00
1 parent bbcb434cd0
commit 558671a75e
5 files changed
+62 -22

No files matched your search

+12
View File
@@ -2187,6 +2187,10 @@ PrefabInstance:
propertyPath: m_Name
value: Timing
objectReference: {fileID: 0}
- target: {fileID: 3132636987330744835, guid: dcb64e3a91f1fec7ab6098f4f407830b, type: 3}
propertyPath: m_IsActive
value: 1
objectReference: {fileID: 0}
- target: {fileID: 4743817676494260426, guid: dcb64e3a91f1fec7ab6098f4f407830b, type: 3}
propertyPath: m_RootOrder
value: 4
@@ -2231,10 +2235,18 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5878833558283183865, guid: dcb64e3a91f1fec7ab6098f4f407830b, type: 3}
propertyPath: m_Enabled
value: 1
objectReference: {fileID: 0}
- target: {fileID: 5975136821837370545, guid: dcb64e3a91f1fec7ab6098f4f407830b, type: 3}
propertyPath: ship
value:
objectReference: {fileID: 1972710421}
- target: {fileID: 5975136821837370545, guid: dcb64e3a91f1fec7ab6098f4f407830b, type: 3}
propertyPath: m_Enabled
value: 1
objectReference: {fileID: 0}
m_RemovedComponents: []
m_RemovedGameObjects: []
m_AddedGameObjects: []
+21 -8
View File
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UIElements;
@@ -16,24 +17,28 @@ public class ShipUi : MonoBehaviour {
Label speed;
Label timer;
Label sectors;
VisualElement sectors;
VisualElement timeSheet;
void OnEnable() {
speed = uiDocument.rootVisualElement.Q<Label>("speed");
timer = uiDocument.rootVisualElement.Q<Label>("timer");
sectors = uiDocument.rootVisualElement.Q<Label>("sectors");
sectors = uiDocument.rootVisualElement.Q<VisualElement>("sectors");
timeSheet = uiDocument.rootVisualElement.Q<VisualElement>("timeSheet");
}
void FixedUpdate() {
timer.text = $"{timing.sectorTime:0.0}";
speed.text = $"{ship.Speed():0}";
timer.text = $"{timing.lapTime:0.0}";
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 timing.lapTimes) {
Label l = new Label();
@@ -41,10 +46,18 @@ public class ShipUi : MonoBehaviour {
timeSheet.Add(l);
}
foreach (SectorTime sector in timing.sectorTimes) {
Label l = new Label();
l.text = $"{sector}";
l.AddToClassList(sector.ColourClass());
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}");
l.AddToClassList(s.ColourClass());
return l;
})
.DefaultIfEmpty(new Label("-"))
.Single();
sectors.Add(l);
}
}
+8 -3
View File
@@ -3,13 +3,13 @@
--red: rgb(165, 42, 42);
--blue: rgb(48, 80, 248);
--purple: rgb(164, 35, 188);
--yellow: #ffa100;
--white: #f5f5f5;
--yellow: rgb(255, 161, 0);
--white: rgb(245, 245, 245);
}
#shipUi {
color: var(--white);
font-size: 21px;
font-size: 24px;
}
#list-entry {
@@ -31,6 +31,7 @@
.green {
color: var(--green);
font-weight: bold;
}
.purple {
@@ -40,3 +41,7 @@
.red {
color: var(--red);
}
.yellow {
color: var(--yellow);
}
+12 -4
View File
@@ -23,7 +23,7 @@ public readonly struct SectorTime {
public double Value { get; }
public SectorColour Colour { get; }
public override string ToString() => $"{Sector} {Value:0.000}";
public override string ToString() => $"{Value:0.000}";
public string ColourClass() => Colour switch {
SectorColour.Green => "green",
@@ -55,21 +55,25 @@ public class Timing: MonoBehaviour {
public bool timerEnabled;
public double sectorTime;
public double lapTime;
double sectorStart;
Sector[] sectors;
double lapStart;
public Sector[] sectors;
void Awake() {
lapTimes = new();
sectorTimes = new List<SectorTime>();
sectorStart = 0.0;
sectorTime = 0.0;
sectors = GetComponentsInChildren<Sector>();
sectors = GetComponentsInChildren<Sector>().OrderBy(s => s.sector).ToArray();
Debug.Log("Track Sector Count - " + sectors.Length);
}
void Update() {
if (timerEnabled)
if (timerEnabled) {
sectorTime = Time.timeAsDouble - sectorStart;
lapTime = Time.timeAsDouble - lapStart;
}
}
public void StartTimer() {
@@ -81,6 +85,7 @@ public class Timing: MonoBehaviour {
Debug.Log("Started Timer");
timerEnabled = true;
lapStart = Time.timeAsDouble;
sectorStart = Time.timeAsDouble;
}
@@ -88,6 +93,7 @@ public class Timing: MonoBehaviour {
sectorTimes = new List<SectorTime>();
timerEnabled = false;
sectorTime = 0;
lapTime = 0;
Debug.Log("Timer Stopped");
EndLap();
@@ -115,6 +121,8 @@ public class Timing: MonoBehaviour {
SectorTime s = new SectorTime(sector, sTime);
sectorTimes.Add(s);
sectorStart = Time.timeAsDouble;
if (sectorTimes.Count == sectors.Length) {
EndLap();
NextLap();
+9 -7
View File
@@ -1,18 +1,20 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="False">
<Style src="project://database/Assets/Src/ShipUi.uss?fileID=7433441132597879392&amp;guid=cf92f075876ddf7ffb4cd5c59633ad7b&amp;type=3#ShipUi" />
<ui:VisualElement name="shipUi" style="flex-grow: 1; background-color: rgba(0, 0, 0, 0); color: rgb(255, 255, 255); font-size: 24px; -unity-font: url(&apos;project://database/Assets/UI%20Toolkit/Fonts/Jura-Regular.otf?fileID=12800000&amp;guid=11752fd494516326b889605754cb9e26&amp;type=3#Jura-Regular&apos;); -unity-font-definition: initial;">
<ui:VisualElement name="shipUi" style="flex-grow: 1; background-color: rgba(0, 0, 0, 0); color: rgb(255, 255, 255); font-size: 28px; -unity-font: url(&apos;project://database/Assets/UI%20Toolkit/Fonts/Jura-Medium.otf?fileID=12800000&amp;guid=65648a14640996362a6b0929c723b6e4&amp;type=3#Jura-Medium&apos;); -unity-font-definition: initial;">
<ui:VisualElement name="timeSheet" style="flex-grow: 1; background-color: rgba(0, 0, 0, 0); align-items: flex-end;">
<ui:Label tabindex="-1" text="Lap 00 - 1:28.64" display-tooltip-when-elided="true" />
</ui:VisualElement>
<ui:VisualElement name="Middle" style="flex-grow: 1; background-color: rgba(0, 0, 0, 0);" />
<ui:VisualElement name="Bottom" style="flex-grow: 0; background-color: rgba(0, 0, 0, 0); flex-direction: column; align-items: center; justify-content: flex-end; color: rgb(255, 255, 255);">
<ui:Label tabindex="-1" text="Time" display-tooltip-when-elided="true" name="timer" />
<ui:VisualElement name="sectors" style="flex-grow: 0; background-color: rgba(0, 0, 0, 0); flex-direction: row;">
<ui:Label tabindex="-1" text="32.0" display-tooltip-when-elided="true" class="red" />
<ui:Label tabindex="-1" text="64.0" display-tooltip-when-elided="true" class="green" />
<ui:VisualElement name="Bottom" style="flex-grow: 0; background-color: rgba(0, 0, 0, 0.26); flex-direction: column; align-items: stretch; justify-content: flex-end; color: rgb(255, 255, 255); width: 33%; align-self: center;">
<ui:VisualElement name="upper" style="flex-grow: 1; background-color: rgba(0, 0, 0, 0); flex-direction: row; justify-content: space-between; align-items: auto; width: auto; align-self: stretch; flex-shrink: 0;">
<ui:Label tabindex="-1" text="Speed" display-tooltip-when-elided="true" name="speed" style="align-self: flex-start; align-items: auto;" />
<ui:Label tabindex="-1" text="Time" display-tooltip-when-elided="true" name="timer" style="align-self: flex-end; align-items: auto;" />
</ui:VisualElement>
<ui:VisualElement name="sectors" style="flex-grow: 0; background-color: rgba(0, 0, 0, 0); flex-direction: row; align-self: stretch; align-items: stretch; justify-content: space-between;">
<ui:Label tabindex="-1" text="32.0" display-tooltip-when-elided="true" class="yellow" />
<ui:Label tabindex="-1" text="64.0" display-tooltip-when-elided="true" class="green" style="-unity-font-style: normal; -unity-font: url(&apos;project://database/Assets/UI%20Toolkit/Fonts/Jura-Medium.otf?fileID=12800000&amp;guid=65648a14640996362a6b0929c723b6e4&amp;type=3#Jura-Medium&apos;);" />
<ui:Label tabindex="-1" text="1:28.8" display-tooltip-when-elided="true" class="purple" />
</ui:VisualElement>
<ui:Label tabindex="-1" text="Speed" display-tooltip-when-elided="true" name="speed" />
</ui:VisualElement>
</ui:VisualElement>
</ui:UXML>