Added new item generation, v1.4.0

This commit is contained in:
Nicola Sovic
2022-07-09 18:38:54 +02:00
parent b3af7501a5
commit 8dbc177dcb
8 changed files with 268 additions and 145 deletions

View File

@@ -0,0 +1,21 @@
using Newtonsoft.Json.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Assets.Scripts
{
public class Book : Item
{
public Book(int luck) : base(luck)
{
}
public Book(JToken json) : base(json)
{
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e8249a768f68d4447869491bc0a86fa6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,411 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Assets.Scripts
{
public class Equipment : Item
{
ItemPlace place;
Dictionary<string, int> attributes;
public Equipment(int luck) : base(luck)
{
attributes = new Dictionary<string, int>();
int numberOfAttributes = 1;
if (rarity > ItemRarity.COMMON)
{
numberOfAttributes = 2;
}
if (rarity > ItemRarity.RARE)
{
numberOfAttributes = 3;
}
if (rarity == ItemRarity.LEGENDARY)
{
numberOfAttributes = 0;
}
calculateAttributes(luck, numberOfAttributes);
place = (ItemPlace)rand.Next(8);
editName();
loadImage();
}
public Equipment(JToken json) : base(json)
{
attributes = new Dictionary<string, int>();
place = (ItemPlace)Enum.Parse(typeof(ItemPlace), json["place"].ToString());
loadAttributes(json);
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)
{
switch (GameObject.Find("Player").GetComponent<Player>().getClass().classname)
{
case "Warrior":
replacement = "greatshield";
break;
case "Thief":
replacement = "shield";
break;
case "Mage":
replacement = "orb";
break;
}
}
else if (place == ItemPlace.RIGHTHAND)
{
switch (GameObject.Find("Player").GetComponent<Player>().getClass().classname)
{
case "Warrior":
replacement = "sword";
break;
case "Thief":
replacement = "dagger";
break;
case "Mage":
replacement = "wand";
break;
}
}
if (itemName != null)
{
if (replacement.Length > 0)
{
itemName = rarity.ToString() + " " + replacement + " of " + itemName;
}
else
{
itemName = rarity.ToString() + " " + place.ToString() + " of " + itemName;
}
}
else
{
if (replacement.Length > 0)
{
itemName = rarity.ToString() + " " + replacement;
}
else
{
itemName = rarity.ToString() + " " + place.ToString();
}
}
itemName = itemName.ToLower();
itemName = char.ToUpper(itemName[0]) + itemName.Substring(1);
}
private void calculateAttributes(int luck, int numberOfAttributes)
{
if (numberOfAttributes != 0)
{
int[] indexes = new int[numberOfAttributes];
int index;
for (int i = 0; i < numberOfAttributes; i++)
{
index = calculateIndex(indexes);
switch (index)
{
case 1:
if (i == 0)
{
itemName = "luck";
}
attributes.Add("LCK", Mathf.RoundToInt((float)(3 - (2 * i) + rand.Next(luck) * 0.1)));
break;
case 2:
if (i == 0)
{
itemName = "intelligence";
}
attributes.Add("INT", Mathf.RoundToInt((float)(3 - (2 * i) + rand.Next(luck) * 0.1)));
break;
case 3:
if (i == 0)
{
itemName = "dexterity";
}
attributes.Add("DEX", Mathf.RoundToInt((float)(3 - (2 * i) + rand.Next(luck) * 0.1)));
break;
case 4:
if (i == 0)
{
itemName = "strength";
}
attributes.Add("STR", Mathf.RoundToInt((float)(3 - (2 * i) + rand.Next(luck) * 0.1)));
break;
case 5:
if (i == 0)
{
itemName = "health";
}
attributes.Add("HP", Mathf.RoundToInt((float)(10 - (2 * i) + rand.Next(luck) * 0.1)));
break;
case 6:
if (i == 0)
{
itemName = "mana";
}
attributes.Add("MP", Mathf.RoundToInt((float)(10 - (2 * i) + rand.Next(luck) * 0.1)));
break;
case 7:
if (i == 0)
{
itemName = "health regeneration";
}
attributes.Add("HPR", Mathf.RoundToInt((float)(3 - (2 * i) + rand.Next(luck) * 0.1)));
break;
case 8:
if (i == 0)
{
itemName = "mana regeneration";
}
attributes.Add("MPR", Mathf.RoundToInt((float)(3 - (2 * i) + rand.Next(luck) * 0.1)));
break;
}
indexes[i] = index;
}
}
else
{
int bonus = rand.Next(luck);
attributes.Add("MPR", Mathf.RoundToInt((float)(3 + bonus * 0.1)));
attributes.Add("HPR", Mathf.RoundToInt((float)(3 + bonus * 0.1)));
attributes.Add("MP", Mathf.RoundToInt((float)(3 + bonus * 0.1)));
attributes.Add("HP", Mathf.RoundToInt((float)(3 + bonus * 0.1)));
attributes.Add("STR", Mathf.RoundToInt((float)(3 + bonus * 0.1)));
attributes.Add("DEX", Mathf.RoundToInt((float)(3 + bonus * 0.1)));
attributes.Add("INT", Mathf.RoundToInt((float)(3 + bonus * 0.1)));
attributes.Add("LCK", Mathf.RoundToInt((float)(3 + bonus * 0.1)));
}
}
private int calculateIndex(int[] indexes)
{
int counter = 0;
int index = 0;
while (true)
{
index = rand.Next(8) + 1;
counter = 0;
for (int j = 0; j < indexes.Length; j++)
{
if (indexes[j] == index)
{
counter++;
break;
}
}
if (counter == 0)
{
break;
}
}
return index;
}
public ItemPlace getPlace()
{
return place;
}
public string getDisplayText()
{
string displayText = "";
displayText = displayText + itemName + "\r\n";
if (rarity == ItemRarity.LEGENDARY)
{
displayText = displayText + "All attributes: +" + attributes["STR"];
}
else
{
foreach (string key in attributes.Keys)
{
displayText = displayText + key + ": +" + attributes[key] + "\r\n";
}
}
return displayText;
}
override
protected void loadImage()
{
switch (place)
{
case ItemPlace.LEFTHAND:
image = Resources.Load<Texture>("Equipment/" + GameObject.Find("Player").GetComponent<Player>().getClass().classname + "/Inv_LeftHand");
break;
case ItemPlace.RIGHTHAND:
image = Resources.Load<Texture>("Equipment/" + GameObject.Find("Player").GetComponent<Player>().getClass().classname + "/Inv_RightHand");
break;
case ItemPlace.HELMET:
image = Resources.Load<Texture>("Equipment/Inv_Helmet");
break;
case ItemPlace.BOOTS:
image = Resources.Load<Texture>("Equipment/Inv_Boots");
break;
case ItemPlace.SHOULDER:
image = Resources.Load<Texture>("Equipment/Inv_Shoulders");
break;
case ItemPlace.AMULET:
image = Resources.Load<Texture>("Equipment/Inv_Amulet");
break;
case ItemPlace.RING:
image = Resources.Load<Texture>("Equipment/Inv_Ring");
break;
case ItemPlace.ARMOR:
image = Resources.Load<Texture>("Equipment/Inv_Chest");
break;
}
}
public Dictionary<string, int> getAttributes()
{
return attributes;
}
override
public string saveGame()
{
string result = "";
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]);
if (counter < attributes.Count - 1)
{
result = result + ",\r\n";
}
counter++;
}
return result;
}
override
public string getInformation()
{
string text = "Stats:\r\n";
if (rarity == ItemRarity.LEGENDARY)
{
text = "All stats: +" + attributes["STR"];
}
else
{
foreach (string key in attributes.Keys)
{
text = text + key + ": +" + attributes[key] + "\r\n";
}
}
return text;
}
override
public void move(InventorySlot startDrag, Inventory inventory, InventorySlot endDrag)
{
bool isSwap = false;
if (endDrag.place != place && endDrag.place != ItemPlace.BAG)
{
return;
}
if (endDrag.place == ItemPlace.BAG)
{
Item endItem = endDrag.getItem(endDrag.getCurrentBag());
if (endItem != null)
{
if (((Equipment)endItem).getPlace() != startDrag.place)
{
return;
}
if (startDrag.place == ItemPlace.BAG)
{
startDrag.setItem(endItem, endDrag.getCurrentBag());
}
else
{
startDrag.setEquip((Equipment)endItem);
inventory.calculateStatBoost(attributes, false);
inventory.calculateStatBoost(((Equipment)endItem).getAttributes(), true);
}
isSwap = true;
}
endDrag.setItem(this, endDrag.getCurrentBag());
}
else
{
if (endDrag.getEquip() != null)
{
if (startDrag.place == ItemPlace.BAG)
{
startDrag.setItem(endDrag.getEquip(), startDrag.getCurrentBag());
isSwap = true;
inventory.calculateStatBoost(endDrag.getEquip().getAttributes(), false);
}
else
{
return;
}
}
inventory.calculateStatBoost(getAttributes(), true);
endDrag.setEquip(this);
}
if (!isSwap)
{
if (startDrag.place != ItemPlace.BAG)
{
inventory.calculateStatBoost(startDrag.getEquip().getAttributes(), false);
startDrag.removeEquip();
}
else
{
startDrag.removeItem();
}
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 55ead727b6b732f4da13cd6ca6dec7a0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,123 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Assets.Scripts
{
public class Item
{
protected System.Random rand = new System.Random();
protected string itemName;
protected ItemRarity rarity;
public Texture image;
public Color32 rarityColor;
public Item(int luck)
{
luck = luck + GameObject.Find("Inventory").GetComponent<Inventory>().getEquipmentBonus()["LCK"];
calculateRarity(luck);
setColor();
loadImage();
}
public Item(JToken json)
{
rarity = (ItemRarity)Enum.Parse(typeof(ItemRarity), json["rarity"].ToString());
itemName = json["itemName"].ToString();
setColor();
loadImage();
}
private void calculateRarity(int luck)
{
int number = rand.Next(100);
if (number + luck < 70)
{
rarity = ItemRarity.COMMON;
}
else if (number + luck >= 70 && number + luck < 95)
{
rarity = ItemRarity.RARE;
}
else if (number + luck >= 95 && number + luck < 120)
{
rarity = ItemRarity.EPIC;
}
else if (number + luck >= 120)
{
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);
break;
}
}
protected virtual void loadImage()
{
}
public virtual string saveGame()
{
string result = "";
result = result + FileHandler.generateJSON("rarity", "\"" + rarity + "\"") + ",\r\n";
result = result + FileHandler.generateJSON("itemName", "\"" + itemName + "\"") + ",\r\n";
return result;
}
public void setName(string name)
{
this.itemName = name;
}
public string getName()
{
return itemName;
}
public ItemRarity getRarity()
{
return rarity;
}
public virtual string getInformation()
{
return "";
}
public virtual void move(InventorySlot startDrag, Inventory inventory, InventorySlot endDrag)
{
if (endDrag.place != ItemPlace.BAG)
{
return;
}
Item item = endDrag.getItem(endDrag.getCurrentBag());
if (item != null)
{
endDrag.setItem(this, startDrag.getCurrentBag());
}
startDrag.setItem(item, endDrag.getCurrentBag());
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6e5d8aa39c30de2449dac7d2891af392
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: