while i’m at it, i think i’m doing a lot wrong. because i’m dumb … not an easy fix for that lol.
here’s my code. i’m trying to wrap my mind around how i should use the architecture:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace CoS
{
//certain things don't want to store in Item Database? external file system could be inefficient or a slow down
public class Item : MonoBehaviour
{
heroInventory inv;
public Transform hero;
GameObject itemonground;
private void Start()
{
inv = new heroInventory();
}
//Wrapper class containing old info in a prettier way
public class ItemInfo
{
//Make all of these readonly so you can be sure they won't be changed.
public readonly string name;
public readonly string description;
public readonly double price;
public int quantity; //this doesn't belong here?
public readonly string sprite;
public readonly string type; //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public readonly int dr_crushing; //need to make objects for these, so i don't have so many properties
public readonly int dr_cutting;
public readonly int dr_impaling;
public readonly int pd_crushing;
public readonly int pd_cutting;
public readonly int pd_impaling;
public readonly int thrusting;
public readonly int swinging;
public readonly int thrusting_modifier;
public readonly int swinging_modifier;
public ItemInfo(string name, string description, double price, int quantity, string sprite, string type,
int dr_crushing, int dr_cutting, int dr_impaling, int pd_crushing, int pd_cutting, int pd_impaling,
int thrusting, int swinging, int thrusting_modifier, int swinging_modifier)
{
this.name = name;
this.description = description;
this.price = price;
this.quantity = quantity;
this.sprite = sprite;
this.type = type;
this.dr_crushing = dr_crushing;
this.dr_cutting = dr_cutting;
this.dr_impaling = dr_impaling;
this.pd_crushing = pd_crushing;
this.pd_cutting = pd_cutting;
this.pd_impaling = pd_impaling;
this.thrusting = thrusting;
this.swinging = swinging;
this.thrusting_modifier = thrusting_modifier;
this.swinging_modifier = swinging_modifier;
}
}
//Keep the named IDs, but put them in an enum.
public enum ItemID
{
NOTHING = 0,
HP_POTION = 1,
MP_POTION = 2,
SWORD = 3,
SHIELD = 4,
}
//Replacement of item
public static class ItemDatabase
{
public static Dictionary<ItemID, ItemInfo> items;
static ItemDatabase()
{
items = new Dictionary<ItemID, ItemInfo>(5);
//You probably don't need an explicit representation of nothing? Kept it in.
items[ItemID.NOTHING] = new ItemInfo("Nothing", "Nothing", 0, 0, "nothing", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
items[ItemID.HP_POTION] = new ItemInfo("HP Potion", "Heals HP", 10, 0, "obj_HP_potion", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
items[ItemID.MP_POTION] = new ItemInfo("MP Potion", "Restores MP", 10, 0, "obj_mp_potion", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
items[ItemID.SWORD] = new ItemInfo("Sword", "Shard object", 100, 0, "obj_SWORD", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
items[ItemID.SHIELD] = new ItemInfo("Shield", "Blocks attacks", 50, 0, "obj_SHIELD", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
}
//doing this wrong.
/**********************
* Inventory Functions
* *******************/
// remove from inventory
public void Inv_Remove(Item.ItemInfo item, int qnty)
{
//DID THIS WRONG! Need inventory list
//In the script we will remove an item from the inventory
var found = -1;
//We will check if the item exists and take its place
for (var i = 0; i < 5; i++)
{
//ItemID eyedee1 = (ItemID)i;
if (inv.inventory[i].name == item.name) //maybe have a unique id rather than use name? possibility.
{
found = i;
break;
}
}
if (found != -1)
{
//If we found the item, we remove it
inv.inventory[found].quantity -= qnty;//qnty is the amount to remove
if (inv.inventory[found].quantity <= 0)
{
inv.inventory[found] = new Item.ItemInfo("Nothing", "Nothing", 0, 0, "nothing", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
} //If there is no more of the item we change the place to an empty place (Nothing item)
}
}
//In this script we will add items to the inventory
public void Inv_Add(Item.ItemInfo item, int qnty)
{
var found = -1;
bool fullflag = false;
for (var i = 0; i < inv.invcapacity; i++)
{
if (inv.inventory[i].name == item.name)
{
found = i; //We affect to found the place of the item if we find it
break;
}
}
//If it doesn't exist
if (found == -1)
{
//Let's check if there is an empty place to add our item
for (var j = 0; j < inv.invcapacity; j++)
{
if (inv.inventory[j].name == "Nothing")
{
found = j;
break;
}
else if (j == (inv.invcapacity - 1) & found != j)
{
fullflag = true;
break;
}
}
}
//should have an "inventory is full" contingency
if (fullflag)
{
// print this out! in a game-friendly gui - depends on whether looting or picking up, or buying
Debug.Log("inventory is full, cannot pick up object");
}
//Now let's add our item to the place we have *
if (found != -1)
{
inv.inventory[found] = item; //Argument 0 is the id of the item
inv.inventory[found].quantity += qnty; //Argument 1 is the amount of the item to add
}
// need a script for looting, for buying, and for picking up
}
// Drop an item
public void Inv_Drop(Item.ItemInfo item)
{
// item = item clicked in gui - right click and click drop
//Instantiate(hero, item);
//not sure what to use in place of "item"
Inv_Remove(item, 1);
}
}
}
specifically, i think i may have done the inv_remove and inv_add scripts wrong. i should be using the item database but i’m not? it seems like there’s something wrong, if anybody could point it out for me. thanks MathiasDG btw.