-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathSuitAddonLoader.cs
249 lines (220 loc) · 7.67 KB
/
SuitAddonLoader.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.Content;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Terraria;
using Terraria.Audio;
using Terraria.DataStructures;
using Terraria.GameContent;
//using Terraria.GameContent.Liquid;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using MetroidMod.Content.Items.Armors;
using MetroidMod.Default;
using MetroidMod.ID;
using MetroidMod.Common.Players;
namespace MetroidMod
{
public static class SuitAddonLoader
{
internal static readonly List<ModSuitAddon> addons = new();
internal static readonly List<GlobalSuitAddon> globalAddons = new();
internal static readonly Dictionary<int, string> unloadedAddons = new();
internal static bool TryGetValue(this IList<ModSuitAddon> list, int type, out ModSuitAddon modSuitAddon) =>
list.TryGetValue(i => i.Type == type, out modSuitAddon);
internal static bool TryGetValue(this IList<ModSuitAddon> list, string fullName, out ModSuitAddon modSuitAddon) =>
list.TryGetValue(i => i.FullName == fullName, out modSuitAddon);
internal static bool TryGetValue(this IList<ModSuitAddon> list, Item item, out ModSuitAddon modSuitAddon) =>
list.TryGetValue(i => i.ItemType == item.type, out modSuitAddon);
public static bool TryGetAddon(Item item, out ModSuitAddon modSuitAddon) =>
addons.TryGetValue(item, out modSuitAddon);
public static bool TryGetAddon(int type, out ModSuitAddon modSuitAddon) =>
addons.TryGetValue(type, out modSuitAddon);
public static bool TryGetAddon(string fullName, out ModSuitAddon modSuitAddon) =>
addons.TryGetValue(fullName, out modSuitAddon);
public static bool TryGetAddon<T>(out ModSuitAddon modSuitAddon) where T : ModSuitAddon =>
addons.TryGetValue(i => i is T, out modSuitAddon);
public static int AddonCount => addons.Count;
public static ModSuitAddon GetAddon(Item item) =>
addons.TryGetValue(item, out ModSuitAddon modSuitAddon) ? modSuitAddon : null;
public static ModSuitAddon GetAddon(int type) =>
addons.TryGetValue(type, out ModSuitAddon modSuitAddon) ? modSuitAddon : null;
public static ModSuitAddon GetAddon(string fullName) =>
addons.TryGetValue(fullName, out ModSuitAddon modSuitAddon) ? modSuitAddon : null;
public static ModSuitAddon GetAddon<T>() where T : ModSuitAddon =>
addons.TryGetValue(i => i is T, out ModSuitAddon modSuitAddon) ? modSuitAddon : null;
public static bool IsASuitTile(Tile tile)
{
foreach(ModSuitAddon addon in addons)
{
if(tile.TileType == addon.TileType) { return true; }
}
return false;
}
/*public static void OnUpdate(Player player, Item[] items)
{
foreach (Item item in items)
{
if (item.type == ItemID.None) { continue; }
SuitAddonItem addonItem = (SuitAddonItem)item.ModItem;
ModSuitAddon addon = addonItem.modSuitAddon;
addon.OnUpdate(player);
}
}*/
public static string GetAddonSlotName(int AddonSlot)
{
return AddonSlot switch
{
SuitAddonSlotID.Tanks_Energy => "Energy Tank",
SuitAddonSlotID.Tanks_Reserve => "Reserve Tank",
SuitAddonSlotID.Suit_Varia => "Varia",
SuitAddonSlotID.Suit_Utility => "Utility",
SuitAddonSlotID.Suit_Augment => "Augmentation",
SuitAddonSlotID.Suit_LunarAugment => "Secondary Augmentation",
SuitAddonSlotID.Misc_Grip => "Hand",
SuitAddonSlotID.Misc_Attack => "Attack",
SuitAddonSlotID.Boots_JumpHeight => "Boots",
SuitAddonSlotID.Boots_Jump => "Jump",
SuitAddonSlotID.Boots_Speed => "Speed Augmentation",
SuitAddonSlotID.Visor_Scan => "Scan Visor",
SuitAddonSlotID.Visor_Utility => "Utility Visor",
SuitAddonSlotID.Visor_AltVision => "Alt Visor",
_ => "Unknown"
};
}
public static string GetSetBonusText(Player player)
{
string returnVal = "\n";
int index = 0;
Item[] items = new Item[SuitAddonSlotID.Count];
foreach (Item item in (player.armor[0].ModItem as PowerSuitHelmet).SuitAddons)
{
items[index++] = item;
}
foreach (Item item in (player.armor[1].ModItem as PowerSuitBreastplate).SuitAddons)
{
items[index++] = item;
}
foreach (Item item in (player.armor[2].ModItem as PowerSuitGreaves).SuitAddons)
{
items[index++] = item;
}
foreach (Item item in items)
{
if (item.type == ItemID.None) { continue; }
SuitAddonItem addonItem = (SuitAddonItem)item.ModItem;
ModSuitAddon addon = addonItem?.modSuitAddon;
string temp = addon.GetSetBonusText();
if (temp != null && temp != "")
{
returnVal += temp + "\n";
}
}
return returnVal;
}
public static void OnUpdateArmorSet(Player player)
{
int index = 0;
Item[] items = new Item[SuitAddonSlotID.Count];
foreach (Item item in (player.armor[0].ModItem as PowerSuitHelmet).SuitAddons)
{
items[index++] = item;
}
foreach (Item item in (player.armor[1].ModItem as PowerSuitBreastplate).SuitAddons)
{
items[index++] = item;
}
foreach (Item item in (player.armor[2].ModItem as PowerSuitGreaves).SuitAddons)
{
items[index++] = item;
}
ModSuitAddon[] suitAddons = new ModSuitAddon[items.Length];
for (int i = 0; i < items.Length; i++)
{
if (items[i] == null || !TryGetAddon(items[i], out ModSuitAddon addon)) { continue; }
suitAddons[i] = addon;
addon.OnUpdateArmorSet(player, items[i].stack);
}
foreach (GlobalSuitAddon gsa in globalAddons)
{
gsa.OnUpdateArmorSet(MPlayer.GetPowerSuit(player), player);
}
}
public static void OnUpdateVanitySet(Player player)
{
Item[] items = (GetBreastplate(player, true).ModItem as PowerSuitBreastplate).SuitAddons;
for (int i = SuitAddonSlotID.Suit_Varia; i <= SuitAddonSlotID.Suit_LunarAugment; i++)
{
if (items[i] == null || !TryGetAddon(items[i], out ModSuitAddon addon)) { continue; }
addon.OnUpdateVanitySet(player);
}
foreach (GlobalSuitAddon gsa in globalAddons)
{
gsa.OnUpdateVanitySet(MPlayer.GetPowerSuit(player), player);
}
}
public static Item GetBreastplate(Player player, bool lookingForVanity)
{
if (player.armor[1].type == ModContent.ItemType<PowerSuitBreastplate>() && (!lookingForVanity || player.armor[11].IsAir))
{
return player.armor[1];
}
if (player.armor[11].type == ModContent.ItemType<PowerSuitBreastplate>() && lookingForVanity)
{
return player.armor[11];
}
return null;
}
public static void ArmorSetShadows(Player player)
{
Item[] items = (GetBreastplate(player, true).ModItem as PowerSuitBreastplate).SuitAddons;
ModSuitAddon[] suitAddons = new ModSuitAddon[items.Length];
for (int i = 0; i < items.Length; i++)
{
if (items[i] == null || !TryGetAddon(items[i], out ModSuitAddon addon)) { continue; }
suitAddons[i] = addon;
addon.ArmorSetShadows(player);
}
foreach (GlobalSuitAddon gsa in globalAddons)
{
gsa.ArmorSetShadows(suitAddons, player);
}
}
internal static void ReloadTypes(TagCompound unloadedTag)
{
unloadedAddons.Clear();
Dictionary<string, object> unloaded = new(unloadedTag);
foreach ((string name, object type) in unloaded)
{
unloadedAddons[(int)type] = name;
}
HashSet<int> reserveTypes = new();
foreach ((int type, string name) in unloadedAddons)
{
if (addons.TryGetValue(name, out ModSuitAddon modSuitAddon))
{
modSuitAddon.ChangeType(type);
reserveTypes.Add(type);
}
}
int freeType = 3;
foreach (ModSuitAddon modSuitAddon in addons)
{
if (reserveTypes.Contains(modSuitAddon.Type)) { continue; }
while (reserveTypes.Contains(freeType)) { freeType++; }
modSuitAddon.ChangeType(freeType);
freeType++;
}
}
internal static void Unload()
{
addons.Clear();
globalAddons.Clear();
unloadedAddons.Clear();
}
}
}