299 lines
10 KiB
C#
299 lines
10 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace Assets.Scripts
|
|
{
|
|
public class Item
|
|
{
|
|
private System.Random rand = new System.Random();
|
|
ItemRarity rarity;
|
|
ItemPlace place;
|
|
string itemName;
|
|
Dictionary<string, int> attributes;
|
|
public Texture image;
|
|
public Color32 rarityColor;
|
|
|
|
public Item(int luck)
|
|
{
|
|
attributes = new Dictionary<string, int>();
|
|
luck = luck + GameObject.Find("Inventory").GetComponent<Inventory>().getEquipmentBonus()["LCK"];
|
|
int numberOfAttributes = 1;
|
|
calculateRarity(luck);
|
|
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);
|
|
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);
|
|
loadImage();
|
|
}
|
|
|
|
private void calculateRarity(int luck)
|
|
{
|
|
int number = rand.Next(100);
|
|
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 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 0:
|
|
if (i == 0)
|
|
{
|
|
itemName = "luck";
|
|
}
|
|
attributes.Add("LCK", Mathf.RoundToInt((float)(3 - (2 * i) + luck * 0.1)));
|
|
break;
|
|
case 1:
|
|
if (i == 0)
|
|
{
|
|
itemName = "intelligence";
|
|
}
|
|
attributes.Add("INT", Mathf.RoundToInt((float)(3 - (2 * i) + luck * 0.1)));
|
|
break;
|
|
case 2:
|
|
if (i == 0)
|
|
{
|
|
itemName = "dexterity";
|
|
}
|
|
attributes.Add("DEX", Mathf.RoundToInt((float)(3 - (2 * i) + luck * 0.1)));
|
|
break;
|
|
case 3:
|
|
if (i == 0)
|
|
{
|
|
itemName = "strength";
|
|
}
|
|
attributes.Add("STR", Mathf.RoundToInt((float)(3 - (2 * i) + luck * 0.1)));
|
|
break;
|
|
case 4:
|
|
if (i == 0)
|
|
{
|
|
itemName = "health";
|
|
}
|
|
attributes.Add("HP", Mathf.RoundToInt((float)(10 - (2 * i) + luck * 0.1)));
|
|
break;
|
|
case 5:
|
|
if (i == 0)
|
|
{
|
|
itemName = "mana";
|
|
}
|
|
attributes.Add("MP", Mathf.RoundToInt((float)(10 - (2 * i) + luck * 0.1)));
|
|
break;
|
|
case 6:
|
|
if (i == 0)
|
|
{
|
|
itemName = "health regeneration";
|
|
}
|
|
attributes.Add("HPR", Mathf.RoundToInt((float)(3 - (2 * i) + luck * 0.1)));
|
|
break;
|
|
case 7:
|
|
if (i == 0)
|
|
{
|
|
itemName = "mana regeneration";
|
|
}
|
|
attributes.Add("MPR", Mathf.RoundToInt((float)(3 - (2 * i) + luck * 0.1)));
|
|
break;
|
|
}
|
|
indexes[i] = index;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
attributes.Add("MPR", Mathf.RoundToInt((float)(3 + luck * 0.1)));
|
|
attributes.Add("HPR", Mathf.RoundToInt((float)(3 + luck * 0.1)));
|
|
attributes.Add("MP", Mathf.RoundToInt((float)(3 + luck * 0.1)));
|
|
attributes.Add("HP", Mathf.RoundToInt((float)(3 + luck * 0.1)));
|
|
attributes.Add("STR", Mathf.RoundToInt((float)(3 + luck * 0.1)));
|
|
attributes.Add("DEX", Mathf.RoundToInt((float)(3 + luck * 0.1)));
|
|
attributes.Add("INT", Mathf.RoundToInt((float)(3 + luck * 0.1)));
|
|
attributes.Add("LCK", Mathf.RoundToInt((float)(3 + luck * 0.1)));
|
|
}
|
|
}
|
|
|
|
private int calculateIndex(int[] indexes)
|
|
{
|
|
int counter = 0;
|
|
int index = 0;
|
|
while (true)
|
|
{
|
|
index = rand.Next(8);
|
|
counter = 0;
|
|
for (int j = 0; j < indexes.Length; j++)
|
|
{
|
|
if (indexes[j] == index)
|
|
{
|
|
counter++;
|
|
break;
|
|
}
|
|
}
|
|
if (counter == 0)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
return index;
|
|
}
|
|
|
|
public string getName()
|
|
{
|
|
return itemName;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
private 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;
|
|
}
|
|
|
|
public ItemRarity getRarity()
|
|
{
|
|
return rarity;
|
|
}
|
|
}
|
|
|
|
} |