-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathHitBox.java
More file actions
143 lines (108 loc) · 3.59 KB
/
HitBox.java
File metadata and controls
143 lines (108 loc) · 3.59 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
package net.countercraft.movecraft.util.hitboxes;
import com.google.common.collect.Iterators;
import com.google.common.collect.UnmodifiableIterator;
import net.countercraft.movecraft.MovecraftLocation;
import net.countercraft.movecraft.exception.EmptyHitBoxException;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.stream.Stream;
public interface HitBox extends Iterable<MovecraftLocation>{
int getMinX();
int getMinY();
int getMinZ();
int getMaxX();
int getMaxY();
int getMaxZ();
default int getXLength() {
if (isEmpty())
return 0;
return Math.abs(getMaxX() - getMinX());
}
default int getYLength() {
if (isEmpty())
return 0;
return Math.abs(getMaxY() - getMinY());
}
default int getZLength() {
if (isEmpty())
return 0;
return Math.abs(getMaxZ() -getMinZ());
}
default boolean isEmpty() {
return size() == 0;
}
int size();
private static int average(int high, int low) {
return (high&low) + (high^low) / 2;
}
@NotNull
default MovecraftLocation getMidPoint() throws EmptyHitBoxException {
if (isEmpty())
throw new EmptyHitBoxException();
return new MovecraftLocation(average(getMaxX(), getMinX()), average(getMaxY(), getMinY()),average(getMaxZ(), getMinZ()));
}
@NotNull
@Override
Iterator<MovecraftLocation> iterator();
boolean contains(@NotNull MovecraftLocation location);
default boolean contains(int x, int y, int z) {
return contains(new MovecraftLocation(x, y, z));
}
boolean containsAll(Collection<? extends MovecraftLocation> collection);
default boolean inBounds(double x, double y, double z) {
if (isEmpty())
return false;
return x >= getMinX() && x <= getMaxX() &&
y >= getMinY() && y <= getMaxY()&&
z >= getMinZ() && z <= getMaxZ();
}
default boolean inBounds(MovecraftLocation location) {
return inBounds(location.getX(),location.getY(),location.getZ());
}
@NotNull
default SolidHitBox boundingHitBox(){
return new SolidHitBox(new MovecraftLocation(this.getMinX(),this.getMinY(),this.getMinZ()),
new MovecraftLocation(this.getMaxX(),this.getMaxY(),this.getMaxZ()));
}
@NotNull
default Set<MovecraftLocation> asSet(){
return new HitBoxSetView(this);
}
@NotNull
@Contract(pure = true)
default Stream<MovecraftLocation> stream(){
return asSet().stream();
}
@NotNull
HitBox difference(HitBox other);
@NotNull
HitBox intersection(HitBox other);
@NotNull
HitBox union(HitBox other);
@NotNull
HitBox symmetricDifference(HitBox other);
int getMinYAt(int x, int z);
class HitBoxSetView extends AbstractSet<MovecraftLocation> {
private final HitBox backing;
public HitBoxSetView(HitBox backing) {
this.backing = backing;
}
@NotNull
@Override
public UnmodifiableIterator<MovecraftLocation> iterator() {
return Iterators.unmodifiableIterator(backing.iterator());
}
@Override
public int size() {
return backing.size();
}
@Override
public boolean contains(Object location) {
return location instanceof MovecraftLocation && backing.contains((MovecraftLocation) location);
}
}
}