217 lines
6.2 KiB
C#
217 lines
6.2 KiB
C#
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, bool isEquipment)
|
|
{
|
|
if(isEquipment){
|
|
luck = luck + GameObject.Find("Inventory").GetComponent<Inventory>().getEquipmentBonus()["LCK"];
|
|
calculateRarity(luck);
|
|
setColor();
|
|
}
|
|
else{
|
|
chooseItem();
|
|
rarity = ItemRarity.COMMON;
|
|
rarityColor = Color.white;
|
|
loadImage();
|
|
}
|
|
|
|
}
|
|
|
|
public Item(string name)
|
|
{
|
|
rarity = ItemRarity.COMMON;
|
|
itemName = name;
|
|
rarityColor = Color.white;
|
|
loadImage();
|
|
}
|
|
|
|
public Item(JToken json)
|
|
{
|
|
rarity = (ItemRarity)Enum.Parse(typeof(ItemRarity), json["rarity"].ToString());
|
|
itemName = json["itemName"].ToString();
|
|
if(json["place"] != null){
|
|
setColor();
|
|
}
|
|
else{
|
|
rarityColor = Color.white;
|
|
}
|
|
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()
|
|
{
|
|
switch (itemName)
|
|
{
|
|
case "Slimeball":
|
|
image = Resources.Load<Texture>("Items/Inv_Slimeball");
|
|
break;
|
|
case "Wood":
|
|
image = Resources.Load<Texture>("Items/Inv_Wood");
|
|
break;
|
|
case "Rock":
|
|
image = Resources.Load<Texture>("Items/Inv_Stone");
|
|
break;
|
|
case "Iron ore":
|
|
image = Resources.Load<Texture>("Items/Inv_Iron");
|
|
break;
|
|
case "Gold ore":
|
|
image = Resources.Load<Texture>("Items/Inv_Gold");
|
|
break;
|
|
case "Copper ore":
|
|
image = Resources.Load<Texture>("Items/Inv_Copper");
|
|
break;
|
|
case "Tin ore":
|
|
image = Resources.Load<Texture>("Items/Inv_Tin");
|
|
break;
|
|
}
|
|
}
|
|
|
|
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()
|
|
{
|
|
string result = "";
|
|
switch (itemName)
|
|
{
|
|
case "Slimeball":
|
|
result = "Soft and sticky... Does it have a use?";
|
|
break;
|
|
case "Wood":
|
|
result = "Quite a nice piece of wood";
|
|
break;
|
|
case "Rock":
|
|
result = "Is it... Dwayne?";
|
|
break;
|
|
case "Iron ore":
|
|
result = "The most basic ore";
|
|
break;
|
|
case "Gold ore":
|
|
result = "Wooooow... Shiny.";
|
|
break;
|
|
case "Copper ore":
|
|
result = "Cool";
|
|
break;
|
|
case "Tin ore":
|
|
result = "Got inspired by Minecraft, huh?";
|
|
break;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
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)
|
|
{
|
|
startDrag.setItem(item, endDrag.getCurrentBag());
|
|
}
|
|
else
|
|
{
|
|
startDrag.removeItem();
|
|
}
|
|
|
|
endDrag.setItem(this, startDrag.getCurrentBag());
|
|
}
|
|
|
|
private void chooseItem()
|
|
{
|
|
int index = rand.Next(4);
|
|
switch (index)
|
|
{
|
|
case 0:
|
|
itemName = "Slimeball";
|
|
break;
|
|
case 1:
|
|
itemName = "Rock";
|
|
break;
|
|
case 2:
|
|
itemName = "Iron ore";
|
|
break;
|
|
case 3:
|
|
itemName = "Gold ore";
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
} |