gold_sprint_lpr/Assets/Scripts/GameScene/BikeManager.cs
2018-03-15 07:05:47 +01:00

127 lines
3.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BikeManager : MonoBehaviour {
[SerializeField]
public int playerID; // {get; set; }
// metric system ftw
private float normalSpeed;
// external conf
private float distanceToRun;
public float Speed {get; set; }
private Queue<float> impulses = new Queue<float>();
private float lastTimeImp;
private float lastTimeDlt;
public float maxTimeDlt = 1.0f;
private float filteredSpeed;
private Tachometer taco;
private VideoManager vm;
private GameManager gm;
void Awake() {
vm = gameObject.GetComponent<VideoManager>();
vm.SetSkin(playerID);
gm = GameManager.Instance;
gm.RegisterBike(this, playerID);
}
// Use this for initialization
void Start () {
distanceToRun = gm.GetRaceDistance();
//taco = gameObject.AddComponent<RAWTachometer>();
taco = gameObject.AddComponent<PreciseTachometer>();
taco.SetPlayer(playerID);
normalSpeed = distanceToRun / gm.GetRaceNormalTime();
}
// Update is called once per frame
void Update () {
if (taco.GetDistanceRun() < distanceToRun) {
Speed = taco.GetSpeed();
vm.speed = Speed / normalSpeed;
} else {
// Player finished
vm.speed = 1f;
}
}
public bool LoadingComplete() {
return vm.setUp;
}
void FixedUpdate() {
}
// int getBikeImpulses(float timeWindow = .5f) {
// if (impulses.Count == 0) {
// return 0;
// }
//
// float time = Time.time;
// float delta = time - impulses.Peek();
// while (delta > timeWindow) {
// impulses.Dequeue();
// if (impulses.Count == 0) {
// break;
// }
// delta = time - impulses.Peek();
// }
// return impulses.Count;
// }
//
// float getBikeSpeedApproxFiltered(float max = 90 / 3.6f) {
// float speed = getBikeSpeedApprox();
// if (speed < max)
// filteredSpeed = speed;
// return filteredSpeed;
// }
//
// float getBikeSpeedApprox() {
// float newTimeDlt = Time.time - lastTimeImp;
//
// if (lastTimeDlt < 0.01)
// return 0f;
// if (newTimeDlt < lastTimeDlt)
// return distancePerTick / lastTimeDlt;
// if (newTimeDlt < maxTimeDlt)
// return distancePerTick / newTimeDlt;
// return 0f;
// }
//
// float getBikeSpeedLegacy(float timeWindow = .5f) {
// int imp = getBikeImpulses(timeWindow);
// return imp * distancePerTick / timeWindow;
// }
//
// void WheelTic() {
// impulses.Enqueue(Time.time);
// distanceRun += distancePerTick;
// //Debug.Log(GetProgress());
//
// lastTimeDlt = Time.time - lastTimeImp;
// lastTimeImp = Time.time;
//
// Debug.Log(lastTimeDlt);
// }
public float GetProgress() {
return taco.GetDistanceRun() / distanceToRun;
}
public float GetDistance() {
return taco.GetDistanceRun();
}
}