mirror of
https://github.com/DerTyp7/harvestdale-unity.git
synced 2025-10-29 04:42:08 +01:00
added basic inventory system
This commit is contained in:
8
Assets/Items.meta
Normal file
8
Assets/Items.meta
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 29418d6f59e7b5047989b2c7ebef2535
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
18
Assets/Items/Apple.asset
Normal file
18
Assets/Items/Apple.asset
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
%YAML 1.1
|
||||||
|
%TAG !u! tag:unity3d.com,2011:
|
||||||
|
--- !u!114 &11400000
|
||||||
|
MonoBehaviour:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 0}
|
||||||
|
m_Enabled: 1
|
||||||
|
m_EditorHideFlags: 0
|
||||||
|
m_Script: {fileID: 11500000, guid: 46a784de2d881964e8ee1583a44b5720, type: 3}
|
||||||
|
m_Name: Apple
|
||||||
|
m_EditorClassIdentifier:
|
||||||
|
uuid: apple
|
||||||
|
itemName: Apple
|
||||||
|
stackable: 1
|
||||||
|
maxStackSize: 100
|
||||||
8
Assets/Items/Apple.asset.meta
Normal file
8
Assets/Items/Apple.asset.meta
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 601d7a33a6c338d49a35134d5c772940
|
||||||
|
NativeFormatImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
mainObjectFileID: 11400000
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
18
Assets/Items/Banana.asset
Normal file
18
Assets/Items/Banana.asset
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
%YAML 1.1
|
||||||
|
%TAG !u! tag:unity3d.com,2011:
|
||||||
|
--- !u!114 &11400000
|
||||||
|
MonoBehaviour:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 0}
|
||||||
|
m_Enabled: 1
|
||||||
|
m_EditorHideFlags: 0
|
||||||
|
m_Script: {fileID: 11500000, guid: 46a784de2d881964e8ee1583a44b5720, type: 3}
|
||||||
|
m_Name: Banana
|
||||||
|
m_EditorClassIdentifier:
|
||||||
|
uuid: banana
|
||||||
|
itemName: Banana
|
||||||
|
stackable: 0
|
||||||
|
maxStackSize: 1
|
||||||
8
Assets/Items/Banana.asset.meta
Normal file
8
Assets/Items/Banana.asset.meta
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: cbbbe8384f7ec2845b4145744d51ecf8
|
||||||
|
NativeFormatImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
mainObjectFileID: 11400000
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -12,7 +12,9 @@ MonoBehaviour:
|
|||||||
m_Script: {fileID: 11500000, guid: e283aa008f05f1e46b99a7987202ec09, type: 3}
|
m_Script: {fileID: 11500000, guid: e283aa008f05f1e46b99a7987202ec09, type: 3}
|
||||||
m_Name: Carrot
|
m_Name: Carrot
|
||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
cropName: Carrot
|
uuid:
|
||||||
|
itemName: Carrot
|
||||||
|
quantity: 1
|
||||||
daysToGrow: 6
|
daysToGrow: 6
|
||||||
sprites:
|
sprites:
|
||||||
- {fileID: -1568493089, guid: 81f3e7dd6e7b15747a4a5c1923fb8521, type: 3}
|
- {fileID: -1568493089, guid: 81f3e7dd6e7b15747a4a5c1923fb8521, type: 3}
|
||||||
@@ -202,9 +202,11 @@ GameObject:
|
|||||||
- component: {fileID: 224768190}
|
- component: {fileID: 224768190}
|
||||||
- component: {fileID: 224768189}
|
- component: {fileID: 224768189}
|
||||||
- component: {fileID: 224768188}
|
- component: {fileID: 224768188}
|
||||||
|
- component: {fileID: 224768193}
|
||||||
- component: {fileID: 224768187}
|
- component: {fileID: 224768187}
|
||||||
- component: {fileID: 224768191}
|
- component: {fileID: 224768191}
|
||||||
- component: {fileID: 224768192}
|
- component: {fileID: 224768192}
|
||||||
|
- component: {fileID: 224768194}
|
||||||
m_Layer: 0
|
m_Layer: 0
|
||||||
m_Name: Player
|
m_Name: Player
|
||||||
m_TagString: Untagged
|
m_TagString: Untagged
|
||||||
@@ -350,6 +352,34 @@ CapsuleCollider2D:
|
|||||||
m_Offset: {x: 0.005384445, y: -0.8123081}
|
m_Offset: {x: 0.005384445, y: -0.8123081}
|
||||||
m_Size: {x: 0.3146143, y: 0.3146143}
|
m_Size: {x: 0.3146143, y: 0.3146143}
|
||||||
m_Direction: 0
|
m_Direction: 0
|
||||||
|
--- !u!114 &224768193
|
||||||
|
MonoBehaviour:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 224768186}
|
||||||
|
m_Enabled: 1
|
||||||
|
m_EditorHideFlags: 0
|
||||||
|
m_Script: {fileID: 11500000, guid: d50a6d9f6c7ba0e49a308d097eb64fb7, type: 3}
|
||||||
|
m_Name:
|
||||||
|
m_EditorClassIdentifier:
|
||||||
|
maxSlots: 20
|
||||||
|
items: []
|
||||||
|
--- !u!114 &224768194
|
||||||
|
MonoBehaviour:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 224768186}
|
||||||
|
m_Enabled: 1
|
||||||
|
m_EditorHideFlags: 0
|
||||||
|
m_Script: {fileID: 11500000, guid: fd29210254821be4dab438ab0312df18, type: 3}
|
||||||
|
m_Name:
|
||||||
|
m_EditorClassIdentifier:
|
||||||
|
testItem1: {fileID: 11400000, guid: 601d7a33a6c338d49a35134d5c772940, type: 2}
|
||||||
|
testItem2: {fileID: 11400000, guid: cbbbe8384f7ec2845b4145744d51ecf8, type: 2}
|
||||||
--- !u!1 &519420028
|
--- !u!1 &519420028
|
||||||
GameObject:
|
GameObject:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
|||||||
@@ -1,34 +1,84 @@
|
|||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class Inventory : MonoBehaviour
|
public class Inventory : MonoBehaviour
|
||||||
{
|
{
|
||||||
[SerializeField] private int slots = 10;
|
public int maxSlots = 20; // maximum number of slots in the inventory
|
||||||
[SerializeField] private int stackSize = 100;
|
public List<InventoryItem> items = new List<InventoryItem>(); // list of items in the inventory
|
||||||
[SerializeField] private List<Item> items = new List<Item>();
|
|
||||||
|
|
||||||
|
// adds an item to the inventory, returns the quantity of items which could not be added
|
||||||
public Item Add(Item item)
|
public int Add(Item item, int count)
|
||||||
{
|
{
|
||||||
foreach (Item i in items)
|
int remainingCount = count;
|
||||||
|
|
||||||
|
// check if the item is stackable
|
||||||
|
if (item.stackable)
|
||||||
{
|
{
|
||||||
if (i.uuid == item.uuid) // item already is in list
|
// look for an existing stack of the same item in the inventory
|
||||||
|
foreach (InventoryItem invItem in items)
|
||||||
{
|
{
|
||||||
if (i.quantity + item.quantity <= stackSize)
|
if (invItem.item == item && invItem.count < item.maxStackSize)
|
||||||
{// enough space
|
{
|
||||||
i.quantity += item.quantity;
|
// add as many items as possible to the stack
|
||||||
item.quantity = 0;
|
int space = item.maxStackSize - invItem.count;
|
||||||
}
|
int toAdd = Mathf.Min(space, remainingCount);
|
||||||
else
|
invItem.count += toAdd;
|
||||||
{// Not enough space
|
remainingCount -= toAdd;
|
||||||
i.quantity = stackSize;
|
|
||||||
item.quantity = Mathf.Max(0, i.quantity - item.quantity);
|
// exit the loop if all items have been added
|
||||||
|
if (remainingCount == 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return item;
|
|
||||||
|
|
||||||
|
// if there is still remaining count, add new stacks of the item
|
||||||
|
while (remainingCount > 0 && items.Count < maxSlots)
|
||||||
|
{
|
||||||
|
int toAdd = Mathf.Min(item.maxStackSize, remainingCount);
|
||||||
|
items.Add(new InventoryItem(item, toAdd));
|
||||||
|
remainingCount -= toAdd;
|
||||||
|
}
|
||||||
|
|
||||||
|
// return the quantity of items which could not be added
|
||||||
|
return remainingCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// removes an item from the inventory, returns the quantity of items which could not be removed
|
||||||
|
public int Remove(Item item, int count)
|
||||||
|
{
|
||||||
|
int remainingCount = count;
|
||||||
|
|
||||||
|
// look for an existing stack of the item in the inventory
|
||||||
|
for (int i = 0; i < items.Count; i++)
|
||||||
|
{
|
||||||
|
InventoryItem invItem = items[i];
|
||||||
|
if (invItem.item == item)
|
||||||
|
{
|
||||||
|
// remove as many items as possible from the stack
|
||||||
|
int toRemove = Mathf.Min(invItem.count, remainingCount);
|
||||||
|
invItem.count -= toRemove;
|
||||||
|
remainingCount -= toRemove;
|
||||||
|
|
||||||
|
// remove the stack if it's now empty
|
||||||
|
if (invItem.count == 0)
|
||||||
|
{
|
||||||
|
items.RemoveAt(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
// exit the loop if all items have been removed
|
||||||
|
if (remainingCount == 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// return the quantity of items which could not be removed
|
||||||
|
return remainingCount;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
18
Assets/Scripts/InventoryItem.cs
Normal file
18
Assets/Scripts/InventoryItem.cs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
// represents an item in the inventory and its count
|
||||||
|
[System.Serializable]
|
||||||
|
public class InventoryItem
|
||||||
|
{
|
||||||
|
public Item item;
|
||||||
|
public int count;
|
||||||
|
|
||||||
|
public InventoryItem(Item item, int count)
|
||||||
|
{
|
||||||
|
this.item = item;
|
||||||
|
this.count = count;
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Assets/Scripts/InventoryItem.cs.meta
Normal file
11
Assets/Scripts/InventoryItem.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 352cc16dc45cea64b92b7fd088daff22
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -8,5 +8,15 @@ public class Item : ScriptableObject
|
|||||||
public string uuid;
|
public string uuid;
|
||||||
public string itemName;
|
public string itemName;
|
||||||
|
|
||||||
public int quantity = 1;
|
public bool stackable = true;
|
||||||
|
public int maxStackSize = 100;
|
||||||
|
|
||||||
|
|
||||||
|
private void OnEnable()
|
||||||
|
{
|
||||||
|
if (!stackable)
|
||||||
|
{
|
||||||
|
maxStackSize = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
60
Assets/Scripts/PlayerController.cs
Normal file
60
Assets/Scripts/PlayerController.cs
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class PlayerController : MonoBehaviour
|
||||||
|
{
|
||||||
|
|
||||||
|
public Item testItem1;
|
||||||
|
public Item testItem2;
|
||||||
|
// Start is called before the first frame update
|
||||||
|
void Start()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update is called once per frame
|
||||||
|
void Update()
|
||||||
|
{
|
||||||
|
Inventory inventory = GetComponent<Inventory>();
|
||||||
|
|
||||||
|
if (Input.GetKeyDown(KeyCode.U))
|
||||||
|
{
|
||||||
|
// add 5 apples
|
||||||
|
int remainingCount = inventory.Add(testItem1, 5);
|
||||||
|
Debug.Log("Added " + (5 - remainingCount) + " apples to inventory.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Input.GetKeyDown(KeyCode.I))
|
||||||
|
{ // add 15 more apples
|
||||||
|
int remainingCount = inventory.Add(testItem1, 15);
|
||||||
|
Debug.Log("Added " + (15 - remainingCount) + " apples to inventory.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Input.GetKeyDown(KeyCode.O))
|
||||||
|
{
|
||||||
|
// add 10 bananas
|
||||||
|
int remainingCount = inventory.Add(testItem2, 10);
|
||||||
|
Debug.Log("Added " + (10 - remainingCount) + " bananas to inventory.");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (Input.GetKeyDown(KeyCode.J))
|
||||||
|
{
|
||||||
|
int count = 3;
|
||||||
|
int applesRemaining = inventory.Remove(testItem1, count);
|
||||||
|
Debug.Log("Removed " + (count - applesRemaining) + " apples.");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Input.GetKeyDown(KeyCode.K))
|
||||||
|
{
|
||||||
|
int count = 5;
|
||||||
|
int bananasRemaining = inventory.Remove(testItem2, count);
|
||||||
|
Debug.Log("Removed " + (count - bananasRemaining) + " bananas.");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Assets/Scripts/PlayerController.cs.meta
Normal file
11
Assets/Scripts/PlayerController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: fd29210254821be4dab438ab0312df18
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
Reference in New Issue
Block a user