tModLoader v0.11.8.9
A mod to make and play Terraria mods
EquipLoader.cs
Go to the documentation of this file.
1using Microsoft.Xna.Framework.Graphics;
2using System;
3using System.Collections.Generic;
4
5namespace Terraria.ModLoader
6{
7 //todo: further documentation
11 public static class EquipLoader
12 {
13 //in Terraria.Main.DrawPlayer and Terraria.Main.DrawPlayerHead get rid of checks for slot too high (not necessary for loading)
14 private static readonly IDictionary<EquipType, int> nextEquip = new Dictionary<EquipType, int>();
15
16 internal static readonly IDictionary<EquipType, IDictionary<int, EquipTexture>> equipTextures =
17 new Dictionary<EquipType, IDictionary<int, EquipTexture>>();
18
19 //list of equiptypes and slots registered for an item id. Used for SetDefaults
20 internal static readonly IDictionary<int, IDictionary<EquipType, int>> idToSlot =
21 new Dictionary<int, IDictionary<EquipType, int>>();
22
23 //holds mappings of slot id -> item id for head/body/legs
24 //used to populate Item.(head/body/leg)Type for Manequinns
25 internal static readonly IDictionary<EquipType, IDictionary<int, int>> slotToId =
26 new Dictionary<EquipType, IDictionary<int, int>>();
27
28 //slot id -> texture name for body slot female/arm textures
29 internal static readonly IDictionary<int, string> femaleTextures = new Dictionary<int, string>();
30 internal static readonly IDictionary<int, string> armTextures = new Dictionary<int, string>();
31
32 public static readonly EquipType[] EquipTypes = (EquipType[])Enum.GetValues(typeof(EquipType));
33
34 static EquipLoader() {
35 foreach (EquipType type in EquipTypes) {
36 nextEquip[type] = GetNumVanilla(type);
37 equipTextures[type] = new Dictionary<int, EquipTexture>();
38 }
39 slotToId[EquipType.Head] = new Dictionary<int, int>();
40 slotToId[EquipType.Body] = new Dictionary<int, int>();
41 slotToId[EquipType.Legs] = new Dictionary<int, int>();
42 }
43
44 internal static int ReserveEquipID(EquipType type) {
45 int reserveID = nextEquip[type];
46 nextEquip[type]++;
47 return reserveID;
48 }
49
56 public static EquipTexture GetEquipTexture(EquipType type, int slot) {
57 EquipTexture texture;
58 return equipTextures[type].TryGetValue(slot, out texture) ? texture : null;
59 }
60
61 internal static void ResizeAndFillArrays() {
62 Array.Resize(ref Main.armorHeadLoaded, nextEquip[EquipType.Head]);
63 Array.Resize(ref Main.armorBodyLoaded, nextEquip[EquipType.Body]);
64 Array.Resize(ref Main.armorLegsLoaded, nextEquip[EquipType.Legs]);
65 Array.Resize(ref Main.accHandsOnLoaded, nextEquip[EquipType.HandsOn]);
66 Array.Resize(ref Main.accHandsOffLoaded, nextEquip[EquipType.HandsOff]);
67 Array.Resize(ref Main.accBackLoaded, nextEquip[EquipType.Back]);
68 Array.Resize(ref Main.accFrontLoaded, nextEquip[EquipType.Front]);
69 Array.Resize(ref Main.accShoesLoaded, nextEquip[EquipType.Shoes]);
70 Array.Resize(ref Main.accWaistLoaded, nextEquip[EquipType.Waist]);
71 Array.Resize(ref Main.wingsLoaded, nextEquip[EquipType.Wings]);
72 Array.Resize(ref Main.accShieldLoaded, nextEquip[EquipType.Shield]);
73 Array.Resize(ref Main.accNeckLoaded, nextEquip[EquipType.Neck]);
74 Array.Resize(ref Main.accFaceLoaded, nextEquip[EquipType.Face]);
75 Array.Resize(ref Main.accballoonLoaded, nextEquip[EquipType.Balloon]);
76 foreach (EquipType type in EquipTypes) {
77 for (int k = GetNumVanilla(type); k < nextEquip[type]; k++) {
78 GetLoadedArray(type)[k] = true;
79 }
80 }
81 Array.Resize(ref Main.armorHeadTexture, nextEquip[EquipType.Head]);
82 Array.Resize(ref Main.armorBodyTexture, nextEquip[EquipType.Body]);
83 Array.Resize(ref Main.femaleBodyTexture, nextEquip[EquipType.Body]);
84 Array.Resize(ref Main.armorArmTexture, nextEquip[EquipType.Body]);
85 Array.Resize(ref Main.armorLegTexture, nextEquip[EquipType.Legs]);
86 Array.Resize(ref Main.accHandsOnTexture, nextEquip[EquipType.HandsOn]);
87 Array.Resize(ref Main.accHandsOffTexture, nextEquip[EquipType.HandsOff]);
88 Array.Resize(ref Main.accBackTexture, nextEquip[EquipType.Back]);
89 Array.Resize(ref Main.accFrontTexture, nextEquip[EquipType.Front]);
90 Array.Resize(ref Main.accShoesTexture, nextEquip[EquipType.Shoes]);
91 Array.Resize(ref Main.accWaistTexture, nextEquip[EquipType.Waist]);
92 Array.Resize(ref Main.wingsTexture, nextEquip[EquipType.Wings]);
93 Array.Resize(ref Main.accShieldTexture, nextEquip[EquipType.Shield]);
94 Array.Resize(ref Main.accNeckTexture, nextEquip[EquipType.Neck]);
95 Array.Resize(ref Main.accFaceTexture, nextEquip[EquipType.Face]);
96 Array.Resize(ref Main.accBalloonTexture, nextEquip[EquipType.Balloon]);
97 foreach (EquipType type in EquipTypes) {
98 foreach (var entry in equipTextures[type]) {
99 int slot = entry.Key;
100 EquipTexture texture = entry.Value;
101 GetTextureArray(type)[slot] = ModContent.GetTexture(texture.Texture);
102 if (type == EquipType.Body) {
103 Main.femaleBodyTexture[slot] = ModContent.GetTexture(femaleTextures[slot]);
104 Main.armorArmTexture[slot] = ModContent.GetTexture(armTextures[slot]);
105 }
106 }
107 }
108 Array.Resize(ref Item.headType, nextEquip[EquipType.Head]);
109 foreach (var entry in slotToId[EquipType.Head]) {
110 Item.headType[entry.Key] = entry.Value;
111 }
112 Array.Resize(ref Item.bodyType, nextEquip[EquipType.Body]);
113 foreach (var entry in slotToId[EquipType.Body]) {
114 Item.bodyType[entry.Key] = entry.Value;
115 }
116 Array.Resize(ref Item.legType, nextEquip[EquipType.Legs]);
117 foreach (var entry in slotToId[EquipType.Legs]) {
118 Item.legType[entry.Key] = entry.Value;
119 }
120 }
121
122 internal static void Unload() {
123 foreach (EquipType type in EquipTypes) {
124 nextEquip[type] = GetNumVanilla(type);
125 equipTextures[type].Clear();
126 }
127 idToSlot.Clear();
128 slotToId[EquipType.Head].Clear();
129 slotToId[EquipType.Body].Clear();
130 slotToId[EquipType.Legs].Clear();
131 femaleTextures.Clear();
132 armTextures.Clear();
133 }
134
135 internal static int GetNumVanilla(EquipType type) {
136 switch (type) {
137 case EquipType.Head:
138 return Main.numArmorHead;
139 case EquipType.Body:
140 return Main.numArmorBody;
141 case EquipType.Legs:
142 return Main.numArmorLegs;
143 case EquipType.HandsOn:
144 return Main.numAccHandsOn;
145 case EquipType.HandsOff:
146 return Main.numAccHandsOff;
147 case EquipType.Back:
148 return Main.numAccBack;
149 case EquipType.Front:
150 return Main.numAccFront;
151 case EquipType.Shoes:
152 return Main.numAccShoes;
153 case EquipType.Waist:
154 return Main.numAccWaist;
155 case EquipType.Wings:
156 return Main.maxWings;
157 case EquipType.Shield:
158 return Main.numAccShield;
159 case EquipType.Neck:
160 return Main.numAccNeck;
161 case EquipType.Face:
162 return Main.numAccFace;
163 case EquipType.Balloon:
164 return Main.numAccBalloon;
165 }
166 return 0;
167 }
168
169 internal static bool[] GetLoadedArray(EquipType type) {
170 switch (type) {
171 case EquipType.Head:
172 return Main.armorHeadLoaded;
173 case EquipType.Body:
174 return Main.armorBodyLoaded;
175 case EquipType.Legs:
176 return Main.armorLegsLoaded;
177 case EquipType.HandsOn:
178 return Main.accHandsOnLoaded;
179 case EquipType.HandsOff:
180 return Main.accHandsOffLoaded;
181 case EquipType.Back:
182 return Main.accBackLoaded;
183 case EquipType.Front:
184 return Main.accFrontLoaded;
185 case EquipType.Shoes:
186 return Main.accShoesLoaded;
187 case EquipType.Waist:
188 return Main.accWaistLoaded;
189 case EquipType.Wings:
190 return Main.wingsLoaded;
191 case EquipType.Shield:
192 return Main.accShieldLoaded;
193 case EquipType.Neck:
194 return Main.accNeckLoaded;
195 case EquipType.Face:
196 return Main.accFaceLoaded;
197 case EquipType.Balloon:
198 return Main.accballoonLoaded;
199 }
200 return null;
201 }
202
203 internal static Texture2D[] GetTextureArray(EquipType type) {
204 switch (type) {
205 case EquipType.Head:
206 return Main.armorHeadTexture;
207 case EquipType.Body:
208 return Main.armorBodyTexture;
209 case EquipType.Legs:
210 return Main.armorLegTexture;
211 case EquipType.HandsOn:
212 return Main.accHandsOnTexture;
213 case EquipType.HandsOff:
214 return Main.accHandsOffTexture;
215 case EquipType.Back:
216 return Main.accBackTexture;
217 case EquipType.Front:
218 return Main.accFrontTexture;
219 case EquipType.Shoes:
220 return Main.accShoesTexture;
221 case EquipType.Waist:
222 return Main.accWaistTexture;
223 case EquipType.Wings:
224 return Main.wingsTexture;
225 case EquipType.Shield:
226 return Main.accShieldTexture;
227 case EquipType.Neck:
228 return Main.accNeckTexture;
229 case EquipType.Face:
230 return Main.accFaceTexture;
231 case EquipType.Balloon:
232 return Main.accBalloonTexture;
233 }
234 return null;
235 }
236
237 internal static void SetSlot(Item item) {
238 IDictionary<EquipType, int> slots;
239 if (!idToSlot.TryGetValue(item.type, out slots))
240 return;
241
242 foreach (var entry in slots) {
243 int slot = entry.Value;
244 switch (entry.Key) {
245 case EquipType.Head:
246 item.headSlot = slot;
247 break;
248 case EquipType.Body:
249 item.bodySlot = slot;
250 break;
251 case EquipType.Legs:
252 item.legSlot = slot;
253 break;
254 case EquipType.HandsOn:
255 item.handOnSlot = (sbyte)slot;
256 break;
257 case EquipType.HandsOff:
258 item.handOffSlot = (sbyte)slot;
259 break;
260 case EquipType.Back:
261 item.backSlot = (sbyte)slot;
262 break;
263 case EquipType.Front:
264 item.frontSlot = (sbyte)slot;
265 break;
266 case EquipType.Shoes:
267 item.shoeSlot = (sbyte)slot;
268 break;
269 case EquipType.Waist:
270 item.waistSlot = (sbyte)slot;
271 break;
272 case EquipType.Wings:
273 item.wingSlot = (sbyte)slot;
274 break;
275 case EquipType.Shield:
276 item.shieldSlot = (sbyte)slot;
277 break;
278 case EquipType.Neck:
279 item.neckSlot = (sbyte)slot;
280 break;
281 case EquipType.Face:
282 item.faceSlot = (sbyte)slot;
283 break;
284 case EquipType.Balloon:
285 item.balloonSlot = (sbyte)slot;
286 break;
287 }
288 }
289 }
290
291 internal static int GetPlayerEquip(Player player, EquipType type) {
292 switch (type) {
293 case EquipType.Head:
294 return player.head;
295 case EquipType.Body:
296 return player.body;
297 case EquipType.Legs:
298 return player.legs;
299 case EquipType.HandsOn:
300 return player.handon;
301 case EquipType.HandsOff:
302 return player.handoff;
303 case EquipType.Back:
304 return player.back;
305 case EquipType.Front:
306 return player.front;
307 case EquipType.Shoes:
308 return player.shoe;
309 case EquipType.Waist:
310 return player.waist;
311 case EquipType.Wings:
312 return player.wings;
313 case EquipType.Shield:
314 return player.shield;
315 case EquipType.Neck:
316 return player.neck;
317 case EquipType.Face:
318 return player.face;
319 case EquipType.Balloon:
320 return player.balloon;
321 }
322 return 0;
323 }
324 }
325}
This serves as a central place to store equipment slots and their corresponding textures....
Definition: EquipLoader.cs:12
static readonly IDictionary< EquipType, int > nextEquip
Definition: EquipLoader.cs:14
static EquipTexture GetEquipTexture(EquipType type, int slot)
Gets the equipment texture for the specified equipment type and ID.
Definition: EquipLoader.cs:56
static readonly EquipType[] EquipTypes
Definition: EquipLoader.cs:32
This serves as a place for you to program behaviors of equipment textures. This is useful for equipme...
Definition: EquipTexture.cs:9
EquipType
This is an enum of all the types of equipment that exist. An equipment type is defined as a type or l...
Definition: EquipType.cs:7