Exemplo n.º 1
0
    public bool BuildTrack()
    {
        Debug.Log("Start Creating");
        ////////////////////////////////////////////////
        /////TEste
        ///
        //try
        //{


        // Check if a BezierSpline already exists
        if (GameObject.Find("BezierSpline") != null)
        {
            Debug.Log("BezierSpline already Exists");
            GameObject splineOld = GameObject.Find("BezierSpline");
            DestroyImmediate(splineOld);
        }
        else
        {
            Debug.Log("BezierSpline dont Exist.\n Creating one");
        }

        BezierSpline spline = new GameObject("BezierSpline").AddComponent <BezierSpline>();
        //////////////////////
        //Read file
        /////////////////////
        ///

        List <string> cordenadaX = new List <string>();
        List <string> cordenadaY = new List <string>();
        List <string> cordenadaZ = new List <string>();

        string m_Path = Application.dataPath;

        m_Path = m_Path + "/kukaPoints.txt";
        //Output the Game data path to the console
        Debug.Log("dataPath : " + m_Path);
        using (var reader = new StreamReader(m_Path))
        {
            while (!reader.EndOfStream)
            {
                var line   = reader.ReadLine();
                var values = line.Split(',', ' ');
                Debug.Log(values[2].ToString());
                Debug.Log(values[5].ToString());
                Debug.Log(values[8].ToString());

                cordenadaX.Add(values[2]);
                cordenadaZ.Add(values[5]);
                cordenadaY.Add(values[8]);
            }
        }

        //
        Vector3 homePositionTrack;

        homePositionTrack.x = 0;
        homePositionTrack.y = 0;
        homePositionTrack.z = 0;

        float fx_old = 0;
        float fy_old = 0;
        float fz_old = 0;

        for (int i = 0; i < cordenadaX.Count - 1; i++)
        {
            cordenadaX[i] = cordenadaX[i].Replace('.', ',');
            cordenadaY[i] = cordenadaY[i].Replace('.', ',');
            cordenadaZ[i] = cordenadaZ[i].Replace('.', ',');

            float fx = float.Parse(cordenadaX[i]);
            float fy = float.Parse(cordenadaY[i]);
            float fz = float.Parse(cordenadaZ[i]);


            if (i == 0)
            {
                homePositionTrack.x = fx;
                homePositionTrack.y = fy;
                homePositionTrack.z = fz;
                fx = 0;
                fy = 0;
                fz = 0;
            }
            else
            {
                if (Math.Abs(fx - fx_old) > 2)
                {
                    Vector3 pointPosition = new Vector3(fx - homePositionTrack.x, fy - homePositionTrack.y, fz - homePositionTrack.z);
                    //Vector3 pointPosition = new Vector3(fx, fy, fz);
                    spline.InsertNewPointAt2(spline.GetNumPoints(), pointPosition);
                    spline.Refresh();
                }
                //	else
                //		Debug.LogError("bbbbbbbbbbbbbbbbbbbbbbbbb "+ fx);
            }
            fx_old = fx;
            fy_old = fy;
            fz_old = fz;
        }
        Debug.Log("Spline Done");
        spline.RemovePointAt(0);
        spline.RemovePointAt(1);

        spline.AutoConstructSpline();


        leftRailPrefab  = (GameObject)Resources.Load("prefabs/Left Rail", typeof(GameObject));
        rightRailPrefab = (GameObject)Resources.Load("prefabs/Right Rail", typeof(GameObject));
        crossBeamPrefab = (GameObject)Resources.Load("prefabs/CrossBeam", typeof(GameObject));


        vr.BuildRollercoasterTrack(GameObject.Find("Track"), GameObject.Find("BezierSpline"), leftRailPrefab, rightRailPrefab, crossBeamPrefab, resolution);
        cart = GameObject.Find("Rollercoaster");
        cart.transform.Translate(0, 3, 0);
        return(true);
        //}
        //catch
        //{
        //    return false;
        //}
    }