literally half of the game
This commit is contained in:
parent
1590b5faa6
commit
b842289076
77 changed files with 11904 additions and 72 deletions
30
Assets/Scripts/Inventory/CraftingSystem.cs
Normal file
30
Assets/Scripts/Inventory/CraftingSystem.cs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class CraftingSystem : MonoBehaviour
|
||||
{
|
||||
public bool TryCraftingItem(Item requestedItem)
|
||||
{
|
||||
List<InventoryManager.ItemAmount> missingItems = new();
|
||||
foreach (InventoryManager.ItemAmount requiredMaterial in requestedItem.requiredMaterials)
|
||||
{
|
||||
if (InventoryManager.instance.CheckItemAmount(requiredMaterial.item) < requiredMaterial.amount)
|
||||
{
|
||||
InventoryManager.ItemAmount newMissingItem = new();
|
||||
newMissingItem.item = requiredMaterial.item;
|
||||
newMissingItem.amount = requiredMaterial.amount - InventoryManager.instance.CheckItemAmount(requiredMaterial.item);
|
||||
}
|
||||
}
|
||||
if (missingItems.Count > 0)
|
||||
{
|
||||
return false; //supposed to return what items were missing
|
||||
}
|
||||
foreach (InventoryManager.ItemAmount requiredMaterial in requestedItem.requiredMaterials)
|
||||
{
|
||||
InventoryManager.instance.RemoveItem(requiredMaterial.item, requiredMaterial.amount);
|
||||
}
|
||||
InventoryManager.instance.AddItem(requestedItem, requestedItem.craftedAmount);
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Inventory/CraftingSystem.cs.meta
Normal file
2
Assets/Scripts/Inventory/CraftingSystem.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7d679797190d2f6689dcb050b4d2ace8
|
||||
|
|
@ -1,20 +1,56 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
|
||||
public class InventoryManager : MonoBehaviour
|
||||
{
|
||||
#region Statication
|
||||
|
||||
public static InventoryManager instance;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (instance != null && instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
DontDestroyOnLoad(this);
|
||||
instance = this;
|
||||
}
|
||||
|
||||
#endregion
|
||||
[Serializable]
|
||||
public class ItemAmount
|
||||
{
|
||||
public Item item;
|
||||
public int amount;
|
||||
}
|
||||
public Item[] itemList; //list of all items in the game
|
||||
public int maxCapacity;
|
||||
public int currentlyStored;
|
||||
public Dictionary<Item, int> storedItems = new();
|
||||
public Item[] itemList;
|
||||
public int money;
|
||||
[Header("UI")]
|
||||
public TextMeshProUGUI moneyUI;
|
||||
public GameObject inventoryUI;
|
||||
public InventoryUIObject templateUIObject;
|
||||
public Transform inventoryGrid;
|
||||
public Dictionary<Item, InventoryUIObject> allInventoryUIObjects = new();
|
||||
|
||||
private void Start()
|
||||
{
|
||||
UpdateMoneyUI();
|
||||
}
|
||||
|
||||
public bool RemoveMoney(int change)
|
||||
{
|
||||
if (money - change >= 0)
|
||||
{
|
||||
money -= change;
|
||||
UpdateMoneyUI();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
@ -22,6 +58,12 @@ public class InventoryManager : MonoBehaviour
|
|||
public void AddMoney(int change)
|
||||
{
|
||||
money += change;
|
||||
UpdateMoneyUI();
|
||||
}
|
||||
|
||||
public void UpdateMoneyUI()
|
||||
{
|
||||
moneyUI.text = $"Money: {money}";
|
||||
}
|
||||
|
||||
public int AddItem(Item itemToAdd, int amount = 1)
|
||||
|
|
@ -36,6 +78,8 @@ public class InventoryManager : MonoBehaviour
|
|||
{
|
||||
storedItems[itemToAdd] += amount;
|
||||
currentlyStored += amount;
|
||||
UpdateTaskTracker();
|
||||
Debug.Log($"ADDED ALL {itemToAdd.itemName}");
|
||||
return 0;
|
||||
}
|
||||
//return the amount that wasn't able to be stored.
|
||||
|
|
@ -43,11 +87,21 @@ public class InventoryManager : MonoBehaviour
|
|||
storedItems[itemToAdd] += stored;
|
||||
currentlyStored += stored;
|
||||
amount -= stored;
|
||||
UpdateTaskTracker();
|
||||
Debug.Log($"ADDED {stored} OF {itemToAdd.itemName} AND DROPPED {amount}");
|
||||
return amount;
|
||||
}
|
||||
Debug.Log($"COULD NOT ADD {itemToAdd.itemName}; INVENTORY FULL");
|
||||
return -1;
|
||||
}
|
||||
|
||||
public void UpdateTaskTracker()
|
||||
{
|
||||
if (TaskManager.instance.activeTasks.Count > 0)
|
||||
{
|
||||
TaskManager.instance.CheckInventory();
|
||||
}
|
||||
}
|
||||
public bool RemoveItem(Item itemToRemove, int amount = 1)
|
||||
{
|
||||
if (!storedItems.ContainsKey(itemToRemove) || storedItems[itemToRemove] < amount)
|
||||
|
|
@ -57,4 +111,34 @@ public class InventoryManager : MonoBehaviour
|
|||
storedItems[itemToRemove] -= amount;
|
||||
return true;
|
||||
}
|
||||
|
||||
public int CheckItemAmount(Item itemToCheck)
|
||||
{
|
||||
return storedItems[itemToCheck];
|
||||
}
|
||||
public void UpdateInventoryUI()
|
||||
{
|
||||
foreach (var item in storedItems)
|
||||
{
|
||||
if (allInventoryUIObjects.ContainsKey(item.Key))
|
||||
{
|
||||
if (item.Value == 0)
|
||||
{
|
||||
Destroy(allInventoryUIObjects[item.Key]);
|
||||
allInventoryUIObjects.Remove(item.Key);
|
||||
}
|
||||
else
|
||||
{
|
||||
allInventoryUIObjects[item.Key].SetObject(item.Key);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
InventoryUIObject newUIObject = Instantiate(templateUIObject, inventoryGrid);
|
||||
newUIObject.gameObject.SetActive(true);
|
||||
newUIObject.SetObject(item.Key);
|
||||
allInventoryUIObjects[item.Key] = newUIObject;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
18
Assets/Scripts/Inventory/InventoryUIObject.cs
Normal file
18
Assets/Scripts/Inventory/InventoryUIObject.cs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class InventoryUIObject : MonoBehaviour
|
||||
{
|
||||
public TextMeshProUGUI itemNameText;
|
||||
public TextMeshProUGUI amountText;
|
||||
public Image itemIcon;
|
||||
|
||||
public void SetObject(Item itemToSet)
|
||||
{
|
||||
Debug.Log("here");
|
||||
itemNameText.text = itemToSet.itemName; //it would be reset every time you open the inventory instead of updating only the amount but whatever it doesn't matter
|
||||
amountText.text = $"x{InventoryManager.instance.CheckItemAmount(itemToSet)}";
|
||||
itemIcon.sprite = itemToSet.icon;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Inventory/InventoryUIObject.cs.meta
Normal file
2
Assets/Scripts/Inventory/InventoryUIObject.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6f2a88e73d2507a229d0c3d869826b39
|
||||
|
|
@ -1,8 +1,14 @@
|
|||
using UnityEngine;
|
||||
|
||||
public class Item : MonoBehaviour
|
||||
[CreateAssetMenu(fileName = "New Item", menuName = "Item")]
|
||||
public class Item : ScriptableObject
|
||||
{
|
||||
[Header("Identification")]
|
||||
public string itemName;
|
||||
public string description;
|
||||
public Sprite icon;
|
||||
[Header("Crafting")]
|
||||
public InventoryManager.ItemAmount[] requiredMaterials;
|
||||
public int craftedAmount = 1; //amount of items received after being crafted
|
||||
public int value; //items will be sold at a percentage of value?
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue