forked from abluescarab/tModLoader-WingSlot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WingSlotPlayer.cs
419 lines (360 loc) · 14.6 KB
/
WingSlotPlayer.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.DataStructures;
using Terraria.Graphics.Shaders;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using TerraUI;
using TerraUI.Objects;
using TerraUI.Utilities;
namespace WingSlot {
internal class WingSlotPlayer : ModPlayer {
private const string HIDDEN_TAG = "hidden";
private const string WINGS_TAG = "wings";
private const string VANITY_WINGS_TAG = "vanitywings";
private const string WING_DYE_TAG = "wingdye";
private const string WING_DYE_LAYER = "WingDye";
private PlayerLayer wingsDye;
public UIItemSlot EquipWingSlot;
public UIItemSlot VanityWingSlot;
public UIItemSlot WingDyeSlot;
/// <summary>
/// Whether to autoload the ModPlayer.
/// </summary>
public override bool Autoload(ref string name) {
return true;
}
/// <summary>
/// Initialize the ModPlayer.
/// </summary>
public override void Initialize() {
EquipWingSlot = new UIItemSlot(Vector2.Zero, context: Contexts.EquipAccessory, hoverText: "Wings",
conditions: Slot_Conditions, drawBackground: Slot_DrawBackground, scaleToInventory: true);
VanityWingSlot = new UIItemSlot(Vector2.Zero, context: Contexts.EquipAccessoryVanity, hoverText:
Language.GetTextValue("LegacyInterface.11") + " Wings",
conditions: Slot_Conditions, drawBackground: Slot_DrawBackground, scaleToInventory: true);
WingDyeSlot = new UIItemSlot(Vector2.Zero, context: Contexts.EquipDye, conditions: WingDyeSlot_Conditions,
drawBackground: WingDyeSlot_DrawBackground, scaleToInventory: true);
VanityWingSlot.Partner = EquipWingSlot;
EquipWingSlot.BackOpacity = VanityWingSlot.BackOpacity = WingDyeSlot.BackOpacity = .8f;
// Big thanks to thegamemaster1234 for the example code used to write this!
wingsDye = new PlayerLayer(UIUtils.Mod.Name, WING_DYE_LAYER, delegate (PlayerDrawInfo drawInfo) {
Player player = drawInfo.drawPlayer;
WingSlotPlayer wsp = player.GetModPlayer<WingSlotPlayer>(UIUtils.Mod);
Item wings = wsp.GetDyedWings();
Item dye = wsp.WingDyeSlot.Item;
int index = Main.playerDrawData.Count - 1;
if(dye.stack <= 0 || wings.stack <= 0 || !wings.active || wings.noUseGraphic || player.mount.Active ||
(wsp.VanityWingSlot.Item.stack <= 0 && !wsp.EquipWingSlot.ItemVisible && player.wingFrame == 0))
return;
if(wings.flame)
index -= 1;
if(index < 0 || index > Main.playerDrawData.Count)
return;
DrawData data = Main.playerDrawData[index];
data.shader = GameShaders.Armor.GetShaderIdFromItemId(dye.type);
Main.playerDrawData[index] = data;
});
InitializeWings();
}
/// <summary>
/// Modify draw layers to draw the wing dye.
/// </summary>
/// <param name="layers"></param>
public override void ModifyDrawLayers(List<PlayerLayer> layers) {
if(!Main.gameMenu) {
layers.Insert(layers.IndexOf(PlayerLayer.Wings) + 1, wingsDye);
}
}
/// <summary>
/// Update player with the equipped wings.
/// </summary>
public override void UpdateEquips(ref bool wallSpeedBuff, ref bool tileSpeedBuff, ref bool tileRangeBuff) {
Item wings = EquipWingSlot.Item;
Item vanityWings = VanityWingSlot.Item;
if(wings.stack > 0) {
player.VanillaUpdateEquip(wings);
player.VanillaUpdateAccessory(player.whoAmI, wings, !EquipWingSlot.ItemVisible, ref wallSpeedBuff, ref tileSpeedBuff,
ref tileRangeBuff);
}
if(vanityWings.stack > 0) {
player.VanillaUpdateVanityAccessory(vanityWings);
}
}
/// <summary>
/// Save the mod settings.
/// </summary>
public override TagCompound Save() {
return new TagCompound {
{ HIDDEN_TAG, EquipWingSlot.ItemVisible },
{ WINGS_TAG, ItemIO.Save(EquipWingSlot.Item) },
{ VANITY_WINGS_TAG, ItemIO.Save(VanityWingSlot.Item) },
{ WING_DYE_TAG, ItemIO.Save(WingDyeSlot.Item) }
};
}
/// <summary>
/// Load the mod settings.
/// </summary>
public override void Load(TagCompound tag) {
SetWings(false, ItemIO.Load(tag.GetCompound(WINGS_TAG)));
SetWings(true, ItemIO.Load(tag.GetCompound(VANITY_WINGS_TAG)));
SetDye(ItemIO.Load(tag.GetCompound(WING_DYE_TAG)));
EquipWingSlot.ItemVisible = tag.GetBool(HIDDEN_TAG);
}
/// <summary>
/// Load legacy mod settings.
/// </summary>
public override void LoadLegacy(BinaryReader reader) {
int hide = 0;
InitializeWings();
ushort installedFlag = reader.ReadUInt16();
if(installedFlag == 0) {
try { hide = reader.ReadInt32(); }
catch(EndOfStreamException) { hide = 0; }
EquipWingSlot.ItemVisible = (hide == 1 ? false : true);
Item wings1 = EquipWingSlot.Item;
Item wings2 = VanityWingSlot.Item;
int context = ReadWingsLegacy(ref wings1, reader);
ReadWingsLegacy(ref wings2, reader);
if(context == (int)Contexts.EquipAccessory) {
SetWings(false, wings1);
SetWings(true, wings2);
}
else if(context == (int)Contexts.EquipAccessoryVanity) {
SetWings(true, wings1);
SetWings(false, wings2);
}
}
}
/// <summary>
/// Read the wings in legacy mod settings.
/// </summary>
internal static int ReadWingsLegacy(ref Item wings, BinaryReader reader) {
try {
ItemIO.LoadLegacy(wings, reader, false, false);
return reader.ReadInt32();
}
catch(EndOfStreamException) {
return -1;
}
}
/// <summary>
/// Draw the wing slot backgrounds.
/// </summary>
private void Slot_DrawBackground(UIObject sender, SpriteBatch spriteBatch) {
UIItemSlot slot = (UIItemSlot)sender;
if(ShouldDrawSlots()) {
slot.OnDrawBackground(spriteBatch);
if(slot.Item.stack == 0) {
Texture2D tex = mod.GetTexture(WingSlot.WING_SLOT_BACK_TEX);
Vector2 origin = tex.Size() / 2f * Main.inventoryScale;
Vector2 position = slot.Rectangle.TopLeft();
spriteBatch.Draw(
tex,
position + (slot.Rectangle.Size() / 2f) - (origin / 2f),
null,
Color.White * 0.35f,
0f,
origin,
Main.inventoryScale,
SpriteEffects.None,
0f); // layer depth 0 = front
}
}
}
/// <summary>
/// Control what can be placed in the wing slots.
/// </summary>
private bool Slot_Conditions(Item item) {
if(item.wingSlot > 0) {
return true;
}
return false;
}
/// <summary>
/// Draw the wing dye slot background.
/// </summary>
private void WingDyeSlot_DrawBackground(UIObject sender, SpriteBatch spriteBatch) {
UIItemSlot slot = (UIItemSlot)sender;
if(ShouldDrawSlots()) {
slot.OnDrawBackground(spriteBatch);
if(slot.Item.stack == 0) {
Texture2D tex = Main.extraTexture[54];
Rectangle rectangle = tex.Frame(3, 6, 1 % 3, 1 / 3);
rectangle.Width -= 2;
rectangle.Height -= 2;
Vector2 origin = rectangle.Size() / 2f * Main.inventoryScale;
Vector2 position = slot.Rectangle.TopLeft();
spriteBatch.Draw(
tex,
position + (slot.Rectangle.Size() / 2f) - (origin / 2f),
new Rectangle?(rectangle),
Color.White * 0.35f,
0f,
origin,
Main.inventoryScale,
SpriteEffects.None,
0f); // layer depth 0 = front
}
}
}
/// <summary>
/// Control what can be placed in the wing dye slot.
/// </summary>
private bool WingDyeSlot_Conditions(Item item) {
if(item.dye > 0 && item.hairDye < 0) {
return true;
}
return false;
}
/// <summary>
/// Draw the wing slots.
/// </summary>
/// <param name="spriteBatch">drawing SpriteBatch</param>
public void Draw(SpriteBatch spriteBatch) {
if(ShouldDrawSlots()) {
int mapH = 0;
int rX = 0;
int rY = 0;
float origScale = Main.inventoryScale;
Main.inventoryScale = 0.85f;
if(Main.mapEnabled) {
if(!Main.mapFullscreen && Main.mapStyle == 1) {
mapH = 256;
}
if((mapH + 600) > Main.screenHeight) {
mapH = Main.screenHeight - 600;
}
}
rX = Main.screenWidth - 92 - (47 * 2);
rY = mapH + 174;
if(Main.netMode == 1) {
rX -= 47;
}
EquipWingSlot.Position = new Vector2(rX, rY);
VanityWingSlot.Position = new Vector2(rX -= 47, rY);
WingDyeSlot.Position = new Vector2(rX -= 47, rY);
VanityWingSlot.Draw(spriteBatch);
EquipWingSlot.Draw(spriteBatch);
WingDyeSlot.Draw(spriteBatch);
Main.inventoryScale = origScale;
EquipWingSlot.Update();
VanityWingSlot.Update();
WingDyeSlot.Update();
UIUtils.UpdateInput();
}
}
/// <summary>
/// Whether to draw the UIItemSlots.
/// </summary>
/// <returns>whether to draw the slots</returns>
public bool ShouldDrawSlots() {
if(Main.playerInventory && Main.EquipPage == 2) {
return true;
}
return false;
}
/// <summary>
/// Initialize the items in the UIItemSlots.
/// </summary>
private void InitializeWings() {
EquipWingSlot.Item = new Item();
VanityWingSlot.Item = new Item();
WingDyeSlot.Item = new Item();
EquipWingSlot.Item.SetDefaults();
VanityWingSlot.Item.SetDefaults();
WingDyeSlot.Item.SetDefaults();
}
/// <summary>
/// Set the item in the specified slot.
/// </summary>
/// <param name="isVanity">whether to equip in the vanity slot</param>
/// <param name="item">wings</param>
public void SetWings(bool isVanity, Item item) {
if(!isVanity) {
EquipWingSlot.Item = item.Clone();
}
else {
VanityWingSlot.Item = item.Clone();
}
}
/// <summary>
/// Clear the wings from the specified slot.
/// </summary>
/// <param name="isVanity">whether to unequip from the vanity slot</param>
public void ClearWings(bool isVanity) {
if(!isVanity) {
EquipWingSlot.Item = new Item();
EquipWingSlot.Item.SetDefaults();
}
else {
VanityWingSlot.Item = new Item();
VanityWingSlot.Item.SetDefaults();
}
}
/// <summary>
/// Set the wing dye.
/// </summary>
/// <param name="item">dye</param>
public void SetDye(Item item) {
WingDyeSlot.Item = item.Clone();
}
/// <summary>
/// Clear the wing dye.
/// </summary>
public void ClearDye() {
WingDyeSlot.Item = new Item();
WingDyeSlot.Item.SetDefaults();
}
/// <summary>
/// Equip a set of wings.
/// </summary>
/// <param name="isVanity">whether the wings should go in the vanity slot</param>
/// <param name="item">wings</param>
public void EquipWings(bool isVanity, Item item) {
UIItemSlot slot = (isVanity ? VanityWingSlot : EquipWingSlot);
int fromSlot = Array.FindIndex(player.inventory, i => i == item);
// from inv to slot
if(fromSlot > -1) {
item.favorited = false;
player.inventory[fromSlot] = slot.Item.Clone();
UIUtils.PlaySound(Sounds.Grab);
Recipe.FindRecipes();
SetWings(isVanity, item);
}
}
/// <summary>
/// Equip a dye.
/// </summary>
/// <param name="item">dye to equip</param>
public void EquipDye(Item item) {
int fromSlot = Array.FindIndex(player.inventory, i => i == item);
// from inv to slot
if(fromSlot > -1) {
item.favorited = false;
player.inventory[fromSlot] = WingDyeSlot.Item.Clone();
UIUtils.PlaySound(Sounds.Grab);
Recipe.FindRecipes();
SetDye(item);
}
}
/// <summary>
/// Get the set of wings that a dye should be applied to.
/// </summary>
/// <returns>dyed wings</returns>
public Item GetDyedWings() {
if(VanityWingSlot.Item.stack > 0) {
return VanityWingSlot.Item;
}
else if(EquipWingSlot.Item.stack > 0) {
return EquipWingSlot.Item;
}
return new Item();
}
}
}