not really sure but whatever
This commit is contained in:
parent
a1675d40b6
commit
06efeb5406
5 changed files with 97 additions and 0 deletions
|
@ -9,11 +9,14 @@ public enum ControlMode {
|
|||
|
||||
public class EntityPlayer : Entity
|
||||
{
|
||||
public Inventory inventory;
|
||||
|
||||
protected ControlMode controlMode;
|
||||
protected Animator animator;
|
||||
|
||||
protected override void Start() {
|
||||
base.Start();
|
||||
inventory = new Inventory();
|
||||
animator = GetComponent<Animator>();
|
||||
controlMode = ControlMode.Move;
|
||||
}
|
||||
|
|
47
Assets/Scripts/Inventory.cs
Normal file
47
Assets/Scripts/Inventory.cs
Normal file
|
@ -0,0 +1,47 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[System.Serializable]
|
||||
public class ItemNotInInventoryException : System.Exception
|
||||
{
|
||||
public ItemNotInInventoryException() { }
|
||||
public ItemNotInInventoryException(string message) : base(message) { }
|
||||
public ItemNotInInventoryException(string message, System.Exception inner) : base(message, inner) { }
|
||||
protected ItemNotInInventoryException(
|
||||
System.Runtime.Serialization.SerializationInfo info,
|
||||
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
|
||||
}
|
||||
|
||||
public class Inventory {
|
||||
public List<InventoryItem> items;
|
||||
|
||||
public InventoryItem GetItemByName(string name) {
|
||||
foreach(InventoryItem item in items) {
|
||||
if(name == item.Name)
|
||||
{
|
||||
return item;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void AddItem(InventoryItem item) {
|
||||
//Check if it's mergeable
|
||||
InventoryItem other = GetItemByName(item.Name);
|
||||
if(other == null) {
|
||||
items.Add(item);
|
||||
}
|
||||
else {
|
||||
other.quantity += item.quantity;
|
||||
}
|
||||
}
|
||||
|
||||
public void UseItem(InventoryItem item) {
|
||||
if(!items.Contains(item)) throw new ItemNotInInventoryException();
|
||||
item.Use();
|
||||
if(item.quantity <= 0) {
|
||||
items.Remove(item);
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/Inventory.cs.meta
Normal file
11
Assets/Scripts/Inventory.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 52aeb2895cff09140a9bc5981d3efc8e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
25
Assets/Scripts/InventoryItem.cs
Normal file
25
Assets/Scripts/InventoryItem.cs
Normal file
|
@ -0,0 +1,25 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class InventoryItem {
|
||||
|
||||
public int quantity = 1;
|
||||
|
||||
public virtual string Name {
|
||||
//Two items with the same name will stack!
|
||||
get {
|
||||
Debug.LogError("No name given to an item");
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Use() {
|
||||
Debug.LogWarning("Use not overridden");
|
||||
quantity -= 1;
|
||||
}
|
||||
|
||||
public InventoryItem(int quantity) {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
}
|
11
Assets/Scripts/InventoryItem.cs.meta
Normal file
11
Assets/Scripts/InventoryItem.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: af1bf08d5ae9ead4796f0b9ade785254
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in a new issue