165 lines
4.1 KiB
C#
165 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
using UnityEngine;
|
|
using UnityEditor;
|
|
|
|
public enum SectorColour {
|
|
Green,
|
|
Purple,
|
|
White,
|
|
Yellow,
|
|
}
|
|
|
|
public readonly struct SectorTime {
|
|
public SectorTime(int i, double t) {
|
|
Sector = i;
|
|
Seconds = t;
|
|
Colour = SectorColour.White;
|
|
}
|
|
|
|
public int Sector { get; }
|
|
public double Seconds { get; }
|
|
public SectorColour Colour { get; }
|
|
|
|
public override string ToString() => $"{Seconds:0.000}";
|
|
|
|
// public string ColourClass() => Colour switch {
|
|
// SectorColour.Green => "green",
|
|
// SectorColour.Purple => "purple",
|
|
// SectorColour.White => "white",
|
|
// SectorColour.Yellow => "yellow",
|
|
// _ => throw new Exception("unknown colour"),
|
|
// };
|
|
}
|
|
|
|
public struct Lap {
|
|
public int Number { get; set; }
|
|
public List<SectorTime> Sectors { get; set; }
|
|
|
|
// 0:1:39
|
|
public override string ToString() {
|
|
return $"{TimeSpan.FromSeconds(TotalTime()):m\\:ss\\.fff}";
|
|
}
|
|
|
|
public double TotalTime() {
|
|
return Sectors.Sum(s => s.Seconds);
|
|
}
|
|
}
|
|
|
|
public class Timing: MonoBehaviour {
|
|
[SerializeField]
|
|
public List<Lap> lapTimes;
|
|
[SerializeField]
|
|
public List<SectorTime> sectorTimes;
|
|
public bool timerEnabled;
|
|
|
|
public double sectorTime;
|
|
public double lapTime;
|
|
double sectorStart;
|
|
double lapStart;
|
|
public Sector[] sectors;
|
|
|
|
void Awake() {
|
|
lapTimes = new();
|
|
sectorTimes = new List<SectorTime>();
|
|
sectorStart = 0.0;
|
|
sectorTime = 0.0;
|
|
sectors = GetComponentsInChildren<Sector>().OrderBy(s => s.sector).ToArray();
|
|
Debug.Log("Track Sector Count - " + sectors.Length);
|
|
}
|
|
|
|
void Update() {
|
|
if (timerEnabled) {
|
|
sectorTime = Time.timeAsDouble - sectorStart;
|
|
lapTime = Time.timeAsDouble - lapStart;
|
|
}
|
|
}
|
|
|
|
public void StartTimer() {
|
|
if (sectors.Length == 0) {
|
|
Debug.LogWarning("No Sectors found in timing");
|
|
return;
|
|
}
|
|
|
|
Debug.Log("Started Timer");
|
|
|
|
timerEnabled = true;
|
|
lapStart = Time.timeAsDouble;
|
|
sectorStart = Time.timeAsDouble;
|
|
}
|
|
|
|
public void StopTimer() {
|
|
sectorTimes = new List<SectorTime>();
|
|
timerEnabled = false;
|
|
sectorTime = 0;
|
|
lapTime = 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);
|
|
|
|
sectorStart = Time.timeAsDouble;
|
|
|
|
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 { Number = lapTimes.Count, Sectors = sectorTimes };
|
|
Debug.Log($"EndLap {l}");
|
|
lapTimes.Add(l);
|
|
}
|
|
}
|
|
|
|
public SectorColour GetSectorColour(SectorTime s) {
|
|
if (lapTimes.Count == 0)
|
|
return SectorColour.Green;
|
|
|
|
var fastestLap = lapTimes.MinBy(l => l.TotalTime());
|
|
var fastestSector = fastestLap.Sectors.Single(fls => fls.Sector == s.Sector);
|
|
|
|
return (s.Seconds < fastestSector.Seconds) switch {
|
|
true => SectorColour.Green,
|
|
false => SectorColour.Yellow,
|
|
};
|
|
}
|
|
}
|
|
|