TIMING
This commit is contained in:
1 parent
d49bafd535
commit
62a8de8b86
99 files changed
+5837
-673
No files matched your search
@@ -0,0 +1,130 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
public readonly struct SectorTime {
|
||||
public SectorTime(int i, double t) {
|
||||
Sector = i;
|
||||
Value = t;
|
||||
}
|
||||
|
||||
public int Sector { get; }
|
||||
public double Value { get; }
|
||||
|
||||
public override string ToString() => $"{Sector} {Value:0.000}";
|
||||
}
|
||||
|
||||
public readonly struct Lap {
|
||||
public Lap(int i, List<SectorTime> s) {
|
||||
Sectors = s;
|
||||
Number = i;
|
||||
}
|
||||
|
||||
public int Number { get; }
|
||||
public List<SectorTime> Sectors { get; }
|
||||
|
||||
public override string ToString() {
|
||||
return $"Lap {Number} : {TotalTime():0.000} {String.Join(" ", Sectors.ConvertAll(s => s.ToString()))}";
|
||||
}
|
||||
|
||||
double TotalTime() {
|
||||
return Sectors.Sum(s => s.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public class Timing: MonoBehaviour {
|
||||
[SerializeField]
|
||||
public List<Lap> lapTimes;
|
||||
[SerializeField]
|
||||
public List<SectorTime> sectorTimes;
|
||||
public bool timerEnabled;
|
||||
|
||||
public double sectorTime;
|
||||
double sectorStart;
|
||||
Sector[] sectors;
|
||||
|
||||
void Awake() {
|
||||
lapTimes = new List<Lap>();
|
||||
sectorTimes = new List<SectorTime>();
|
||||
sectorStart = 0.0;
|
||||
sectorTime = 0.0;
|
||||
sectors = GetComponentsInChildren<Sector>();
|
||||
Debug.Log("Track Sector Count - " + sectors.Length);
|
||||
}
|
||||
|
||||
void Update() {
|
||||
if (timerEnabled)
|
||||
sectorTime = Time.timeAsDouble - sectorStart;
|
||||
}
|
||||
|
||||
public void StartTimer() {
|
||||
if (sectors.Length == 0) {
|
||||
Debug.LogWarning("No Sectors found in timing");
|
||||
return;
|
||||
}
|
||||
|
||||
Debug.Log("Started Timer");
|
||||
|
||||
timerEnabled = true;
|
||||
sectorStart = Time.timeAsDouble;
|
||||
}
|
||||
|
||||
public void StopTimer() {
|
||||
sectorTimes = new List<SectorTime>();
|
||||
timerEnabled = false;
|
||||
sectorTime = 0;
|
||||
Debug.Log("Timer Stopped");
|
||||
|
||||
EndLap();
|
||||
}
|
||||
|
||||
public void NextSector(int sector) {
|
||||
if (!timerEnabled)
|
||||
return;
|
||||
|
||||
// they turned around or somethin?
|
||||
if (sectorTimes.Any(s => s.Sector == sector))
|
||||
return;
|
||||
|
||||
// this basically makes sure the timer doesn't do anything weird
|
||||
// due to respawns, driving in the wrong direction etc
|
||||
// you have to hit the sectors in numbered order
|
||||
// ---------------
|
||||
// make sure sectors are only accessible by being hit by following
|
||||
// the track correctly!
|
||||
if (sector != sectorTimes.Count + 1)
|
||||
return;
|
||||
|
||||
double sTime = Time.timeAsDouble - sectorStart;
|
||||
|
||||
SectorTime s = new SectorTime(sector, sTime);
|
||||
sectorTimes.Add(s);
|
||||
|
||||
if (sectorTimes.Count == sectors.Length) {
|
||||
EndLap();
|
||||
NextLap();
|
||||
}
|
||||
}
|
||||
|
||||
void NextLap() {
|
||||
Debug.Log("NextLap" + lapTimes.Count);
|
||||
sectorTimes = new List<SectorTime>();
|
||||
StartTimer();
|
||||
}
|
||||
|
||||
public void EndLap() {
|
||||
// when live reloading this happens
|
||||
if (sectorTimes == null)
|
||||
Awake();
|
||||
|
||||
if (sectorTimes.Count > 0) {
|
||||
Lap l = new Lap(lapTimes.Count, sectorTimes);
|
||||
Debug.Log($"EndLap {l}");
|
||||
lapTimes.Add(l);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user