BikeGameObjectDataLogger.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using Logging.Base;
  5. using UnityEngine;
  6. namespace Logging.Data
  7. {
  8. public readonly struct BikeGameObjectDataLog : ISerializable
  9. {
  10. private readonly long timestamp;
  11. private readonly float positionX;
  12. private readonly float positionY;
  13. private readonly float positionZ;
  14. private readonly float rotationX;
  15. private readonly float rotationY;
  16. private readonly float rotationZ;
  17. private readonly float velocityX;
  18. private readonly float velocityY;
  19. private readonly float velocityZ;
  20. private readonly int collisionCounter;
  21. private readonly int coinCounter;
  22. private readonly int hasFinished;
  23. private readonly int hasFinishedSlalom1;
  24. private readonly int hasFinishedSlalom2;
  25. private readonly string condition;
  26. public BikeGameObjectDataLog(long timestamp, float positionX, float positionY, float positionZ, float rotationX,
  27. float rotationY, float rotationZ, float velocityX, float velocityY, float velocityZ, int collisionCounter, int coinCounter,
  28. int hasFinished, int hasFinishedSlalom1, int hasFinishedSlalom2, string condition)
  29. {
  30. this.timestamp = timestamp;
  31. this.positionX = positionX;
  32. this.positionY = positionY;
  33. this.positionZ = positionZ;
  34. this.rotationX = rotationX;
  35. this.rotationY = rotationY;
  36. this.rotationZ = rotationZ;
  37. this.velocityX = velocityX;
  38. this.velocityY = velocityY;
  39. this.velocityZ = velocityZ;
  40. this.collisionCounter = collisionCounter;
  41. this.coinCounter = coinCounter;
  42. this.hasFinished = hasFinished;
  43. this.hasFinishedSlalom1 = hasFinishedSlalom1;
  44. this.hasFinishedSlalom2 = hasFinishedSlalom2;
  45. this.condition = condition;
  46. }
  47. public KeyValuePair<long, string[]> Serialize()
  48. {
  49. return new KeyValuePair<long, string[]>(
  50. timestamp, new[]
  51. {
  52. positionX.ToString("F6", CultureInfo.InvariantCulture),
  53. positionY.ToString("F6", CultureInfo.InvariantCulture),
  54. positionZ.ToString("F6", CultureInfo.InvariantCulture),
  55. rotationX.ToString("F6", CultureInfo.InvariantCulture),
  56. rotationY.ToString("F6", CultureInfo.InvariantCulture),
  57. rotationZ.ToString("F6", CultureInfo.InvariantCulture),
  58. velocityX.ToString("F6", CultureInfo.InvariantCulture),
  59. velocityY.ToString("F6", CultureInfo.InvariantCulture),
  60. velocityZ.ToString("F6", CultureInfo.InvariantCulture),
  61. collisionCounter.ToString("F6", CultureInfo.InvariantCulture),
  62. coinCounter.ToString("F6", CultureInfo.InvariantCulture),
  63. hasFinished.ToString("F6", CultureInfo.InvariantCulture),
  64. hasFinishedSlalom1.ToString("F6", CultureInfo.InvariantCulture),
  65. hasFinishedSlalom2.ToString("F6", CultureInfo.InvariantCulture),
  66. condition,
  67. });
  68. }
  69. }
  70. public class BikeGameObjectDataLogger : SensorDataLogger<BikeGameObjectDataLog>
  71. {
  72. public Rigidbody bike;
  73. private Transform bikeTransform;
  74. public override string Key => "bike_game_object_data";
  75. public GameObject bikePlayer;
  76. public override void Start()
  77. {
  78. base.Start();
  79. bikeTransform = bike.transform;
  80. // TODO: This is a bit crude
  81. bikePlayer = GameObject.Find("bike");
  82. }
  83. private void Update()
  84. {
  85. var pos = bikeTransform.position;
  86. var rot = bikeTransform.rotation.eulerAngles;
  87. var velocity = bike.velocity;
  88. var playerStats = bikePlayer.GetComponent<PlayerStats>();
  89. Log(new BikeGameObjectDataLog(Helpers.RoundToLong(Time.time * 1000),
  90. pos.x, pos.y, pos.z,
  91. rot.x, rot.y, rot.z,
  92. velocity.x, velocity.y, velocity.z,
  93. playerStats.collisionCounter,
  94. playerStats.coinCounter,
  95. playerStats.hasFinished,
  96. playerStats.hasFinishedSlalom1,
  97. playerStats.hasFinishedSlalom2,
  98. playerStats.condition
  99. ));
  100. }
  101. public override IEnumerable<BikeGameObjectDataLog> ReadLog(IEnumerable<IEnumerable<string>> lines)
  102. {
  103. throw new NotImplementedException(); //TODO
  104. }
  105. }
  106. }