Esempio n. 1
0
    public static TrackLayout CreateFromTrackCollection(TrackCollection tc)
    {
        HashSet <TrackSectionSerializer>  sectionList  = new HashSet <TrackSectionSerializer>();
        HashSet <TrackJunctionSerializer> junctionList = new HashSet <TrackJunctionSerializer>();

        for (int i = 0; i < tc.sections.Length; i++)
        {
            if (tc.sections[i] != null)
            {
                TrackJunction j = tc.sections[i] as TrackJunction;
                if (j != null)
                {
                    junctionList.Add(new TrackJunctionSerializer(j));
                }
                else
                {
                    sectionList.Add(new TrackSectionSerializer(tc.sections[i]));
                }
            }
        }

        TrackLayout layout = ScriptableObject.CreateInstance <TrackLayout>();

        layout.trackSections = new TrackSectionSerializer[sectionList.Count];
        sectionList.CopyTo(layout.trackSections);
        layout.trackJunctions = new TrackJunctionSerializer[junctionList.Count];
        junctionList.CopyTo(layout.trackJunctions);

        return(layout);
    }
Esempio n. 2
0
    public TrackJunction ToTrackJunction()
    {
        TrackJunction junction = new TrackJunction(Position.ToWorldPosition(), Rotation.ToWorldRotation(), SectionIndices);

        junction.index = Index;
        junction.PreviousSectionIndex = PrevIndex;
        return(junction);
    }
Esempio n. 3
0
    /*List<TrackSection> FindConnectedTrackSections(TrackSection start)
     * {
     *  List<TrackSection> list = new List<TrackSection>();
     *  list.Add(start);
     *  TrackSection current = start;
     *  while(current.Next != null && !list.Contains(current.Next))
     *  {
     *      current = current.Next;
     *      list.Add(current);
     *  }
     *  return list;
     * }*/

    /*public void OnDrawGizmos()
     * {
     *  foreach(TrackSection section in trackSections)
     *  {
     *      Vector3 start = section.position.Vector3();
     *      Vector3 end = section.GetPositionOnTrack(section.length).Vector3();
     *      Gizmos.DrawLine(start, end);
     *      Gizmos.DrawCube(start, Vector3.one);
     *      if(section.Next == null)
     *      {
     *          Gizmos.DrawCube(end, Vector3.one);
     *      }
     *  }
     * }*/

    /// <summary>
    /// TODO: Create wrapper for junctions
    /// </summary>
    /// <param name="junction"></param>
    /// <returns></returns>
    GameObject CreateJunctionObject(TrackJunction junction)
    {
        Junction go = GameObject.Instantiate(junctionPrefab);

        go.Init(junction);
        go.transform.SetParent(transform);

        return(go.gameObject);
    }
Esempio n. 4
0
    //public int ActiveSectionArrayIndex;

    public TrackJunctionSerializer(TrackJunction junction)
    {
        Position = new WorldPositionSerializer(junction.position);
        Rotation = new WorldRotationSerializer(junction.rotation);

        Index          = junction.index;
        PrevIndex      = junction.PreviousSectionIndex;
        SectionIndices = junction.Sections.ToArray();
    }
Esempio n. 5
0
    /// <summary>
    /// Add a section to the collection. Junctions get detected automatically.
    /// </summary>
    /// <param name="section"></param>
    /// <returns>Index of section</returns>
    public int Add(TrackSection section)
    {
        sections[currentIndex] = section;
        TrackJunction j = section as TrackJunction;

        if (j != null)
        {
            junctions[currentIndex] = j;
        }
        section.index = currentIndex;
        return(currentIndex++);
    }
Esempio n. 6
0
    public void Init(TrackJunction junction)
    {
        this.junction       = junction;
        slider              = GetComponentInChildren <Slider>();
        slider.wholeNumbers = true;
        slider.minValue     = 0;
        slider.maxValue     = junction.Sections.Count - 1;
        slider.onValueChanged.AddListener(OnUIChanged);

        GetComponent <Canvas>().worldCamera = Camera.main;

        transform.position = junction.position.Vector3() + Vector3.up * 5f;
    }
Esempio n. 7
0
    //Checks if subject is our next section. May seem useless, but is needed for switches
    public virtual bool CheckNextSection(TrackSection subject)
    {
        //If a junction is passed as the subject, check all it's sections to see if those match
        TrackJunction junction = subject as TrackJunction;

        if (junction != null)
        {
            foreach (int s in junction.Sections)
            {
                if (CheckNextSection(TrackCollection.instance.Get(s)))
                {
                    return(true);
                }
            }
            return(false);
        }
        return(subject.index == m_nextSectionIndex);
    }
Esempio n. 8
0
    /// <summary>
    /// Add a section to the collection. Junctions get detected automatically.
    /// </summary>
    /// <param name="section"></param>
    /// <returns>Index of section</returns>
    public bool Add(TrackSection section, int index)
    {
        if (Get(index) == null)
        {
            sections[index] = section;
            TrackJunction j = section as TrackJunction;
            if (j != null)
            {
                junctions[index] = j;
            }

            if (currentIndex <= index)
            {
                currentIndex = index + 1;
            }
            return(true);
        }
        return(false);
    }
Esempio n. 9
0
    public static void PopulateTrackCollection(TrackLayout layout, TrackCollection target)
    {
        target.Clear();
        foreach (TrackSectionSerializer serializedTrack in layout.trackSections)
        {
            TrackSection track = serializedTrack.ToTrackSection();
            if (!target.Add(track, track.index))
            {
                Debug.LogError("Could not add track section to collection at index " + track.index);
            }
        }

        foreach (TrackJunctionSerializer serializedJunction in layout.trackJunctions)
        {
            TrackJunction track = serializedJunction.ToTrackJunction();
            if (!target.Add(track, track.index))
            {
                Debug.LogError("Could not add track junction to collection at index " + track.index);
            }
        }
    }
Esempio n. 10
0
    public static void CreateTrackLayout()
    {
        TrackCollection trackCollection = TrackCollection.GetInstance();

        /*
         * TrackSection a = new TrackSection(new WorldPosition(0, 0, 20f, 20f), new WorldRotation(25, false), 750f, true, 90f);
         * trackCollection.Add(a);
         * TrackSection b = a.CreateNext(750f, true, 90f);
         * b = b.CreateNext(750f, true, 90f);
         * b = b.CreateNext(750f, true, 90f);
         *
         * b.NextSectionIndex = a.index;
         * a.PreviousSectionIndex = b.index;
         */



        // Create start section
        TrackSection start = new TrackSection(new WorldPosition(0, 0, 20f, 10f), new WorldRotation(-100.0f, false), 200.0f);

        trackCollection.Add(start);
        TrackSection currentSection = start;

        currentSection = currentSection.CreateNext(100.0f, false, 0.0f);

        // Create a new junction
        TrackJunction junction = new TrackJunction(currentSection.GetPositionOnTrack(currentSection.length), currentSection.GetRotationOnTrack(currentSection.length));

        trackCollection.Add(junction);
        // Attach it to the previous section
        junction.PreviousSectionIndex   = currentSection.index;
        currentSection.NextSectionIndex = junction.index;

        // Create the junction sections
        TrackSection switchSectionCurved = junction.CreateSection(200.0f, true, 90.0f);
        // 127.32395447351626861510701069801
        TrackSection switchSectionStraight = junction.CreateSection(2.0f * 127.32395f, false, 0.0f);

        // Create a loop
        currentSection = switchSectionStraight;
        // NOTE: It's split into two parts because junction sections from the same junction need at least 2 other section before they connect to eachother
        currentSection = currentSection.CreateNext(200.0f, true, 90.0f);
        currentSection = currentSection.CreateNext(400.0f, true, 180.0f);
        // Join it up
        currentSection.NextSectionIndex      = switchSectionCurved.index;
        switchSectionCurved.NextSectionIndex = currentSection.index;

        // Create a new piece of track on the other end of the start piece
        currentSection = new TrackSection(start.position, start.rotation.Oppisite, 20.0f);
        currentSection.PreviousSectionIndex = start.index;
        start.PreviousSectionIndex          = trackCollection.Add(currentSection);

        // Create a new junction
        junction = new TrackJunction(currentSection.GetPositionOnTrack(currentSection.length), currentSection.GetRotationOnTrack(currentSection.length));
        trackCollection.Add(junction);

        // Attach it to the previous section
        junction.PreviousSectionIndex   = currentSection.index;
        currentSection.NextSectionIndex = junction.index;
        // Create the junction sections
        switchSectionCurved = junction.CreateSection(200.0f, true, 90.0f);
        //127.32395447351626861510701069801
        switchSectionStraight = junction.CreateSection(2.0f * 127.32395f, false, 0.0f);

        // Create a loop
        currentSection = switchSectionStraight;
        // NOTE: It's split into two parts because junction sections from the same junction need at least 2 other section before they connect to eachother
        // Add a junction and attach it
        junction = new TrackJunction(currentSection.GetPositionOnTrack(currentSection.length), currentSection.GetRotationOnTrack(currentSection.length));
        trackCollection.Add(junction);

        junction.PreviousSectionIndex   = currentSection.index;
        currentSection.NextSectionIndex = junction.index;
        // Create the curved loop junction section
        currentSection = junction.CreateSection(200.0f, true, 90.0f);
        // Create the straight junction section
        TrackSection branch = junction.CreateSection(200.0f, false, 0.0f);

        // Finish the loop
        currentSection = currentSection.CreateNext(400.0f, true, 180.0f);
        // Join it up
        currentSection.NextSectionIndex      = switchSectionCurved.index;
        switchSectionCurved.NextSectionIndex = currentSection.index;

        branch = branch.CreateNext(400, true, 10);
        branch = branch.CreateNext(100, false, 0);
        branch = branch.CreateNext(500, true, -20);
        branch = branch.CreateNext(1000, false, 0);



        //Save it

        TrackLayout trackLayout = TrackLayout.CreateFromTrackCollection(trackCollection);

        AssetDatabase.CreateAsset(trackLayout, "Assets/Resources/DemoTrackLayout.asset");
        AssetDatabase.SaveAssets();

        EditorUtility.FocusProjectWindow();

        Selection.activeObject = trackLayout;
    }