Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.9 KiB

  1. /************************************************************************************
  2. Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
  3. See SampleFramework license.txt for license terms. Unless required by applicable law
  4. or agreed to in writing, the sample code is provided AS IS WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied. See the license for specific
  6. language governing permissions and limitations under the license.
  7. ************************************************************************************/
  8. using UnityEngine;
  9. using UnityEngine.Assertions;
  10. namespace OculusSampleFramework
  11. {
  12. public class TrainTrack : MonoBehaviour
  13. {
  14. [SerializeField] private float _gridSize = 0.5f;
  15. [SerializeField] private int _subDivCount = 20;
  16. [SerializeField] private Transform _segmentParent = null;
  17. [SerializeField] private Transform _trainParent = null;
  18. // regeneration is optional
  19. [SerializeField] private bool _regnerateTrackMeshOnAwake = false;
  20. private float _trainLength = -1.0f;
  21. private TrackSegment[] _trackSegments = null;
  22. public float TrackLength
  23. {
  24. get
  25. {
  26. return _trainLength;
  27. }
  28. private set
  29. {
  30. _trainLength = value;
  31. }
  32. }
  33. private void Awake()
  34. {
  35. Assert.IsNotNull(_segmentParent);
  36. Assert.IsNotNull(_trainParent);
  37. Regenerate();
  38. }
  39. public TrackSegment GetSegment(float distance)
  40. {
  41. int childCount = _segmentParent.childCount;
  42. for (int i = 0; i < childCount; i++)
  43. {
  44. var segment = _trackSegments[i];
  45. var nextSegment = _trackSegments[(i + 1) % childCount];
  46. if (distance >= segment.StartDistance && (distance < nextSegment.StartDistance || i == childCount - 1))
  47. {
  48. return segment;
  49. }
  50. }
  51. return null;
  52. }
  53. public void Regenerate()
  54. {
  55. _trackSegments = _segmentParent.GetComponentsInChildren<TrackSegment>();
  56. TrackLength = 0;
  57. int childCount = _segmentParent.childCount;
  58. TrackSegment lastSegment = null;
  59. var ratio = 0.0f;
  60. for (int i = 0; i < childCount; i++)
  61. {
  62. var segment = _trackSegments[i];
  63. segment.SubDivCount = _subDivCount;
  64. ratio = segment.setGridSize(_gridSize);
  65. if (lastSegment != null)
  66. {
  67. var endPose = lastSegment.EndPose;
  68. segment.transform.position = endPose.Position;
  69. segment.transform.rotation = endPose.Rotation;
  70. segment.StartDistance = TrackLength;
  71. }
  72. if (_regnerateTrackMeshOnAwake)
  73. {
  74. segment.RegenerateTrackAndMesh();
  75. }
  76. TrackLength += segment.SegmentLength;
  77. lastSegment = segment;
  78. }
  79. SetScale(ratio);
  80. }
  81. private void SetScale(float ratio)
  82. {
  83. _trainParent.localScale = new Vector3(ratio, ratio, ratio);
  84. var cars = _trainParent.GetComponentsInChildren<TrainCar>();
  85. var locomotive = _trainParent.GetComponentInChildren<TrainLocomotive>();
  86. locomotive.Scale = ratio;
  87. foreach (var car in cars)
  88. {
  89. car.Scale = ratio;
  90. }
  91. }
  92. }
  93. }