forked from valheimPlus/ValheimPlus
/
HotkeyBar.cs
106 lines (93 loc) · 4.52 KB
/
HotkeyBar.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using HarmonyLib;
using UnityEngine;
using ValheimPlus.Configurations;
using System.Linq;
using TMPro;
namespace ValheimPlus.UI
{
/// <summary>
/// Shows current and total ammo counts below bow icon, if bow is equipped in hotbar
/// </summary>
[HarmonyPatch(typeof(HotkeyBar), nameof(HotkeyBar.UpdateIcons))]
public static class HotkeyBar_UpdateIcons_Patch
{
private const string hudObjectName = "BowAmmoCounts";
private const string noAmmoDisplay = "No Ammo";
private static void Postfix(ref HotkeyBar __instance, ref Player player)
{
if (Configuration.Current.Hud.IsEnabled && Configuration.Current.Hud.displayBowAmmoCounts > 0)
DisplayAmmoCountsUnderBowHotbarIcon(__instance, player);
}
private static void DisplayAmmoCountsUnderBowHotbarIcon(HotkeyBar __instance, Player player)
{
if (player == null) return;
GameObject ammoCounter = GameObject.Find(hudObjectName);
// Find the bow in the hotbar
ItemDrop.ItemData bow = null;
foreach (ItemDrop.ItemData item in __instance.m_items)
{
if (item.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Bow)
{
if (bow == null || player.IsItemEquiped(item))
bow = item;
}
}
// If there is no bow or it is not equipped, remove the text element
if (bow == null || (Configuration.Current.Hud.displayBowAmmoCounts == 1 && !player.IsItemEquiped(bow)))
{
if (ammoCounter != null)
{
GameObject.Destroy(ammoCounter);
ammoCounter = null;
}
return;
}
// Make sure we have a valid index
if (__instance.m_elements.Count >= bow.m_gridPos.x && bow.m_gridPos.x >= 0)
{
// Create a new text element to display the ammo counts
HotkeyBar.ElementData element = __instance.m_elements[bow.m_gridPos.x];
TMP_Text ammoCounterText;
if (ammoCounter == null)
{
ammoCounter = GameObject.Instantiate(element.m_amount.gameObject, element.m_amount.gameObject.transform.parent, false);
ammoCounter.name = hudObjectName;
ammoCounter.SetActive(true);
Vector3 offset = element.m_amount.gameObject.transform.position - element.m_icon.transform.position - new Vector3(0, 15);
ammoCounter.transform.Translate(offset);
ammoCounterText = ammoCounter.GetComponentInChildren<TMP_Text>();
ammoCounterText.fontSize -= 2;
}
else
{
ammoCounterText = ammoCounter.GetComponentInChildren<TMP_Text>();
}
// Attach it to the hotbar icon
ammoCounter.gameObject.transform.SetParent(element.m_amount.gameObject.transform.parent, false);
// Find the active ammo being used for thebow
ItemDrop.ItemData ammoItem = player.m_ammoItem;
if (ammoItem == null)
ammoItem = player.GetInventory().GetAmmoItem(bow.m_shared.m_ammoType);
// Calculate totals to display for current ammo type and all types
int currentAmmo = 0;
int totalAmmo = 0;
var inventoryItems = player.GetInventory().GetAllItems();
foreach (ItemDrop.ItemData inventoryItem in inventoryItems)
{
if (inventoryItem.m_shared.m_ammoType == bow.m_shared.m_ammoType &&
(inventoryItem.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Ammo || inventoryItem.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Consumable))
{
totalAmmo += inventoryItem.m_stack;
if (inventoryItem.m_shared.m_name == ammoItem.m_shared.m_name)
currentAmmo += inventoryItem.m_stack;
}
}
// Change the visual display text for the UI
if (totalAmmo == 0)
ammoCounterText.text = noAmmoDisplay;
else
ammoCounterText.text = ammoItem.m_shared.m_name.Split('_').Last() + "\n" + currentAmmo + "/" + totalAmmo;
}
}
}
}