204 lines
7.0 KiB
C#
204 lines
7.0 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 Item(int luck)
|
|
{
|
|
attributes = new Dictionary<string, int>();
|
|
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);
|
|
if (itemName != null)
|
|
{
|
|
itemName = rarity.ToString() + " " + place.ToString() + " of " + itemName;
|
|
}
|
|
else
|
|
{
|
|
itemName = rarity.ToString() + " " + place.ToString();
|
|
}
|
|
itemName = itemName.ToLower();
|
|
itemName = char.ToUpper(itemName[0]) + itemName.Substring(1);
|
|
}
|
|
|
|
private void calculateRarity(int luck)
|
|
{
|
|
int number = rand.Next(101);
|
|
if (number + luck < 74)
|
|
{
|
|
rarity = ItemRarity.COMMON;
|
|
}
|
|
else if (number + luck >= 75 && number + luck < 104)
|
|
{
|
|
rarity = ItemRarity.RARE;
|
|
}
|
|
else if (number + luck >= 105 && number + luck < 113)
|
|
{
|
|
rarity = ItemRarity.EPIC;
|
|
}
|
|
else if (number + luck >= 114)
|
|
{
|
|
rarity = ItemRarity.LEGENDARY;
|
|
}
|
|
}
|
|
|
|
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)(5 - (2 * i) + luck * 0.2)));
|
|
break;
|
|
case 1:
|
|
if (i == 0)
|
|
{
|
|
itemName = "intelligence";
|
|
}
|
|
attributes.Add("INT", Mathf.RoundToInt((float)(5 - (2 * i) + luck * 0.2)));
|
|
break;
|
|
case 2:
|
|
if (i == 0)
|
|
{
|
|
itemName = "dexterity";
|
|
}
|
|
attributes.Add("DEX", Mathf.RoundToInt((float)(5 - (2 * i) + luck * 0.2)));
|
|
break;
|
|
case 3:
|
|
if (i == 0)
|
|
{
|
|
itemName = "strength";
|
|
}
|
|
attributes.Add("STR", Mathf.RoundToInt((float)(5 - (2 * i) + luck * 0.2)));
|
|
break;
|
|
case 4:
|
|
if (i == 0)
|
|
{
|
|
itemName = "health";
|
|
}
|
|
attributes.Add("HP", Mathf.RoundToInt((float)(5 - (2 * i) + luck * 0.2)));
|
|
break;
|
|
case 5:
|
|
if (i == 0)
|
|
{
|
|
itemName = "mana";
|
|
}
|
|
attributes.Add("MP", Mathf.RoundToInt((float)(5 - (2 * i) + luck * 0.2)));
|
|
break;
|
|
case 6:
|
|
if (i == 0)
|
|
{
|
|
itemName = "health regeneration";
|
|
}
|
|
attributes.Add("HPR", Mathf.RoundToInt((float)(5 - (2 * i) + luck * 0.2)));
|
|
break;
|
|
case 7:
|
|
if (i == 0)
|
|
{
|
|
itemName = "mana regeneration";
|
|
}
|
|
attributes.Add("MPR", Mathf.RoundToInt((float)(5 - (2 * i) + luck * 0.2)));
|
|
break;
|
|
}
|
|
indexes[0] = index;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
attributes.Add("MPR", Mathf.RoundToInt((float)(5 + luck * 0.2)));
|
|
attributes.Add("HPR", Mathf.RoundToInt((float)(5 + luck * 0.2)));
|
|
attributes.Add("MP", Mathf.RoundToInt((float)(5 + luck * 0.2)));
|
|
attributes.Add("HP", Mathf.RoundToInt((float)(5 + luck * 0.2)));
|
|
attributes.Add("STR", Mathf.RoundToInt((float)(5 + luck * 0.2)));
|
|
attributes.Add("DEX", Mathf.RoundToInt((float)(5 + luck * 0.2)));
|
|
attributes.Add("INT", Mathf.RoundToInt((float)(5 + luck * 0.2)));
|
|
attributes.Add("LCK", Mathf.RoundToInt((float)(5 + luck * 0.2)));
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
} |