Exemple #1
0
        public void CreateHighway(float lane0Length)
        {
            CURVE_LANE0_RADIUS       = MID_RADIUS - LANE_SPACING * (NumLanes - 1) / 2f;
            MIN_HIGHWAY_LANE0_LENGTH = CURVE_LANE0_RADIUS * 2 * Mathf.PI;
            if (lane0Length < MIN_HIGHWAY_LANE0_LENGTH)
            {
                Debug.LogError("Highway length must be longer than " + MIN_HIGHWAY_LANE0_LENGTH);
                return;
            }

            int tempNumCars = NumCars;

            if (lane0Length < DotsHighway.Lane0Length)
            {
                ClearCars();
            }

            float straightPieceLength = (lane0Length - MIN_HIGHWAY_LANE0_LENGTH) / 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 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 curvePiece = pieces[i] as CurvePiece;
                    curvePiece.SetStartPosition(pos);
                    curvePiece.startRotation = rot;

                    pos += curvePiece.startRotationQ * new Vector3(MID_RADIUS, 0, MID_RADIUS);
                    rot  = Mathf.PI / 2 * (i / 2 + 1);
                }
            }
            DotsHighway.Create(pieces, NumLanes);
            SetNumCars(tempNumCars);
        }
        public unsafe void CreateHighway(float lane0Length)
        {
            if (lane0Length < Highway1.MinHighwayLane0Length)
            {
                Debug.LogError("Highway length must be longer than " + Highway1.MinHighwayLane0Length);
                return;
            }

            int tempNumCars = GetNumCars();

            if (lane0Length < highway1.innerLaneLength)
            {
                ClearCars();
            }

            float straightPieceLength = Highway1.GetStraightPieceLength(highway1.innerLaneLength);

            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 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 curvePiece = pieces[i] as CurvePiece;
                    curvePiece.SetStartPosition(pos);
                    curvePiece.startRotation = rot;

                    pos += curvePiece.startRotationQ * new Vector3(Highway1.MidRadius, 0, Highway1.MidRadius);
                    rot  = Mathf.PI / 2 * (i / 2 + 1);
                }
            }

            highway1.innerLaneLength = Mathf.Max(Highway1.MinHighwayLane0Length, lane0Length);

            if (highway1.pieces.IsCreated)
            {
                highway1.pieces.Dispose();
            }

            highway1.pieces = new NativeArray <HighwayPiece1>(8, Allocator.Persistent);

            for (int i = 0; i < 8; ++i)
            {
                HighwayPiece1 piece1;

                piece1.startX        = pieces[i].startX;
                piece1.startZ        = pieces[i].startZ;
                piece1.startRotation = pieces[i].startRotation;

                for (int j = 0; j < 4; ++j)
                {
                    piece1.length[j] = pieces[i].length(j);
                }

                highway1.pieces[i] = piece1;
            }

            SetNumCars(tempNumCars);
        }