From 349e023cbf067b0d0be077f90fcd80aa6fb03616 Mon Sep 17 00:00:00 2001 From: Nicola Sovic Date: Mon, 4 Jul 2022 13:24:54 +0200 Subject: [PATCH] Added inventory load, fixed inventory save, v1.3.0 --- Assets/Scripts/FileHandler.cs | 3 +- Assets/Scripts/Inventory.cs | 115 ++++++++++++++++++++++++++------ Assets/Scripts/InventorySlot.cs | 14 ++++ Assets/Scripts/Item.cs | 88 +++++++++++++++++++++--- Assets/Scripts/UIHandler.cs | 2 +- save.json | 10 +-- 6 files changed, 193 insertions(+), 39 deletions(-) diff --git a/Assets/Scripts/FileHandler.cs b/Assets/Scripts/FileHandler.cs index c1ff10c..8f52522 100644 --- a/Assets/Scripts/FileHandler.cs +++ b/Assets/Scripts/FileHandler.cs @@ -22,7 +22,7 @@ namespace Assets.Scripts sw.Close(); } - public static void loadGame(Player player, WorldGenerator worldGenerator) + public static void loadGame(Player player, WorldGenerator worldGenerator, Inventory inventory) { if (hasSaveFile()) { @@ -35,6 +35,7 @@ namespace Assets.Scripts JObject json = JsonConvert.DeserializeObject(jsonString); player.loadPlayer(json["player"]); worldGenerator.loadWorld(json["world"]); + inventory.loadInventory(json["inventory"]); } } diff --git a/Assets/Scripts/Inventory.cs b/Assets/Scripts/Inventory.cs index 4e8f15c..543722a 100644 --- a/Assets/Scripts/Inventory.cs +++ b/Assets/Scripts/Inventory.cs @@ -1,5 +1,7 @@ +using Newtonsoft.Json.Linq; using System.Collections; using System.Collections.Generic; +using System.Linq; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; @@ -33,15 +35,7 @@ namespace Assets.Scripts // Start is called before the first frame update void Start() { - statBoost = new Dictionary(); - statBoost.Add("HP", 0); - statBoost.Add("MP", 0); - statBoost.Add("HPR", 0); - statBoost.Add("MPR", 0); - statBoost.Add("STR", 0); - statBoost.Add("DEX", 0); - statBoost.Add("INT", 0); - statBoost.Add("LCK", 0); + createStatBoost(); tooltip = GameObject.Find("TooltipHandler").GetComponent(); itemDisplay.transform.localScale = new Vector3(0,0,0); changeCurrentBag(0); @@ -54,6 +48,22 @@ namespace Assets.Scripts checkEquipColors(); } + private void createStatBoost() + { + if (statBoost == null) + { + statBoost = new Dictionary(); + statBoost.Add("HP", 0); + statBoost.Add("MP", 0); + statBoost.Add("HPR", 0); + statBoost.Add("MPR", 0); + statBoost.Add("STR", 0); + statBoost.Add("DEX", 0); + statBoost.Add("INT", 0); + statBoost.Add("LCK", 0); + } + } + public void addItem(Item item) { if (item != null) @@ -217,18 +227,7 @@ namespace Assets.Scripts public Dictionary getEquipmentBonus() { - if (statBoost == null) - { - statBoost = new Dictionary(); - statBoost.Add("HP", 0); - statBoost.Add("MP", 0); - statBoost.Add("HPR", 0); - statBoost.Add("MPR", 0); - statBoost.Add("STR", 0); - statBoost.Add("DEX", 0); - statBoost.Add("INT", 0); - statBoost.Add("LCK", 0); - } + createStatBoost(); return statBoost; } @@ -307,5 +306,79 @@ namespace Assets.Scripts result = result + "\r\n}"; return result; } + + public void loadInventory(JToken json) + { + loadEquipment(json["equipment"]); + var jsonData = JObject.Parse(json["bags"].ToString()).Children(); + List tokens = jsonData.Children().ToList(); + int counter = 0; + foreach (JToken slot in tokens) + { + if (slot["bag1"].ToString() != "empty") + { + slots[counter].GetComponent().loadSlot(slot["bag1"], 0); + } + if (slot["bag2"].ToString() != "empty") + { + slots[counter].GetComponent().loadSlot(slot["bag2"], 1); + } + if (slot["bag3"].ToString() != "empty") + { + slots[counter].GetComponent().loadSlot(slot["bag3"], 2); + } + counter++; + } + } + + private void loadEquipment(JToken slot) + { + GameObject equip = head; + string slotname = ""; + for (int i = 0; i < 8; i++) + { + switch (i) + { + case 0: + equip = head; + slotname = "head"; + break; + case 1: + equip = rightHand; + slotname = "rightHand"; + break; + case 2: + equip = leftHand; + slotname = "leftHand"; + break; + case 3: + equip = amulet; + slotname = "amulet"; + break; + case 4: + equip = feet; + slotname = "feet"; + break; + case 5: + equip = shoulders; + slotname = "shoulders"; + break; + case 6: + equip = chest; + slotname = "chest"; + break; + case 7: + equip = ring; + slotname = "ring"; + break; + } + if (slot[slotname].ToString() != "empty") + { + equip.GetComponent().loadSlot(slot[slotname], -1); + createStatBoost(); + calculateStatBoost(equip.GetComponent().getEquip().getAttributes(), true); + } + } + } } } \ No newline at end of file diff --git a/Assets/Scripts/InventorySlot.cs b/Assets/Scripts/InventorySlot.cs index 15b2948..f028e54 100644 --- a/Assets/Scripts/InventorySlot.cs +++ b/Assets/Scripts/InventorySlot.cs @@ -1,3 +1,4 @@ +using Newtonsoft.Json.Linq; using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -275,5 +276,18 @@ namespace Assets.Scripts } return result; } + + public void loadSlot(JToken json, int bag) + { + if (bag == -1) + { + equip = new Item(json); + } + else + { + items[bag] = new Item(json); + } + + } } } \ No newline at end of file diff --git a/Assets/Scripts/Item.cs b/Assets/Scripts/Item.cs index c1d287f..9774eff 100644 --- a/Assets/Scripts/Item.cs +++ b/Assets/Scripts/Item.cs @@ -1,3 +1,5 @@ +using Newtonsoft.Json.Linq; +using System; using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -34,6 +36,60 @@ namespace Assets.Scripts } calculateAttributes(luck, numberOfAttributes); place = (ItemPlace)rand.Next(8); + editName(); + setColor(); + loadImage(); + } + + public Item(JToken json) + { + attributes = new Dictionary(); + rarity = (ItemRarity)Enum.Parse(typeof(ItemRarity), json["rarity"].ToString()); + place = (ItemPlace)Enum.Parse(typeof(ItemPlace), json["place"].ToString()); + itemName = json["itemName"].ToString(); + loadAttributes(json); + setColor(); + loadImage(); + } + + private void loadAttributes(JToken json) + { + if (json["MPR"] != null) + { + attributes.Add("MPR", int.Parse(json["MPR"].ToString())); + } + if (json["HPR"] != null) + { + attributes.Add("HPR", int.Parse(json["HPR"].ToString())); + } + if (json["MP"] != null) + { + attributes.Add("MP", int.Parse(json["MP"].ToString())); + } + if (json["HP"] != null) + { + attributes.Add("HP", int.Parse(json["HP"].ToString())); + } + if (json["STR"] != null) + { + attributes.Add("STR", int.Parse(json["STR"].ToString())); + } + if (json["DEX"] != null) + { + attributes.Add("DEX", int.Parse(json["DEX"].ToString())); + } + if (json["INT"] != null) + { + attributes.Add("INT", int.Parse(json["INT"].ToString())); + } + if (json["LCK"] != null) + { + attributes.Add("LCK", int.Parse(json["LCK"].ToString())); + } + } + + private void editName() + { string replacement = ""; if (place == ItemPlace.LEFTHAND) { @@ -75,7 +131,7 @@ namespace Assets.Scripts { itemName = rarity.ToString() + " " + place.ToString() + " of " + itemName; } - + } else { @@ -90,12 +146,6 @@ namespace Assets.Scripts } itemName = itemName.ToLower(); itemName = char.ToUpper(itemName[0]) + itemName.Substring(1); - loadImage(); - } - - public Item(string save) - { - } private void calculateRarity(int luck) @@ -104,22 +154,37 @@ namespace Assets.Scripts if (number + luck < 80) { rarity = ItemRarity.COMMON; - rarityColor = new Color32(0,255,20,255); } else if (number + luck >= 80 && number + luck < 100) { rarity = ItemRarity.RARE; - rarityColor = new Color32(0,100,255, 255); } else if (number + luck >= 100 && number + luck < 120) { rarity = ItemRarity.EPIC; - rarityColor = new Color32(255,0,230, 255); } else if (number + luck >= 120) { rarity = ItemRarity.LEGENDARY; - rarityColor = new Color32(255,230,0, 255); + } + } + + private void setColor() + { + switch (rarity) + { + case ItemRarity.COMMON: + rarityColor = new Color32(0, 255, 20, 255); + break; + case ItemRarity.RARE: + rarityColor = new Color32(0, 100, 255, 255); + break; + case ItemRarity.EPIC: + rarityColor = new Color32(255, 0, 230, 255); + break; + case ItemRarity.LEGENDARY: + rarityColor = new Color32(255, 230, 0, 255); + break; } } @@ -306,6 +371,7 @@ namespace Assets.Scripts int counter = 0; result = result + FileHandler.generateJSON("rarity", "\"" + rarity + "\"") + ",\r\n"; result = result + FileHandler.generateJSON("place", "\"" + place + "\"") + ",\r\n"; + result = result + FileHandler.generateJSON("itemName", "\"" + itemName + "\"") + ",\r\n"; foreach (string key in attributes.Keys) { result = result + FileHandler.generateJSON(key, attributes[key]); diff --git a/Assets/Scripts/UIHandler.cs b/Assets/Scripts/UIHandler.cs index 1d1613f..9a991bf 100644 --- a/Assets/Scripts/UIHandler.cs +++ b/Assets/Scripts/UIHandler.cs @@ -516,7 +516,7 @@ namespace Assets.Scripts } else { - FileHandler.loadGame(GameObject.Find("Player").GetComponent(), GameObject.Find("WorldGenerator").GetComponent()); + FileHandler.loadGame(GameObject.Find("Player").GetComponent(), GameObject.Find("WorldGenerator").GetComponent(), GameObject.Find("Inventory").GetComponent()); hideOtherElements(introduction); introduction.transform.localScale = new Vector3(0, 0, 0); tutorial.transform.localScale = new Vector3(0,0,0); diff --git a/save.json b/save.json index 391e8f8..7f71d1f 100644 --- a/save.json +++ b/save.json @@ -3,7 +3,7 @@ "playername": "Nicola", "maxHealth": 110, "maxSecondary": 10, -"secondary": 15, +"secondary": 10, "health": 110, "strength": 7, "dexterity": 5, @@ -43,14 +43,14 @@ }, "bags": { "slot0": { -"bag1": "empty", -"bag2": "empty", -"bag3": { +"bag1": { "rarity": "COMMON", "place": "BOOTS", "itemName": "Common boots of mana", "MP": 12 -} +}, +"bag2": "empty", +"bag3": "empty" }, "slot1": { "bag1": "empty",