Skip to content

Commit

Permalink
Merge pull request #37 from Hydraxous/inventoryGuiFix
Browse files Browse the repository at this point in the history
Inventory gui fix
  • Loading branch information
Hydraxous authored Mar 19, 2023
2 parents 600a25a + aa27826 commit fbffde3
Show file tree
Hide file tree
Showing 11 changed files with 462 additions and 143 deletions.
163 changes: 98 additions & 65 deletions Components/InventoryControllerDeployer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using System.Runtime.CompilerServices;

namespace UltraFunGuns
{
Expand All @@ -25,89 +26,121 @@ public class InventoryControllerDeployer : MonoBehaviour

private void Awake()
{

inventoryKey = UltraFunGuns.INVENTORY_KEY.Value;
om = MonoSingleton<OptionsManager>.Instance;
canvas = GetComponent<RectTransform>();
pauseMenu = transform.Find("PauseMenu").gameObject;
HydraLoader.prefabRegistry.TryGetValue("UFGInventoryUI", out GameObject controllerObject);
HydraLoader.prefabRegistry.TryGetValue("UFGInventoryButton", out GameObject pauseMenuInvButton);
invControllerButton = GameObject.Instantiate<GameObject>(pauseMenuInvButton, canvas).GetComponent<Button>();
invControllerButton.onClick.AddListener(OpenInventory);
invController = GameObject.Instantiate<GameObject>(controllerObject, canvas).GetComponent<InventoryController>();
invController.gameObject.SetActive(false);
invControllerButton.gameObject.SetActive(false);
}

configHelpMessage = invController.transform.Find("ConfigMessage");
private void Update()
{
CheckThings();
NewCheckStatus();
}

configHelpButton = invController.transform.Find("MenuBorder/SlotNames").GetComponent<Button>();
configHelpButton.onClick.AddListener(SendConfigHelpMessage);
private void CheckThings()
{
if(om == null)
{
om = MonoSingleton<OptionsManager>.Instance;
}

if(canvas == null)
{
canvas = GetComponent<RectTransform>();
}

if(pauseMenu == null)
{
pauseMenu = transform.Find("PauseMenu").gameObject;
}

if (invController == null)
{
HydraLoader.prefabRegistry.TryGetValue("UFGInventoryUI", out GameObject controllerObject);
invController = GameObject.Instantiate<GameObject>(controllerObject, canvas).GetComponent<InventoryController>();
invController.gameObject.SetActive(false);
configHelpMessage = invController.transform.Find("ConfigMessage");
configHelpButton = invController.transform.Find("MenuBorder/SlotNames").GetComponent<Button>();
configHelpButton.onClick.AddListener(SendConfigHelpMessage);
}

if (invControllerButton == null)
{
HydraLoader.prefabRegistry.TryGetValue("UFGInventoryButton", out GameObject pauseMenuInvButton);
invControllerButton = GameObject.Instantiate<GameObject>(pauseMenuInvButton, canvas).GetComponent<Button>();
invControllerButton.onClick.AddListener(OpenInventory);
invControllerButton.gameObject.SetActive(false);
}
}

private void Update()

private void NewCheckStatus()
{
if (UltraFunGuns.InLevel())
if (!UltraFunGuns.InLevel())
{

if (om.paused)
{
if (inventoryManagerOpen)
{
invController.gameObject.SetActive(true);
}
else
{
invController.gameObject.SetActive(false);
invControllerButton.gameObject.SetActive(true);
configHelpMessage.gameObject.SetActive(false);
sentBindingMessage = false;
}
return;
}

}
else
{
if(invController.data.firstTimeModLoaded)
{
MonoSingleton<HudMessageReceiver>.Instance.SendHudMessage(String.Format("UFG: Set a custom loadout for UFG weapons with [<color=orange>{0}</color>] or in the pause menu.",inventoryKey.ToString()), "", "", 2);
invController.data.firstTimeModLoaded = false;
}

if (inventoryManagerOpen)
{
inventoryManagerOpen = false;
invController.gameObject.SetActive(false);

}
invControllerButton.gameObject.SetActive(false);
}

if(Input.GetKeyDown(inventoryKey) && !om.paused)
if (inventoryManagerOpen)
{

if (Input.GetKeyDown(KeyCode.Escape))
{
OpenInventory();
CloseInventory();
}
}

invControllerButton.gameObject.SetActive(om.paused && !inventoryManagerOpen);

if (Input.GetKeyDown(UltraFunGuns.INVENTORY_KEY.Value) && !om.paused)
{
OpenInventory();
}
}

public void OpenInventory()
{
if(!inventoryManagerOpen)
if (invController.gameObject.activeInHierarchy)
{
if (!om.paused)
{
om.Pause();
}
return;
}

if (invController.data.firstTimeUsingInventory)
{
MonoSingleton<HudMessageReceiver>.Instance.SendHudMessage("WARNING: Having UFG weapons enabled at any point will enable the Major Assists for the duration of the level.", "", "", 4);
invController.data.firstTimeUsingInventory = false;
}
pauseMenu.SetActive(false);
invControllerButton.gameObject.SetActive(false);
invController.gameObject.SetActive(true);
inventoryManagerOpen = true;
}
if (om.paused)
{
om.UnPause();
}

om.paused = true;

invController.RefreshSlotKeyDisplays();

GameState ufgInvState = new GameState("ufg_inv", invController.gameObject);
ufgInvState.cursorLock = LockMode.Unlock;
ufgInvState.playerInputLock = LockMode.Lock;
ufgInvState.cameraInputLock = LockMode.Lock;
ufgInvState.priority = 2;
GameStateManager.Instance.RegisterState(ufgInvState);

invControllerButton.gameObject.SetActive(false);
invController.gameObject.SetActive(true);
inventoryManagerOpen = true;
}

public void CloseInventory()
{
inventoryManagerOpen = false;
om.paused = false;
configHelpMessage.gameObject.SetActive(false);
invController.gameObject.SetActive(false);
if (invController.data.firstTimeModLoaded)
{
MonoSingleton<HudMessageReceiver>.Instance.SendHudMessage(String.Format("UFG: Set a custom loadout for UFG weapons with [<color=orange>{0}</color>] or in the pause menu.", inventoryKey.ToString()), "", "", 2);
invController.data.firstTimeModLoaded = false;

}
else if (!invController.data.lecturedAboutVersion && !UltraFunGuns.UsingLatestVersion)
{
MonoSingleton<HudMessageReceiver>.Instance.SendHudMessage($"UFG: You are using an outdated version of UltraFunGuns. Consider updating to <color=orange>{UltraFunGuns.RELEASE_VERSION}</color>.", "", "", 2);
invController.data.lecturedAboutVersion = true;
}
}

public void SendConfigHelpMessage()
Expand Down
100 changes: 99 additions & 1 deletion Components/UFGWeaponManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
using UnityEngine;
using HarmonyLib;
using UnityEngine.SceneManagement;
using System.Reflection;
using System;
using System.Linq;

namespace UltraFunGuns
{
Expand All @@ -11,6 +14,8 @@ public class UFGWeaponManager : MonoBehaviour
{
GunControl gc;

public static bool WeaponsInUse { get; private set; }

private List<List<string>> weaponKeySlots = new List<List<string>>();

//Empty slots for the weapons. Don't remove this.
Expand Down Expand Up @@ -57,6 +62,8 @@ private void Awake()

public List<List<string>> CreateWeaponKeyset(InventoryControllerData invControllerData)
{
int wepCount = 0;

List<List<string>> newWeaponKeys = new List<List<string>>();
for (int x = 0; x < invControllerData.slots.Length; x++)
{
Expand All @@ -65,17 +72,28 @@ public List<List<string>> CreateWeaponKeyset(InventoryControllerData invControll
{
if(invControllerData.slots[x].slotNodes[y].weaponEnabled)
{
++wepCount;
newWeaponKeyList.Add(invControllerData.slots[x].slotNodes[y].weaponKey);
}
}
newWeaponKeys.Add(newWeaponKeyList);
}



return newWeaponKeys;
}

//Gets weapon prefabs from the Data loader and instantiates them into the world and adds them to the gun controllers lists.
public void DeployWeapons(bool firstTime = false)
{

if(!LevelCheck.InLevel())
{
WeaponsInUse = false;
return;
}

string sceneName = SceneManager.GetActiveScene().name;
bool deploy = true;

Expand Down Expand Up @@ -136,6 +154,8 @@ public void DeployWeapons(bool firstTime = false)
//adds weapons to the gun controller
private void AddWeapons()
{
bool weaponsUsed = false;

for (int j = 0; j < customSlots.Count; j++)
{
if (gc.slots.Contains(customSlots[j]))
Expand All @@ -152,13 +172,30 @@ private void AddWeapons()
{
if (!gc.allWeapons.Contains(wep))
{
weaponsUsed = true;
gc.allWeapons.Add(wep);
AddWeaponToFreshnessDict(wep);
}
}

}


if (LevelCheck.CurrentLevelType == LevelCheck.UKLevelType.Endless)
{
if (WeaponsInUse)
{
if (!EndlessGrid.Instance.GetComponent<Collider>().enabled)
{
HudMessageReceiver.Instance.SendHudMessage("You must restart the level after disabling UFG weapons for your score to count.");
weaponsUsed = true;
}
}else if(weaponsUsed)
{
HudMessageReceiver.Instance.SendHudMessage("Warning: having any UFG weapons enabled will prevent your CyberGrind score from being submitted.");
}
}

WeaponsInUse = weaponsUsed;
}

//TODO fix this, for some reason the input on switching to weapons doesn't work past slot 7. No its not because of the keycodes, that was an attempt to fix it. Inspect the GunControl class closer.
Expand Down Expand Up @@ -201,5 +238,66 @@ private void NewStyleItem(string name, string text)
MonoSingleton<StyleHUD>.Instance.RegisterStyleItem("hydraxous.ultrafunguns." + name, text);
}
}

//Credit to Agent of Nyarlathotep for this, thanks fren <3
private static Dictionary<GameObject, float> FreshnessList
{
get
{
var field = typeof(StyleHUD).GetField("weaponFreshness", BindingFlags.NonPublic | BindingFlags.Instance);
Dictionary<GameObject, float> freshnessList = field.GetValue(MonoSingleton<StyleHUD>.Instance) as Dictionary<GameObject, float>;
return freshnessList;
}

set
{
if (value != null)
{
var field = typeof(StyleHUD).GetField("weaponFreshness", BindingFlags.NonPublic | BindingFlags.Instance);
field.SetValue(MonoSingleton<StyleHUD>.Instance, value);
}
}
}

/// <summary>
/// Adds weapon to style hud freshness
/// </summary>
/// <param name="go">gameObject to add</param>
/// <returns></returns>
public static bool AddWeaponToFreshnessDict(GameObject go)
{

if (go == null)
{
//HydraLogger.Log($"WeaponManager: Attempted to register null gameobject into freshness dict.", DebugChannel.Error);
return false;
}

try
{
Dictionary<GameObject, float> freshnessDict = FreshnessList;
if (!freshnessDict.ContainsKey(go))
{
freshnessDict.Add(go, 10f);
FreshnessList = freshnessDict;
return true;
}

//HydraLogger.Log($"WeaponManager: Attempted to register existing weapon to freshness dict.", DebugChannel.Error);
return false;

}
catch (Exception ex)
{
//HydraLogger.Log($"WeaponManager: Could not register {go.name} to freshness dict.\n{ex.Message}\n{ex.StackTrace}", DebugChannel.Fatal);
}

return false;
}

private void OnDestroy()
{
WeaponsInUse = false;
}
}
}
Loading

0 comments on commit fbffde3

Please sign in to comment.