BikeGameObjectDataLogger.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. public BikeGameObjectDataLog(long timestamp, float positionX, float positionY, float positionZ, float rotationX,
  22. float rotationY, float rotationZ, float velocityX, float velocityY, float velocityZ, int collisionCounter)
  23. {
  24. this.timestamp = timestamp;
  25. this.positionX = positionX;
  26. this.positionY = positionY;
  27. this.positionZ = positionZ;
  28. this.rotationX = rotationX;
  29. this.rotationY = rotationY;
  30. this.rotationZ = rotationZ;
  31. this.velocityX = velocityX;
  32. this.velocityY = velocityY;
  33. this.velocityZ = velocityZ;
  34. this.collisionCounter = collisionCounter;
  35. }
  36. public KeyValuePair<long, string[]> Serialize()
  37. {
  38. return new KeyValuePair<long, string[]>(
  39. timestamp, new[]
  40. {
  41. positionX.ToString("F6", CultureInfo.InvariantCulture),
  42. positionY.ToString("F6", CultureInfo.InvariantCulture),
  43. positionZ.ToString("F6", CultureInfo.InvariantCulture),
  44. rotationX.ToString("F6", CultureInfo.InvariantCulture),
  45. rotationY.ToString("F6", CultureInfo.InvariantCulture),
  46. rotationZ.ToString("F6", CultureInfo.InvariantCulture),
  47. velocityX.ToString("F6", CultureInfo.InvariantCulture),
  48. velocityY.ToString("F6", CultureInfo.InvariantCulture),
  49. velocityZ.ToString("F6", CultureInfo.InvariantCulture),
  50. collisionCounter.ToString("F6", CultureInfo.InvariantCulture)
  51. });
  52. }
  53. }
  54. public class BikeGameObjectDataLogger : SensorDataLogger<BikeGameObjectDataLog>
  55. {
  56. public Rigidbody bike;
  57. private Transform bikeTransform;
  58. public override string Key => "bike_game_object_data";
  59. public GameObject bikePlayer;
  60. public override void Start()
  61. {
  62. base.Start();
  63. bikeTransform = bike.transform;
  64. // TODO: This is a bit crude
  65. bikePlayer = GameObject.Find("bike");
  66. }
  67. private void Update()
  68. {
  69. var pos = bikeTransform.position;
  70. var rot = bikeTransform.rotation.eulerAngles;
  71. var velocity = bike.velocity;
  72. var playerStats = bikePlayer.GetComponent<PlayerStats>();
  73. Log(new BikeGameObjectDataLog(Helpers.RoundToLong(Time.time * 1000),
  74. pos.x, pos.y, pos.z,
  75. rot.x, rot.y, rot.z,
  76. velocity.x, velocity.y, velocity.z, playerStats.collisionCounter));
  77. }
  78. public override IEnumerable<BikeGameObjectDataLog> ReadLog(IEnumerable<IEnumerable<string>> lines)
  79. {
  80. throw new NotImplementedException(); //TODO
  81. }
  82. }
  83. }