// Chooses a random section that is allowed static GameObject RandomSection() { bool cont = true; GameObject prospectiveSection; for (int i = 0;i < 10 && cont;i++){ int r = Random.Range(0,1000); // Pick a section from the appropriate list if(currentState == sectionState.lab){ prospectiveSection = labSections[r % labSections.Count]; } else if(currentState == sectionState.lhc){ prospectiveSection = lhcSections[r % lhcSections.Count]; } else { Debug.LogError("INVALID STATE"); return null; } SectionProperties sp = prospectiveSection.GetComponent<SectionProperties>(); // Don't use the same section twice in a row if(prospectiveSection == previousSection){ continue; } // Make sure we aren't going more than 90 degrees left or right Transform endPoint = sp.endPointObj.transform; Vector3 prospectiveDirection; prospectiveDirection = (endPoint.rotation * sectionRotation).eulerAngles; if(prospectiveDirection.y < 269.9 && prospectiveDirection.y > 89.9) { continue; } // If this was a level change section, change level! if(sp.sectionType == 2){ currentState = sectionState.lhc; } else if(sp.sectionType == 3){ currentState = sectionState.lab; } previousSection = prospectiveSection; return prospectiveSection; } Debug.LogError("NO VALID SECTIONS FOUND. YOU MUST INCLUDE START SECTION, AND AT LEAST TWO OTHER SECTIONS"); return null; }
// Initialises maximumSection number of sections public static void InitialiseSections() { // Make parent for sections sectionHolder = new GameObject("SectionHolder"); vehicle = GameObject.Find("Vehicle"); // Must start with lab section currentState = sectionState.lab; // Pull in all the Sections in the scene GameObject[] availableSections = UnityEngine.GameObject.FindGameObjectsWithTag("Section"); if(availableSections.Length > 0){ initialised = true; } else { initialised = false; return; } foreach(GameObject section in availableSections){ SectionProperties sp = section.GetComponent<SectionProperties>(); switch(sp.sectionType){ case 0: case 2: labSections.Add(section); break; case 1: case 3: lhcSections.Add(section); break; default: Debug.LogError("Unrecognised section type"); break; } } instantiatedSections = new Section[maximumSections]; // Always start with start section GameObject startSection = GameObject.FindGameObjectWithTag("Start Section"); AddSection(startSection,false); for(int i = 1; i < maximumSections; i++){ // Don't try and remove previous sections during initialisation AddSection(RandomSection(),false); } }