Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
GuiSoulgauge.cs
Go to the documentation of this file.
1 using Loot.Soulforging;
2 using Loot.UI.Common;
3 using Microsoft.Xna.Framework;
4 using Microsoft.Xna.Framework.Graphics;
5 using Terraria;
6 using Terraria.UI;
7 
8 namespace Loot.UI.Tabs.Soulforging
9 {
10  internal class GuiSoulgauge : UIElement
11  {
12  // TODO generify to animated element?
13  private class GaugeDrawing
14  {
15  public const int MAX_FRAME = 4;
16  public const int MAX_TICK = 6;
17  public static Vector2 FRAME = new Vector2(124, 106);
18 
19  public static bool IsAnimation(GaugeLevel level)
20  => level != GaugeLevel.ZERO && level != GaugeLevel.HUNDRED;
21 
22  public static Texture2D GetTextureByLevel(GaugeLevel level)
23  {
24  switch (level)
25  {
26  default:
27  case GaugeLevel.ZERO:
28  return Assets.Textures.Soulgauge.EmptyTexture;
29  case GaugeLevel.HUNDRED:
30  return Assets.Textures.Soulgauge.FullTexture;
31  case GaugeLevel.TWENTY:
32  return Assets.Textures.Soulgauge.Anim20;
33  case GaugeLevel.FOURTY:
34  return Assets.Textures.Soulgauge.Anim40;
35  case GaugeLevel.SIXTY:
36  return Assets.Textures.Soulgauge.Anim60;
37  case GaugeLevel.EIGHTY:
38  return Assets.Textures.Soulgauge.Anim80;
39  }
40  }
41 
42  private static short _tick;
43  private static short _frame;
44  private static Texture2D _activeTexture;
45 
46  public static void Update()
47  {
48  _tick++;
49  if (_tick >= MAX_TICK)
50  {
51  _tick = 0;
52  _frame++;
53  if (_frame >= MAX_FRAME)
54  {
55  _frame = 0;
56  }
57  }
58  }
59 
60  public static Rectangle GetDrawFrame()
61  => new Rectangle(0, _frame * (int)FRAME.Y, (int)FRAME.X, (int)FRAME.Y);
62  }
63 
64  // TODO trivial asf
65  public string GaugeText(GaugeLevel level)
66  {
67  switch (level)
68  {
69  default:
70  case GaugeLevel.ZERO:
71  return "0";
72  case GaugeLevel.HUNDRED:
73  return "100";
74  case GaugeLevel.TWENTY:
75  return "20";
76  case GaugeLevel.FOURTY:
77  return "40";
78  case GaugeLevel.SIXTY:
79  return "60";
80  case GaugeLevel.EIGHTY:
81  return "80";
82  }
83  }
84 
85  public int GetRelativePercentage()
86  {
87  var player = Main.LocalPlayer.GetModPlayer<LootEssencePlayer>();
88  return (int)((float)player.Essence / (float)player.TopEssence * 100f);
89  }
90 
91  public GaugeLevel GetGaugeLevel()
92  {
93  var relative = GetRelativePercentage();
94  if (relative <= 0) return GaugeLevel.ZERO;
95  if (relative <= 20) return GaugeLevel.TWENTY;
96  if (relative <= 40) return GaugeLevel.FOURTY;
97  if (relative <= 60) return GaugeLevel.SIXTY;
98  if (relative <= 80) return GaugeLevel.EIGHTY;
99  if (relative <= 100) return GaugeLevel.HUNDRED;
100  return GaugeLevel.ZERO;
101  }
102 
103  // TODO this wouldn't be stored here, test for now
104  public GaugeLevel GaugeLevel => GetGaugeLevel();
105  public Texture2D DrawTexture => GaugeDrawing.GetTextureByLevel(GaugeLevel);
106 
107  public override void OnInitialize()
108  {
109  base.OnInitialize();
110  Width.Set(GaugeDrawing.FRAME.X, 0);
111  Height.Set(GaugeDrawing.FRAME.Y, 0);
112  }
113 
114  protected override void DrawSelf(SpriteBatch spriteBatch)
115  {
116  var player = Main.LocalPlayer.GetModPlayer<LootEssencePlayer>();
117  if (IsMouseHovering)
118  {
119  Main.hoverItemName =
120  $"Soul level: {GetRelativePercentage()}% (Total essence: {player.Essence})";
121  }
122 
124  CalculatedStyle dimensions = GetDimensions();
125 
126  if (GaugeDrawing.IsAnimation(GaugeLevel))
127  {
128  spriteBatch.Draw(DrawTexture, dimensions.Position(), GaugeDrawing.GetDrawFrame(), Color.White);
129  }
130  else
131  {
132  spriteBatch.Draw(DrawTexture, dimensions.Position(), Color.White);
133  }
134  }
135  }
136 }
static Texture2D GetTextureByLevel(GaugeLevel level)
Definition: GuiSoulgauge.cs:22