Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
WeaponOutSupport.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using Loot.Api.Graphics;
6 using Loot.Api.Graphics.Shader;
7 using Microsoft.Xna.Framework;
8 using Microsoft.Xna.Framework.Graphics;
9 using Terraria;
10 using Terraria.DataStructures;
11 using Terraria.ModLoader;
12 
13 namespace Loot.ModSupport
14 {
15  internal class WeaponOutSupport : ModSupport
16  {
17  public override string ModName => "WeaponOut";
18 
19  public override bool CheckValidity(Mod mod) => mod.Version >= new Version(1, 6, 4);
20 
21  public override void AddClientSupport(Mod mod)
22  {
23  On.Terraria.Main.DrawPlayer += MainOnDrawPlayer;
24  On.Terraria.DataStructures.DrawData.Draw += DrawDataOnDraw;
25  mod.Call("AddCustomPreDrawMethod", (Func<Player, Item, DrawData, bool>)CustomPreDraw);
26  }
27 
33  private void MainOnDrawPlayer(On.Terraria.Main.orig_DrawPlayer orig, Main self, Player drawplayer, Vector2 position, float rotation, Vector2 rotationorigin, float shadow)
34  {
35  _lightColor = null;
36  _cache.Clear();
37  orig(self, drawplayer, position, rotation, rotationorigin, shadow);
38  }
39 
45  private void DrawDataOnDraw(On.Terraria.DataStructures.DrawData.orig_Draw orig, ref DrawData self, SpriteBatch sb)
46  {
47  _lightColor = _lightColor ?? Lighting.GetColor((int)(Main.LocalPlayer.MountedCenter.X / 16), (int)(Main.LocalPlayer.MountedCenter.Y / 16));
48  if (!DrawWeaponOutFists(self, sb) && !DrawWeaponOutFromCache(self, sb))
49  orig(ref self, sb);
50  }
51 
52  // Specific useStyle used for WO's fists
53  private const int WEAPONOUT_FISTS_USESTYLE = 102115116;
54 
55  private bool DrawWeaponOutFists(DrawData self, SpriteBatch sb)
56  {
57  if (Main.LocalPlayer.HeldItem.useStyle != WEAPONOUT_FISTS_USESTYLE) return false;
58  bool isHandOn = Main.LocalPlayer.handon > 0 && self.texture == Main.accHandsOnTexture[Main.LocalPlayer.handon];
59  bool isHandOff = Main.LocalPlayer.handoff > 0 && self.texture == Main.accHandsOffTexture[Main.LocalPlayer.handoff];
60  if (!isHandOn && !isHandOff) return false;
61 
62  var info = GraphicsGlobalItem.GetInfo(Main.LocalPlayer.HeldItem);
63  if (info.HasShaders)
64  {
65  for (int i = 0; i < info.ShaderEntities.Count; i++)
66  {
67  var entity = info.ShaderEntities[i];
68  if (entity == null) continue;
69  entity.Properties.SkipUpdatingDrawData = true;
70  entity.DrawData = self;
71  entity.DoDrawLayeredEntity(sb, _lightColor.Value, self.color, self.scale.LengthSquared(), self.rotation,
72  info.GlowmaskEntities[i], self.texture, self.texture, self.texture);
73  }
74 
75  return true;
76  }
77 
78  foreach (var entity in info.GlowmaskEntities.Where(x => x != null))
79  {
80  entity.Properties.SkipUpdatingDrawData = true;
81  entity.DrawData = self;
82  entity.DoDrawGlowmask(sb, _lightColor.Value, self.color, self.rotation, self.scale.LengthSquared(), entity.Entity.whoAmI, self.texture);
83  }
84 
85  return info.HasGlowmasks;
86  }
87 
88  private bool DrawWeaponOutFromCache(DrawData self, SpriteBatch sb)
89  {
90  var data = self;
91  var cachedDatas = _cache.Where(x => x.drawData.texture == data.texture).ToList();
92  if (cachedDatas.Any())
93  {
94  foreach (var cachedData in cachedDatas)
95  {
96  var shaderEntity = cachedData.shaderEntity;
97  var glowmaskEntity = cachedData.glowmaskEntity;
98  var plr = cachedData.player;
99  _lightColor = _lightColor ?? Lighting.GetColor((int)(plr.MountedCenter.X / 16), (int)(plr.MountedCenter.Y / 16));
100 
101  if (shaderEntity != null)
102  {
103  shaderEntity.Properties.SkipUpdatingDrawData = true;
104  shaderEntity.DrawData = self;
105  shaderEntity.DoDrawLayeredEntity(sb, _lightColor.Value, self.color, self.scale.LengthSquared(), self.rotation, glowmaskEntity);
106  }
107  else if (glowmaskEntity != null)
108  {
109  glowmaskEntity.Properties.SkipUpdatingDrawData = true;
110  glowmaskEntity.DrawData = self;
111  glowmaskEntity.DoDrawGlowmask(sb, _lightColor.Value, self.color, self.rotation, self.scale.LengthSquared(), glowmaskEntity.Entity.whoAmI);
112  }
113  }
114 
115  return true;
116  }
117 
118  return false;
119  }
120 
121  private Color? _lightColor;
122  private readonly List<(DrawData drawData, Player player, ShaderEntity shaderEntity, GlowmaskEntity glowmaskEntity)> _cache
123  = new List<(DrawData, Player, ShaderEntity, GlowmaskEntity)>();
124 
131  private bool CustomPreDraw(Player player, Item item, DrawData drawDrata)
132  {
133  var info = GraphicsGlobalItem.GetInfo(item);
134  if (info.HasShaders)
135  {
136  for (int i = 0; i < info.ShaderEntities.Count; i++)
137  {
138  ShaderEntity entity = info.ShaderEntities[i];
139  if (entity != null)
140  {
141  _cache.Add((drawDrata, player, entity, info.GlowmaskEntities[i]));
142  }
143  }
144  }
145  else if (info.HasGlowmasks)
146  {
147  foreach (var entity in info.GlowmaskEntities.Where(x => x != null))
148  {
149  _cache.Add((drawDrata, player, null, entity));
150  }
151  }
152  return true;
153  }
154  }
155 }
This class is responsible for handling ShaderEntity and GlowmaskEntity that come from the Modifiers o...
Defines a Shader entity, part of GraphicsEntity<T> The entity defines a particular &#39;shader&#39; "of" the ...
Definition: ShaderEntity.cs:16
static GraphicsGlobalItem GetInfo(Item item)