/// <summary> /// Creates a *new* InvItem, using only InvNames. Anything other than a potion has a BattleStats /// included, and thus is also an InvEqItem. If the item is to remain in the inventory even when /// at zero quantity, pass in persist=true. This occurs for the Shop inventory when out-of-stock. /// /// This method is necessary, otherwise all inventories will reference only one of each kind of /// item, stored here. /// </summary> /// <param name="name">InvNames name of the item to be created.</param> /// <param name="persist">Whether the InvItem will persist in the Inventory at Quantity = 0.</param> /// <returns></returns> public static InvItem MakeNewInvItem(InvNames name, bool persist = false) { // Potions do not require BattleStats, and are not InvEqItems. if (Data[name].Type == InvType.Potion) { InvItem orig = InvData.Data[name]; InvItem newItem = new InvItem(name, orig.FullName, orig.Description, orig.Sprite, orig.ID, orig.Price, orig.Quantity, orig.Type, orig.Subtype, orig.Slot, orig.Rank, persist); return(newItem); } else { // InvEqItem includes tomes and equipment. InvEqItem orig = (InvEqItem)InvData.Data[name]; InvEqItem newItem = new InvEqItem(name, orig.FullName, orig.Description, orig.Sprite, orig.ID, orig.Price, orig.Quantity, orig.Type, orig.Subtype, orig.Slot, orig.Rank, orig.BStats, persist); return(newItem); } }
/// <summary> /// Reads configuration data from a file. If the file read fails, the constructor contains /// default values for each kind of configuration data /// REFACTOR: Break this into three different constructors /// </summary> public ConfigData(ConfigDataType type) { // Read and save configuration data from file StreamReader input = null; // Three kinds of ConfigData switch (type) { case ConfigDataType.EnemyData: #region EnemyData try { // Create stream reader input input = File.OpenText(Path.Combine( Application.streamingAssetsPath, EnemyDataFileName)); // Populate StatNames from header row string currentLine = input.ReadLine(); string[] names = currentLine.Split(','); BattleStatNames[] statHeader = new BattleStatNames[names.Length]; // Validate the header row before importing BattleStats validate = new BattleStats(); for (int i = 1; i < names.Length; i++) { statHeader[i] = (BattleStatNames)Enum.Parse(typeof(BattleStatNames), names[i]); if (!validate.ValidateOrder(i - 1, statHeader[i])) { errorMessage = "Headers do not match BattleStats.\nUsing default settings."; Debug.Log(errorMessage); SetEnemyStatDataDefaultValues(); break; } } // Only procede forward if there is no error with the headers if (errorMessage == null) { // Populate values for enemyData currentLine = input.ReadLine(); while (currentLine != null) { // Parse current line into name and stat values string[] tokens = currentLine.Split(','); EnemyName enemyName = (EnemyName)Enum.Parse( typeof(EnemyName), tokens[0]); int[] intTokens = new int[tokens.Length - 1]; for (int i = 0; i < tokens.Length - 1; i++) { int bStat; intTokens[i] = int.TryParse(tokens[i + 1], out bStat) ? bStat : 0; } // import the array of ints into a new BattleStats BattleStats battleStats = new BattleStats(); battleStats.Import(intTokens); // add to enemyStatData enemyStatData.Add(enemyName, battleStats); // queue next line in csv currentLine = input.ReadLine(); } } } catch (Exception e) { // send the error to the console for debugging Debug.Log(e); errorMessage = "Problem loading file. \nUsing default settings.\n"; Debug.Log(errorMessage); SetEnemyStatDataDefaultValues(); } finally { // close files that were opened if (input != null) { input.Close(); } } break; // end of EnemyData #endregion EnemyData case ConfigDataType.InvData: #region InvData try { // create stream reader input input = File.OpenText(Path.Combine( Application.streamingAssetsPath, InvDataFileName)); // populate StatNames from header row string currentLine = input.ReadLine(); string[] headings = currentLine.Split(','); BattleStatNames[] statHeader = new BattleStatNames[headings.Length]; // Validate the header row before importing // 0 name, 1 fullname, 2 description, 3 id, 4 price, 5 type, 6 subtype, 7 slot, // 8 rank, 9+ Battlestats BattleStats validate = new BattleStats(); for (int i = 9; i < headings.Length; i++) { statHeader[i] = (BattleStatNames)Enum.Parse(typeof(BattleStatNames), headings[i]); if (!validate.ValidateOrder(i - 9, statHeader[i])) { errorMessage = "Headers do not match BattleStats.\nUsing default settings."; Debug.Log(errorMessage); SetInvDataDefaultValues(); break; } } // only procede forward if there is no error with the headers if (errorMessage == null) { // populate values for enemyData currentLine = input.ReadLine(); while (currentLine != null) { // parse current line into name and stat values // 0 name, 1 fullname, 2 description, 3 id, 4 price, 5 type, 6 subtype, 7 slot, 8 rank // 0 name string[] tokens = currentLine.Split(','); InvNames invName = (InvNames)Enum.Parse( typeof(InvNames), tokens[0]); // 1 fullname string fullName = tokens[1]; // 2 description string description = tokens[2]; // 3 id int id = int.Parse(tokens[3]); // 4 price int price = int.Parse(tokens[4]); // 5 type InvType invType = (InvType)Enum.Parse( typeof(InvType), tokens[5]); // 6 subtype InvSubtype invSubtype = (InvSubtype)Enum.Parse( typeof(InvSubtype), tokens[6]); Sprite sprite = Resources.Load <Sprite>(@"Sprites\InvItems\" + invType.ToString() + @"\" + invSubtype.ToString() + @"\" + invName.ToString()); // 7 slot EquipSlots slot = (EquipSlots)Enum.Parse( typeof(EquipSlots), tokens[7]); // 8 rank int rank = int.Parse(tokens[8]); // BattleStats is only included for InvEqItems (equipment and tomes) // This is an InvEqItem if (invType == InvType.Tome || invType == InvType.Weapon || invType == InvType.Armor) { // 9+ BattleStats int[] intTokens = new int[tokens.Length - 9]; for (int i = 0; i < tokens.Length - 9; i++) { int bStat; intTokens[i] = int.TryParse(tokens[i + 9], out bStat) ? bStat : 0; } // import the array of ints into a new BattleStats BattleStats battleStats = new BattleStats(); battleStats.Import(intTokens); // create a new InvEqItem InvEqItem item = new InvEqItem(invName, fullName, description, sprite, id, price, 0, invType, invSubtype, slot, rank, battleStats); // add to invStatData invStatData.Add(invName, item); } // else it is a potion, so add a regular InvItem else { // create a new InvEqItem InvItem item = new InvItem(invName, fullName, description, sprite, id, price, 0, invType, invSubtype, slot, rank); // add to invStatData invStatData.Add(invName, item); } // queue next line in csv currentLine = input.ReadLine(); } } } catch (Exception e) { // send the error to the console for debugging Debug.Log(e); errorMessage = "Problem loading file. \nUsing default settings.\n"; Debug.Log(errorMessage); SetInvDataDefaultValues(); } finally { // close files that were opened if (input != null) { input.Close(); } } break; #endregion InvData case ConfigDataType.AbilityData: #region AbilityData try { // create stream reader input input = File.OpenText(Path.Combine( Application.streamingAssetsPath, AbilityDataFileName)); // populate StatNames from header row string currentLine = input.ReadLine(); string[] headings = currentLine.Split(','); // ignoring the header row // populate values for abilityData currentLine = input.ReadLine(); while (currentLine != null) { // parse current line into name and stat values // 0 name, 1 isPhysical, 2 mp, 3 noReduction, 4 modifier, // 5 noMiss, 6 hitOverride, 7 noCrit // 0 name string[] tokens = currentLine.Split(','); BattleMode name = (BattleMode)Enum.Parse( typeof(BattleMode), tokens[0]); // 1 isPhyical bool isPhysical = bool.Parse(tokens[1].ToLower()); // 2 mp int?mp = null; if (int.TryParse(tokens[2], out int num)) { mp = num; } else { mp = null; } // 3 noReduction bool noReduction = bool.Parse(tokens[3].ToLower()); // 4 modifier float modifier = float.Parse(tokens[4]); // 5 noMiss bool noMiss = bool.Parse(tokens[5].ToLower()); // 6 hitOverride int?hitOverride = null; if (int.TryParse(tokens[6], out int num2)) { hitOverride = num2; } else { hitOverride = null; } // 7 noCrit bool noCrit = bool.Parse(tokens[7].ToLower()); // create a new battleAbility BattleAbility ability = new BattleAbility(name, isPhysical, mp, noReduction, modifier, noMiss, hitOverride, noCrit); // add to invStatData abilityStatData.Add(name, ability); // queue next line in csv currentLine = input.ReadLine(); } } catch (Exception e) { // send the error to the console for debugging Debug.Log(e); errorMessage = "Problem loading file. \nUsing default settings.\n"; Debug.Log(errorMessage); SetAbilityDataDefaultValues(); } finally { // close files that were opened if (input != null) { input.Close(); } } break; #endregion AbilityData default: break; } }
// Refresh each of the 6 slots of equipment public void RefreshEquipped() { // Retrieve hero's personal inventory of equipped items equipment = BattleLoader.Party.Hero[ID].Equipment; // If the slot property returns null, nothing is equipped and the // display should be left blank, as it is at instantiation // If a slot property returns a non-null InvNames field, retrieve the // InvEqItem stored in the hero's inventory and set the display if (equipment.Weapon != null) { InvEqItem weapon = (InvEqItem)(equipment.GetItem((InvNames)equipment.Weapon)); weaponText.text = weapon.FullName; weaponStatsText.text = CreateSummary(weapon.BStats); weaponImage.sprite = weapon.Sprite; weaponImage.enabled = true; } if (equipment.Helm != null) { InvEqItem helm = (InvEqItem)equipment.GetItem((InvNames)equipment.Helm); helmText.text = helm.FullName; helmStatsText.text = CreateSummary(helm.BStats); helmImage.sprite = helm.Sprite; helmImage.enabled = true; } if (equipment.Armor != null) { InvEqItem armor = (InvEqItem)equipment.GetItem((InvNames)equipment.Armor); armorText.text = armor.FullName; armorStatsText.text = CreateSummary(armor.BStats); armorImage.sprite = armor.Sprite; armorImage.enabled = true; } if (equipment.Gloves != null) { InvEqItem gloves = (InvEqItem)equipment.GetItem((InvNames)equipment.Gloves); glovesText.text = gloves.FullName; glovesStatsText.text = CreateSummary(gloves.BStats); glovesImage.sprite = gloves.Sprite; glovesImage.enabled = true; } if (equipment.Belt != null) { InvEqItem belt = (InvEqItem)equipment.GetItem((InvNames)equipment.Belt); beltText.text = belt.FullName; beltStatsText.text = CreateSummary(belt.BStats); beltImage.sprite = belt.Sprite; beltImage.enabled = true; } if (equipment.Boots != null) { InvEqItem boots = (InvEqItem)equipment.GetItem((InvNames)equipment.Boots); bootsText.text = boots.FullName; bootsStatsText.text = CreateSummary(boots.BStats); bootsImage.sprite = boots.Sprite; bootsImage.enabled = true; } // Refresh HP/MP, from PartyMenuPanel RefreshPanel(); }
// Called by MakeSelection(). Produces a string that compares a current item with the selected item string CompareEquipped(int index) { // For each stat, store the change in value between BattleStats Dictionary <BattleStatNames, int> comparison = new Dictionary <BattleStatNames, int>(); // Store the selected InvEqItem from the partyStash InvEqItem selectedItem = (InvEqItem)partyStash.Contents[(int)index]; // Retrieve the currently equipped InvEqItem from the heroStash InvEqItem equippedItem = null; foreach (InvEqItem item in heroStash.Contents) { if (item.Slot == selectedItem.Slot) { equippedItem = item; break; } } // Retrieve an array of ints from the selected item and the current item (if any) int[] selectedValues = selectedItem.BStats.Export(); int[] equippedValues; if (equippedItem == null) { equippedValues = new int[11] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; } else { equippedValues = equippedItem.BStats.Export(); } // For each stat in BattleStats, store the difference between the two sets for (int i = 0; i <= 10; i++) { int value = selectedValues[i] - equippedValues[i]; if (value != 0) { comparison.Add((BattleStatNames)i, value); } } // Build a comparison string string output = ""; string format = "+#;-#;(0)"; foreach (KeyValuePair <BattleStatNames, int> pair in comparison) { switch (pair.Key) { case BattleStatNames.BaseHPMax: output += "HP"; break; case BattleStatNames.BaseMPMax: output += "MP"; break; case BattleStatNames.BaseHit: output += "Hit"; break; case BattleStatNames.BaseEvade: output += "Eva"; break; case BattleStatNames.CritChance: output += "Crit"; break; default: output += pair.Key.ToString().Substring(0, 3); break; } output += ": " + pair.Value.ToString(format) + "\n"; } // Return the comparison string return(output); }