using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; using SiegeSong; namespace SiegeSong { public class InventoryManager : MonoBehaviour { public InstanceManager InstanceManager; public EquipmentManager EquipmentManager; public ApparrelManager ApparrelManager; 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 Transform DroppedItemStartPosition; private bool downArrowInputOldState; private bool upArrowInputOldState; private bool openInputOldState; private bool activateInputOldState; private bool dropInputOldState; 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 TransferRows; private List InventoryRows; private Dictionary rows; void Start() { rows = new Dictionary(); InventoryRows = new List(); Inventory.InventoryManager = this; SelectedAmount = 1; } public void OnPointerClick(PointerEventData eventData) { Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name); } void Update() { if (InventoryRows == null) InventoryRows = new List(); if (TransferScreenOpen || InventoryScreenOpen) { if (!upArrowInputOldState && UpArrowInputNewState) { var soundIndex = Random.Range(0, MenuClickSounds.Length); Actor.AudioSource.clip = MenuClickSounds[soundIndex]; Actor.AudioSource.Play(); if (SelectedItemIndex > 0) SelectedItemIndex--; else SelectedItemIndex = TransferScreenOpen && !TransferTargetSelf ? TransferOther.Contents.Count - 1 : Inventory.Contents.Count - 1; if (InventoryScreenOpen) UpdateInventoryScreen(); if (TransferScreenOpen) UpdateTransferScreen(); } if (!downArrowInputOldState && DownArrowInputNewState) { var soundIndex = Random.Range(0, MenuClickSounds.Length); Actor.AudioSource.clip = MenuClickSounds[soundIndex]; Actor.AudioSource.Play(); if (SelectedItemIndex >= (TransferScreenOpen && !TransferTargetSelf ? TransferOther.Contents.Count - 1 : Inventory.Contents.Count - 1)) SelectedItemIndex = 0; else SelectedItemIndex++; if (InventoryScreenOpen) UpdateInventoryScreen(); if (TransferScreenOpen) UpdateTransferScreen(); } if (UpArrowInputNewState) upArrowInputOldState = true; if (DownArrowInputNewState) downArrowInputOldState = true; if (!DownArrowInputNewState && !UpArrowInputNewState) { upArrowInputOldState = false; downArrowInputOldState = false; } if (!OpenInputNewState) { if (ActivateInputNewState || DropInputNewState) { if (InventoryScreenOpen || (TransferScreenOpen && TransferTargetSelf)) SelectedItemID = Inventory.GetIDByOwnedIndex(SelectedItemIndex); if (TransferScreenOpen && !TransferTargetSelf) SelectedItemID = TransferOther.GetIDByOwnedIndex(SelectedItemIndex); if (DropInputNewState) dropInputOldState = true; if (ActivateInputNewState) activateInputOldState = true; } if (activateInputOldState && !ActivateInputNewState) { if (TransferScreenOpen) { if (SelectedItemID != 0) { 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(); } } } else if (InventoryScreenOpen) { var soundIndex = Random.Range(0, MenuActivateSounds.Length); Actor.AudioSource.clip = MenuActivateSounds[soundIndex]; Actor.AudioSource.Play(); ActivateItem(); if (InventoryScreenOpen) UpdateInventoryScreen(); if (TransferScreenOpen) UpdateTransferScreen(); } } } if (dropInputOldState && !DropInputNewState) { if (TransferScreenOpen) { SelectedItemIndex = 0; TransferTargetSelf = !TransferTargetSelf; } else if (InventoryScreenOpen) { DropItem(); if (InventoryScreenOpen) UpdateInventoryScreen(); if (TransferScreenOpen) UpdateTransferScreen(); } } if (!ActivateInputNewState) activateInputOldState = false; if (!DropInputNewState) dropInputOldState = false; } } 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() { TransferSelfLabel.color = TransferTargetSelf ? enabledTextColor : disabledTextColor; TransferSelfLabel.text = Actor.Stats.FirstName; TransferOtherLabel.color = TransferTargetSelf ? disabledTextColor : enabledTextColor; TransferOtherLabel.text = TransferOther.Name; TransferOtherPointer.active = TransferTargetSelf; TransferSelfPointer.active = !TransferTargetSelf; if (TransferRows != null) { foreach (var uiRow in TransferRows) { uiRow.active = false; GameObject.Destroy(uiRow); } } for (var i = 0; i < TransferRowArea.transform.childCount; i++) { var row = TransferRowArea.transform.GetChild(i); row.gameObject.active = false; GameObject.Destroy(row); } TransferRows = new List(); 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(); uiRowProperties.InventoryManager = this; var targetItemId = TransferTargetSelf ? Inventory.GetIDByOwnedIndex(i) : TransferOther.GetIDByOwnedIndex(i); var targetItem = GetItemByID(targetItemId); if (targetItem != null && targetItem.GetComponent() != null) { uiRowProperties.ItemID = targetItem.GetComponent().ID; var uiRowRect = uiRow.GetComponent(); uiRow.transform.parent = TransferRowArea.transform; uiRow.transform.localScale = Vector3.one; 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(ApparrelManager.CurrentlyActiveIDs); child.GetComponent().enabled = activeIdsList.Contains(Inventory.GetIDByOwnedIndex(i)); } else if (TransferOther.gameObject.GetComponentInChildren() != null) { var activeIdsList = new List(TransferOther.gameObject.GetComponentInChildren().CurrentlyActiveIDs); child.GetComponent().enabled = activeIdsList.Contains(Inventory.GetIDByOwnedIndex(i)); } } if (child.name == "highlight") child.active = SelectedItemIndex == i; //foreach (Transform hlChildTransform in childTransform) //hlChildTransform.gameObject.GetComponent().enabled = SelectedItemIndex == i; if (child.name == "name") child.GetComponent().text = GetItemByID(TransferTargetSelf ? Inventory.GetIDByOwnedIndex(i) : TransferOther.GetIDByOwnedIndex(i)).GetComponent().FriendlyName; if (child.name == "weight") child.GetComponent().text = GetItemByID(TransferTargetSelf ? Inventory.GetIDByOwnedIndex(i) : TransferOther.GetIDByOwnedIndex(i)).GetComponent().PhysicalWeight.ToString(); if (child.name == "value") child.GetComponent().text = GetItemByID(TransferTargetSelf ? Inventory.GetIDByOwnedIndex(i) : TransferOther.GetIDByOwnedIndex(i)).GetComponent().MonetaryValue.ToString(); } TransferRows.Add(uiRow); } } } public void OpenInventoryScreen() { var soundIndex = Random.Range(0, OpenMenuSounds.Length); Actor.AudioSource.clip = OpenMenuSounds[soundIndex]; Actor.AudioSource.Play(); WorldHudWrapper.active = false; MenuWrapper.active = true; InventoryScreenOpen = true; InventoryScreen.active = true; TransferTargetSelf = true; UpdateInventoryScreen(); } public void OpenTransferMenu(Container transferOther) { var soundIndex = Random.Range(0, OpenMenuSounds.Length); Actor.AudioSource.clip = OpenMenuSounds[soundIndex]; Actor.AudioSource.Play(); WorldHudWrapper.active = false; MenuWrapper.active = true; SelectedItemIndex = 0; TransferOther = transferOther; TransferScreenOpen = true; TransferTargetSelf = false; TransferScreen.active = true; activateInputOldState = ActivateInputNewState; UpdateTransferScreen(); } public void CloseInventoryScreen() { var soundIndex = Random.Range(0, OpenMenuSounds.Length); Actor.AudioSource.clip = OpenMenuSounds[soundIndex]; Actor.AudioSource.Play(); WorldHudWrapper.active = true; MenuWrapper.active = false; InventoryScreenOpen = false; InventoryScreen.active = false; } public void CloseTransferScreen() { var soundIndex = Random.Range(0, OpenMenuSounds.Length); Actor.AudioSource.clip = OpenMenuSounds[soundIndex]; Actor.AudioSource.Play(); WorldHudWrapper.active = true; MenuWrapper.active = false; TransferTargetSelf = true; TransferScreenOpen = false; TransferScreen.active = false; } public void UpdateInventoryScreen() { CurrentHealthLabel.text = Actor.Stats.Health.ToString(); MaxHealthLabel.text = Actor.Stats.MaxHealth.ToString(); HealthSlider.value = Actor.Stats.Health; CurrentStaminaLabel.text = Actor.Stats.Stamina.ToString(); MaxStaminaLabel.text = Actor.Stats.MaxStamina.ToString(); StaminaSlider.value = Actor.Stats.Stamina; CurrentMagicLabel.text = Actor.Stats.Magic.ToString(); MaxMagicLabel.text = Actor.Stats.MaxMagic.ToString(); MagicSlider.value = Actor.Stats.Magic; CurrentCarryWeightLabel.text = Actor.Stats.CarryWeight.ToString(); MaxCarryWeightLabel.text = Actor.Stats.MaxCarryWeight.ToString(); CurrentGoldLabel.text = Actor.Stats.Gold.ToString(); foreach(var row in InventoryRows) if (row.active) row.active = false; for (var i = 0; i < Inventory.Contents.Count; i++) { var rowExists = false; var uiRow = rows != null && rows.ContainsKey(Inventory.GetIDByOwnedIndex(i)) ? rows[Inventory.GetIDByOwnedIndex(i)] : Instantiate(InventoryRowPrefab); var uiRowProperties = uiRow.GetComponent(); var uiRowRect = uiRow.GetComponent(); uiRow.active = true; uiRowProperties.InventoryManager = this; uiRowProperties.ItemID = Inventory.GetIDByOwnedIndex(i); uiRow.transform.parent = InventoryRowArea.transform; uiRow.transform.localScale = Vector3.one; uiRowRect.position = new Vector2(0, inventoryRowHeight * -(i + 1)); uiRowRect.offsetMin = new Vector2(0, inventoryRowHeight * -(i + 1)); uiRowRect.offsetMax = new Vector2(0, inventoryRowHeight * -(i)); uiRowRect.sizeDelta = new Vector2(0, inventoryRowHeight); if (SelectedItemIndex == i) { DescriptionLabel.text = GetItemByID(Inventory.GetIDByOwnedIndex(i)).GetComponent().Description; EffectsLabel.text = string.Join("\n", GetItemByID(Inventory.GetIDByOwnedIndex(i)).GetComponent().Effects); } var targetItem = GetItemByID(uiRowProperties.ItemID); if (Inventory != null && Inventory.Contents != null && Inventory.Contents.Count > 0 && InventoryRows != null && targetItem != null && targetItem.GetComponent() != null) { foreach (Transform childTransform in uiRow.transform) { var child = childTransform.gameObject; if (child.name == "icon") { if (TransferTargetSelf) { var activeIdsList = new List(ApparrelManager.CurrentlyActiveIDs); child.GetComponent().enabled = activeIdsList.Contains(Inventory.GetIDByOwnedIndex(i)); } else if (TransferOther != null && TransferOther.gameObject.GetComponentInChildren() != null) { var activeIdsList = new List(TransferOther.gameObject.GetComponentInChildren().CurrentlyActiveIDs); child.GetComponent().enabled = activeIdsList.Contains(Inventory.GetIDByOwnedIndex(i)); } else { child.GetComponent().enabled = false; } } if (child.name == "highlight") foreach (Transform hlChildTransform in childTransform) hlChildTransform.gameObject.GetComponent().enabled = SelectedItemIndex == i; if (child.name == "name") child.GetComponent().text = GetItemByID(Inventory.GetIDByOwnedIndex(i)).GetComponent().FriendlyName; if (child.name == "quantity") child.GetComponent().text = Inventory.Contents.ContainsKey(i) ? Inventory.Contents[i].ToString() : "1"; if (child.name == "weight") child.GetComponent().text = GetItemByID(Inventory.GetIDByOwnedIndex(i)).GetComponent().PhysicalWeight.ToString(); if (child.name == "value") child.GetComponent().text = GetItemByID(Inventory.GetIDByOwnedIndex(i)).GetComponent().MonetaryValue.ToString(); if (child.name == "condition") { var item = GetItemByID(Inventory.GetIDByOwnedIndex(i)).GetComponent(); child.GetComponent().text = item.PhysicalCondition + "/" + item.MaxPhysicalCondition; } } if (!rows.ContainsKey(Inventory.GetIDByOwnedIndex(i))) { InventoryRows.Add(uiRow); rows.Add(Inventory.GetIDByOwnedIndex(i), uiRow); } } } } public GameObject GetItemByID(int ID) { for (var i = 0; i < ItemDictionary.transform.childCount; i++) { if (ItemDictionary.transform.GetChild(i).GetComponent().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(); var droppedObject = new WorldObject(); 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.DroppableKey = inventoryItem.DroppableKey; InstanceManager.InstantiateWorldObject(droppedObject); } RemoveItem(); } public void AddItem() { Inventory.AddItem(SelectedItemID, SelectedAmount); if (InventoryScreenOpen) UpdateInventoryScreen(); if (TransferScreenOpen) UpdateTransferScreen(); } public void RemoveItem() { Inventory.RemoveItem(SelectedItemID, SelectedAmount, this); if (InventoryScreenOpen) UpdateInventoryScreen(); if (TransferScreenOpen) UpdateTransferScreen(); } public void ActivateItem() { for (var i = 0; i < SelectedAmount; i++) { var gameObject = GetItemByID(SelectedItemID); if (gameObject) { var item = gameObject.GetComponent(); if (item != null) item.Activate(ApparrelManager); } } if (InventoryScreenOpen) UpdateInventoryScreen(); if (TransferScreenOpen) UpdateTransferScreen(); } } }