//Takes the transfer segments defined earlier and instantiates them based on sprite size. TerrainReferenceClass InitializeTerrain() { //Make sure that the level length is not less than what has to be instantiated. if (levelLength < transferSegments.endVariations.Length + transferSegments.introductoryVariations.Length) { Debug.Log("The level length is not large enough to hold all of the required variations, changing to " + (transferSegments.endVariations.Length + transferSegments.introductoryVariations.Length)); levelLength = transferSegments.endVariations.Length + transferSegments.introductoryVariations.Length; } //This is recorded and changed as more terrain is added. float currentXPosition = 0; //This will be returned once filled in. TerrainReferenceClass createdMaze = new TerrainReferenceClass(levelLength + transferSegments.introductoryVariations.Length + transferSegments.introductoryVariations.Length); //This holds the main maze part. Transform parentMaze = new GameObject("Maze").transform; parentMaze.localPosition = new Vector3(0, 0, -8); //Instantiate the starting point. GameObject instantiatedStartPoint = LayTerrainAsset(transferSegments.startSegment, Vector3.zero, Quaternion.identity, parentMaze); instantiatedStartPoint.transform.localPosition = Vector3.zero; currentXPosition += GetSpriteSizeFromGameObject(instantiatedStartPoint).x / 2f; //Apparently this can be null (weird error) for (int i = 0; i < transferSegments.introductoryVariations.Length; i++) { //Instantiate the next introductory variation. float halfWidth = GetSpriteSizeFromGameObject(transferSegments.introductoryVariations [i].variationReference.gameObject).x / 2f; currentXPosition += halfWidth; GameObject createdAsset = LayTerrainAsset(transferSegments.introductoryVariations [i].variationReference.gameObject, new Vector3(currentXPosition, 0, 0), Quaternion.identity, parentMaze); createdMaze.layer1[i] = createdAsset.transform; currentXPosition += halfWidth; } //For all levelLength values. Start at the length of introductory variations and move on from there to the level length minus the number of . for (int i = transferSegments.introductoryVariations.Length; i < levelLength - transferSegments.endVariations.Length; i++) { //Half-Width and currentX position are used for all variations. VariationReference chosenVariationLayer1 = ScriptingUtilities.GetRandomObjectFromArray(transferSegments.l1Variations); GameObject chosenObjectLayer1 = chosenVariationLayer1.variationReference; float halfWidth = GetSpriteSizeFromGameObject(chosenObjectLayer1).x / 2f; currentXPosition += halfWidth; //Layer 1 Vector3 pointToInstantiateLayer1Object = new Vector3(currentXPosition, 0, 0); GameObject instantiatedObjectLayer1 = LayTerrainAsset(chosenObjectLayer1, pointToInstantiateLayer1Object, Quaternion.identity, parentMaze); if (Random.Range(0, 2) == 1 && chosenVariationLayer1.canBeFlipped) { instantiatedObjectLayer1.transform.localScale = new Vector3(-1, 1, 1); } createdMaze.layer1[i + transferSegments.introductoryVariations.Length] = instantiatedObjectLayer1.transform; //Layer 2 //Make sure that layer 2 objects do exist. if (transferSegments.l2Variations.Length != 0) { VariationReference chosenVariationLayer2 = ScriptingUtilities.GetRandomObjectFromArray(transferSegments.l2Variations); GameObject chosenObjectLayer2 = chosenVariationLayer2.variationReference; Vector3 pointToInstantiateLayer2Object = new Vector3(currentXPosition, -(GetSpriteSizeFromGameObject(chosenObjectLayer1).y / 2f + GetSpriteSizeFromGameObject(chosenObjectLayer2).y / 2f), 0); GameObject instantiatedObjectLayer2 = LayTerrainAsset(chosenObjectLayer2, pointToInstantiateLayer2Object, Quaternion.identity, parentMaze); if (Random.Range(0, 2) == 1 && chosenVariationLayer2.canBeFlipped) { instantiatedObjectLayer2.transform.localScale = new Vector3(-1, 1, 1); } createdMaze.layer2[i] = instantiatedObjectLayer2.transform; //Layer 3 //Make sure that layer 3 objects do exist (dependent on whether layer 2 objects exist as well). if (transferSegments.l3Variations.Length != 0) { VariationReference chosenVariationLayer3 = ScriptingUtilities.GetRandomObjectFromArray(transferSegments.l3Variations); GameObject chosenObjectLayer3 = chosenVariationLayer3.variationReference; Vector3 pointToInstantiateLayer3Object = new Vector3(currentXPosition, instantiatedObjectLayer2.transform.position.y - (GetSpriteSizeFromGameObject(chosenObjectLayer2).y / 2f + GetSpriteSizeFromGameObject(chosenObjectLayer3).y / 2f), 0); GameObject instantiatedObjectLayer3 = LayTerrainAsset(chosenObjectLayer3, pointToInstantiateLayer3Object, Quaternion.identity, parentMaze); if (Random.Range(0, 2) == 1 && chosenVariationLayer3.canBeFlipped) { instantiatedObjectLayer3.transform.localScale = new Vector3(-1, 1, 1); } createdMaze.layer3[i] = instantiatedObjectLayer3.transform; } } //Add current X position currentXPosition += halfWidth; } //Apparently this can be null (weird error) for (int i = 0; i < transferSegments.endVariations.Length; i++) { //Instantiate the next introductory variation. float halfWidth = GetSpriteSizeFromGameObject(transferSegments.endVariations [i].variationReference.gameObject).x / 2f; currentXPosition += halfWidth; GameObject createdAsset = LayTerrainAsset(transferSegments.endVariations [i].variationReference.gameObject, new Vector3(currentXPosition, 0, 0), Quaternion.identity, parentMaze); createdMaze.layer1[i + levelLength + transferSegments.introductoryVariations.Length] = createdAsset.transform; currentXPosition += halfWidth; } //Instantiate the end segment. currentXPosition += GetSpriteSizeFromGameObject(transferSegments.startSegment).x / 2f; GameObject instantiatedEndPoint = LayTerrainAsset(transferSegments.startSegment, new Vector3(currentXPosition, 0, 0), Quaternion.identity, parentMaze); instantiatedEndPoint.transform.localScale = new Vector3(-1, 1, 1); instantiatedEndPoint.transform.localPosition = new Vector3(instantiatedEndPoint.transform.position.x, instantiatedEndPoint.transform.position.y, 0); //Calculate the float level length, then send it over to CurrentLevelVariableManagement. float levelLengthX = instantiatedEndPoint.transform.position.x - instantiatedStartPoint.transform.position.x; //Set the level length (will be used for things like the particle effect). CurrentLevelVariableManagement.SetLevelLengthX(levelLengthX); return(createdMaze); }
IEnumerator LoadEverything() { createdLoadingBar.InitializeNewAction(.1f, "Loading Main UI"); AsyncOperation loadingOperation = SceneManager.LoadSceneAsync("MainGameUI", LoadSceneMode.Additive); while (!loadingOperation.isDone) { yield return(null); } Debug.Log("Loading Main Game UI is complete."); //Update the loading bar. yield return(new WaitForSeconds(.1f)); createdLoadingBar.InitializeNewAction(.15f, "Loading Inventory"); //Initialize everything!!! //Create slots, and define 2D array values. if (CreateInventorySlots != null) { CreateInventorySlots(); } else { Debug.LogError("CreateInventorySlots was null!"); // Used with PanelLayout } if (CreateHotbarSlots != null) { CreateHotbarSlots(); } else { Debug.LogError("CreateHotbarSlots was null!"); //Used with HotbarPanelLayout (Otherwise createdUISlots gets the hotbarslots return). } //Initialize Slots if (InitializeSlots != null) { InitializeSlots(); } else { Debug.LogError("InitializeSlots was null!"); //Used with SlotScript } Debug.Log("Initialized slots"); //UI stuff. yield return(new WaitForSeconds(.1f)); createdLoadingBar.InitializeNewAction(.2f, "Loading UI Stuff"); //Hide/Show if (EnableUIHideShow != null) { EnableUIHideShow(); } else { Debug.LogError("EnableUIHideShow was null!"); //Used with InventoryHideShow } //Health Panels if (InitializeUIHealthController != null) { InitializeUIHealthController(); } else { Debug.LogError("InitializeUIHealthController was null!"); //Used for UIHealthController } if (InitializeHealthPanels != null) { InitializeHealthPanels(); } else { Debug.LogError("InitializeHealthPanels was null!"); //Used for HealthPanelReference and PlayerHealthPanelReference. } //Interactable Panels if (InitializeInteractablePanelController != null) { InitializeInteractablePanelController(); } else { Debug.LogError("InitializeInteractablePanelController was null!"); } if (InitializeInteractablePanels != null) { InitializeInteractablePanels(); } else { Debug.LogError("InitializeInteractablePanels was null!"); } //Speech control if (InitializeUISpeechControl != null) { InitializeUISpeechControl(); } else { Debug.LogError("InitializeUISpeechControl was null!"); } //Objective Manager if (InitializeObjectiveManager != null) { InitializeObjectiveManager(); } else { Debug.LogError("InitializeObjectiveManager was null!"); //Used for ObjectiveManager } //Lay out the level yield return(new WaitForSeconds(.1f)); createdLoadingBar.InitializeNewAction(.25f, "Creating Terrain"); TerrainReferenceClass initializedMaze = null; if (InitializeTerrain != null) { initializedMaze = InitializeTerrain(); } else { Debug.LogError("InitializeTerrain was null!"); //Used with LevelLayout } //Player stuff. yield return(new WaitForSeconds(.1f)); createdLoadingBar.InitializeNewAction(.5f, "Initializing Player"); if (CreatePlayer != null) { CreatePlayer(); } else { Debug.LogError("CreatePlayer was null!"); //Used for CreateLevelItems (Instantiating player) } //Has to be done after the player is instantiated. CurrentLevelVariableManagement.SetLevelReferences(); if (InitializeHotbarManager != null) { InitializeHotbarManager(); } else { Debug.LogError("InitializeHotbarItems was null!"); //Used for initializing the HotbarManager. } if (InitializeCostume != null) { InitializeCostume(); } else { Debug.LogError("InitializeCostume was null!"); //Used for PlayerCostumeManager } if (InitializeBackgroundManager != null) { InitializeBackgroundManager(); } else { Debug.LogError("InitializeBackgroundScroller was null!"); //Initialize the BackgroundScroller class. } if (InitializePlayer != null) { InitializePlayer(); } else { Debug.LogError("InitializePlayer was null!"); //Used for initializing the HumanoidBaseReferenceClass. } if (InitializeCameraFunctions != null) { InitializeCameraFunctions(); } else { Debug.LogError("InitializeCameraFunctions was null!"); // Used for camera controller. } if (InitializeTimeIndicator != null) { InitializeTimeIndicator(); } else { Debug.LogError("InitializeTimeIndicator was null!!"); //Used for TimeIndicator. } //Initialize the enemies. yield return(new WaitForSeconds(.1f)); createdLoadingBar.InitializeNewAction(.75f, "Initializing Enemies"); if (CreateTerrainItems != null) { CreateTerrainItems(initializedMaze); } else { Debug.LogError("CreateTerrainItems was null!"); //Used for instantiating the enemies and trees. } if (InitializeEnemyHealthControllers != null) { InitializeEnemyHealthControllers(); } else { Debug.LogError("InitializeEnemyHealthControllers was null!"); //Used for initializing CharacterHealthController. } if (InitializeEnemies != null) { InitializeEnemies(); } else { Debug.LogError("InitializeEnemies was null!"); //Used for all enemies (requires player being instantiated). } //NPCs yield return(new WaitForSeconds(.1f)); createdLoadingBar.InitializeNewAction(.85f, "Initializing NPCs"); if (InitializeNPCPanelControllers != null) { InitializeNPCPanelControllers(); } else { Debug.LogError("InitializeNPCPanelControllers was null!"); } if (InitializeNPCs != null) { InitializeNPCs(); } else { Debug.LogError("InitializeNPCs was null!"); } //Particle effect (world) yield return(new WaitForSeconds(.1f)); createdLoadingBar.InitializeNewAction(.9f, "Initializing Particle Effects"); if (InitializeSystemWideParticleEffect != null) { InitializeSystemWideParticleEffect(); } else { Debug.LogError("InitializeSystemWideParticleEffect was null!"); } //Purchase panels if (InitializePurchasePanels != null) { InitializePurchasePanels(); } else { Debug.LogError("InitializePurchasePanels was null!"); } if (InitializePurchasePanelManager != null) { InitializePurchasePanelManager(); } else { Debug.LogError("InitializePurchasePanelManager is null!"); } if (SetInactiveObjects != null) { SetInactiveObjects(); } else { Debug.LogError("HideInventories is null!"); } //Just mention that EventManager has been completed successfully. yield return(new WaitForSeconds(.1f)); createdLoadingBar.InitializeNewAction(1, "Completed successfully!"); yield return(new WaitForSeconds(1.5f)); Debug.Log("Completed EventManager"); //Delete the loading bar Destroy(createdLoadingBar.gameObject); }
//Parse through all enemy item points. void CreateTerrainItems(TerrainReferenceClass mazeSegments) { //This list will hold the positions of all of the enemy item positions. List <Transform> allEnemyItemPoints = new List <Transform> (); //This prevents a ton of future errors and if-checking. if (initialGameElements.Length != 0) { //Instantiate all items for (int i = 0; i < mazeSegments.layer1.Length; i++) { //Preventing future errors. if (mazeSegments.layer1[i] != null) { if (mazeSegments.layer1[i].Find("Points") != null) { Transform enemyItemsTransform = mazeSegments.layer1[i].Find("Points").Find("EnemyItems"); //Check to make sure enemy item points exist. if (enemyItemsTransform != null) { //Find the points Transform[] enemyItemPoints = ScriptingUtilities.ParseChildrenFromTransform(enemyItemsTransform); for (int j = 0; j < enemyItemPoints.Length; j++) { //Add all transforms to the master list. allEnemyItemPoints.Add(enemyItemPoints[j]); } } } else { Debug.Log("Did not find points upon which to instantiate enemies on increment " + i + " with variation name " + mazeSegments.layer1[i].gameObject.name); } } else { Debug.LogError("MazeSegments.layer1[" + i + "] is null!!!!"); } } } else { Debug.LogError("No initial game elements are set!"); } //Make sure that the previous step added transforms to the list. if (allEnemyItemPoints.Count > 0) { //Sort the object references SortObjectArray(); //Make sure that the variations have the capacity to fill all required items. if (requiredItems.Length <= allEnemyItemPoints.Count) { //List of empty points List <int> emptyPoints = new List <int> (); //Populate the list of points. for (int j = 0; j < allEnemyItemPoints.Count; j++) { emptyPoints.Add(j); } //All required items. for (int j = 0; j < requiredItems.Length; j++) { int chosenLoc = Random.Range(0, emptyPoints.Count); //Choose a point out of the existing points. int pointToInstantiateRequiredItem = emptyPoints [chosenLoc]; //Instantiate the object GameObject createdElement = InstantiateElementOnPoint(requiredItems [j].elementReference, allEnemyItemPoints [pointToInstantiateRequiredItem].position ); createdElement.transform.SetParent(allEnemyItemPoints [pointToInstantiateRequiredItem]); //Remove the point that was just used from the list. emptyPoints.RemoveAt(chosenLoc); } //For the rest of the points. while (emptyPoints.Count > 0) { int chosenLoc = Random.Range(0, emptyPoints.Count); int pointToInstantiateRequiredItem = emptyPoints [chosenLoc]; //Instantiate the object InstantiatableObjectReference chosenElement = ChooseElement(); //Check to make sure that it is not supposed to instantiate anything. if (chosenElement != null) { //Instantiate element GameObject createdElement = InstantiateElementOnPoint(chosenElement.elementReference, allEnemyItemPoints [pointToInstantiateRequiredItem].position ); createdElement.transform.SetParent(allEnemyItemPoints [pointToInstantiateRequiredItem]); } //Remove the point that was just used from the list. emptyPoints.RemoveAt(chosenLoc); } } } }