using System.Collections; using System.Collections.Generic; using UnityEngine; namespace HighwayRacers { public class Highway1 : MonoBehaviour { [Header("Prefabs")] public GameObject straightPiecePrefab; public GameObject curvePiecePrefab; private HighwayPiece[] pieces = new HighwayPiece[8]; //------------------------------------------------------------------------------------------------ // UNITY MESSAGE //------------------------------------------------------------------------------------------------ private void Start() { CreateHighway(CommonFields.LANE0_LENGTH); } //------------------------------------------------------------------------------------------------ // PUBLIC METHODS //------------------------------------------------------------------------------------------------ public float length(float lane) { return CommonFields.straightPieceLength * 4 + curvePieceLength(lane) * 4; } //------------------------------------------------------------------------------------------------ public static float curvePieceRadius(float lane) { return CommonFields.CURVE_LANE0_RADIUS + lane * CommonFields.LANE_SPACING; } //------------------------------------------------------------------------------------------------ public static float curvePieceLength(float lane) { return curvePieceRadius(lane) * Mathf.PI / 2; } //------------------------------------------------------------------------------------------------ public void CreateHighway(float lane0Length) { Debug.Log(string.Format(">> Highway.CreateHighway(lane0Length={0})", lane0Length)); if (lane0Length < CommonFields.MIN_HIGHWAY_LANE0_LENGTH) { Debug.LogError("Highway length must be longer than " + CommonFields.MIN_HIGHWAY_LANE0_LENGTH); return; } float straightPieceLength = (lane0Length - CommonFields.CURVE_LANE0_RADIUS * 4) / 4; Vector3 pos = Vector3.zero; float rot = 0; for (int i = 0; i < 8; i++) { if (i % 2 == 0) { // straight piece if (pieces[i] == null) { pieces[i] = Instantiate(straightPiecePrefab, transform).GetComponent(); } StraightPiece straightPiece = pieces[i] as StraightPiece; straightPiece.SetStartPosition(pos); straightPiece.startRotation = rot; straightPiece.SetLength(straightPieceLength); pos += straightPiece.startRotationQ * new Vector3(0, 0, straightPieceLength); } else { // curve piece if (pieces[i] == null) { pieces[i] = Instantiate(curvePiecePrefab, transform).GetComponent(); } CurvePiece curvePiece = pieces[i] as CurvePiece; curvePiece.SetStartPosition(pos); curvePiece.startRotation = rot; pos += curvePiece.startRotationQ * new Vector3(CommonFields.MID_RADIUS, 0, CommonFields.MID_RADIUS); rot = Mathf.PI / 2 * (i / 2 + 1); } } //CommonFields.LANE0_LENGTH = lane0Length; } } }