68 lines
1.6 KiB
C#
68 lines
1.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System.Linq;
|
|
|
|
public abstract class Tachometer : MonoBehaviour {
|
|
|
|
private string inputID;
|
|
|
|
// Config
|
|
private int ticPerTurn;
|
|
private float wheelDiameter;
|
|
|
|
// Process
|
|
protected float distancePerTick;
|
|
protected float normalSpeed;
|
|
|
|
protected List<float> impulses = new List<float>();
|
|
|
|
public void SetPlayer(int playerID) {
|
|
inputID = "WheelP" + (playerID + 1);
|
|
LoadConfig(playerID);
|
|
}
|
|
|
|
private void LoadConfig(int playerID) {
|
|
var gm = GameManager.Instance;
|
|
ticPerTurn = gm.GetSensorsCount(playerID);
|
|
wheelDiameter = gm.GetBikeWheelDiameter(playerID);
|
|
|
|
Debug.Log("Config Loaded player " + playerID);
|
|
Debug.Log("SensorsCount: " + ticPerTurn);
|
|
Debug.Log("Diameter: " + wheelDiameter);
|
|
|
|
distancePerTick = wheelDiameter * Mathf.PI / ticPerTurn;
|
|
normalSpeed = gm.GetRaceDistance() / gm.GetRaceNormalTime();
|
|
}
|
|
|
|
private void RegisterTic() {
|
|
impulses.Add(Time.time);
|
|
}
|
|
|
|
public virtual float GetSpeed() {
|
|
return -1f;
|
|
}
|
|
|
|
public float GetPlayerSpeed() {
|
|
if (GetDistanceRun() >= GameManager.Instance.GetRaceDistance())
|
|
return 1f;
|
|
|
|
return GetSpeed () / normalSpeed;
|
|
}
|
|
|
|
public float GetDistanceRun() {
|
|
return distancePerTick * impulses.Count();
|
|
}
|
|
|
|
void Start() {
|
|
}
|
|
|
|
void Update() {
|
|
if (GameManager.Instance.Status != States.Race)
|
|
return;
|
|
|
|
if (Input.GetButtonDown(inputID))
|
|
RegisterTic();
|
|
}
|
|
}
|