-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapHandler.java
More file actions
189 lines (151 loc) · 4.86 KB
/
Copy pathMapHandler.java
File metadata and controls
189 lines (151 loc) · 4.86 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
143
144
145
146
147
148
149
150
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import hsa.*;
public class MapHandler
{
private static boolean [][] _map;
private static float _gridLength = 25;
private static int _xSize, _ySize;
//************************
//loadMap(String file)
//Loads the map from file
//************************
public static void loadMap(String file){
TextInputFile f = new TextInputFile(file);
_xSize = f.readInt();
_ySize = f.readInt();
_map = new boolean [_xSize] [_ySize];
for (int y = 0; y < _ySize; y++){
for(int x = 0; x < _xSize; x++){
int value = f.readInt();
_map [x] [y] = (value == 1);
}
}
}
//************************
//isWall(int x, int y)
//Returns if the specified cord is a wall
//************************
public static boolean isWall(float x, float y){
int xGrid = MathHelper.catSeg(x, _gridLength);
int yGrid = MathHelper.catSeg(y, _gridLength);
if (xGrid >= 0 && xGrid < _xSize &&
yGrid >= 0 && yGrid < _ySize ){
return _map [xGrid] [yGrid];
}
return true;
}
//************************
//castRay(float x, float y, float degree)
//Returns distance to wall from x,y at angle degree.
//************************
public static float castRay(float x, float y, float degree){
float [] array = new float [2];
array [0] = 999999999;
array [1] = 999999999;
int count = 0;
degree = MathHelper.normalizeAngle(degree);
float angle = (float)Math.toRadians(degree);
if (degree > 0 && degree < 180){
array [count] = _castRayNorth(x, y, angle);
count++;
}
if (degree > 90 && degree < 270){
array [count] = _castRayWest(x,y,angle);
count++;
}
if (degree > 180 && degree < 360){
array [count] = _castRaySouth(x, y , angle);
count++;
}
if (degree > 270 || degree < 90){
array [count] = _castRayEast(x, y, angle);
}
float distance = MathHelper.smallNumber(array[0], array[1]);
return distance;
}
//*=*=*=*=*=*=*=*=*=
//_castRay$DIRECTION$(float x, float y, float angle)
//Casts rays in each of the directions. Used to simplify implimentation.
//Each only handles ray casts in its direction. Otherwise it will crash.
//VVVVVVVVVVVVVVVVVV
private static float _castRayNorth(float x, float y, float angle) {
boolean isWall = false;
float yDistence = 0, xDistence = 0;
int xCheck = MathHelper.catSeg(x, _gridLength);;
int yCheck = MathHelper.catSeg(y, _gridLength);
while (isWall == false) {
yCheck--;
yDistence = y - ((yCheck + 1) * _gridLength);
if (angle != Math.PI * 0.5) {
xDistence = yDistence / (float) Math.tan(angle);
xCheck = MathHelper.catSeg(x + xDistence, _gridLength);
if(xCheck<0 || xCheck>=_xSize){
return 999999999;
}
}
isWall = _map[xCheck][yCheck];
}
float distance = MathHelper.distance(xDistence, yDistence);
return distance;
}
private static float _castRaySouth(float x, float y, float angle) {
boolean isWall = false;
float yDistence = 0, xDistence = 0;
int xCheck = MathHelper.catSeg(x, _gridLength);
int yCheck = MathHelper.catSeg(y, _gridLength);
while (isWall == false) {
yCheck++;
yDistence = y - (yCheck * _gridLength);
if (angle != Math.PI * 1.5) {
xDistence = yDistence / (float) Math.tan(angle);
xCheck = MathHelper.catSeg(x + xDistence, _gridLength);
if(xCheck<0 || xCheck>=_xSize){
return 999999999;
}
}
isWall = _map[xCheck][yCheck];
}
float distance = MathHelper.distance(xDistence, yDistence);
return distance;
}
private static float _castRayWest(float x, float y, float angle) {
boolean isWall = false;
float yDistence = 0, xDistence = 0;
int xCheck = MathHelper.catSeg(x, _gridLength);
int yCheck = MathHelper.catSeg(y, _gridLength);
while (isWall == false) {
xCheck--;
xDistence = ((xCheck + 1) * _gridLength) - x;
if (angle != Math.PI) {
yDistence = -xDistence * (float) Math.tan(angle);
yCheck = MathHelper.catSeg(y + yDistence, _gridLength);
if(yCheck<0 || yCheck>=_ySize){
return 999999999;
}
}
isWall = _map[xCheck][yCheck];
}
float distance = MathHelper.distance(xDistence, yDistence);
return distance;
}
private static float _castRayEast(float x, float y, float angle) {
boolean isWall = false;
float yDistence = 0, xDistence = 0;
int xCheck = MathHelper.catSeg(x, _gridLength);
int yCheck = MathHelper.catSeg(y, _gridLength);
while (isWall == false) {
xCheck++;
xDistence = (xCheck * _gridLength) - x;
if (angle != 0) {
yDistence = -xDistence * (float) Math.tan(angle);
yCheck = MathHelper.catSeg(y + yDistence, _gridLength);
if(yCheck<0 || yCheck>=_ySize){
return 999999999;
}
}
isWall = _map[xCheck][yCheck];
}
float distance = MathHelper.distance(xDistence, yDistence);
return distance;
}
//^^^^^^^^^^^^^^^^^^
}