public void Load() { BinaryFormatter bf = new BinaryFormatter(); Debug.Log(Application.persistentDataPath); if ( File.Exists ( Application.persistentDataPath + "/" + SaveFileScript.CurrentSaveFile.ToString() + "/" + gameObject.transform.root.gameObject.GetComponent <DungeonScript>().DungeonName.ToString() + "/Level" + gameObject.transform.GetSiblingIndex().ToString() + ".Bannana" ) ) { FileStream file = File.Open(Application.persistentDataPath + "/" + SaveFileScript.CurrentSaveFile.ToString() + "/" + gameObject.transform.root.gameObject.GetComponent <DungeonScript>().DungeonName.ToString() + "/Level" + gameObject.transform.GetSiblingIndex().ToString() + ".Bannana", FileMode.Open); myContents = (LevelContents)bf.Deserialize(file); file.Close(); if (DungeonScript.CurrentDungeon.CurrentLevelIndex == gameObject.transform.GetSiblingIndex()) { StartCoroutine(RebuildLevel()); } } else { throw new ArgumentException("Level Doesn't Exist, Don't mess with savefiles!"); } }
public void Unload() { if (currentLevel == gameObject) { GameObject[] Monsters = GameObject.FindGameObjectsWithTag("Monster"); foreach (GameObject monster in Monsters) { MonsterList.Add(new MonsterData { position = ConvertToArray(monster.transform.position), rotation = ConvertToArray(monster.transform.rotation.eulerAngles), Level = monster.GetComponent <MonsterScript>().MonsterLevel, MonsterType = monster.GetComponent <MonsterScript>().monsterType }); Destroy(monster); } GameObject[] droppedStuffs = GameObject.FindGameObjectsWithTag("Shiny"); List <WeaponData> droppedWeaponData = new List <WeaponData>(); List <Array> droppedWeaponPositions = new List <Array>(); List <Array> droppedWeaponRotations = new List <Array>(); List <Array> droppedConsumablePositions = new List <Array>(); List <Array> droppedConsumableRotations = new List <Array>(); List <ConsumableInfo> droppedConsumableInfo = new List <ConsumableInfo>(); foreach (GameObject droppedItem in droppedStuffs) { if (droppedItem.GetComponent <DroppedConsumableScript>()) { ConsumableInfo consumable = new ConsumableInfo() { amountCarried = droppedItem.GetComponent <DroppedConsumableScript>().amount, consumableType = droppedItem.GetComponent <DroppedConsumableScript>().ConsumableType }; droppedConsumableInfo.Add(consumable); droppedConsumablePositions.Add(ConvertToArray(droppedItem.transform.position)); droppedConsumableRotations.Add(ConvertToArray(droppedItem.transform.rotation.eulerAngles)); } else if (droppedItem.GetComponent <DroppedWeaponScript>()) { DroppedWeaponScript currentWeapon = droppedItem.gameObject.GetComponent <DroppedWeaponScript>(); // setting these up so that we can turn them into arrays then save the inventory as an array List <EnchantmentType> currentEnchants = new List <EnchantmentType>(); List <int> currentEnchantStrengths = new List <int>(); List <int> currentEnchantCosts = new List <int>(); List <int> currentEnchantPenalties = new List <int>(); foreach (EnchantmentScript enchant in currentWeapon.gameObject.GetComponents <EnchantmentScript>()) { currentEnchants.Add(enchant.EnchantType); currentEnchantStrengths.Add(enchant.Strength); currentEnchantCosts.Add(enchant.ManaCost); currentEnchantPenalties.Add(enchant.ManaPenalty); } WeaponData currentData = new WeaponData() { reactionCount = 0, weaponType = currentWeapon.WeaponCategory, weaponVariant = currentWeapon.WeaponVariation, enchants = currentEnchants.ToArray(), enchantmentStrengths = currentEnchantStrengths.ToArray(), enchantmentCosts = currentEnchantCosts.ToArray(), enchantmentPenalties = currentEnchantPenalties.ToArray() }; droppedWeaponData.Add(currentData); droppedWeaponPositions.Add(ConvertToArray(droppedItem.transform.position)); droppedWeaponRotations.Add(ConvertToArray(droppedItem.transform.rotation.eulerAngles)); } } // converting the lists we filled to something we can use float[][] consumablePos = new float[droppedConsumablePositions.Count][]; for (int i = 0; i < consumablePos.Length; i++) { consumablePos[i] = (float[])droppedConsumablePositions[i]; } float[][] consumableRot = new float[droppedConsumableRotations.Count][]; for (int i = 0; i < consumableRot.Length; i++) { consumableRot[i] = (float[])droppedConsumableRotations[i]; } float[][] droppedWeaponPos = new float[droppedWeaponPositions.Count][]; for (int i = 0; i < droppedWeaponPos.Length; i++) { droppedWeaponPos[i] = (float[])droppedWeaponPositions[i]; } float[][] droppedWeaponRot = new float[droppedWeaponRotations.Count][]; for (int i = 0; i < droppedWeaponRot.Length; i++) { droppedWeaponRot[i] = (float[])droppedWeaponRotations[i]; } // putting it all into contents so that we can reload it later. myContents = new LevelContents() { AllMonsters = MonsterList.ToArray(), myConsumableLoot = droppedConsumableInfo.ToArray(), myConsumablePositions = consumablePos, myConsumableRotations = consumableRot, myWeaponLoot = droppedWeaponData.ToArray(), myWeaponLootPositions = droppedWeaponPos, myWeaponRotations = droppedWeaponRot, eF = exitFound, map = map }; } }
void LevelFromContents(LevelContents levelContents) { // populate the monsters foreach (MonsterData monsterData in levelContents.AllMonsters) { GameObject CurrentMonster = Instantiate(Resources.Load("EnemyPrefabs/TargetDummy" /* + monsterData.MonsterType.ToString()*/) as GameObject); CurrentMonster.transform.position = ConvertToVector(monsterData.position); CurrentMonster.transform.rotation = Quaternion.Euler(ConvertToVector(monsterData.rotation)); MonsterScript monsterScript = CurrentMonster.GetComponent <MonsterScript>(); monsterScript.MonsterLevel = monsterData.Level; // not sure that we will be using monster buffs, if we have time we will fix this /*for (int i = 0; i < monsterData.CurrentBuffs.Length; i++) * { * Type mytype = Type.GetType(monsterData.CurrentBuffs[i].ToString()); * * CurrentMonster.AddComponent(mytype); * * BuffScript[] BuffArray = CurrentMonster.GetComponents<BuffScript>(); * * BuffArray[i].BuffStrength = monsterData.BuffStrengths[i]; * }*/ } if (levelContents.myConsumableLoot.Length > 0) { for (int i = 0; i < levelContents.myConsumableLoot.Length; i++) { GameObject currentConsumable = Instantiate(Resources.Load("DroppedConsumables/" + levelContents.myConsumableLoot[i].consumableType.ToString()) as GameObject); currentConsumable.transform.position = ConvertToVector(levelContents.myConsumablePositions[i]); currentConsumable.transform.rotation = Quaternion.Euler(ConvertToVector(levelContents.myConsumableRotations[i])); currentConsumable.GetComponent <DroppedConsumableScript>().amount = levelContents.myConsumableLoot[i].amountCarried; } } if (levelContents.myWeaponLoot.Length > 0) { for (int i = 0; i < levelContents.myWeaponLoot.Length; i++) { WeaponData currentWeaponData = levelContents.myWeaponLoot[i]; GameObject currentWeapon = Instantiate(Resources.Load("DroppedWeapons/" + currentWeaponData.weaponType.ToString() + "/" + currentWeaponData.weaponVariant) as GameObject); currentWeapon.transform.position = ConvertToVector(levelContents.myWeaponLootPositions[i]); currentWeapon.transform.rotation = Quaternion.Euler(ConvertToVector(levelContents.myWeaponRotations[i])); if (currentWeaponData.enchants != null) { for (i = 0; i < currentWeaponData.enchants.Length; i++) { EnchantmentType enchant = currentWeaponData.enchants[i]; Type mytype = Type.GetType(enchant.ToString() + "Script"); currentWeapon.AddComponent(mytype); EnchantmentScript[] myEnchants = currentWeapon.GetComponents <EnchantmentScript>(); myEnchants[i].Strength = currentWeaponData.enchantmentStrengths[i]; myEnchants[i].ManaPenalty = currentWeaponData.enchantmentPenalties[i]; myEnchants[i].ManaCost = currentWeaponData.enchantmentCosts[i]; } } } } map = levelContents.map; LocalExitScript.Exit.transform.position = map.end; LocalEntranceScript.Entrance.transform.position = map.start; if (DungeonScript.CurrentDungeon.CurrentLevelIndex == DungeonScript.CurrentDungeon.MaxLevelIndex) { LocalExitScript.Exit.SetActive(false); } else { LocalExitScript.Exit.SetActive(true); } }