-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exts.cs
121 lines (102 loc) · 2.77 KB
/
Exts.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Xml;
using System.Reflection;
namespace AlienGame
{
static class RectangleExts
{
public static Rectangle Fix(this Rectangle r)
{
if (r.Width < 0)
{
r.X = r.X + r.Width;
r.Width = -r.Width;
}
if (r.Height < 0)
{
r.Y = r.Y + r.Height;
r.Height = -r.Height;
}
++r.Width;
++r.Height;
return r;
}
public static void Remove<T>(this List<T> t, IEnumerable<T> toRemove)
{
t.RemoveAll(x => toRemove.Contains(x));
}
public static Rectangle ToPointRect(this Point p)
{
return new Rectangle(p.X, p.Y, 1, 1);
}
public static Rectangle SquaresToPixels(this Rectangle r)
{
return new Rectangle(r.Left * 40, r.Top * 40, r.Width * 40, r.Height * 40);
}
public static Point SquareToCenter(this Point p)
{
return new Point(p.X * 40 + 20, p.Y * 40 + 20);
}
public static Color WithAlpha(this Color c, int alpha)
{
return Color.FromArgb(alpha, c);
}
public static void WriteAttribute(this XmlWriter w, string name, string value)
{
w.WriteAttributeString(name, value);
}
public static void WriteAttribute(this XmlWriter w, string name, int value)
{
w.WriteAttributeString(name, value.ToString());
}
public static void WriteAttribute(this XmlWriter w, string name, float value)
{
w.WriteAttributeString(name, value.ToString());
}
public static int GetAttributeInt(this XmlElement e, string name, int def)
{
int val = def;
return int.TryParse(e.GetAttribute(name), out val) ? val : def;
}
public static float GetAttributeFloat(this XmlElement e, string name, float def)
{
float val = def;
return float.TryParse(e.GetAttribute(name), out val) ? val : def;
}
public static Point ToSquare(this Point p)
{
return new Point(p.X / 40, p.Y / 40);
}
public static int DistanceSqTo(this Point p, Point q)
{
var dx = p.X - q.X;
var dy = p.Y - q.Y;
return dx * dx + dy * dy;
}
public static T ClosestTo<T>(this IEnumerable<T> ts, Actor a)
where T : Actor
{
return ts.OrderBy(t => t.Position.DistanceSqTo(a.Position))
.FirstOrDefault();
}
public static IEnumerable<T> Concat<T>(this IEnumerable<T> xs, params T[] ys)
{
return xs.Concat((IEnumerable<T>)ys);
}
public static IEnumerable<Type> ConcreteSubtypes( this Type u )
{
return Assembly.GetExecutingAssembly().GetTypes()
.Where(t => !t.IsAbstract && u.IsAssignableFrom(t));
}
public static Point Lerp(this float t, Point a, Point b)
{
return new Point(
(int)((1 - t) * a.X + t * b.X),
(int)((1 - t) * a.Y + t * b.Y));
}
}
}