Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
GraphicsEntity.cs
Go to the documentation of this file.
1 using System;
2 using Microsoft.Xna.Framework;
3 using Microsoft.Xna.Framework.Graphics;
4 using Terraria;
5 using Terraria.DataStructures;
6 using Terraria.ModLoader;
7 
8 namespace Loot.Api.Graphics
9 {
14  public abstract class GraphicsEntity<T> where T : GraphicsProperties
15  {
16  public DrawData DrawData;
17  public Color DrawColor { get; set; }
18  public bool UseDestinationRectangle => DrawData.useDestinationRectangle;
19  public Rectangle? DestinationRectangle => DrawData.destinationRectangle;
20 
21  public Entity Entity { get; protected set; }
22  public T Properties { get; protected set; }
23  public short Order { get; protected set; }
24  public bool NeedsUpdate { get; set; } = true;
25 
26  protected GraphicsEntity(object subjectIdentity)
27  {
28  if (subjectIdentity == Entity) return;
29  SetIdentity(subjectIdentity);
30  }
31 
32  internal void SetIdentity(object subjectIdentity)
33  {
34  if (subjectIdentity is ModItem modItem)
35  {
36  Entity = modItem.item;
37  }
38  else if (subjectIdentity is ModNPC modNpc)
39  {
40  Entity = modNpc.npc;
41  }
42  else if (subjectIdentity is ModProjectile modProjectile)
43  {
44  Entity = modProjectile.projectile;
45  }
46  else if (subjectIdentity is ModPlayer modPlayer)
47  {
48  Entity = modPlayer.player;
49  }
50  else if (subjectIdentity is Entity entity)
51  {
52  Entity = entity;
53  }
54  else
55  {
56  throw new Exception($"Invalid subject for GraphicsEntityIdentity - {subjectIdentity}");
57  }
58  }
59 
60  protected abstract void LoadAssets(Item item);
61 
65  public void TryGettingDrawData(float rotation, float scale)
66  {
67  if (Properties.SkipUpdatingDrawData && !NeedsUpdate) return;
68 
69  DrawData = new DrawData
70  {
71  color = DrawColor,
72  effect = SpriteEffects.None,
73  rotation = rotation,
74  scale = new Vector2(scale, scale)
75  };
76 
77  //if (Main.LocalPlayer.gravDir == -1)
78  // DrawData.effect |= SpriteEffects.FlipVertically;
79  if (Entity.direction == -1)
80  {
81  DrawData.effect |= SpriteEffects.FlipHorizontally;
82  }
83  }
84 
85  private float offY;
86  private float offX;
87 
88  public void TryUpdatingDrawData(Texture2D texture)
89  {
90  if (Properties.SkipUpdatingDrawData && !NeedsUpdate) return;
91 
92  var frame = texture.Frame();
93  offX = Main.screenPosition.X - texture.Width * 0.5f;
94  offY = Main.screenPosition.Y - texture.Height * 0.5f;
95  DrawData.position = new Vector2
96  (
97  Entity.position.X - offX,
98  Entity.position.Y - offY
99  );
100  DrawData.origin = frame.Size() * 0.5f;
101 
102  if (UseDestinationRectangle)
103  {
104  if (DestinationRectangle.HasValue)
105  {
106  DrawData.destinationRectangle = DestinationRectangle.Value;
107  }
108  else
109  {
110  DrawData.destinationRectangle = frame;
111 
112  if (Entity is NPC npc)
113  {
114  DrawData.destinationRectangle = npc.frame;
115  }
116  else if (Entity is Projectile projectile)
117  {
118  DrawData.destinationRectangle.Y = projectile.frame * projectile.height;
119  }
120  }
121  }
122  }
123 
124  public void DrawEntity(SpriteBatch spriteBatch)
125  {
126  if (DrawData.useDestinationRectangle)
127  {
128  spriteBatch.Draw(DrawData.texture, DrawData.destinationRectangle, DrawData.sourceRect, DrawData.color, DrawData.rotation, DrawData.origin, DrawData.effect, 0f);
129  }
130  else
131  {
132  spriteBatch.Draw(DrawData.texture, DrawData.position, DrawData.sourceRect, DrawData.color, DrawData.rotation, DrawData.origin, DrawData.scale, DrawData.effect, 0f);
133  }
134  NeedsUpdate = false;
135  }
136  }
137 }
void TryGettingDrawData(float rotation, float scale)
Sets up drawing data initially
void DrawEntity(SpriteBatch spriteBatch)
void TryUpdatingDrawData(Texture2D texture)
Defines the base abstract of a GraphicsEntity
Defines a set of properties for a GraphicsEntity<T>
GraphicsEntity(object subjectIdentity)