コード例 #1
0
    public void LoadGame()
    {
        Debug.Log(Application.persistentDataPath);

        if (File.Exists(Application.persistentDataPath + "/gamesave.save"))
        {
            BinaryFormatter bf   = new BinaryFormatter();
            FileStream      file = File.Open(Application.persistentDataPath + "/gamesave.save", FileMode.Open);
            Save            save = (Save)bf.Deserialize(file);
            file.Close();

            lastLevelWon = save.s_lastLevelWon;

            //Aqui se hace lo contrario que en el CreateSaveGameObject. Se toman las variables del save y se aplican esos valores a las variables que se usan en código
            currentExp = save.s_currentXp;

            characterDataForCurrentLevel.Clear();

            for (int i = 0; i < allCharacters.Length; i++)
            {
                if (save.s_charactersUnlocked.Contains(allCharacters[i].idCharacter))
                {
                    characterDataForCurrentLevel.Add(allCharacters[i]);
                    allCharacters[i].isCharacterUnlocked = true;
                    allCharacters[i].HideShowMeshCharacterData(true);
                }
            }

            allLevelNodes = FindObjectsOfType <LevelNode>();

            //List<LevelNode> allLevelNodesList = new List<LevelNode>(allLevelNodes);

            //allLevelNodesList.Sort(delegate (LevelNode a, LevelNode b)
            //{
            //    return (a.GetComponent<LevelNode>().idLevel).CompareTo(b.GetComponent<LevelNode>().idLevel);

            //});

            //allLevelNodes = allLevelNodesList.ToArray();

            levelIDsUnlocked.Clear();

            levelIDsUnlocked = new List <int>(save.s_levelIDsUnlocked);

            for (int i = 0; i < allLevelNodes.Length; i++)
            {
                for (int j = 0; j < levelIDsUnlocked.Count; j++)
                {
                    if (allLevelNodes[i].idLevel == levelIDsUnlocked[j])
                    {
                        if (lastLevelWon <= allLevelNodes[i].idLevel)
                        {
                            lastLevelWon = allLevelNodes[i].idLevel;
                        }

                        allLevelNodes[i].WinLevel();
                        allLevelNodes[i].UnlockThisLevel();
                        allLevelNodes[i].UnlockConnectedLevels();
                        break;
                    }
                }
            }


            //for (int i = 0; i < save.s_levelIDsUnlocked.Count; i++)
            //{
            //    levelIDsUnlocked.Add(save.s_levelIDsUnlocked[i]);

            //    if (allLevelNodes[save.s_levelIDsUnlocked[i-1]].idLevel < lastLevelWon)
            //    {
            //        allLevelNodes[save.s_levelIDsUnlocked[i-1]].WinLevel();
            //    }

            //    else if (allLevelNodes[save.s_levelIDsUnlocked[i-1]].idLevel == lastLevelWon-1)
            //    {
            //        allLevelNodes[save.s_levelIDsUnlocked[i-1]].UnlockConnectedLevels();
            //    }

            //    //allLevelNodes[save.s_levelIDsUnlocked[i]].UnlockThisLevel();
            //}

            //if (allLevelNodes[2].isUnlocked)
            //{
            //    allLevelNodes[1].UnlockThisLevel();
            //}


            #region Characters

            UITableManager UITM = FindObjectOfType <UITableManager>();


            KnightData _knight = FindObjectOfType <KnightData>();

            _knight.unitPowerLevel = save.s_KnightPowerLevel;
            if (_knight.isCharacterUnlocked)
            {
                for (int i = 0; i < save.s_KnightSkillsIds.Count; i++)
                {
                    _knight.idSkillsBought.Add(save.s_KnightSkillsIds[i]);
                }

                UITM.SetCharacterUpgradeBookInfo(_knight);
            }

            RogueData _rogue = FindObjectOfType <RogueData>();

            _rogue.unitPowerLevel = save.s_RoguePowerLevel;
            if (_rogue.isCharacterUnlocked)
            {
                for (int i = 0; i < save.s_RogueSkillsIds.Count; i++)
                {
                    _rogue.idSkillsBought.Add(save.s_RogueSkillsIds[i]);
                }

                UITM.SetCharacterUpgradeBookInfo(_rogue);
            }

            MageData _mage = FindObjectOfType <MageData>();

            _mage.unitPowerLevel = save.s_MagePowerLevel;
            if (_mage.isCharacterUnlocked)
            {
                for (int i = 0; i < save.s_MageSkillsIds.Count; i++)
                {
                    _mage.idSkillsBought.Add(save.s_MageSkillsIds[i]);
                }

                UITM.SetCharacterUpgradeBookInfo(_mage);
            }

            BerserkerData _berserker = FindObjectOfType <BerserkerData>();

            _berserker.unitPowerLevel = save.s_BerserkerPowerLevel;
            if (_berserker.isCharacterUnlocked)
            {
                for (int i = 0; i < save.s_BerserkerSkillsIds.Count; i++)
                {
                    _berserker.idSkillsBought.Add(save.s_BerserkerSkillsIds[i]);
                }

                UITM.SetCharacterUpgradeBookInfo(_berserker);
            }

            ValkyrieData _valkyrie = FindObjectOfType <ValkyrieData>();

            _valkyrie.unitPowerLevel = save.s_ValkyriePowerLevel;
            if (_valkyrie.isCharacterUnlocked)
            {
                for (int i = 0; i < save.s_ValkyrieSkillsIds.Count; i++)
                {
                    _valkyrie.idSkillsBought.Add(save.s_ValkyrieSkillsIds[i]);
                }

                UITM.SetCharacterUpgradeBookInfo(_valkyrie);
            }

            DruidData _druid = FindObjectOfType <DruidData>();

            _druid.unitPowerLevel = save.s_DruidPowerLevel;
            if (_druid.isCharacterUnlocked)
            {
                for (int i = 0; i < save.s_DruidSkillsIds.Count; i++)
                {
                    _druid.idSkillsBought.Add(save.s_DruidSkillsIds[i]);
                }

                UITM.SetCharacterUpgradeBookInfo(_druid);
            }

            MonkData _monk = FindObjectOfType <MonkData>();

            _monk.unitPowerLevel = save.s_MonkPowerLevel;
            if (_monk.isCharacterUnlocked)
            {
                for (int i = 0; i < save.s_MonkSkillsIds.Count; i++)
                {
                    _monk.idSkillsBought.Add(save.s_MonkSkillsIds[i]);
                }

                UITM.SetCharacterUpgradeBookInfo(_monk);
            }

            SamuraiData _samurai = FindObjectOfType <SamuraiData>();

            _samurai.unitPowerLevel = save.s_SamuraiPowerLevel;
            if (_samurai.isCharacterUnlocked)
            {
                for (int i = 0; i < save.s_SamuraiSkillsIds.Count; i++)
                {
                    _samurai.idSkillsBought.Add(save.s_SamuraiSkillsIds[i]);
                }

                UITM.SetCharacterUpgradeBookInfo(_samurai);
            }


            #endregion

            enemiesKilled         = save.s_enemiesKilled;
            randomLevelsCompleted = save.s_randomLevelsCompleted;

            Debug.Log("File Load");
            Debug.Log("-----");
        }

        else
        {
            Debug.Log("Se ha intentado cargar y no existe archivo de guardado");

            SaveGame();
        }
    }