Animal Ambiguity was my second Game Jam Project. This time I worked on the inventory system and learned about scripable objects. This project was a great learning experience for me as I learned how to manage game state and create intuitive player interactions.
Here's a piece of code I implemented for the inventory system in the Scrappy Squirrel project:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class Inventory : MonoBehaviour
{
private Item[] items = new Item[5];
private int itemIndex = 0;
void Start()
{
removeCurrentItem();
}
void Update()
{
if(Input.GetAxis("Mouse ScrollWheel") > 0)
{
itemIndex++;
if(itemIndex >= 5)
{
itemIndex = 0;
}
}
if(Input.GetAxis("Mouse ScrollWheel") < 0)
{
itemIndex--;
if(itemIndex < 0)
{
itemIndex = 4;
}
}
if(Input.GetKeyDown(KeyCode.Q)){
removeCurrentItem();
}
}
public Item getCurrentItem()
{
return items[itemIndex];
}
public bool givePlayerItem(Item item)
{
for (int i = 0; i < items.Length; i++)
{
if(items[i] == null)
{
items[i] = item;
return true;
}
}
return false;
}
public bool hasItem(Item item)
{
for (int i = 0; i < items.Length; i++)
{
if(items[i] == item)
{
return true;
}
}
return false;
}
public void removeCurrentItem()
{
items[itemIndex] = null;
}
}
This inventory system manages a collection of up to 5 items. It allows for scrolling through items, adding new items, checking for item possession, and removing items. The system uses Unity's input system to detect mouse scroll and key presses for interaction. This was an integral part of the game mechanics, allowing players to collect and manage items throughout their adventure. Implementing this system taught me a lot about managing game state and creating intuitive player interactions.