-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
213 lines (205 loc) · 7.56 KB
/
Utils.cs
File metadata and controls
213 lines (205 loc) · 7.56 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System;
using System.Collections.Generic;
using System.Linq;
using InteXi.Scripts;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using static InteXi.Scripts.Constants;
namespace InteXi
{
public static class Utils
{
private static bool once = true;
private static int count = 0;
private static int counter = 0;
public static Rectangle UVecToRect(Vector2 vector, Vector2 size)
{
return new Rectangle((int)vector.X, (int)vector.Y, (int)size.X, (int)size.Y);
}
public static Vector2 USnap(Vector2 position, Vector2 size)
{
int snappedX = (int)(position.X / size.X) * (int)size.X;
int snappedY = (int)(position.Y / size.Y) * (int)size.Y;
return new Vector2(snappedX, snappedY);
}
public static Point USnapPoint(Point position, Vector2 size)
{
int snappedX = (int)Math.Floor(position.X / size.X) * (int)size.X;
int snappedY = (int)Math.Floor(position.Y / size.Y) * (int)size.Y;
return new Point(snappedX, snappedY);
}
public static int USnap(float position, float size)
{
return (int)Math.Floor(position / size) * (int)size;
}
public static int UBoolToInt(bool boolean)
{
return boolean ? 1 : 0;
}
public static float Ueaseoutcubic(float x)
{
return 1 - MathF.Pow(1 - x, 3);
}
public static int USolidType(int i, int j, string[] mapdata, char symbol, out bool NotUpped)
{
string surroundings;
char left = (j > 0) ? mapdata[i][j - 1] : 'o';
char right = (j < mapdata[i].Length - 1) ? mapdata[i][j + 1] : 'o';
char top = (i > 0) ? mapdata[i - 1][j] : 'o';
char bottom = (i < mapdata.Length - 1) ? mapdata[i + 1][j] : 'o';
surroundings = $"{left}{right}{top}{bottom}";
NotUpped = !SolidTilesSymbols.Contains(surroundings[2]);
char[] surroundingsArray = surroundings.ToCharArray();
for (int s = 0; s < 4; s++)
{
if (surroundingsArray[s] == symbol)
{
surroundingsArray[s] = 's';
}
else surroundingsArray[s] = 'o';
}
surroundings = new string(surroundingsArray);
int type = surroundings switch
{
"oooo" => 0,
"ssoo" => 1,
"ssos" => 1,
"osos" => 2,
"soos" => 3,
"ssss" => 4,
"ssso" => 4,
"soss" => 4,
"osss" => 4,
"ooss" => 4,
"osso" => 5,
"soso" => 6,
"ooos" => 7,
"ooso" => 8,
"osoo" => 9,
"sooo" => 10,
_ => 0
};
return type;
}
public static int UShelfType(int i, int j, string[] mapdata,out bool NotUpped)
{
char left = (j > 0) ? mapdata[i][j - 1] : 'o';
char right = (j < mapdata[i].Length - 1) ? mapdata[i][j + 1] : 'o';
string surroundings = $"{left}{right}";
NotUpped = !SolidTilesSymbols.Contains((i > 0) ? mapdata[i - 1][j] : 'o');
char[] surroundingsArray = surroundings.ToCharArray();
for (int s = 0; s < 2; s++){
if (SolidTilesSymbols.Contains(surroundingsArray[s])){
surroundingsArray[s] = 'i';
}
else surroundingsArray[s] = 'o';
}
surroundings = new string(surroundingsArray);
int type = surroundings switch
{
"oo" => 0,
"ii" => 0,
"io" => 1,
"ip" => 1,
"op" => 1,
"oi" => 2,
"pi" => 2,
"po" => 2,
"pp" => 3,
_ => 0
};
return type;
}
public static int UWaterType(int i, int j, string[] mapdata, out char name)
{
char top = (i > 0) ? mapdata[i - 1][j] : 'o';
char bottom = (i < mapdata.Length - 1) ? mapdata[i + 1][j] : 'o';
int type = $"{top}{bottom}".Replace("l", "o").Replace("i", "o").Replace("p", "o").Replace("r", "o") switch
{
"oo" => 0,
"ow" => 0,
"wo" => 1,
"ww" => 1,
_ => 0
};
name = (type == 0) ? 't' : 'b';
return type;
}
public static bool UKey(Keys key)
{
return Keyboard.GetState().IsKeyDown(key);
}
public static void UD<T>(T thing)
{
Console.WriteLine(thing + " -> " + count);
count++;
}
public static bool UWithin(float x, float above, float below)
{
return (x <= above) && (x >= below);
}
public static void UDrawLines(SpriteBatch sp, Texture2D pixel, Vector2[] points, Color color, int width = 1)
{
for (int i = 0; i < points.Length - 1; i++)
{
DrawLine(sp, pixel, points[i], points[i + 1], color, width);
}
}
public static void DrawLine(SpriteBatch sp, Texture2D Pixel, Vector2 point1, Vector2 point2, Color color, int width = 1)
{
float angle = MathF.Atan2(point2.Y - point1.Y, point2.X - point1.X);
float length = Vector2.Distance(point1, point2);
Vector2 midway = (point1 + point2) * 0.5f;
sp.Draw(Pixel, midway, null, color, angle, new Vector2(0.5f, 0.5f), new Vector2((float)Math.Ceiling(length), width), SpriteEffects.None, 1);
}
public static void doevery(int interval, Action action)
{
if (counter == interval)
{
action();
counter = 0;
}
counter++;
}
public static Vector2 URoundVect(Vector2 vector)
{
return new Vector2(MathF.Round(vector.X), MathF.Round(vector.Y));
}
public static Vector2 UFloorVect(Vector2 vector)
{
return new Vector2((int)vector.X, (int)vector.Y);
}
public static void InitNodesDirs(Vector2 GridUnit,out List<Vector2> Front, out List<Vector2> Up, out List<Vector2> Down)
{
int w = (int)GridUnit.X;
int h = (int)GridUnit.Y;
Front = [new(w, 0), new(2 * w, 0), new(3 * w, 0), new(4 * w, 0)];
Up = [new(w, -h), new(w, -2 * h), new(2 * w, -h)];
Down = [new(w, h), new(w, 2 * h), new(w, 3 * h), new(w, 4 * h), new(2 * w, 2 * h), new(2 * w, h), new(3 * w, h)];
}
public static void UWriteList<T>(List<T> list)
{
list.ForEach(x => Console.Write(x+" "));
}
public static float FracPart(float value)
{
return (float)Math.Round(value - Math.Truncate(value), 6);
}
public static int UnAccumelat(float value) {
if (Math.Abs(value) >= 1) {
int sign = MathF.Sign(value);
return sign;
}
return 0;
}
public static T FindInterval<T>(int time, Dictionary<int,T> intervals){
foreach (var D in intervals) {
if (time < D.Key) {
return D.Value;
}
}
return intervals.First().Value;
}
}
}