2using System.Reflection;
3using System.Runtime.CompilerServices;
4using System.Collections.Generic;
5using System.Collections.Immutable;
10using Microsoft.Xna.Framework;
16 public class AttachedItems
18 public static void InitStatic()
20 CUI.OnDispose += () =>
23 PrefabMapping.Clear();
29 public static Dictionary<ushort, AttachedItemHandle> All =
new();
30 public static Dictionary<int, Type> PrefabMapping =
new();
32 public static HashSet<AttachedItemHandle> PrevSelected =
new();
33 public static HashSet<AttachedItemHandle> Selected =
new();
35 public static void UpdateAll()
39 if (Character.Controlled is { } controlled)
41 if (controlled.SelectedItem !=
null && controlled.CanInteractWith(controlled.SelectedItem))
43 if (All.ContainsKey(controlled.SelectedItem.ID))
45 Selected.Add(All[controlled.SelectedItem.ID]);
48 UpdateItem(controlled.SelectedItem);
50 if (controlled.Inventory !=
null)
52 foreach (Item item
in controlled.Inventory.AllItems)
54 if (controlled.HasEquippedItem(item))
56 if (All.ContainsKey(item.ID)) Selected.Add(All[item.ID]);
63 foreach (var handle
in Selected.Except(PrevSelected)) handle.Select();
64 foreach (var handle
in PrevSelected.Except(Selected)) handle.Deselect();
65 (PrevSelected, Selected) = (Selected, PrevSelected);
68 public static void UpdateItem(Item item)
70 if (All.ContainsKey(item.ID))
72 All[item.ID].Update();
76 public static void ShakeTheRefs()
78 List<ushort> keys =
new List<ushort>(All.Keys);
79 foreach (ushort key
in keys)
81 if (All[key].IsDead) All.Remove(key);
85 public static void Connect(Item item, CUIComponent component, Action<Item, CUIComponent> callback =
null)
87 if (item ==
null || component ==
null)
return;
89 AttachedItemHandle handle = component.AttachedItemHandle;
90 handle ??=
new AttachedItemHandle(item, component);
91 if (callback !=
null) handle.OnUpdate += callback;
92 component.AttachedItemHandle = handle;
96 All[item.ID] = handle;
99 public static void ConnectPrefabs(ItemPrefab prefab, Type CUIType) => ConnectPrefabs(prefab.Identifier, CUIType);
100 public static void ConnectPrefabs(
string prefabName, Type CUIType) => ConnectPrefabs(
new Identifier(prefabName), CUIType);
101 public static void ConnectPrefabs(Identifier prefabId, Type CUIType)
103 if (CUIType ==
null)
return;
105 PrefabMapping[prefabId.HashCode] = CUIType;
107 foreach (Item item
in Item.ItemList)
109 ConnectIfMapped(item);
113 internal static void ConnectIfMapped(Item item)
115 Type CUIType = PrefabMapping.GetValueOrDefault(item.Prefab.Identifier.HashCode);
116 if (item ==
null || CUIType ==
null)
return;
118 CUIComponent component = (CUIComponent)Activator.CreateInstance(CUIType);
120 AttachedItemHandle handle = component.AttachedItemHandle;
121 handle ??=
new AttachedItemHandle(item, component);
122 component.AttachedItemHandle = handle;
124 All[item.ID] = handle;
133 private WeakReference<Item> Reference;
139 if (Reference ==
null)
return null;
140 if (Reference.TryGetTarget(out Item target))
return target;
143 set => Reference =
new WeakReference<Item>(value);
145 public bool IsDead => Component ==
null;
160 public Action<Item, CUIComponent> AddOnUpdate {
set =>
OnUpdate += value; }
161 public Action<Item, CUIComponent> AddOnSelect {
set =>
OnSelect += value; }
162 public Action<Item, CUIComponent> AddOnDeselect {
set =>
OnDeselect += value; }
164 internal void Update() =>
OnUpdate?.Invoke(Item, Component);
165 internal void Select() =>
OnSelect?.Invoke(Item, Component);
166 internal void Deselect() =>
OnDeselect?.Invoke(Item, Component);
167 public AttachedItemHandle() { }
168 public AttachedItemHandle(Item item, CUIComponent component)
171 Component = component;
Link between Item and CUIComponent.
Action< Item, CUIComponent > OnUpdate
Called on Item.UpdateHud.
Action< Item, CUIComponent > OnDeselect
Called when item hud is closed.
Action< Item, CUIComponent > OnSelect
Called when item hud is opened.
Base class for all components.