BikeGameObjectDataLogger.cs 3.0 KB

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