/
Extensions.cs
143 lines (128 loc) · 4.77 KB
/
Extensions.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using XoticEngine.Components;
using XoticEngine.GameObjects;
using XoticEngine.Shapes;
namespace XoticEngine
{
public static class Extensions
{
//Drawing
public static void Draw(this SpriteBatch s, IXDrawable d)
{
//Draw an IXDrawable
s.Draw(d.Sprite, d.Position, d.SourceRectangle, d.DrawColor, d.Rotation, d.Origin, d.Scale, d.Effects, d.Depth);
}
public static void DrawLine(this SpriteBatch s, Line l, Color color)
{
//Calculate the direction
Vector2 direction = l.P2.ToVector2() - l.P1.ToVector2();
//Draw a rotated rectangle with height 1 (a line)
s.Draw(Assets.DummyTexture, new Rectangle(l.P1.X, l.P1.Y, (int)direction.Length(), 1), null, color, direction.GetAngle(), Vector2.Zero, SpriteEffects.None, 0);
}
public static void DrawPath(this SpriteBatch s, Path p, int pointSize, Color color)
{
//Draw each point from the path
foreach (Vector2 v in p.Points)
s.Draw(Assets.DummyTexture, v, null, color, 0, new Vector2(0.5f), pointSize, SpriteEffects.None, 0);
}
//Point to Vector2
public static Vector2 ToVector2(this Point p)
{
//Return a vector from a point
return new Vector2(p.X, p.Y);
}
public static Point ToPoint(this Vector2 v)
{
//Round a vector to a point
return new Point((int)Math.Round(v.X, MidpointRounding.AwayFromZero), (int)Math.Round(v.Y, MidpointRounding.AwayFromZero));
}
//Vector2
public static Vector2 GetNormalized(this Vector2 v)
{
Vector2 n = v;
n.Normalize();
return n;
}
//Rectangle size
public static Point Size(this Rectangle r)
{
return new Point(r.Width, r.Height);
}
//Angle and direction
public static float GetAngle(this Vector2 v)
{
//Get the angle from a direction vector (after normalizing)
return MathF.Atan2(v.Y / v.Length(), v.X / v.Length());
}
public static Vector2 GetDirection(this double a)
{
return ((float)a).GetDirection();
}
public static Vector2 GetDirection(this float a)
{
//Get the direction from an angle
return new Vector2(MathF.Cos(a), MathF.Sin(a));
}
public static Vector2 Rotate(this Vector2 v, float radians)
{
//Rotate the vector
return new Vector2((float)(Math.Cos(radians) * v.X - Math.Sin(radians) * v.Y), (float)(Math.Sin(radians) * v.X + Math.Cos(radians) * v.Y));
}
//Random
public static float NextFloat(this Random r)
{
//Return a random float
return (float)r.NextDouble();
}
public static int NextSign(this Random r)
{
//Return -1 or 1
return r.Next(2) * 2 - 1;
}
public static Color NextColor(this Random r)
{
//Return a random color
return new Color(r.NextFloat(), r.NextFloat(), r.NextFloat());
}
//Text wrap
public static string Wrap(this SpriteFont font, string s, int lineWidth)
{
string wrapped = String.Empty;
string line = String.Empty;
//Get all words
string[] words = s.Split(' ');
for (int i = 0; i < words.Length; i++)
{
//Check for newline
if (words[i] == "\n")
{
wrapped += line + "\n";
line = String.Empty;
}
//Check if the line is longer than the width of the textbox
else if (font.MeasureString(line + words[i]).X >= lineWidth)
{
wrapped += line + "\n";
line = words[i] + " ";
}
//Add a word to the line
else
line += words[i] + " ";
//TODO: Check if the word is longer than the line
}
//Return the wrapped text plus the last line
return wrapped + line;
}
//SpriteBatchSettings
public static void Begin(this SpriteBatch s, SpriteBatchSettings settings)
{
//Begin the spritebatch with all the spritebatch settings
s.Begin(settings.SortMode, settings.BlendState, settings.SamplerState, settings.DepthStencilState, settings.RasterizerState, settings.Effect, settings.TransformMatrix);
}
}
}