Position.java 3.61 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
3
4
package emu.grasscutter.utils;

import java.io.Serializable;

Melledy's avatar
Melledy committed
5
import com.google.gson.annotations.SerializedName;
6
import com.github.davidmoten.rtreemulti.geometry.Point;
7
import dev.morphia.annotations.Entity;
Melledy's avatar
Melledy committed
8
9
import emu.grasscutter.net.proto.VectorOuterClass.Vector;

10
@Entity
Melledy's avatar
Melledy committed
11
12
13
public class Position implements Serializable {
	private static final long serialVersionUID = -2001232313615923575L;
	
Melledy's avatar
Melledy committed
14
	@SerializedName(value="x", alternate={"_x", "X"})
Melledy's avatar
Melledy committed
15
	private float x;
Melledy's avatar
Melledy committed
16
17
	
	@SerializedName(value="y", alternate={"_y", "Y"})
Melledy's avatar
Melledy committed
18
	private float y;
Melledy's avatar
Melledy committed
19
20
	
	@SerializedName(value="z", alternate={"_z", "Z"})
Melledy's avatar
Melledy committed
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
	private float z;
	
	public Position() {

	}
	
	public Position(float x, float y) {
		set(x, y);
	}
	
	public Position(float x, float y, float z) {
		set(x, y, z);
	}
	
	public Position(String p) {
		String[] split = p.split(",");
		if (split.length >= 2) {
			this.x = Float.parseFloat(split[0]);
			this.y = Float.parseFloat(split[1]);
		}
		if (split.length >= 3) {
			this.z = Float.parseFloat(split[2]);
		}
	}
	
	public Position(Vector vector) {
		this.set(vector);
	}

	public Position(Position pos) {
		this.set(pos);
	}

	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getZ() {
		return z;
	}

	public void setZ(float z) {
		this.z = z;
	}
	
	public float getY() {
		return y;
	}

	public void setY(float y) {
		this.y = y;
	}
	
	public Position set(float x, float y) {
		this.x = x;
		this.y = y;
		return this;
	}
	
	// Deep copy
	public Position set(Position pos) {
		return this.set(pos.getX(), pos.getY(), pos.getZ());
	}
	
	public Position set(Vector pos) {
		return this.set(pos.getX(), pos.getY(), pos.getZ());
	}
	
	public Position set(float x, float y, float z) {
		this.x = x;
		this.y = y;
		this.z = z;
		return this;
	}

	public Position add(Position add) {
		this.x += add.getX();
		this.y += add.getY();
		this.z += add.getZ();
		return this;
	}
	
	public Position addX(float d) {
		this.x += d;
		return this;
	}
	
	public Position addY(float d) {
		this.y += d;
		return this;
	}
	
	public Position addZ(float d) {
		this.z += d;
		return this;
	}

	public Position subtract(Position sub) {
		this.x -= sub.getX();
		this.y -= sub.getY();
		this.z -= sub.getZ();
		return this;
	}
	
	/** In radians
	 * */
	public Position translate(float dist, float angle) {
		this.x += dist * Math.sin(angle);
		this.y += dist * Math.cos(angle);
		return this;
	}

	public boolean equal2d(Position other) {
138
139
140
141
142
143
144
145
146
147
148
149
150
		// Y is height
		return getX() == other.getX() && getZ() == other.getZ();
	}

	public boolean equal3d(Position other) {
		return getX() == other.getX() && getY() == other.getY() && getZ() == other.getZ();
	}

	public double computeDistance(Position b){
		double detX = getX()-b.getX();
		double detY = getY()-b.getY();
		double detZ = getZ()-b.getZ();
		return Math.sqrt(detX*detX+detY*detY+detZ*detZ);
Melledy's avatar
Melledy committed
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
	}
	public Position translateWithDegrees(float dist, float angle) {
		angle = (float) Math.toRadians(angle);
		this.x += dist * Math.sin(angle);
		this.y += -dist * Math.cos(angle);
		return this;
	}
	
	@Override
	public Position clone() {
		return new Position(x, y, z);
	}

	@Override
	public String toString() {
		return "(" + this.getX() + ", " + this.getY() + ", " + this.getZ() + ")";
	}
	
	public Vector toProto() {
		return Vector.newBuilder()
			.setX(this.getX())
			.setY(this.getY())
			.setZ(this.getZ())
			.build();
	}
176
177
	public Point toPoint(){
		return Point.create(x,y,z);
Akka's avatar
Akka committed
178
	}
179
180
181
182
183
184
185
186
187
188
189
190

	/**
	 * To XYZ array for Spatial Index
	 */
	public double[] toDoubleArray(){
		return new double[]{ x, y, z};
	}
	/**
	 * To XZ array for Spatial Index (Blocks)
	 */
	public double[] toXZDoubleArray(){
		return new double[]{x, z};
Akka's avatar
Akka committed
191
	}
Melledy's avatar
Melledy committed
192
}