Good taco

This commit is contained in:
Florent Guiotte 2018-03-13 23:32:26 +01:00
parent fda04762c6
commit 35bf13bf72
8 changed files with 165 additions and 91 deletions

View File

@ -573,7 +573,7 @@ SpriteRenderer:
m_SortingLayer: 0 m_SortingLayer: 0
m_SortingOrder: 0 m_SortingOrder: 0
m_Sprite: {fileID: 21300000, guid: ff874d8acc5af46fb9cd88466b65e0af, type: 3} m_Sprite: {fileID: 21300000, guid: ff874d8acc5af46fb9cd88466b65e0af, type: 3}
m_Color: {r: 1, g: 0, b: 0, a: 1} m_Color: {r: 0.75, g: 0.13786764, b: 0.13786764, a: 1}
m_FlipX: 0 m_FlipX: 0
m_FlipY: 0 m_FlipY: 0
m_DrawMode: 0 m_DrawMode: 0
@ -877,10 +877,6 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
playerID: 0 playerID: 0
distanceToRun: 300
wheelDiameter: 0.7
ticPerTurn: 1
speedIntegrationTime: 1
maxTimeDlt: 1 maxTimeDlt: 1
--- !u!1 &1051925801 --- !u!1 &1051925801
GameObject: GameObject:
@ -1307,10 +1303,6 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
playerID: 1 playerID: 1
distanceToRun: 400
wheelDiameter: 0.559
ticPerTurn: 2
speedIntegrationTime: 1
maxTimeDlt: 1 maxTimeDlt: 1
--- !u!114 &1718131635 --- !u!114 &1718131635
MonoBehaviour: MonoBehaviour:

View File

@ -8,20 +8,12 @@ public class BikeManager : MonoBehaviour {
[SerializeField] [SerializeField]
public int playerID; // {get; set; } public int playerID; // {get; set; }
// metric system ftw // metric system ftw
[SerializeField]
private float distanceToRun = 100f; // {get; set; }
[SerializeField]
private float wheelDiameter = .559f; // {get; set; }
[SerializeField]
private int ticPerTurn = 4;
[SerializeField]
private float speedIntegrationTime = .5f;
private string inputID;
private float distancePerTick;
private float distanceRun;
private float normalSpeed; private float normalSpeed;
// external conf
private float distanceToRun;
public float Speed {get; set; } public float Speed {get; set; }
private Queue<float> impulses = new Queue<float>(); private Queue<float> impulses = new Queue<float>();
@ -29,6 +21,7 @@ public class BikeManager : MonoBehaviour {
private float lastTimeDlt; private float lastTimeDlt;
public float maxTimeDlt = 1.0f; public float maxTimeDlt = 1.0f;
private float filteredSpeed; private float filteredSpeed;
private Tachometer taco;
private VideoManager vm; private VideoManager vm;
@ -43,34 +36,20 @@ public class BikeManager : MonoBehaviour {
gm.RegisterBike(this, playerID); gm.RegisterBike(this, playerID);
distanceToRun = gm.GetRaceDistance(); distanceToRun = gm.GetRaceDistance();
Debug.Log("Hello ima stupid bike and i should go until " + gm.GetRaceDistance());
taco = gameObject.AddComponent<RAWTachometer>();
taco.SetPlayer(playerID);
vm = gameObject.GetComponent<VideoManager>(); vm = gameObject.GetComponent<VideoManager>();
// TODO: Refactor w a player manager ?
switch (playerID) {
case 0:
inputID = "WheelP1";
break;
case 1:
inputID = "WheelP2";
break;
}
distancePerTick = wheelDiameter * Mathf.PI / ticPerTurn;
Debug.Log("Distance to run:" + distanceToRun);
Debug.Log("Distance per tick:" + distancePerTick);
normalSpeed = distanceToRun / vm.raceFinishTime; normalSpeed = distanceToRun / vm.raceFinishTime;
} }
// Update is called once per frame // Update is called once per frame
void Update () { void Update () {
if (Input.GetButtonDown(inputID))
WheelTic();
//Speed = getBikeSpeedLegacy(speedIntegrationTime); //Speed = getBikeSpeedLegacy(speedIntegrationTime);
Speed = getBikeSpeedApproxFiltered(); Speed = taco.GetSpeed();
vm.speed = Speed / normalSpeed; vm.speed = Speed / normalSpeed;
} }
@ -78,63 +57,63 @@ public class BikeManager : MonoBehaviour {
void FixedUpdate() { void FixedUpdate() {
} }
int getBikeImpulses(float timeWindow = .5f) { // int getBikeImpulses(float timeWindow = .5f) {
if (impulses.Count == 0) { // if (impulses.Count == 0) {
return 0; // return 0;
} // }
//
float time = Time.time; // float time = Time.time;
float delta = time - impulses.Peek(); // float delta = time - impulses.Peek();
while (delta > timeWindow) { // while (delta > timeWindow) {
impulses.Dequeue(); // impulses.Dequeue();
if (impulses.Count == 0) { // if (impulses.Count == 0) {
break; // break;
} // }
delta = time - impulses.Peek(); // delta = time - impulses.Peek();
} // }
return impulses.Count; // return impulses.Count;
} // }
//
float getBikeSpeedApproxFiltered(float max = 90 / 3.6f) { // float getBikeSpeedApproxFiltered(float max = 90 / 3.6f) {
float speed = getBikeSpeedApprox(); // float speed = getBikeSpeedApprox();
if (speed < max) // if (speed < max)
filteredSpeed = speed; // filteredSpeed = speed;
return filteredSpeed; // return filteredSpeed;
} // }
//
float getBikeSpeedApprox() { // float getBikeSpeedApprox() {
float newTimeDlt = Time.time - lastTimeImp; // float newTimeDlt = Time.time - lastTimeImp;
//
if (lastTimeDlt < 0.01) // if (lastTimeDlt < 0.01)
return 0f; // return 0f;
if (newTimeDlt < lastTimeDlt) // if (newTimeDlt < lastTimeDlt)
return distancePerTick / lastTimeDlt; // return distancePerTick / lastTimeDlt;
if (newTimeDlt < maxTimeDlt) // if (newTimeDlt < maxTimeDlt)
return distancePerTick / newTimeDlt; // return distancePerTick / newTimeDlt;
return 0f; // return 0f;
} // }
//
float getBikeSpeedLegacy(float timeWindow = .5f) { // float getBikeSpeedLegacy(float timeWindow = .5f) {
int imp = getBikeImpulses(timeWindow); // int imp = getBikeImpulses(timeWindow);
return imp * distancePerTick / timeWindow; // return imp * distancePerTick / timeWindow;
} // }
//
void WheelTic() { // void WheelTic() {
impulses.Enqueue(Time.time); // impulses.Enqueue(Time.time);
distanceRun += distancePerTick; // distanceRun += distancePerTick;
//Debug.Log(GetProgress()); // //Debug.Log(GetProgress());
//
lastTimeDlt = Time.time - lastTimeImp; // lastTimeDlt = Time.time - lastTimeImp;
lastTimeImp = Time.time; // lastTimeImp = Time.time;
//
Debug.Log(lastTimeDlt); // Debug.Log(lastTimeDlt);
} // }
public float GetProgress() { public float GetProgress() {
return distanceRun / distanceToRun; return taco.GetDistanceRun() / distanceToRun;
} }
public float GetDistance() { public float GetDistance() {
return distanceRun; return taco.GetDistanceRun();
} }
} }

View File

@ -10,6 +10,7 @@ public class ConfigurationLoader {
public float distance; public float distance;
public float normalTime; public float normalTime;
public float[] diameters = new float[2]; public float[] diameters = new float[2];
public int[] sensors = new int[2];
public ConfigurationLoader() { public ConfigurationLoader() {
Load(); Load();
@ -24,6 +25,8 @@ public class ConfigurationLoader {
normalTime = obj["Course"]["TempsNormal"].n; normalTime = obj["Course"]["TempsNormal"].n;
diameters[0] = obj["Vélos"]["Joueur1"]["DiamètreRoue"].n / 1000f; diameters[0] = obj["Vélos"]["Joueur1"]["DiamètreRoue"].n / 1000f;
diameters[1] = obj["Vélos"]["Joueur2"]["DiamètreRoue"].n / 1000f; diameters[1] = obj["Vélos"]["Joueur2"]["DiamètreRoue"].n / 1000f;
sensors[0] = (int) obj["Vélos"]["Joueur1"]["NombreDeCapteurs"].n;
sensors[1] = (int) obj["Vélos"]["Joueur2"]["NombreDeCapteurs"].n;
} }
private void Check() { private void Check() {

View File

@ -69,4 +69,8 @@ public class GameManager : MonoBehaviour {
public float GetBikeWheelDiameter(int bikeID) { public float GetBikeWheelDiameter(int bikeID) {
return conf.diameters[bikeID]; return conf.diameters[bikeID];
} }
public int GetSensorsCount(int bikeID) {
return conf.sensors[bikeID];
}
} }

View File

@ -0,0 +1,16 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class RAWTachometer : Tachometer {
public float delta = 1f;
public override float GetSpeed() {
int c = impulses.Count(i => i > Time.time - delta);
Debug.Log("I ru speed Count: " + impulses.Count() + " c " + c);
return c;
}
}

View File

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 00df707dfe3734d81bfa5bec6c30526f
timeCreated: 1520974537
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,54 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public abstract class Tachometer : MonoBehaviour {
private string inputID;
// Config
private float ticPerTurn;
private float wheelDiameter;
// Process
private float distancePerTick;
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);
distancePerTick = wheelDiameter * Mathf.PI / ticPerTurn;
}
private void RegisterTic() {
Debug.Log("Register");
impulses.Add(Time.time);
}
public virtual float GetSpeed() {
return -1f;
}
public float GetDistanceRun() {
return distancePerTick * impulses.Count();
}
void Start() {
}
void Update() {
if (Input.GetButtonDown(inputID))
RegisterTic();
}
}

View File

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 02a8f1d343fd64322a2ef2e772360ef3
timeCreated: 1520973602
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: