Ejemplo n.º 1
0
    /// <summary>
    /// Загрузка игры
    /// </summary>
    public void LoadGame()
    {
        SavesInfo savesInfo = Serializator.DeXmlSavesInfo(savesInfoPath);

        if (!Serializator.HasSavesInfo(savesInfoPath))
        {
            return;
        }
        if (savesInfo == null)
        {
            return;
        }
        if (!savesInfo.saves[profileNumber].hasData)
        {
            return;
        }
        GameData gData = Serializator.DeXml(datapath + profileNumber.ToString() + ".xml");

        if (gData == null)
        {
            return;
        }
        savesInfo.saves[profileNumber].loadSceneName = SceneManager.GetActiveScene().name;

        GameGeneralData gGData = gData.gGData;
        LevelData       lData  = gData.lData;
        GameStatistics  gStats = GetComponent <GameStatistics>();

        #region GeneralLoad

        if (lData == null? true: !lData.active)//Произошёл переход на новый уровень и нужно учесть только данные, необходимые на протяжении всей игры
        {
            if (gGData != null)
            {
                startCheckpointNumber = gGData.firstCheckpointNumber;
                HeroController hero = SpecialFunctions.player.GetComponent <HeroController>();

                //Сначала переместим главного героя к последнему чекпоинту
                CheckpointController currentCheckpoint = checkpoints.Find(x => (x.checkpointNumb == startCheckpointNumber));
                if (currentCheckpoint != null)
                {
                    SpecialFunctions.MoveToCheckpoint(currentCheckpoint);
                }

                #region heroEquipment

                EquipmentInfo eInfo = gGData.eInfo;
                if (eInfo != null && gStats != null)
                {
                    if (gStats.WeaponDict.ContainsKey(eInfo.weapon))
                    {
                        hero.SetItem(gStats.WeaponDict[eInfo.weapon], true);
                    }
                    foreach (string itemName in eInfo.bagItems)
                    {
                        hero.Bag.Clear();
                        if (gStats.ItemDict.ContainsKey(itemName))
                        {
                            hero.Bag.Add(gStats.ItemDict[itemName]);
                        }
                    }
                }

                GetComponent <GameStatistics>().ResetStatistics();

                #endregion //heroEquipment
            }
        }

        #endregion //GeneralLoad

        #region LevelLoad

        else//Если игрок сохранился на чекпоинте, то у него есть прогресс на уровне и именно его мы и загружаем
        {
            HeroController hero = SpecialFunctions.player.GetComponent <HeroController>();

            //Сначала переместим главного героя к последнему чекпоинту
            CheckpointController currentCheckpoint = checkpoints.Find(x => (x.checkpointNumb == lData.checkpointNumber));
            if (currentCheckpoint != null)
            {
                SpecialFunctions.MoveToCheckpoint(currentCheckpoint);
            }

            #region heroEquipment

            EquipmentInfo eInfo = lData.eInfo;
            if (eInfo != null && gStats != null)
            {
                if (gStats.WeaponDict.ContainsKey(eInfo.weapon))
                {
                    hero.SetItem(gStats.WeaponDict[eInfo.weapon], true);
                }
                foreach (string itemName in eInfo.bagItems)
                {
                    hero.Bag.Clear();
                    if (gStats.ItemDict.ContainsKey(itemName))
                    {
                        hero.Bag.Add(gStats.ItemDict[itemName]);
                    }
                }
            }

            #endregion //heroEquipment

            GameHistory gHistory = GetComponent <GameHistory>();
            History     history  = gHistory != null? gHistory.history:null;

            #region Quests&Story

            QuestInfo qInfo = lData.qInfo;
            StoryInfo sInfo = lData.sInfo;
            if (qInfo != null && sInfo != null && history != null)
            {
                history.LoadHistory(sInfo, qInfo);
            }

            #endregion //Quests&Story

            #region levelStatistics

            LevelStatsData lStatsInfo = lData.lStatsInfo;
            if (lStatsInfo != null && gStats != null)
            {
                gStats.LoadStaticstics(lStatsInfo);
                gStats.InitializeAllStatistics();
            }

            #endregion //levelStatistics

            #region Drop

            DropData dropInfo = lData.dropInfo;

            //Сначала надо уничтожить все объекты типа drop на уровне
            GameObject[] drops = GameObject.FindGameObjectsWithTag("drop");
            for (int i = drops.Length - 1; i >= 0; i--)
            {
                DestroyImmediate(drops[i]);
            }
            drops = GameObject.FindGameObjectsWithTag("heartDrop");
            for (int i = drops.Length - 1; i >= 0; i--)
            {
                DestroyImmediate(drops[i]);
            }

            //А затем заново их создать
            if (dropInfo != null && gStats != null)
            {
                foreach (DropInfo _dInfo in dropInfo.drops)
                {
                    if (gStats.DropDict.ContainsKey(_dInfo.itemName) && !_dInfo.customDrop)
                    {
                        Instantiate(gStats.DropDict[_dInfo.itemName], _dInfo.position, Quaternion.identity);
                    }
                    else if (gStats.WeaponDict.ContainsKey(_dInfo.itemName))
                    {
                        GameObject newDrop = Instantiate(gStats.itemBase.customDrop, _dInfo.position, Quaternion.identity) as GameObject;
                        newDrop.GetComponent <DropClass>().item = gStats.WeaponDict[_dInfo.itemName];
                    }
                    else if (gStats.ItemDict.ContainsKey(_dInfo.itemName))
                    {
                        GameObject newDrop = Instantiate(gStats.itemBase.customDrop, _dInfo.position, Quaternion.identity) as GameObject;
                        newDrop.GetComponent <DropClass>().item = gStats.ItemDict[_dInfo.itemName];
                    }
                }
            }

            #endregion //Drop

            #region Enemies

            List <EnemyData> enInfo = lData.enInfo;

            if (enInfo != null && monsters != null)
            {
                foreach (EnemyData enData in enInfo)
                {
                    int objId = enData.objId;
                    if (objId < monsters.Count? monsters[objId] != null:false)
                    {
                        monsters[objId].SetAIData(enData);
                        monsters[objId] = null;
                    }
                }
                for (int i = 0; i < monsters.Count; i++)
                {
                    if (monsters[i] != null)
                    {
                        DestroyImmediate(monsters[i].gameObject);
                    }
                }
            }

            #endregion //Enemies

            #region InteractiveObjects

            List <InterObjData> intInfo = lData.intInfo;

            if (intInfo != null && intObjects != null)
            {
                foreach (InterObjData interData in intInfo)
                {
                    int objId = interData.objId;
                    if (objId < intObjects.Count ? intObjects[objId] != null : false)
                    {
                        IInteractive iInter = intObjects[objId].GetComponent <IInteractive>();
                        IMechanism   iMech  = intObjects[objId].GetComponent <IMechanism>();
                        IDamageable  iDmg   = intObjects[objId].GetComponent <IDamageable>();
                        if (iInter != null)
                        {
                            iInter.SetData(interData);
                        }
                        else if (iMech != null)
                        {
                            iMech.SetData(interData);
                        }
                        else if (iDmg != null)
                        {
                            iDmg.SetData(interData);
                        }
                        intObjects[objId] = null;
                    }
                }
                for (int i = 0; i < intObjects.Count; i++)
                {
                    if (intObjects[i] != null)
                    {
                        ChestController      chest      = intObjects[i].GetComponent <ChestController>();
                        CheckpointController checkpoint = intObjects[i].GetComponent <CheckpointController>();
                        if (chest != null)
                        {
                            chest.DestroyClosedChest();
                        }
                        else if (checkpoint != null)
                        {
                            checkpoint.DestroyCheckpoint();
                        }
                        else
                        {
                            DestroyImmediate(intObjects[i]);
                        }
                    }
                }
            }

            #endregion //InteractiveObjects

            #region NPCs

            List <NPCData> npcInfo = lData.npcInfo;

            if (npcInfo != null && NPCs != null)
            {
                foreach (NPCData npcData in npcInfo)
                {
                    int objId = npcData.objId;
                    if (objId < NPCs.Count ? NPCs[objId] != null : false)
                    {
                        NPCs[objId].SetData(npcData);
                        NPCs[objId] = null;
                    }
                }
                for (int i = 0; i < NPCs.Count; i++)
                {
                    if (NPCs[i] != null)
                    {
                        DestroyImmediate(NPCs[i].gameObject);
                    }
                }
            }

            #endregion //NPCs
        }

        #endregion //LevelLoad

        /*
         * //Сначала переместим главного героя к последнему чекпоинту
         * CheckpointController currentCheckpoint = checkpoints.Find(x => (x.checkpointNumb == PlayerPrefs.GetInt("Checkpoint Number")));
         * if (currentCheckpoint != null)
         *  SpecialFunctions.MoveToCheckpoint(currentCheckpoint);
         */
    }