1
Fork 0
This commit is contained in:
Lorenzo Balugani 2019-04-28 18:46:19 +02:00
commit ecbe51769c
5 changed files with 97 additions and 0 deletions

View file

@ -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;
}

View 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);
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 52aeb2895cff09140a9bc5981d3efc8e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View 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;
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: af1bf08d5ae9ead4796f0b9ade785254
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: