wip
This commit is contained in:
1 parent
23c47b8256
commit
db59cd1377
4 files changed
+235
-44
No files matched your search
+83
-30
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public enum SectorColour {
|
||||
Green,
|
||||
@@ -12,31 +13,18 @@ public enum SectorColour {
|
||||
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; }
|
||||
|
||||
[Serializable]
|
||||
public struct SectorTime {
|
||||
public int Sector;
|
||||
public double Seconds;
|
||||
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"),
|
||||
// };
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public struct Lap {
|
||||
public int Number { get; set; }
|
||||
public List<SectorTime> Sectors { get; set; }
|
||||
public string Map;
|
||||
public int Number;
|
||||
public List<SectorTime> Sectors;
|
||||
|
||||
// 0:1:39
|
||||
public override string ToString() {
|
||||
@@ -48,6 +36,45 @@ public struct Lap {
|
||||
}
|
||||
}
|
||||
|
||||
static class LapStorageService {
|
||||
static Dictionary<string, LapJSON> lapsByMap;
|
||||
static string lapStoragePath = Path.Combine(Application.persistentDataPath, "lapStorage.json");
|
||||
|
||||
static void GetPlayerLaps() {
|
||||
// read json
|
||||
// assign it to lapStorage
|
||||
}
|
||||
|
||||
// laps.count
|
||||
static int NumberOfCompletions() {}
|
||||
|
||||
// finds your PB in all completions
|
||||
static int Rank() {}
|
||||
|
||||
// open json
|
||||
public static void PersistLap(Lap lap) {
|
||||
bool found = lapsByMap.TryGetValue(lap.Map, out var lapJson);
|
||||
if (!found)
|
||||
lapJson = new LapJSON { Map = lap.Map, Laps = new List<Lap>() };
|
||||
LapJson.Laps.Add(lap);
|
||||
lapsByMap[lap.Map] = lapJson;
|
||||
|
||||
LapStorage lapStorage = new LapStorage { Laps = lapsByMap.Values.ToList() };
|
||||
File.WriteAllText(lapStoragePath, JsonUtility.ToJson(lapStorage));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class LapJSON {
|
||||
public string Map;
|
||||
public List<Lap> Laps;
|
||||
}
|
||||
|
||||
public class LapStorage {
|
||||
public List<LapJSON> Laps;
|
||||
}
|
||||
|
||||
public class Timing: MonoBehaviour {
|
||||
[SerializeField]
|
||||
public List<Lap> lapTimes;
|
||||
@@ -61,13 +88,23 @@ public class Timing: MonoBehaviour {
|
||||
double lapStart;
|
||||
public Sector[] sectors;
|
||||
|
||||
public LapStorage lapStorage;
|
||||
|
||||
string mapName;
|
||||
|
||||
void Awake() {
|
||||
lapTimes = new();
|
||||
sectorTimes = new List<SectorTime>();
|
||||
sectorStart = 0.0;
|
||||
sectorTime = 0.0;
|
||||
sectors = GetComponentsInChildren<Sector>().OrderBy(s => s.sector).ToArray();
|
||||
|
||||
// set map name
|
||||
Scene scene = SceneManager.GetActiveScene();
|
||||
mapName = scene.name;
|
||||
|
||||
Debug.Log("Track Sector Count - " + sectors.Length);
|
||||
Persist();
|
||||
}
|
||||
|
||||
void Update() {
|
||||
@@ -96,8 +133,6 @@ public class Timing: MonoBehaviour {
|
||||
sectorTime = 0;
|
||||
lapTime = 0;
|
||||
Debug.Log("Timer Stopped");
|
||||
|
||||
EndLap();
|
||||
}
|
||||
|
||||
public void NextSector(int sector) {
|
||||
@@ -119,11 +154,12 @@ public class Timing: MonoBehaviour {
|
||||
|
||||
double sTime = Time.timeAsDouble - sectorStart;
|
||||
|
||||
SectorTime s = new SectorTime(sector, sTime);
|
||||
SectorTime s = new SectorTime { Sector = sector, Seconds = sTime };
|
||||
sectorTimes.Add(s);
|
||||
|
||||
sectorStart = Time.timeAsDouble;
|
||||
|
||||
Debug.Log($"{sectorTimes.Count} / {sectors.Length}");
|
||||
|
||||
if (sectorTimes.Count == sectors.Length) {
|
||||
EndLap();
|
||||
NextLap();
|
||||
@@ -141,11 +177,12 @@ public class Timing: MonoBehaviour {
|
||||
if (sectorTimes == null)
|
||||
Awake();
|
||||
|
||||
if (sectorTimes.Count > 0) {
|
||||
Lap l = new Lap { Number = lapTimes.Count, Sectors = sectorTimes };
|
||||
Debug.Log($"EndLap {l}");
|
||||
lapTimes.Add(l);
|
||||
}
|
||||
Debug.Log($"Ending lap Sectors={sectorTimes.Count} Name={mapName} Lap={lapTimes.Count}");
|
||||
|
||||
Lap l = new Lap { Map = mapName, Number = lapTimes.Count, Sectors = sectorTimes };
|
||||
Debug.Log($"EndLap {l}");
|
||||
lapTimes.Add(l);
|
||||
Persist();
|
||||
}
|
||||
|
||||
public SectorColour GetSectorColour(SectorTime s) {
|
||||
@@ -160,5 +197,21 @@ public class Timing: MonoBehaviour {
|
||||
false => SectorColour.Yellow,
|
||||
};
|
||||
}
|
||||
|
||||
void Load() {
|
||||
try {
|
||||
string json = File.ReadAllText(lapStoragePath);
|
||||
JsonUtility.FromJsonOverwrite(json, lapStorage);
|
||||
} catch {
|
||||
// file doesn't exist yet
|
||||
return Persist();
|
||||
}
|
||||
}
|
||||
|
||||
void Persist() {
|
||||
LapJSON lapJson = new LapJSON { Map = mapName, Laps = lapTimes };
|
||||
string lapJsonStr = JsonUtility.ToJson(lapJson);
|
||||
Debug.Log($"Lap JSON {lapJsonStr}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user