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

@ -13,7 +13,7 @@ namespace Assets.Scripts
Item[] items = new Item[3];
int currentBag = 0;
public ItemPlace place;
Item equip;
Equipment equip;
Inventory inventory;
private void Start()
@ -28,6 +28,11 @@ namespace Assets.Scripts
this.currentBag = currentBag;
}
public int getCurrentBag()
{
return currentBag;
}
public void setItem(Item item, int bag)
{
items[bag] = item;
@ -51,21 +56,7 @@ namespace Assets.Scripts
inventory.itemDisplay.transform.Find("itemImage").GetComponent<RawImage>().texture = item.image;
inventory.itemDisplay.transform.Find("itemImage").GetComponent<RawImage>().color = item.rarityColor;
inventory.itemDisplay.transform.Find("itemName").GetComponent<Text>().text = item.getName();
string text = "Stats:\r\n";
Dictionary<string, int> attributes = item.getAttributes();
if (item.getRarity() == ItemRarity.LEGENDARY)
{
text = "All stats: +" + attributes["STR"];
}
else
{
foreach (string key in attributes.Keys)
{
text = text + key + ": +" + attributes[key] + "\r\n";
}
}
inventory.itemDisplay.transform.Find("itemStats").GetComponent<Text>().text = text;
inventory.itemDisplay.transform.Find("itemStats").GetComponent<Text>().text = item.getInformation();
int changeY = 0;
if (inventory.itemDisplay.transform.localScale == new Vector3(0,0,0))
{
@ -125,7 +116,6 @@ namespace Assets.Scripts
inventory.dragImage.transform.position = new Vector3(0,0,0);
InventorySlot startDrag = inventory.getDrag().GetComponent<InventorySlot>();
Item item;
bool isSwap = false;
if (startDrag.place == ItemPlace.BAG)
{
item = startDrag.getItem(startDrag.currentBag);
@ -136,71 +126,16 @@ namespace Assets.Scripts
}
if (item != null)
{
if (place != item.getPlace() && place != ItemPlace.BAG)
{
return;
}
if (place == ItemPlace.BAG)
{
if (items[currentBag] != null)
{
if (items[currentBag].getPlace() != startDrag.place)
{
return;
}
if (startDrag.place == ItemPlace.BAG)
{
startDrag.setItem(items[currentBag], currentBag);
}
else
{
startDrag.setEquip(items[currentBag]);
inventory.calculateStatBoost(item.getAttributes(), false);
inventory.calculateStatBoost(items[currentBag].getAttributes(), true);
}
isSwap = true;
}
setItem(item, currentBag);
}
else
{
if (equip != null)
{
if (startDrag.place == ItemPlace.BAG)
{
startDrag.setItem(equip, startDrag.currentBag);
isSwap = true;
inventory.calculateStatBoost(equip.getAttributes(), false);
}
else
{
return;
}
}
inventory.calculateStatBoost(item.getAttributes(), true);
setEquip(item);
}
if (!isSwap)
{
if (startDrag.place != ItemPlace.BAG)
{
inventory.calculateStatBoost(startDrag.getEquip().getAttributes(), false);
startDrag.removeEquip();
}
else
{
startDrag.removeItem();
}
}
item.move(startDrag, inventory, this);
}
}
public Item getEquip()
public Equipment getEquip()
{
return equip;
}
public void setEquip(Item item)
public void setEquip(Equipment item)
{
equip = item;
}
@ -281,7 +216,7 @@ namespace Assets.Scripts
{
if (bag == -1)
{
equip = new Item(json);
equip = new Equipment(json);
}
else
{

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

@ -6,22 +6,15 @@ using UnityEngine;
namespace Assets.Scripts
{
public class Item
public class Equipment : 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)
public Equipment(int luck) : base(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;
@ -37,18 +30,14 @@ namespace Assets.Scripts
calculateAttributes(luck, numberOfAttributes);
place = (ItemPlace)rand.Next(8);
editName();
setColor();
loadImage();
}
public Item(JToken json)
public Equipment(JToken json) : base(json)
{
attributes = new Dictionary<string, int>();
rarity = (ItemRarity)Enum.Parse(typeof(ItemRarity), json["rarity"].ToString());
place = (ItemPlace)Enum.Parse(typeof(ItemPlace), json["place"].ToString());
itemName = json["itemName"].ToString();
loadAttributes(json);
setColor();
loadImage();
}
@ -148,46 +137,6 @@ namespace Assets.Scripts
itemName = char.ToUpper(itemName[0]) + itemName.Substring(1);
}
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;
}
}
private void calculateAttributes(int luck, int numberOfAttributes)
{
if (numberOfAttributes != 0)
@ -297,11 +246,6 @@ namespace Assets.Scripts
return index;
}
public string getName()
{
return itemName;
}
public ItemPlace getPlace()
{
return place;
@ -325,7 +269,8 @@ namespace Assets.Scripts
return displayText;
}
private void loadImage()
override
protected void loadImage()
{
switch (place)
{
@ -361,17 +306,13 @@ namespace Assets.Scripts
return attributes;
}
public ItemRarity getRarity()
{
return rarity;
}
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("place", "\"" + place + "\"") + ",\r\n";
result = result + FileHandler.generateJSON("itemName", "\"" + itemName + "\"") + ",\r\n";
foreach (string key in attributes.Keys)
{
@ -384,6 +325,87 @@ namespace Assets.Scripts
}
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

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 1538bd0f82fb96a43bba47a68a430dcd
guid: 6e5d8aa39c30de2449dac7d2891af392
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -102,7 +102,7 @@ namespace Assets.Scripts.Slimes
{
if (new System.Random().Next(100) + 1 < 10 + luck)
{
item = new Item(luck);
item = new Equipment(luck);
}
return item;
}