forked from hollowstrawberry/Virtuous
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tools.cs
162 lines (118 loc) · 5.58 KB
/
Tools.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
using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.GameInput;
using Terraria.Utilities;
namespace Virtuous
{
// This class contains many utilities that help make the code cleaner
public static class Tools
{
// Constants
public const float GoldenRatio = 1.61803398875f;
public const float FullCircle = 2 * (float)Math.PI; // Really just an alias that is also a float
public const float RevolutionPerSecond = FullCircle / 60; // 60 game ticks = 1 second
// Game object utilities
public static Vector2 SpriteCenter(this NPC npc) => npc.Center + new Vector2(0, npc.gfxOffY);
public static Vector2 SpriteCenter(this Player player) => player.Center + new Vector2(0, player.gfxOffY);
public static Vector2 MountedSpriteCenter(this Player player) => player.MountedCenter + new Vector2(0, player.gfxOffY);
public static string InternalName(this NPC npc) => Lang.GetNPCName(npc.type).Key.Split('.').Last();
public static string InternalName(this Item item) => Lang.GetItemName(item.type).Key.Split('.').Last();
public static string InternalName(this Projectile proj) => Lang.GetProjectileName(proj.type).Key.Split('.').Last();
public static bool InternalNameHas(this NPC npc, params string[] values)
=> npc.InternalName().ToUpper().ContainsAny(values.Select(x => x.ToUpper()));
public static bool InternalNameHas(this Item item, params string[] values)
=> item.InternalName().ToUpper().ContainsAny(values.Select(x => x.ToUpper()));
public static bool InternalNameHas(this Projectile proj, params string[] values)
=> proj.InternalName().ToUpper().ContainsAny(values.Select(x => x.ToUpper()));
// Changes the size of the hitbox while keeping its center
public static void ResizeProjectile(int projIndex, int newWidth, int newHeight, bool changeDrawPos = false)
{
Projectile projectile = Main.projectile[projIndex];
if (changeDrawPos)
{
projectile.modProjectile.drawOffsetX += (newWidth - projectile.width) / 2;
projectile.modProjectile.drawOriginOffsetY += (newHeight - projectile.height) / 2;
}
projectile.position += new Vector2(projectile.width / 2, projectile.height / 2);
projectile.width = newWidth;
projectile.height = newHeight;
projectile.position -= new Vector2(projectile.width / 2, projectile.height / 2);
}
// A trick to stop the bugged 1-tick delay between consecutive right-click uses of a weapon
public static void HandleAltUseAnimation(Player player)
{
if (player.altFunctionUse == 2)
{
if (PlayerInput.Triggers.JustReleased.MouseRight) // Stops the animation manually
{
player.itemAnimation = 0;
}
else if (player.itemAnimation == 1) // Doesn't let the hand return to resting position
{
player.altFunctionUse = 1;
player.controlUseItem = true;
}
}
}
// Vector extensions
public static Vector2 Normalized(this Vector2 vector) // Shorthand for SafeNormalize
{
return vector.SafeNormalize(Vector2.UnitX);
}
public static Vector2 OfLength(this Vector2 vector, float length) // Returns a vector with the magnitude changed
{
return vector.Normalized() * length;
}
public static Vector2 Perpendicular(this Vector2 oldVector, float? length = null, bool clockwise = true)
{
Vector2 vector = new Vector2(oldVector.Y, -oldVector.X);
if (!clockwise) vector *= new Vector2(-1, -1);
if (length != null) vector = vector.OfLength((float)length);
return vector;
}
// String extensions
public static string If(this string text, bool condition) // Helps with long string concatenation
{
return condition ? text : "";
}
public static bool ContainsAny(this string text, IEnumerable<string> values)
{
return values.All(text.Contains);
}
// Random extensions
public static Vector2 NextVector2(this UnifiedRandom rand)
{
return Vector2.UnitY.RotatedBy(rand.NextFloat(FullCircle));
}
public static Vector2 NextVector2(this UnifiedRandom rand, float minLength, float maxLength)
{
return rand.NextVector2().OfLength(rand.NextFloat(minLength, maxLength));
}
public static bool OneIn(this UnifiedRandom rand, int amount) // Returns true with a 1/amount chance
{
return rand.Next(amount) == 0;
}
public static T Choose<T>(this UnifiedRandom rand, IList<T> items)
{
if (items.Count == 0) return default(T);
if (items.Count == 1) return items[0];
return items[rand.Next(items.Count)];
}
// Number extensions
public static float ToRadians(this int deg) // Returns the given degrees in radians
{
return deg * (float)Math.PI / 180f;
}
public static float ToRadians(this float deg)
{
return deg * (float)Math.PI / 180f;
}
public static float ToDegrees(this float rad) // Returns the given radians in degrees
{
return rad * 180f / (float)Math.PI;
}
}
}