Exemplo n.º 1
0
    private void LoadStage()
    {
        string        stageFileName      = null;
        int           requestedFactoryID = TileFactory.UNDEFINED_FACTORY_ID;
        TileFactory   modelComponent     = null;
        int           modelIndex         = -1;
        Vector2       stageDimensions    = Vector2.zero;
        Vector3       cameraPosition     = Vector3.zero;
        float         halfStageHeight    = 0f;
        float         newOrthoSize       = 0f;
        StageSettings stageSettings      = null;

        if (stageIndex < 0)
        {
            return;
        }
        if (stageObject == null)
        {
            stageObject = new GameObject("Stage");
            stageObject.transform.SetParent(gameObject.transform, false);
            stageObject.transform.rotation      = Quaternion.identity;
            stageObject.transform.localScale    = Vector3.one;
            stageObject.transform.localPosition = Vector3.zero;
            stageComponent = stageObject.AddComponent <StageController>();
            stageComponent.SetGameController(this);
            stageComponent.SetItemDatabase(itemDatabase);
            stageComponent.SetPlayerModel(playerModel);
            stageComponent.SetEnemyModels(enemyModels);
        }
        else
        {
            if (stageComponent != null)
            {
                stageComponent.Reset();
            }
        }
        if (tileFactoryComponent != null)
        {
            tileFactoryComponent.Clear();
            tileFactoryComponent = null;
        }
        if (tileFactoryObject != null)
        {
            Destroy(tileFactoryObject);
            tileFactoryObject = null;
        }
        /*halmeida - get the file name and the model of the tile factory that the stage wants to use.*/
        if (stageSettingComponents != null)
        {
            if (stageSettingComponents.Length > stageIndex)
            {
                stageSettings = stageSettingComponents[stageIndex];
                /*halmeida - since the array is previously verified, the element is surely not null.*/
                stageFileName      = stageSettings.fileName;
                requestedFactoryID = stageSettings.tileFactoryID;
                if (tileFactoryModelComponents != null)
                {
                    for (int i = 0; i < tileFactoryModelComponents.Length; i++)
                    {
                        modelComponent = tileFactoryModelComponents[i];
                        if (modelComponent != null)
                        {
                            if (modelComponent.factoryID == requestedFactoryID)
                            {
                                modelIndex = i;
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                /*halmeida - player reached the end of the game.*/
                if (stageComponent != null)
                {
                    stageComponent.Clear();
                    stageComponent = null;
                }
                Destroy(stageObject);
                stageObject = null;
                DisplayEnding();
            }
        }
        if ((stageFileName != null) && (modelIndex > -1))
        {
            tileFactoryObject = Instantiate(tileFactoryModels[modelIndex], Vector3.zero, Quaternion.identity) as GameObject;
            if (tileFactoryObject != null)
            {
                tileFactoryComponent = tileFactoryObject.GetComponent <TileFactory>();
                if ((tileFactoryComponent != null) && (stageComponent != null))
                {
                    if (inputManager != null)
                    {
                        inputManager.SetStageController(stageComponent);
                    }
                    stageComponent.SetTileFactory(tileFactoryComponent);
                    stageComponent.SetEnemyStateDurations(stageSettings.durationFragileGhost, stageSettings.durationRecoveringGhost);
                    if (stageComponent.LoadFromResourcesFile("Stages/" + stageFileName))
                    {
                        if (cameraObject != null)
                        {
                            stageDimensions  = stageComponent.GetStructureDimensions();
                            halfStageHeight  = stageDimensions.y / 2f;
                            cameraPosition   = cameraObject.transform.position;
                            cameraPosition.x = stageDimensions.x / 2f;
                            cameraPosition.y = -halfStageHeight;
                            cameraObject.transform.position = cameraPosition;
                            if (cameraComponent != null)
                            {
                                newOrthoSize = (halfStageHeight > cameraOriginalOrthoSize) ? halfStageHeight : cameraOriginalOrthoSize;
                                cameraComponent.orthographicSize = newOrthoSize;
                            }
                        }
                    }
                }
            }
        }
    }