2023-04-14 20:25:54 +02:00

386 lines
13 KiB
C#

using Newtonsoft.Json.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace Assets.Scripts
{
public class Inventory : MonoBehaviour
{
public GameObject head;
public GameObject chest;
public GameObject shoulders;
public GameObject feet;
public GameObject ring;
public GameObject amulet;
public GameObject leftHand;
public GameObject rightHand;
public GameObject dragImage;
public GameObject itemDisplay;
public GameObject[] bags;
public GameObject[] slots;
Dictionary<string, int> statBoost;
public int currentBag = -1;
GameObject startDrag;
TooltipHandler tooltip;
// Start is called before the first frame update
void Start()
{
createStatBoost();
tooltip = GameObject.Find("TooltipHandler").GetComponent<TooltipHandler>();
itemDisplay.transform.localScale = new Vector3(0,0,0);
changeCurrentBag(0);
}
// Update is called once per frame
void Update()
{
checkInventoryColors();
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)
{
if (item != null)
{
bool itemAdded = false;
for (int i = 0; i < bags.Length; i++)
{
for (int j = 0; j < slots.Length; j++)
{
if (slots[j].GetComponent<InventorySlot>().getItem(i) == null)
{
slots[j].GetComponent<InventorySlot>().setItem(item, i);
itemAdded = true;
slots[j].GetComponent<RawImage>().color = item.rarityColor;
slots[j].GetComponent<RawImage>().texture = item.image;
break;
}
}
if (itemAdded)
{
GameObject.Find("UIHandler").GetComponent<UIHandler>().showMessage("SUCCESS;You got an item!");
GameObject.Find("QuestLog").GetComponent<QuestLog>().updateQuests("collect", item, 1);
SteamWorksHandler.getItemAchievement(item);
break;
}
}
if (!itemAdded)
{
GameObject.Find("UIHandler").GetComponent<UIHandler>().showMessage("ERROR;No inventory space left.");
}
}
}
public void changeCurrentBag(int index)
{
if (currentBag != -1)
{
bags[currentBag].transform.Find("Selected").GetComponent<RawImage>().color = Color.white;
}
currentBag = index;
bags[currentBag].transform.Find("Selected").GetComponent<RawImage>().color = Color.cyan;
checkInventoryColors();
foreach(GameObject slot in slots)
{
slot.GetComponent<InventorySlot>().updateCurrentBag(currentBag);
}
}
private void checkInventoryColors()
{
Item item;
for (int i = 0; i < slots.Length; i++)
{
item = slots[i].GetComponent<InventorySlot>().getItem(currentBag);
if (item != null)
{
slots[i].GetComponent<RawImage>().color = item.rarityColor;
slots[i].GetComponent<RawImage>().texture = item.image;
}
else
{
slots[i].GetComponent<RawImage>().color = Color.white;
slots[i].GetComponent<RawImage>().texture = null;
}
}
}
private void checkEquipColors()
{
GameObject slot = head;
Item item;
for (int i = 0; i < 8; i++)
{
switch (i)
{
case 0:
slot = head;
break;
case 1:
slot = rightHand;
break;
case 2:
slot = leftHand;
break;
case 3:
slot = amulet;
break;
case 4:
slot = feet;
break;
case 5:
slot = shoulders;
break;
case 6:
slot = chest;
break;
case 7:
slot = ring;
break;
}
item = slot.GetComponent<InventorySlot>().getEquip();
if (item != null)
{
slot.GetComponent<RawImage>().color = item.rarityColor;
slot.GetComponent<RawImage>().texture = item.image;
}
else
{
slot.GetComponent<RawImage>().color = Color.white;
}
}
}
public void setDrag(GameObject slot)
{
startDrag = slot;
}
public GameObject getDrag()
{
return startDrag;
}
/*public Item getEquip(ItemPlace place)
{
switch (place)
{
case ItemPlace.LEFTHAND:
return leftHand.GetComponent<InventorySlot>().getEquip();
case ItemPlace.RIGHTHAND:
return rightHand.GetComponent<InventorySlot>().getEquip();
case ItemPlace.HELMET:
return head.GetComponent<InventorySlot>().getEquip();
case ItemPlace.BOOTS:
return feet.GetComponent<InventorySlot>().getEquip();
case ItemPlace.SHOULDER:
return shoulders.GetComponent<InventorySlot>().getEquip();
case ItemPlace.AMULET:
return amulet.GetComponent<InventorySlot>().getEquip();
case ItemPlace.RING:
return ring.GetComponent<InventorySlot>().getEquip();
case ItemPlace.ARMOR:
return chest.GetComponent<InventorySlot>().getEquip();
default:
return null;
}
}*/
public void calculateStatBoost(Dictionary<string, int> attributes, bool isAddition)
{
foreach (string key in attributes.Keys)
{
if (isAddition)
{
statBoost[key] = statBoost[key] + attributes[key];
}
else
{
statBoost[key] = statBoost[key] - attributes[key];
}
}
}
public Dictionary<string, int> getEquipmentBonus()
{
createStatBoost();
return statBoost;
}
public string saveGame()
{
string result = "";
int counter = 0;
GameObject equip = head;
string slotname = "";
result = result + "\"equipment\": {\r\n";
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 (equip.GetComponent<InventorySlot>().getEquip() != null)
{
result = result + "\""+slotname+"\": {\r\n";
result = result + equip.GetComponent<InventorySlot>().saveGame();
result = result + "\r\n}";
}
else
{
result = result + "\"" + slotname + "\": \"empty\"";
}
if (i != 7)
{
result = result + ",\r\n";
}
}
result = result + "\r\n},\r\n";
result = result + "\"bags\": {\r\n";
foreach (GameObject slot in slots)
{
result = result + "\"slot" + counter + "\": {\r\n";
result = result + slot.GetComponent<InventorySlot>().saveGame();
result = result + "\r\n}";
if (counter < slots.Length - 1)
{
result = result + ",\r\n";
}
counter++;
}
result = result + "\r\n}";
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);
}
}
}
}
}