-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRay.java
91 lines (79 loc) · 2.98 KB
/
Ray.java
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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Ray {
// more friendly wrapper for native raycast function (see Native.java and native/Native.c)
public Vector2 origin;
public double direction;
private Map<Wall, Vector2> intersections = new HashMap<Wall, Vector2>();
public Ray(Vector2 origin, double direction) {
this.origin = origin;
this.direction = direction;
}
public boolean Intersects(Wall wall) {
Vector2 intersection = Intersection(wall);
return intersection != null;
}
public double Cast(Level level) {
Wall closestWall = null;
double closestDist = Double.MAX_VALUE;
for (Wall wall : level.GetAllWalls()) {
Vector2 intersection = Intersection(wall);
if (intersection != null) {
double distance = Vector2.Distance(origin, intersection);
if (distance < closestDist) {
closestWall = wall;
closestDist = distance;
}
}
}
// If no wall was hit, return -1
if (closestWall != null) {
return closestDist;
}
return -1;
}
public Entity HitscanEntities(Level level) {
HashMap<Entity, Wall> entityWalls = new HashMap<Entity, Wall>();
for (Entity entity : level.entities) {
Wall wall = entity.makeWall();
entityWalls.put(entity, wall);
}
// Find the closest wall
// key value pair of entity and wall
Entity closestWall = null;
Wall closestWallWall = null;
double closestDist = Double.MAX_VALUE;
for (Wall wall : entityWalls.values()) {
Vector2 intersection = Intersection(wall);
if (intersection != null) {
double distance = Vector2.Distance(origin, intersection);
if (distance < closestDist) {
closestWall = entityWalls.entrySet().stream().filter(entry -> entry.getValue().equals(wall)).findFirst().get().getKey();
closestWallWall = wall;
closestDist = distance;
}
}
}
// If no wall was hit, return -1
if (closestDist == Double.MAX_VALUE) {
return null;
}
return closestWall;
}
public Vector2 Intersection(Wall wall) {
if (intersections.containsKey(wall)) {
return intersections.get(wall);
}
double[] result = Native.RayCast(wall.vertA.x, wall.vertA.y, wall.vertB.x, wall.vertB.y, origin.x, origin.y, direction);
// Check the length of the result array
if (result.length == 0) {
return null; // No intersection
}
// Convert the result array to a Vector2
Vector2 intersection = new Vector2(result[0], result[1]);
intersections.put(wall, intersection);
return intersection;
}
}