forked from daeken/OpenEQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensions.cs
74 lines (65 loc) · 2.24 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
using Godot;
using OpenEQ.Network;
using System;
using System.IO;
using System.Threading.Tasks;
using static System.Console;
public static class Extensions {
public static Vector2 ReadVector2(this BinaryReader br) {
return new Vector2(br.ReadSingle(), br.ReadSingle());
}
public static Vector3 ReadVector3(this BinaryReader br) {
return new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
}
public static Quat ReadQuat(this BinaryReader br) {
return new Quat(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
}
public static Quat EulerToQuat(this Vector3 v) {
float c1 = Mathf.cos(v.x / 2), c2 = Mathf.cos(v.y / 2), c3 = Mathf.cos(v.z / 2);
float s1 = Mathf.sin(v.x / 2), s2 = Mathf.sin(v.y / 2), s3 = Mathf.sin(v.z / 2);
return new Quat(
s1 * c2 * c3 + c1 * s2 * s3,
c1 * s2 * c3 - s1 * c2 * s3,
c1 * c2 * s3 + s1 * s2 * c3,
c1 * c2 * c3 - s1 * s2 * s3
);
}
public static Transform ToTransform(this Tuple<Vector3, Quat> data) {
return new Transform(data.Item2, data.Item1);
}
public static Tuple<float, float, float, float> GetPositionHeading(this SpawnPosition position) {
return new Tuple<float, float, float, float>(
position.Y / 8f,
position.Z / 8f,
position.X / 8f,
position.Heading / 8f / 255f
);
}
public static Tuple<float, float, float, float> GetPositionHeading(this UpdatePosition position) {
return new Tuple<float, float, float, float>(
position.Y / 8f,
position.Z / 8f,
position.X / 8f,
position.Heading / 8f / 255f
);
}
public static Tuple<float, float, float, float> GetDeltas(this UpdatePosition position) {
return new Tuple<float, float, float, float>(
position.DeltaY / 64f,
position.DeltaZ / 64f,
position.DeltaX / 64f,
position.DeltaHeading / 64f / 255f
);
}
public static Vector3 XYZ(this Tuple<float, float, float> data) {
return new Vector3(data.Item1, data.Item2, data.Item3);
}
public static Vector3 XYZ(this Tuple<float, float, float, float> data) {
return new Vector3(data.Item1, data.Item2, data.Item3);
}
public static T Get<T>(this WeakReference<T> wr) where T : class {
if(!wr.TryGetTarget(out var r))
return null;
return r;
}
}