using Nethereum.ABI.Encoders; using Photon.Pun; using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using UnityEngine; using UnityEngine.UI; public class InventoryManager : MonoBehaviourPunCallbacks { public LootItemScriptableObject FirstWeapon; public int loadedRounds1 = 0; public LootItemScriptableObject SecondWeapon; public int loadedRounds2 = 0; public LootItemScriptableObject PrimaryWeapon; public int loadedRoundsPrimary = 0; public LootItemScriptableObject EquippedThrowable; private Dictionary m_stock = new Dictionary(); public Dictionary Stock { get { if (m_stock == null) { m_stock = new Dictionary(); } return m_stock; } set { #if UNITY_EDITOR StockDisplay = new StockEntry[value.Count]; for(int i=0; i(); // instance.loadedRounds1 = instance.loadedRounds2 = instance.loadedRoundsPrimary = 0; } public int getStock(LootItemScriptableObject item){ return Stock.GetValueOrDefault(item,0); } [SerializeField] public StockEntry[] StockDisplay; public float pickupRadius; public Vector3 offset; PlayerController player; public static InventoryManager instance; private void Awake() { if(!photonView.IsMine){return;} player = GetComponent(); instance = this; } void Start() { if (!photonView.IsMine) { Destroy(this);return; } UIManager.UpdateInventory(this); } int checkInterval = 5; int z = 0; PickupItem item; void Update() { CheckForPickups(); if (Input.GetKeyDown(KeyCode.E)) { if (item != null) { Pickup(item.type, item.count); item.OnPickup(); item = null; } UIManager.UpdateInventory(this); } } public void GiveDefaultLoots(List loots){ foreach(InventoryEntry item in loots){ Pickup(item.loot, item.count,spawnLeftover:false); } } public void Pickup(LootItemScriptableObject type, int count, bool spawnLeftover = true){ bool autoReload = !spawnLeftover; if (type.isWeapon) { if(!type.isPrimary){ if(type == FirstWeapon || type == SecondWeapon){return;} if (player.activeWeaponSlot == 1) { if (SecondWeapon == null) { NewWeapon(2, type, autoReload); } else { if(spawnLeftover)GameManager.instance.SpawnPickup(SecondWeapon.spawnableName, transform.position + transform.forward,1); NewWeapon(2, type, autoReload); } } else { if (FirstWeapon == null) { NewWeapon(1, type, autoReload); } else if (SecondWeapon == null) { NewWeapon(2, type, autoReload); } else { if(spawnLeftover)GameManager.instance.SpawnPickup(FirstWeapon.spawnableName, transform.position + transform.forward,1); NewWeapon(1, type, autoReload); } } } else { if(PrimaryWeapon == null) { NewPrimaryWeapon(type, autoReload); } else { if(spawnLeftover)GameManager.instance.SpawnPickup(PrimaryWeapon.spawnableName, transform.position + transform.forward,1); NewPrimaryWeapon(type, autoReload); } } TryAutoReload(); GameManager.localPlayer.OnWeaponPickup(); }else{ if (Stock.ContainsKey(type)){ Stock[type] += count; } else { Stock.Add(type, count); } if (type.isThrowable) { EquippedThrowable = type; } if (loadedRounds1 == 0 && player.activeWeaponSlot == 0) { player.reload(); Debug.Log("Auto reload"); } else if (loadedRounds2 == 0 && player.activeWeaponSlot == 1) { TryAutoReload(); } } int prevAmount = getStock(type); int leftover = prevAmount - type.carryingLimit; if(prevAmount> type.carryingLimit && spawnLeftover){ Stock[type] = type.carryingLimit; GameManager.instance.SpawnPickup(type.spawnableName, transform.position + transform.forward,1); } UIManager.UpdateInventory(this); } void TryAutoReload(){ try{ player.reload(); Debug.Log("Auto reload"); }catch(Exception e){ } } void NewWeapon(int slot, LootItemScriptableObject weapon, bool autoReload) { if(slot == 1) { FirstWeapon = weapon; //loadedRounds1 = Mathf.Clamp(weapon.roundsPerClip, 0, Stock.GetValueOrDefault(weapon.ammo, 0)); player.activeWeaponSlot = 0; if(autoReload){ Debug.Log("Auto reloading on pickup"); loadedRounds1 = weapon.roundsPerClip; }else{ Debug.Log("not auto reloading"); loadedRounds1=0; } } else { SecondWeapon = weapon; // loadedRounds2 = Mathf.Clamp(weapon.roundsPerClip, 0, Stock.GetValueOrDefault(weapon.ammo, 0)); player.activeWeaponSlot = 1; if(autoReload){ Debug.Log("Auto reloading on pickup"); loadedRounds2 = weapon.roundsPerClip; }else{ Debug.Log("not auto reloading"); loadedRounds2 = 0; } } } void NewPrimaryWeapon(LootItemScriptableObject weapon,bool autoReload) { PrimaryWeapon = weapon; //loadedRounds1 = Mathf.Clamp(weapon.roundsPerClip, 0, Stock.GetValueOrDefault(weapon.ammo, 0)); player.activeWeaponSlot = 2; if(autoReload){ Debug.Log("Auto reloading on pickup"); loadedRoundsPrimary = weapon.roundsPerClip; }else{ Debug.Log("not auto reloading"); loadedRoundsPrimary = 0; } } public void UseThrowable() { if (EquippedThrowable == null) { return; } int remaining = Stock.GetValueOrDefault(EquippedThrowable, 0); if (remaining <= 0) { return; } Stock[EquippedThrowable]--; if(remaining <= 1) { //Its gone now //Find next throwable EquippedThrowable = null; foreach(KeyValuePair keyValuePair in Stock) { if (keyValuePair.Key.isThrowable && keyValuePair.Value > 0) { EquippedThrowable = keyValuePair.Key; break; } } } UIManager.UpdateInventory(this); } void CheckForPickups() { if (z < checkInterval) { z++; } else { z = 0; item = GetOverlappingPickupItem(); if (item != null) { UIManager.instance.PickupText.text = $"Press E to pickup {item.type.displayName}"; } else { UIManager.instance.PickupText.text = ""; } } } public List GetOverlappingPickupItems() { Collider[] colliders = Physics.OverlapSphere(transform.position + offset, pickupRadius); List pickupItems = new List(); foreach (Collider collider in colliders) { if (collider.GetComponent() != null) { pickupItems.Add(collider.GetComponent()); } } return pickupItems; } int selectedThrowableIndex = 0; public void CycleThrowable(){ List throwables= new List(); foreach(KeyValuePair item in Stock){ if(item.Key.isThrowable && item.Value >0){ throwables.Add(item.Key); } } if(selectedThrowableIndex < throwables.Count -1){ selectedThrowableIndex++; }else{ selectedThrowableIndex = 0; } EquippedThrowable = throwables[selectedThrowableIndex]; UIManager.UpdateInventory(this); } public PickupItem GetOverlappingPickupItem() { Collider[] colliders = Physics.OverlapSphere(transform.position + offset, pickupRadius); List pickupItems = new List(); foreach (Collider collider in colliders) { if (collider.GetComponent() != null) { return collider.GetComponent(); } } return null; } private void OnDrawGizmos() { Gizmos.color = Color.yellow; Gizmos.DrawWireSphere(offset + transform.position, pickupRadius); } } [Serializable] public struct StockEntry { public string type; public int count; } [Serializable] public class PickupObjectEntry { public LootItemScriptableObject loot; public GameObject obj; public Transform leftHandIk; }