add basic inventory, fading walls

This commit is contained in:
2026-02-17 15:26:34 -06:00
parent 6baa6956bf
commit 7123238daa
30 changed files with 6594 additions and 255 deletions
+55 -8
View File
@@ -1,12 +1,16 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class CameraController : MonoBehaviour
{
private float moveSpeed = 50.0f;
private float zoomSpeed = 20.0f;
private bool isTracking = false; // don't track when parented
private bool returningHome = false; // cam is on its way home
private float baseCamSize = 5; // this is how "zoomed" the ortho cam is
private float baseCamSize = 7; // this is how "zoomed" the ortho cam is
private float camMovementTime = 0.9f; // how much time to switch cam positions
private float moveSpeed;
private float zoomSpeed;
public Transform playerCamHome; // camera's home base
public GameObject cameraObj;
@@ -14,29 +18,72 @@ public class CameraController : MonoBehaviour
private Vector3 currentCamPosition;
private float desiredCamSize;
private int positionsSet = 0; // this keeps track of how many cam zones we're in
private List<StaticCamRoom> roomsActive;
public void SetCurrentPosition(Vector3 currentCamPosition, float desiredCamSize)
{
positionsSet++; // we have entered a cam zone
cameraObj.transform.parent = null;
float distanceToMove = Vector3.Distance(this.currentCamPosition, currentCamPosition);
float amountToZoom = Mathf.Abs(this.desiredCamSize - desiredCamSize);
moveSpeed = distanceToMove / camMovementTime;
zoomSpeed = amountToZoom / camMovementTime;
isTracking = true;
returningHome = false;
cameraObj.transform.parent = null;
this.currentCamPosition = currentCamPosition;
this.desiredCamSize = desiredCamSize;
}
public void ReturnCameraToHome()
{
isTracking = false;
returningHome = true;
positionsSet--; // we have exited a cam zone
// if this number is greater than 1, we have left a zone but are still in another
if (positionsSet <= 0)
{
positionsSet = 0;
currentCamPosition = playerCamHome.position;
desiredCamSize = baseCamSize;
float distanceToMove = Vector3.Distance(this.currentCamPosition, playerCamHome.position);
float amountToZoom = Mathf.Abs(this.desiredCamSize - baseCamSize);
moveSpeed = distanceToMove / camMovementTime;
zoomSpeed = amountToZoom / camMovementTime;
isTracking = false;
returningHome = true;
currentCamPosition = playerCamHome.position;
desiredCamSize = baseCamSize;
}
}
public void AddToRoomsActive(StaticCamRoom room)
{
roomsActive.Add(room);
room.RoomEnter();
}
public void RemoveFromRoomsActive(StaticCamRoom room)
{
List<Transform> wallsToKeep = new List<Transform>();
roomsActive.Remove(room);
foreach (StaticCamRoom staticCamRoom in roomsActive)
{
wallsToKeep.AddRange(staticCamRoom.objectsToHide);
}
room.RoomExit(wallsToKeep);
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
cameraComponent = cameraObj.GetComponent<Camera>();
roomsActive = new List<StaticCamRoom>();
ReturnCameraToHome();
}
+7 -6
View File
@@ -19,13 +19,14 @@ private void Awake()
else
{
Instance = this;
DontDestroyOnLoad(gameObject);
DialogueManager = GetComponent<DialogueManager>();
Inventory = GetComponent<Inventory>();
SaveSystem.Load();
}
DontDestroyOnLoad(gameObject);
DialogueManager = GetComponent<DialogueManager>();
Inventory = GetComponent<Inventory>();
SaveSystem.Load();
SaveSystem.Save(); // save off any corruption fixes
}
#region Storybool Save/Load
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: c43e00cda947506459d64184fe41f882
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
+220
View File
@@ -0,0 +1,220 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class Inventory : MonoBehaviour
{
public ItemSlot[] itemSlots;
public BackPackType ownedBackpackType;
InventorySlotUI inventoryDisplay;
private bool showingInventory = false;
private void Update()
{
if (inventoryDisplay == null)
{
inventoryDisplay = GameObject.FindWithTag("InventorySlots").GetComponent<InventorySlotUI>();
inventoryDisplay.gameObject.SetActive(false);
}
if (Input.GetKeyDown(KeyCode.Tab))
{
if (showingInventory)
{
inventoryDisplay.HideSlots();
Time.timeScale = 1;
}
else
{
inventoryDisplay.ShowSlots();
Time.timeScale = 0;
}
showingInventory = !showingInventory;
}
}
// we'll set this up to only try to add one item to the inventory at a time for now
// there won't be stacked items in the wild for right now
public bool AddItem(Item item, int quantity)
{
// first try to stack
for (int i = 0; i < itemSlots.Length; i++)
{
if (itemSlots[i].item == item.id && itemSlots[i].quantity < item.maxStack)
{
itemSlots[i].quantity += 1;
return true;
}
}
// no stack to add to, try to find an empty slot
for (int i = 0; i < itemSlots.Length; i++)
{
if (itemSlots[i].item == ItemIdEnum.NONE)
{
itemSlots[i] = new ItemSlot();
itemSlots[i].quantity = 1;
itemSlots[i].item = item.id;
return true;
}
}
// no open slots, no open stacks, we can't add the item
return false;
}
public bool RemoveItemQuantity(ItemIdEnum itemId, int quantity)
{
if (GetItemQuantity(itemId) >= quantity)
{
int currentQuantity = quantity;
for (int i = 0; i < itemSlots.Length; i++)
{
if (itemSlots[i].item == itemId)
{
// we are trying to remove less items than what exists in the current
// stack, so just subtract from the stack
if (currentQuantity < itemSlots[i].quantity)
{
itemSlots[i].quantity -= currentQuantity;
break;
}
else if (currentQuantity == itemSlots[i].quantity)
{
// we have exactly enough in the current slot, so nullify it and
// exit
itemSlots[i].item = ItemIdEnum.NONE;
break;
}
else
{
// we don't have enough in the stack, but we DO have enough in our
// inventory. nullify the stack, subtract that number from the current
// count, and move to the next item
currentQuantity -= itemSlots[i].quantity;
itemSlots[i].item = ItemIdEnum.NONE;
}
}
}
return true;
}
else
{
return false;
}
}
public int GetItemQuantity(ItemIdEnum itemId)
{
int itemCount = 0;
foreach (ItemSlot slot in itemSlots)
{
if (slot.item == itemId)
{
itemCount += slot.quantity;
}
}
return itemCount;
}
private int GetItemSlotNum()
{
switch (ownedBackpackType)
{
case BackPackType.Pockets:
return 8;
case BackPackType.SmallBag:
return 16;
case BackPackType.LargeBag:
return 32;
}
return 0;
}
#region Inventory Save/Load
public void SaveInventory(ref InventorySaveData data)
{
data.itemSlots = itemSlots;
data.ownedBackpack = ownedBackpackType;
}
public void LoadInventory(InventorySaveData data)
{
ownedBackpackType = data.ownedBackpack;
if (data.itemSlots != null && data.itemSlots.Length > 0)
{
itemSlots = data.itemSlots;
// list is somehow corrupted, fill in the rest of the slots
if (itemSlots.Length < GetItemSlotNum())
{
ItemSlot[] newSlots = new ItemSlot[GetItemSlotNum()];
for (int i = 0; i < itemSlots.Length; i++)
{
if (itemSlots[i] != null)
newSlots[i] = itemSlots[i];
else
newSlots[i] = new ItemSlot();
}
itemSlots = newSlots;
}
}
else if (data.itemSlots != null && data.itemSlots.Length > GetItemSlotNum())
{
// list is bigger than it should be, generate new slots of the proper size
// and save just the items in the slots we SHOULD have. (shouldn't ever happen)
ItemSlot[] newSlots = new ItemSlot[GetItemSlotNum()];
for (int i = 0; i < newSlots.Length; i++)
{
if (itemSlots[i] != null)
newSlots[i] = itemSlots[i];
else
newSlots[i] = new ItemSlot();
}
itemSlots = newSlots;
}
else
{
itemSlots = new ItemSlot[GetItemSlotNum()];
for (int i = 0; i < itemSlots.Length; i++)
{
itemSlots[i] = new ItemSlot();
}
}
}
#endregion
}
public enum BackPackType
{
Pockets = 0,
SmallBag = 1,
LargeBag = 2
}
[System.Serializable]
public class ItemSlot
{
public int quantity;
public ItemIdEnum item;
}
[System.Serializable]
public struct InventorySaveData
{
public ItemSlot[] itemSlots;
public BackPackType ownedBackpack;
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 40e5d5183cb59cf4d8603dca70c1a9b5
@@ -0,0 +1,42 @@
using UnityEngine;
using UnityEngine.UI;
// This is the container for the inventory UI slots
public class InventorySlotUI : MonoBehaviour
{
public ItemSlotUI[] slots;
ItemNameUI itemNameUI;
ItemDescriptionUI itemDescriptionUI;
private void Start()
{
slots = GetComponentsInChildren<ItemSlotUI>();
itemNameUI = GetComponentInChildren<ItemNameUI>();
itemDescriptionUI = GetComponentInChildren<ItemDescriptionUI>();
}
public void ShowSlots()
{
gameObject.SetActive(true);
for (int i = 0; i < slots.Length; i++)
{
if (GameManager.Instance.Inventory.itemSlots[i] != null)
{
slots[i].SetItem(GameManager.Instance.Inventory.itemSlots[i]);
}
}
foreach (ItemSlotUI slot in slots)
{
slot.LoadImage(itemNameUI, itemDescriptionUI);
}
}
public void HideSlots()
{
itemNameUI.nameText = "";
itemDescriptionUI.descriptionText = "";
gameObject.SetActive(false);
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 6035a8fed1049624f8d8077553b85847
@@ -0,0 +1,20 @@
using TMPro;
using UnityEngine;
public class ItemDescriptionUI : MonoBehaviour
{
public string descriptionText = "";
TextMeshProUGUI text;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
text = GetComponent<TextMeshProUGUI>();
}
// Update is called once per frame
void Update()
{
text.text = descriptionText;
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: bd581500fc7211347bbe165c6ea8d780
+20
View File
@@ -0,0 +1,20 @@
using TMPro;
using UnityEngine;
public class ItemNameUI : MonoBehaviour
{
public string nameText = "";
TextMeshProUGUI text;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
text = GetComponent<TextMeshProUGUI>();
}
// Update is called once per frame
void Update()
{
text.text = nameText;
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 798c4437c91e97b43893dcdfeb918617
+53
View File
@@ -0,0 +1,53 @@
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ItemSlotUI : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
// Kind of confusing here... ItemSlot refers to the class that is the DATA for a
// "slot". It is used for serialization and keeping track of the inventory in data.
// ItemSlot simply has an item ID and a quantity. ItemSlotUI is the visual represenation of this.
private ItemSlot slotData;
private int quantity;
private Image image;
private Item itemObj;
// (I hate this approach) passed in from InventorySlotUI
private ItemNameUI itemNameUI;
private ItemDescriptionUI itemDescriptionUI;
private void Start()
{
image = GetComponentsInChildren<Image>()[1]; //ignore parent
}
public void LoadImage(ItemNameUI itemNameUI, ItemDescriptionUI itemDescriptionUI)
{
if (this.itemNameUI == null) this.itemNameUI = itemNameUI;
if (this.itemDescriptionUI == null) this.itemDescriptionUI = itemDescriptionUI;
itemObj = new Item(slotData.item);
image.sprite = Resources.Load<Sprite>(itemObj.itemImagePath);
}
public void SetItem(ItemSlot itemSlot)
{
this.slotData = itemSlot;
}
public void OnPointerEnter(PointerEventData pointerEventData)
{
if (itemObj != null && itemObj.id != ItemIdEnum.NONE)
{
itemNameUI.nameText = itemObj.itemName;
itemDescriptionUI.descriptionText = itemObj.description;
}
}
public void OnPointerExit(PointerEventData pointerEventData)
{
itemNameUI.nameText = "";
itemDescriptionUI.descriptionText = "";
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 26900fa2242c1514fb991ab59cf5f883
+45 -1
View File
@@ -1,14 +1,58 @@
using System.Runtime.InteropServices.WindowsRuntime;
using UnityEngine;
// This is the actual ITEM data structure for keeping track of inventory/item actions
public class Item : MonoBehaviour
public class Item
{
public string itemName;
public ItemIdEnum id;
public int maxStack = 10;
public string description = "";
public string itemImagePath = "";
public Item(string itemName, ItemIdEnum id, int maxStack, string description, string itemImagePath)
{
this.itemName = itemName;
this.id = id;
this.maxStack = maxStack;
this.description = description;
this.itemImagePath = itemImagePath;
}
public Item(ItemIdEnum id)
{
Item newItem = ItemUtilities.GetItemFromId(id);
this.itemName = newItem.itemName;
this.id = newItem.id;
this.maxStack = newItem.maxStack;
this.description = newItem.description;
this.itemImagePath= newItem.itemImagePath;
}
}
public enum ItemIdEnum
{
NONE = 0,
STAR_SHARD = 1
}
public static class ItemUtilities
{
// THIS IS WHERE ALL THE ITEMS ARE DEFINED
public static Item GetItemFromId(ItemIdEnum itemId)
{
switch (itemId)
{
case ItemIdEnum.STAR_SHARD:
return new Item(
itemName: "Star Shard",
id: itemId,
maxStack: 10,
description: "A rare material that can only be found here in the valley.",
itemImagePath: "ItemSprites/starShard");
}
return new Item("", itemId, 0, "", "ItemSprites/noneItem"); ;
}
}
+12 -4
View File
@@ -3,13 +3,21 @@
// this class is specifically referring to items in the world that can be picked up
public class PickupableItem : Interactable
{
public Item item;
public ItemIdEnum itemId;
public int quantity;
private Item itemObj;
private void Start()
{
itemObj = new Item(itemId);
Debug.Log(itemObj.itemName);
}
public override void Interact()
{
if (GameManager.Instance.Inventory.AddItem(item))
if (GameManager.Instance.Inventory.AddItem(itemObj, quantity))
{
Debug.Log("Picked up " + item.itemName + "!");
MoveOutsideRange(); // I don't love this but if we destroy the object we probably need to do this first
Destroy(gameObject);
}
@@ -17,7 +25,7 @@ public override void Interact()
public override void MoveInsideRange()
{
GameManager.Instance.DialogueManager.ShowItemText(item.itemName);
GameManager.Instance.DialogueManager.ShowItemText(itemObj.itemName);
}
public override void MoveOutsideRange()
+22 -6
View File
@@ -11,11 +11,14 @@ public class PlayerController : MonoBehaviour
bool isDigging = false;
Interactable nearestInteractable;
private CharacterController characterController;
private Vector3 moveDir;
private float gravity = 10;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
characterController = GetComponent<CharacterController>();
}
// Update is called once per frame
@@ -24,17 +27,30 @@ void Update()
if (!isDigging)
{
RotatePlayerTowardMouse();
Walk();
TryInteract();
}
Walk();
DigDetector();
}
void Walk()
{
transform.position += DougBody.transform.forward * Input.GetAxis("Vertical") * walkSpeed * Time.deltaTime;
transform.position += DougBody.transform.right * Input.GetAxis("Horizontal") * walkSpeed * Time.deltaTime;
moveDir = Vector3.zero;
if (!characterController.isGrounded)
{
moveDir.y -= gravity * Time.deltaTime;
}
if (!isDigging)
{
Vector3 verticalMovement = DougBody.transform.forward * Input.GetAxis("Vertical") * walkSpeed * Time.deltaTime;
Vector3 horizontalMovement = DougBody.transform.right * Input.GetAxis("Horizontal") * walkSpeed * Time.deltaTime;
moveDir += verticalMovement + horizontalMovement;
}
characterController.Move(moveDir);
}
void DigDetector()
@@ -93,7 +109,7 @@ private void OnTriggerExit(Collider other)
Interactable interactable = other.GetComponent<Interactable>();
// store nearest interactable if it exists
if (interactable != null)
if (interactable != null && nearestInteractable != null)
{
nearestInteractable.MoveOutsideRange();
nearestInteractable = null;
+42 -6
View File
@@ -1,3 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class StaticCamRoom : MonoBehaviour
@@ -7,19 +10,22 @@ public class StaticCamRoom : MonoBehaviour
public Transform[] objectsToHide;
public float camZoom = 5f;
private void RoomEnter()
public void RoomEnter()
{
StopAllCoroutines();
foreach (var obj in objectsToHide)
{
obj.gameObject.SetActive(false);
StartCoroutine(FadeOutMaterial(0.5f, obj.gameObject));
}
}
private void RoomExit()
public void RoomExit(List<Transform> wallsToKeep)
{
StopAllCoroutines();
foreach (var obj in objectsToHide)
{
obj.gameObject.SetActive(true);
if (!wallsToKeep.Contains(obj))
StartCoroutine(FadeInMaterial(0.5f, obj.gameObject));
}
}
@@ -30,7 +36,7 @@ private void OnTriggerEnter(Collider other)
if (controller != null)
{
controller.SetCurrentPosition(camPosition.position, camZoom);
RoomEnter();
controller.AddToRoomsActive(this);
}
}
@@ -41,7 +47,37 @@ private void OnTriggerExit(Collider other)
if (controller != null)
{
controller.ReturnCameraToHome();
RoomExit();
controller.RemoveFromRoomsActive(this);
}
}
private IEnumerator FadeOutMaterial(float fadeSpeed, GameObject wallObj)
{
Renderer rend = wallObj.GetComponent<Renderer>();
Color matColor = rend.material.color;
if (matColor.a > 1f)
matColor = new Color(matColor.r, matColor.g, matColor.b, 1f);
while (rend.material.color.a > 0f)
{
matColor.a -= Time.deltaTime / fadeSpeed;
rend.material.color = matColor;
yield return null;
}
rend.material.color = new Color(matColor.r, matColor.g, matColor.b, 0f);
}
private IEnumerator FadeInMaterial(float fadeSpeed, GameObject wallObj)
{
Renderer rend = wallObj.GetComponent<Renderer>();
Color matColor = rend.material.color;
while (rend.material.color.a < 1f)
{
matColor.a += Time.deltaTime / fadeSpeed;
rend.material.color = matColor;
yield return null;
}
rend.material.color = new Color(matColor.r, matColor.g, matColor.b, 1f);
}
}