Added inventory load, fixed inventory save, v1.3.0

This commit is contained in:
Nicola Sovic 2022-07-04 13:24:54 +02:00
parent 22137fffde
commit 349e023cbf
6 changed files with 193 additions and 39 deletions

View File

@ -22,7 +22,7 @@ namespace Assets.Scripts
sw.Close(); sw.Close();
} }
public static void loadGame(Player player, WorldGenerator worldGenerator) public static void loadGame(Player player, WorldGenerator worldGenerator, Inventory inventory)
{ {
if (hasSaveFile()) if (hasSaveFile())
{ {
@ -35,6 +35,7 @@ namespace Assets.Scripts
JObject json = JsonConvert.DeserializeObject<JObject>(jsonString); JObject json = JsonConvert.DeserializeObject<JObject>(jsonString);
player.loadPlayer(json["player"]); player.loadPlayer(json["player"]);
worldGenerator.loadWorld(json["world"]); worldGenerator.loadWorld(json["world"]);
inventory.loadInventory(json["inventory"]);
} }
} }

View File

@ -1,5 +1,7 @@
using Newtonsoft.Json.Linq;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using UnityEngine; using UnityEngine;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
using UnityEngine.UI; using UnityEngine.UI;
@ -33,15 +35,7 @@ namespace Assets.Scripts
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
statBoost = new Dictionary<string, int>(); createStatBoost();
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);
tooltip = GameObject.Find("TooltipHandler").GetComponent<TooltipHandler>(); tooltip = GameObject.Find("TooltipHandler").GetComponent<TooltipHandler>();
itemDisplay.transform.localScale = new Vector3(0,0,0); itemDisplay.transform.localScale = new Vector3(0,0,0);
changeCurrentBag(0); changeCurrentBag(0);
@ -54,6 +48,22 @@ namespace Assets.Scripts
checkEquipColors(); checkEquipColors();
} }
private void createStatBoost()
{
if (statBoost == null)
{
statBoost = new Dictionary<string, int>();
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) public void addItem(Item item)
{ {
if (item != null) if (item != null)
@ -217,18 +227,7 @@ namespace Assets.Scripts
public Dictionary<string, int> getEquipmentBonus() public Dictionary<string, int> getEquipmentBonus()
{ {
if (statBoost == null) createStatBoost();
{
statBoost = new Dictionary<string, int>();
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);
}
return statBoost; return statBoost;
} }
@ -307,5 +306,79 @@ namespace Assets.Scripts
result = result + "\r\n}"; result = result + "\r\n}";
return result; return result;
} }
public void loadInventory(JToken json)
{
loadEquipment(json["equipment"]);
var jsonData = JObject.Parse(json["bags"].ToString()).Children();
List<JToken> tokens = jsonData.Children().ToList();
int counter = 0;
foreach (JToken slot in tokens)
{
if (slot["bag1"].ToString() != "empty")
{
slots[counter].GetComponent<InventorySlot>().loadSlot(slot["bag1"], 0);
}
if (slot["bag2"].ToString() != "empty")
{
slots[counter].GetComponent<InventorySlot>().loadSlot(slot["bag2"], 1);
}
if (slot["bag3"].ToString() != "empty")
{
slots[counter].GetComponent<InventorySlot>().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<InventorySlot>().loadSlot(slot[slotname], -1);
createStatBoost();
calculateStatBoost(equip.GetComponent<InventorySlot>().getEquip().getAttributes(), true);
}
}
}
} }
} }

View File

@ -1,3 +1,4 @@
using Newtonsoft.Json.Linq;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
@ -275,5 +276,18 @@ namespace Assets.Scripts
} }
return result; return result;
} }
public void loadSlot(JToken json, int bag)
{
if (bag == -1)
{
equip = new Item(json);
}
else
{
items[bag] = new Item(json);
}
}
} }
} }

View File

@ -1,3 +1,5 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
@ -34,6 +36,60 @@ namespace Assets.Scripts
} }
calculateAttributes(luck, numberOfAttributes); calculateAttributes(luck, numberOfAttributes);
place = (ItemPlace)rand.Next(8); place = (ItemPlace)rand.Next(8);
editName();
setColor();
loadImage();
}
public Item(JToken json)
{
attributes = new Dictionary<string, int>();
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 = ""; string replacement = "";
if (place == ItemPlace.LEFTHAND) if (place == ItemPlace.LEFTHAND)
{ {
@ -90,12 +146,6 @@ namespace Assets.Scripts
} }
itemName = itemName.ToLower(); itemName = itemName.ToLower();
itemName = char.ToUpper(itemName[0]) + itemName.Substring(1); itemName = char.ToUpper(itemName[0]) + itemName.Substring(1);
loadImage();
}
public Item(string save)
{
} }
private void calculateRarity(int luck) private void calculateRarity(int luck)
@ -104,22 +154,37 @@ namespace Assets.Scripts
if (number + luck < 80) if (number + luck < 80)
{ {
rarity = ItemRarity.COMMON; rarity = ItemRarity.COMMON;
rarityColor = new Color32(0,255,20,255);
} }
else if (number + luck >= 80 && number + luck < 100) else if (number + luck >= 80 && number + luck < 100)
{ {
rarity = ItemRarity.RARE; rarity = ItemRarity.RARE;
rarityColor = new Color32(0,100,255, 255);
} }
else if (number + luck >= 100 && number + luck < 120) else if (number + luck >= 100 && number + luck < 120)
{ {
rarity = ItemRarity.EPIC; rarity = ItemRarity.EPIC;
rarityColor = new Color32(255,0,230, 255);
} }
else if (number + luck >= 120) else if (number + luck >= 120)
{ {
rarity = ItemRarity.LEGENDARY; rarity = ItemRarity.LEGENDARY;
}
}
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); rarityColor = new Color32(255, 230, 0, 255);
break;
} }
} }
@ -306,6 +371,7 @@ namespace Assets.Scripts
int counter = 0; int counter = 0;
result = result + FileHandler.generateJSON("rarity", "\"" + rarity + "\"") + ",\r\n"; result = result + FileHandler.generateJSON("rarity", "\"" + rarity + "\"") + ",\r\n";
result = result + FileHandler.generateJSON("place", "\"" + place + "\"") + ",\r\n"; result = result + FileHandler.generateJSON("place", "\"" + place + "\"") + ",\r\n";
result = result + FileHandler.generateJSON("itemName", "\"" + itemName + "\"") + ",\r\n";
foreach (string key in attributes.Keys) foreach (string key in attributes.Keys)
{ {
result = result + FileHandler.generateJSON(key, attributes[key]); result = result + FileHandler.generateJSON(key, attributes[key]);

View File

@ -516,7 +516,7 @@ namespace Assets.Scripts
} }
else else
{ {
FileHandler.loadGame(GameObject.Find("Player").GetComponent<Player>(), GameObject.Find("WorldGenerator").GetComponent<WorldGenerator>()); FileHandler.loadGame(GameObject.Find("Player").GetComponent<Player>(), GameObject.Find("WorldGenerator").GetComponent<WorldGenerator>(), GameObject.Find("Inventory").GetComponent<Inventory>());
hideOtherElements(introduction); hideOtherElements(introduction);
introduction.transform.localScale = new Vector3(0, 0, 0); introduction.transform.localScale = new Vector3(0, 0, 0);
tutorial.transform.localScale = new Vector3(0,0,0); tutorial.transform.localScale = new Vector3(0,0,0);

View File

@ -3,7 +3,7 @@
"playername": "Nicola", "playername": "Nicola",
"maxHealth": 110, "maxHealth": 110,
"maxSecondary": 10, "maxSecondary": 10,
"secondary": 15, "secondary": 10,
"health": 110, "health": 110,
"strength": 7, "strength": 7,
"dexterity": 5, "dexterity": 5,
@ -43,14 +43,14 @@
}, },
"bags": { "bags": {
"slot0": { "slot0": {
"bag1": "empty", "bag1": {
"bag2": "empty",
"bag3": {
"rarity": "COMMON", "rarity": "COMMON",
"place": "BOOTS", "place": "BOOTS",
"itemName": "Common boots of mana", "itemName": "Common boots of mana",
"MP": 12 "MP": 12
} },
"bag2": "empty",
"bag3": "empty"
}, },
"slot1": { "slot1": {
"bag1": "empty", "bag1": "empty",