You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

618 lines
25 KiB
C#

using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using SiegeSong;
namespace SiegeSong
{
public class InventoryManager : MonoBehaviour
{
public HintManager HintManager;
public InstanceManager InstanceManager;
public EquipmentManager EquipmentManager;
public ApparrelManager ApparrelManager;
public RuntimeManager RuntimeManager;
public AudioClip[] OpenMenuSounds;
public AudioClip[] MenuClickSounds;
public AudioClip[] MenuActivateSounds;
public GameObject MenuWrapper;
public GameObject WorldHudWrapper;
public bool InventoryScreenOpen;
public GameObject CameraObject;
public GameObject TransferScreen;
public GameObject InventoryScreen;
public Text DescriptionLabel;
public Text EffectsLabel;
//public SkinnedMesh PreviewMesh;
public Text CurrentHealthLabel;
public Text MaxHealthLabel;
public Slider HealthSlider;
public Text CurrentStaminaLabel;
public Text MaxStaminaLabel;
public Slider StaminaSlider;
public Text CurrentMagicLabel;
public Text MaxMagicLabel;
public Slider MagicSlider;
public Text CurrentCarryWeightLabel;
public Text MaxCarryWeightLabel;
public Text CurrentGoldLabel;
public StatisticsManager Actor;
public Container Inventory;
public bool TransferScreenOpen;
public bool TransferTargetSelf;
public Text TransferSelfLabel;
public Text TransferOtherLabel;
public GameObject TransferOtherPointer;
public GameObject TransferSelfPointer;
public Container TransferOther;
public GameObject TransferRowArea;
public GameObject TransferRowPrefab;
public GameObject ItemDictionary;
public int SelectedItemID;
public int SelectedAmount;
public GameObject InventoryRowArea;
public GameObject InventoryRowPrefab;
public bool OpenInputNewState;
public bool UpArrowInputNewState;
public bool DownArrowInputNewState;
public bool ActivateInputNewState;
public bool DropInputNewState;
public int SelectedItemIndex;
public int ActiveTab;
public int ActivePage;
public Transform DroppedItemStartPosition;
public bool DroppingItem;
private Color enabledTextColor;
private Color disabledTextColor;
private float transferRowHeight = 24.0f;
private float inventoryRowHeight = 24.0f;
private float inputAxisThreshhold = 0.1f;
private float rowsXOffset = 0f;
private float rowsYOffset = 0f;
private List<GameObject> TransferRows;
private List<GameObject> inventoryRowObjects;
private Dictionary<int, GameObject> inventoryRowObjectDictionary;
void Start()
{
Inventory.InventoryManager = this;
SelectedAmount = 1;
}
public void SelectNextRow(bool previous = false)
{
var soundIndex = Random.Range(0, MenuClickSounds.Length);
Actor.AudioSource.clip = MenuClickSounds[soundIndex];
Actor.AudioSource.Play();
if (previous)
{
if (SelectedItemIndex > 0)
SelectedItemIndex--;
else
SelectedItemIndex = TransferScreenOpen && !TransferTargetSelf ? TransferOther.Contents.Count - 1 : Inventory.Contents.Count - 1;
}
else
{
if (SelectedItemIndex >= (TransferScreenOpen && !TransferTargetSelf ? TransferOther.Contents.Count - 1 : Inventory.Contents.Count - 1))
SelectedItemIndex = 0;
else
SelectedItemIndex++;
}
if (InventoryScreenOpen || (TransferScreenOpen && TransferTargetSelf))
SelectedItemID = Inventory.Contents.Keys.ElementAt(SelectedItemIndex);
if (TransferScreenOpen && !TransferTargetSelf)
SelectedItemID = TransferOther.Contents.Keys.ElementAt(SelectedItemIndex);
if (InventoryScreenOpen)
UpdateInventoryScreen();
if (TransferScreenOpen)
UpdateTransferScreen();
}
public void SelectPreviousRow()
{
SelectNextRow(true);
}
public void TransferSelectedItem()
{
var soundIndex = Random.Range(0, MenuActivateSounds.Length);
Actor.AudioSource.clip = MenuActivateSounds[soundIndex];
Actor.AudioSource.Play();
if (TransferTargetSelf)
{
TransferOther.AddItem(SelectedItemID, SelectedAmount);
RemoveItem();
if (InventoryScreenOpen)
UpdateInventoryScreen();
if (TransferScreenOpen)
UpdateTransferScreen();
}
else
{
TransferOther.RemoveItem(SelectedItemID, SelectedAmount);
AddItem();
if (InventoryScreenOpen)
UpdateInventoryScreen();
if (TransferScreenOpen)
UpdateTransferScreen();
}
}
public void SwitchTransferTarget()
{
SelectedItemIndex = 0;
TransferTargetSelf = !TransferTargetSelf;
HintManager.SetHint(HintArea.FarLeft, $"{(TransferTargetSelf ? "Take" : "Give")} Item", InputButton.ActivateAccept);
}
void Update()
{
if (TransferScreenOpen || InventoryScreenOpen)
{
if (InventoryScreenOpen)
{
CurrentHealthLabel.text = Actor.Stats.Health.ToString("0");
MaxHealthLabel.text = Actor.Stats.MaxHealth.ToString("0");
HealthSlider.value = Actor.Stats.Health;
CurrentStaminaLabel.text = Actor.Stats.Stamina.ToString("0");
MaxStaminaLabel.text = Actor.Stats.MaxStamina.ToString("0");
StaminaSlider.value = Actor.Stats.Stamina;
CurrentMagicLabel.text = Actor.Stats.Magic.ToString("0");
MaxMagicLabel.text = Actor.Stats.MaxMagic.ToString("0");
MagicSlider.value = Actor.Stats.Magic;
}
}
}
public void NextIndex()
{
if (SelectedItemIndex < Inventory.Contents.Count)
SelectedItemIndex++;
else
SelectedItemIndex = 0;
if (InventoryScreenOpen)
UpdateInventoryScreen();
if (TransferScreenOpen)
UpdateTransferScreen();
}
public void PreviousIndex()
{
if (SelectedItemIndex > 0)
SelectedItemIndex--;
else
SelectedItemIndex = Inventory.Contents.Count;
if (InventoryScreenOpen)
UpdateInventoryScreen();
if (TransferScreenOpen)
UpdateTransferScreen();
}
public void UpdateTransferScreen()
{
if (TransferRows != null)
{
foreach (var uiRow in TransferRows)
{
uiRow.SetActive(false);
GameObject.Destroy(uiRow);
}
}
for (var i = 0; i < TransferRowArea.transform.childCount; i++)
{
var row = TransferRowArea.transform.GetChild(i);
row.gameObject.SetActive(false);
GameObject.Destroy(row);
}
TransferRows = new List<GameObject>();
var itemCount = TransferTargetSelf || TransferOther == null ? Inventory.Contents.Count : (TransferOther.Contents != null ? TransferOther.Contents.Count : 0);
for (var i = 0; i < itemCount; i++)
{
var uiRow = Instantiate(TransferRowPrefab);
var uiRowProperties = uiRow.GetComponent<InventoryRow>();
uiRowProperties.InventoryManager = this;
var targetItemId = TransferTargetSelf ? Inventory.Contents.Keys.ElementAt(i) : TransferOther.Contents.Keys.ElementAt(i);
var targetItem = GetItemByID(targetItemId);
if (targetItem != null && targetItem.GetComponent<InventoryItem>() != null)
{
uiRowProperties.ItemID = targetItem.GetComponent<InventoryItem>().ID;
var uiRowRect = uiRow.GetComponent<RectTransform>();
uiRowRect.position = new Vector2(0, transferRowHeight * -(i + 1));
uiRowRect.offsetMin = new Vector2(0, transferRowHeight * -(i + 1));
uiRowRect.offsetMax = new Vector2(0, transferRowHeight * -(i));
uiRowRect.sizeDelta = new Vector2(0, transferRowHeight);
foreach (Transform childTransform in uiRow.transform)
{
var child = childTransform.gameObject;
if (child.name == "icon")
{
if (TransferTargetSelf)
{
var activeIdsList = new List<int>(ApparrelManager.CurrentlyActiveIDs);
child.GetComponent<Image>().enabled = activeIdsList.Contains(Inventory.Contents.Keys.ElementAt(i));
}
else if (TransferOther.gameObject.GetComponentInChildren<ApparrelManager>() != null)
{
var activeIdsList = new List<int>(TransferOther.gameObject.GetComponentInChildren<ApparrelManager>().CurrentlyActiveIDs);
child.GetComponent<Image>().enabled = activeIdsList.Contains(Inventory.Contents.Keys.ElementAt(i));
}
}
if (child.name == "highlight")
child.SetActive(SelectedItemIndex == i);
//foreach (Transform hlChildTransform in childTransform)
//hlChildTransform.gameObject.GetComponent<Image>().enabled = SelectedItemIndex == i;
if (child.name == "name")
child.GetComponent<Text>().text = GetItemByID(TransferTargetSelf ? Inventory.Contents.Keys.ElementAt(i) : TransferOther.Contents.Keys.ElementAt(i)).GetComponent<InventoryItem>().FriendlyName;
if (child.name == "weight")
child.GetComponent<Text>().text = GetItemByID(TransferTargetSelf ? Inventory.Contents.Keys.ElementAt(i) : TransferOther.Contents.Keys.ElementAt(i)).GetComponent<InventoryItem>().PhysicalWeight.ToString();
if (child.name == "value")
child.GetComponent<Text>().text = GetItemByID(TransferTargetSelf ? Inventory.Contents.Keys.ElementAt(i) : TransferOther.Contents.Keys.ElementAt(i)).GetComponent<InventoryItem>().MonetaryValue.ToString();
}
TransferRows.Add(uiRow);
}
}
}
public void OpenInventoryScreen()
{
var soundIndex = Random.Range(0, OpenMenuSounds.Length);
HintManager.SetHint(HintArea.FarLeft, "Equip to Main-Hand", InputButton.ActivateAccept);
HintManager.SetHint(HintArea.MidLeft, "Equip to Off-Hand", InputButton.JumpClimb);
HintManager.SetHint(HintArea.MidRight, "Drop Item", InputButton.SwapUseAbility);
HintManager.SetHint(HintArea.FarRight, "Exit Menu", InputButton.BackReject);
Actor.AudioSource.clip = OpenMenuSounds[soundIndex];
Actor.AudioSource.Play();
WorldHudWrapper.SetActive(false);
MenuWrapper.SetActive(true);
InventoryScreenOpen = true;
InventoryScreen.SetActive(true);
TransferTargetSelf = true;
UpdateInventoryScreen();
}
public void OpenTransferMenu(Container transferOther)
{
var soundIndex = Random.Range(0, OpenMenuSounds.Length);
HintManager.SetHint(HintArea.FarLeft, $"{(TransferTargetSelf ? "Take" : "Give")} Item", InputButton.ActivateAccept);
HintManager.SetHint(HintArea.MidLeft, "Swap Targets", InputButton.SwapUseAbility);
HintManager.SetHint(HintArea.Center, "Exit Menu", InputButton.BackReject);
HintManager.SetHint(HintArea.MidRight, "");
HintManager.SetHint(HintArea.FarRight, "");
Actor.AudioSource.clip = OpenMenuSounds[soundIndex];
Actor.AudioSource.Play();
WorldHudWrapper.SetActive(false);
MenuWrapper.SetActive(true);
SelectedItemIndex = 0;
TransferOther = transferOther;
TransferScreenOpen = true;
TransferTargetSelf = false;
TransferScreen.SetActive(true);
UpdateTransferScreen();
}
public void CloseInventoryScreen()
{
var soundIndex = Random.Range(0, OpenMenuSounds.Length);
HintManager.SetHint(HintArea.FarLeft, "");
HintManager.SetHint(HintArea.MidLeft, "");
HintManager.SetHint(HintArea.Center, "");
HintManager.SetHint(HintArea.MidRight, "");
HintManager.SetHint(HintArea.FarRight, "");
Actor.AudioSource.clip = OpenMenuSounds[soundIndex];
Actor.AudioSource.Play();
WorldHudWrapper.SetActive(true);
MenuWrapper.SetActive(false);
InventoryScreenOpen = false;
InventoryScreen.SetActive(false);
}
public void CloseTransferScreen()
{
var soundIndex = Random.Range(0, OpenMenuSounds.Length);
Actor.AudioSource.clip = OpenMenuSounds[soundIndex];
Actor.AudioSource.Play();
WorldHudWrapper.SetActive(true);
MenuWrapper.SetActive(false);
TransferTargetSelf = true;
TransferScreenOpen = false;
TransferScreen.SetActive(false);
}
public void AddInventoryRow(int itemID, int itemQuantity)
{
var uiRow = Instantiate(InventoryRowPrefab);
if (inventoryRowObjects == null)
{
inventoryRowObjectDictionary = new Dictionary<int, GameObject>();
inventoryRowObjects = new List<GameObject>();
}
inventoryRowObjectDictionary.Add(itemID, uiRow);
inventoryRowObjects.Add(uiRow);
var uiRowProperties = uiRow.GetComponent<InventoryRow>();
var uiRowRect = uiRow.GetComponent<RectTransform>();
uiRow.SetActive(true);
uiRowProperties.InventoryManager = this;
uiRowProperties.ItemID = itemID;
uiRowRect.rotation = Quaternion.Euler(Vector3.zero);
uiRow.transform.SetParent(InventoryRowArea.transform, false);
uiRowRect.position = new Vector2(0, inventoryRowHeight * -(inventoryRowObjects.Count + 1));
uiRowRect.offsetMin = new Vector2(0, inventoryRowHeight * -(inventoryRowObjects.Count + 1));
uiRowRect.offsetMax = new Vector2(0, inventoryRowHeight * -(inventoryRowObjects.Count));
uiRowRect.sizeDelta = new Vector2(0, inventoryRowHeight);
uiRow.transform.localScale = Vector3.one;
UpdateInventoryRow(itemID, itemQuantity);
}
public void UpdateInventoryRow(int itemID, int itemQuantity, bool selected = false)
{
var uiRow = inventoryRowObjectDictionary[itemID];
var uiRowProperties = uiRow.GetComponent<InventoryRow>();
if (selected)
{
DescriptionLabel.text = GetItemByID(itemID).GetComponent<InventoryItem>().Description;
EffectsLabel.text = string.Join("\n", GetItemByID(itemID).GetComponent<InventoryItem>().Effects);
}
var targetItem = GetItemByID(uiRowProperties.ItemID);
if (inventoryRowObjects != null && targetItem != null && targetItem.GetComponent<InventoryItem>() != null)
{
foreach (Transform childTransform in uiRow.transform)
{
var child = childTransform.gameObject;
if (child.name == "icon")
{
if (TransferTargetSelf)
{
var activeIdsList = new List<int>(ApparrelManager.CurrentlyActiveIDs);
child.GetComponent<Image>().enabled = activeIdsList.Contains(itemID);
}
else if (TransferOther != null && TransferOther.gameObject.GetComponentInChildren<ApparrelManager>() != null)
{
var activeIdsList = new List<int>(TransferOther.gameObject.GetComponentInChildren<ApparrelManager>().CurrentlyActiveIDs);
child.GetComponent<Image>().enabled = activeIdsList.Contains(itemID);
}
else
{
child.GetComponent<Image>().enabled = false;
}
}
if (child.name == "highlight")
foreach (Transform hlChildTransform in childTransform)
hlChildTransform.gameObject.GetComponent<Image>().enabled = selected;
if (child.name == "name")
child.GetComponent<Text>().text = GetItemByID(itemID).GetComponent<InventoryItem>().FriendlyName;
if (child.name == "quantity")
child.GetComponent<Text>().text = itemQuantity.ToString();
if (child.name == "weight")
child.GetComponent<Text>().text = GetItemByID(itemID).GetComponent<InventoryItem>().PhysicalWeight.ToString();
if (child.name == "value")
child.GetComponent<Text>().text = GetItemByID(itemID).GetComponent<InventoryItem>().MonetaryValue.ToString();
if (child.name == "condition")
{
var item = GetItemByID(itemID).GetComponent<InventoryItem>();
child.GetComponent<Text>().text = item.PhysicalCondition + "/" + item.MaxPhysicalCondition;
}
}
}
}
public void UpdateInventoryScreen()
{
CurrentCarryWeightLabel.text = Actor.Stats.CarryWeight.ToString("0");
MaxCarryWeightLabel.text = Actor.Stats.MaxCarryWeight.ToString("0");
CurrentGoldLabel.text = Actor.Stats.Gold.ToString("0");
var container = new Dictionary<int, int>();
switch (ActiveTab)
{
// Inventory
case 0:
var itemCategory = InventoryCategory.Items;
switch (ActivePage)
{
case 0:
itemCategory = InventoryCategory.EquipmentAndAparrel;
break;
case 1:
itemCategory = InventoryCategory.Items;
break;
case 2:
itemCategory = InventoryCategory.MapsAndNotes;
break;
}
for (var i = 0; i < Inventory.Contents.Count; i++)
{
var itemQuantity = Inventory.Contents[Inventory.Contents.Keys.ElementAt(i)];
var itemID = Inventory.Contents.Keys.ElementAt(i);
var inventoryItem = GetItemByID(itemID).GetComponent<InventoryItem>();
if (inventoryItem.Category == itemCategory)
container.Add(itemID, itemQuantity);
}
if (SelectedItemID == 0 && Inventory.Contents.Count > 0)
SelectedItemID = Inventory.Contents.Keys.ElementAt(0);
break;
// Abilities & Intel
case 1:
switch (ActivePage)
{
// Activated Abilities
case 0:
break;
// Passive Abilities
case 1:
break;
// Intel (AKA Dialogue-Only Abilities)
case 2:
break;
}
break;
// Character
case 2:
switch (ActivePage)
{
// Skills & Attributes
case 0:
break;
// Factions & Bounties
case 1:
break;
// Tasks
case 2:
break;
}
break;
}
for (var i = 0; i < container.Count; i++)
{
var containerEntry = container.ElementAt(i);
UpdateInventoryRow(containerEntry.Key, containerEntry.Value, SelectedItemIndex == i);
}
}
public GameObject GetItemByID(int ID)
{
for (var i = 0; i < ItemDictionary.transform.childCount; i++)
{
if (ItemDictionary.transform.GetChild(i).GetComponent<InventoryItem>().ID == ID)
{
return ItemDictionary.transform.GetChild(i).gameObject;
}
}
return null;
}
public void DropItem()
{
for (var i = 0; i < SelectedAmount; i++)
{
var inventoryItem = GetItemByID(SelectedItemID).GetComponent<InventoryItem>();
var droppedObject = new WorldObjectPlacement();
droppedObject.FriendlyName = inventoryItem.FriendlyName;
droppedObject.PositionX = DroppedItemStartPosition.position.x;
droppedObject.PositionY = DroppedItemStartPosition.position.y;
droppedObject.PositionZ = DroppedItemStartPosition.position.z;
droppedObject.RotationX = DroppedItemStartPosition.eulerAngles.x;
droppedObject.RotationY = DroppedItemStartPosition.eulerAngles.y;
droppedObject.RotationZ = DroppedItemStartPosition.eulerAngles.z;
droppedObject.Key = inventoryItem.DroppableKey;
InstanceManager.InstantiateWorldObject(droppedObject);
}
RemoveItem();
DroppingItem = false;
}
public void AddItem(int itemID, int itemQuantity)
{
SelectedItemID = itemID;
SelectedAmount = itemQuantity;
AddItem();
}
public void AddItem()
{
AddInventoryRow(SelectedItemID, SelectedAmount);
Inventory.AddItem(SelectedItemID, SelectedAmount);
UpdateInventoryScreen();
UpdateTransferScreen();
}
public void RemoveItem()
{
if (inventoryRowObjectDictionary.ContainsKey(SelectedItemID))
{
var rowObject = inventoryRowObjectDictionary[SelectedItemID];
Inventory.RemoveItem(SelectedItemID, SelectedAmount, this);
rowObject.SetActive(false);
inventoryRowObjects.Remove(rowObject);
inventoryRowObjectDictionary.Remove(SelectedItemID);
Destroy(rowObject);
if (InventoryScreenOpen)
UpdateInventoryScreen();
if (TransferScreenOpen)
UpdateTransferScreen();
}
}
public void ActivateItem()
{
var soundIndex = Random.Range(0, MenuActivateSounds.Length);
Actor.AudioSource.clip = MenuActivateSounds[soundIndex];
Actor.AudioSource.Play();
for (var i = 0; i < SelectedAmount; i++)
{
var gameObject = GetItemByID(SelectedItemID);
if (gameObject)
{
var item = gameObject.GetComponent<InventoryItem>();
if (item != null)
item.Activate(ApparrelManager);
}
}
if (InventoryScreenOpen)
UpdateInventoryScreen();
if (TransferScreenOpen)
UpdateTransferScreen();
}
}
}