-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPurityCore.cs
438 lines (390 loc) · 12.1 KB
/
PurityCore.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
using System.Threading.Tasks;
using System.Threading;
using System;
namespace PurityEngine {
/// <summary>
/// The class that represents the universe.
/// </summary>
public class Universe : Asset {
/// <summary>
/// The instance of the universe.
/// </summary>
public static Universe instance;
/// <summary>
/// The root entity.
/// </summary>
public Node root = new Node();
public Universe() {
instance = this;
}
/// <summary>
/// Finds an object with a component
/// <summary>
public T Get<T>() {
return root.Get<T>();
}
/// Updates the universe.
/// </summary>
public void Update() {
Time.TimeIteration();
root.Update();
root.AsyncUpdate();
}
}
/// <summary>
/// The class for anything in the game
/// </summary>
public class Node : Asset {
/// <summary>
/// The node's transform
/// </summary>
public Transform transform = new Transform();
/// <summary>
/// The node's child nodes
/// </summary>
public Node[] children = new Node[0];
/// <summary>
/// The node's components
/// </summary>
public Component[] components = new Component[0];
/// <summary>
/// The node's parent node
/// </summary>
public Node parent;
/// <summary>
/// The universe that owns this node
/// </summary>
public Universe universe;
private bool initialized;
public Node() {
}
/// <summary>
/// Calls the start function an all the components
/// </summary>
public void Start() {
for (int i = 0; i < components.Length; i++) {
try {
components[i].node = this;
((dynamic)components[i]).Start();
} catch {}
}
}
/// <summary>
/// Calls the update function an all the components
/// </summary>
public void Update() {
if (!initialized) {
initialized = true;
Start();
}
for (int i = 0; i < components.Length; i++) {
components[i].node = this;
try {
((dynamic)components[i]).Update();
} catch {}
}
for (int i = 0; i < children.Length; i++) {
children[i].universe = universe;
children[i].Update();
}
}
/// <summary>
/// Calls the async update function an all the components (asynchronously)
/// </summary>
public void AsyncUpdate() {
for (int i = 0; i < components.Length; i++) {
if (components[i].GetType().GetMethod("AsyncUpdate") != null) {
Thread th = new Thread((ParameterizedThreadStart)components[i].GetType().GetMethod("AsyncUpdate").CreateDelegate(typeof(ParameterizedThreadStart)));
th.Start();
}
}
for (int i = 0; i < children.Length; i++) {
children[i].AsyncUpdate();
}
}
/// <summary>
/// Gets a component of a specific type
/// </summary>
public T Get<T>() {
for (int i = 0; i < components.Length; i++) {
if (components[i] is T) {
return (T)(dynamic)components[i];
}
}
for (int i = 0; i < children.Length; i++) {
if (children[i].Get<T>() != null) {
return children[i].Get<T>();
}
}
return default(T);
}
}
/// <summary>
/// The Time class, which allows you to get time information.
/// </summary>
public class Time {
/// <summary>
/// The time in seconds since the start of the game
/// </summary>
public static double time = 0;
/// <summary>
/// The time's scaling factor
/// </summary>
public static double timeScale = 1;
/// <summary>
/// The amount of time since the last frame in seconds
/// </summary>
public static double deltaTime;
private static double previousTime;
public static void TimeIteration() {
double t = DateTime.UtcNow.Subtract(System.DateTime.UnixEpoch).TotalSeconds;
if (previousTime == 0) {
previousTime = t;
}
deltaTime = (t - previousTime) * timeScale;
previousTime = t;
time += deltaTime;
}
}
/// <summary>
/// The base of all functionality for Nodes
/// </summary>
public class Component {
/// <summary>
/// The node attached to this component
/// </summary>
public Node node;
/// <summary>
/// The transform attached to the node
/// </summary>
public Transform transform {
get {
return node.transform;
}
}
/// <summary>
/// The universe the node lives in
/// </summary>
public Universe universe {
get {
return node.universe;
}
}
public Component() {
}
/// <summary>
/// Gets a component of a specific type
/// </summary>
public T Get<T>() {
return node.Get<T>();
}
}
/// <summary>
/// The class for colors
/// </summary>
public class Color {
/// <summary>
/// The red component of a color
/// </summary>
public double r;
/// <summary>
/// The green component of a color
/// </summary>
public double g;
/// <summary>
/// The blue component of a color
/// </summary>
public double b;
public Color(double r, double g, double b) {
this.r = r;
this.g = g;
this.b = b;
}
}
/// <summary>
/// The class for images
/// </summary>
public class Image : Asset, Texture {
/// <summary>
/// The array of colors
/// </summary>
public Color[,] colors;
/// <summary>
/// The width of the image
/// </summary>
public int width;
/// <summary>
/// The height of the image
/// </summary>
public int height;
public Image() {
}
public float WidthToHeightRatio() {
return (float)width / (float)height;
}
public Color GetColorAtCoordinate(double x, double y) {
return colors[(int)x, (int)y];
}
}
/// <summary>
/// The interface for images with dynamic resolutions
/// </summary>
public interface Texture {
/// <summary>
/// The width relative to the height
/// </summary>
public float WidthToHeightRatio();
/// <summary>
/// Gets the color at a coordinate
/// </summary>
public Color GetColorAtCoordinate(double x, double y);
}
/// <summary>
/// The class for the position of a component in the world
/// </summary>
public class Transform {
/// <summary>
/// The position of the object
/// </summary>
public Vector3D position;
/// <summary>
/// The rotation of the object
/// </summary>
public Vector3D rotation;
/// <summary>
/// The scale of the object
/// </summary>
public Vector3D scale;
public Transform() {
}
}
/// <summary>
/// A 2D position
/// </summary>
public class Vector2D {
public double x;
public double y;
public Vector2D() {
}
public Vector2D(double x, double y) {
this.x = x;
this.y = y;
}
public static implicit operator Vector3D(Vector2D value) {
return new Vector3D(value.x,value.y,0);
}
public static implicit operator Vector4D(Vector2D value) {
return new Vector4D(value.x,value.y,0,0);
}
public static Vector2D operator +(Vector2D a, Vector2D b)
{
return new Vector2D(a.x + b.x, a.y + b.y);
}
public static Vector2D operator -(Vector2D a, Vector2D b)
{
return new Vector2D(a.x - b.x, a.y - b.y);
}
public static Vector2D operator *(Vector2D a, Vector2D b)
{
return new Vector2D(a.x * b.x, a.y * b.y);
}
public static Vector2D operator /(Vector2D a, Vector2D b)
{
return new Vector2D(a.x / b.x, a.y / b.y);
}
public static Vector2D operator *(Vector2D a, double b) {
return new Vector2D(a.x * b, a.y * b);
}
public static Vector2D operator /(Vector2D a, double b) {
return new Vector2D(a.x / b, a.y / b);
}
}
/// <summary>
/// A 3D position
/// </summary>
public class Vector3D {
public double x;
public double y;
public double z;
public static implicit operator Vector2D(Vector3D value) {
return new Vector2D(value.x,value.y);
}
public static implicit operator Vector4D(Vector3D value) {
return new Vector4D(value.x,value.y,0,0);
}
public Vector3D() {
}
public Vector3D(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public static Vector3D operator +(Vector3D a, Vector3D b)
{
return new Vector3D(a.x + b.x, a.y + b.y, a.z + b.z);
}
public static Vector3D operator -(Vector3D a, Vector3D b)
{
return new Vector3D(a.x - b.x, a.y - b.y, a.z - b.z);
}
public static Vector3D operator *(Vector3D a, Vector3D b)
{
return new Vector3D(a.x * b.x, a.y * b.y, a.z * b.z);
}
public static Vector3D operator /(Vector3D a, Vector3D b)
{
return new Vector3D(a.x / b.x, a.y / b.y, a.z / b.z);
}
public static Vector3D operator *(Vector3D a, double b) {
return new Vector3D(a.x * b, a.y * b, a.z * b);
}
public static Vector3D operator /(Vector3D a, double b) {
return new Vector3D(a.x / b, a.y / b, a.z * b);
}
}
/// <summary>
/// A 4D position
/// </summary>
public class Vector4D {
public double x;
public double y;
public double z;
public double w;
public static implicit operator Vector2D(Vector4D value) {
return new Vector2D(value.x,value.y);
}
public static implicit operator Vector3D(Vector4D value) {
return new Vector3D(value.x,value.y,0);
}
public Vector4D() {
}
public Vector4D (double x, double y, double z, double w) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public static Vector4D operator +(Vector4D a, Vector4D b)
{
return new Vector4D(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
}
public static Vector4D operator -(Vector4D a, Vector4D b)
{
return new Vector4D(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
}
public static Vector4D operator *(Vector4D a, Vector4D b)
{
return new Vector4D(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
}
public static Vector4D operator /(Vector4D a, Vector4D b)
{
return new Vector4D(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w);
}
public static Vector4D operator *(Vector4D a, double b) {
return new Vector4D(a.x * b, a.y * b, a.z * b, a.w * b);
}
public static Vector4D operator /(Vector4D a, double b) {
return new Vector4D(a.x / b, a.y / b, a.z / b, a.w / b);
}
}
}