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

import java.io.Serializable;

5
import dev.morphia.annotations.Entity;
Melledy's avatar
Melledy committed
6
7
import emu.grasscutter.net.proto.VectorOuterClass.Vector;

8
@Entity
Melledy's avatar
Melledy committed
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
public class Position implements Serializable {
	private static final long serialVersionUID = -2001232313615923575L;
	
	private float x;
	private float y;
	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) {
		return getX() == other.getX() && getY() == other.getY();
	}
	
	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();
	}
}