-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCharacter.java
More file actions
357 lines (320 loc) · 14.1 KB
/
Character.java
File metadata and controls
357 lines (320 loc) · 14.1 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import processing.core.PApplet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.awt.event.KeyEvent;
public class Character implements Renderable{
/**
* The player does not leave the center of the screen. The map moves around the character. This gives the impression
* that the character is moving around the map.
*/
private final Point centerOfScreen;
/**
* This is the player's location in the matrix accounted for the scaling and offset. This is not the index into
* the matrix.
*/
private Point locationInMatrix;
private Set<Point> verticies;
private List<Wall> allWalls;
public boolean movingUp,movingDown,movingLeft,movingRight;
private Main sketch;
private final Maze maze;
public Character(Maze maze){
sketch = Main.getInstance();
this.maze = maze;
this.centerOfScreen = new Point(sketch.width / 2, sketch.height / 2);
maze.refresh(1, 1, Maze.WALL_SCALE, Maze.WALL_SCALE);
// start off at the top left of the maze
this.locationInMatrix = new Point(Maze.WALL_SCALE / 2, Maze.WALL_SCALE / 2); // temp
}
public void move(){
int dx = 0;
int dy = 0;
if(movingLeft){
dx-=1;
}
if(movingRight){
dx+=1;
}
if(movingUp){
dy-=1;
}
if(movingDown){
dy+=1;
}
float scalar = sketch.isGodMode()? 7:3;
boolean skipX = false;
boolean skipY = false;
if(!sketch.isGodMode()){
float newX = centerOfScreen.getX()+scalar*dx+dx*10;
float newY = centerOfScreen.getY()+scalar*dy+dy*10;
Point newLocation = new Point(newX,newY);
Ray movement = new Ray(centerOfScreen, newLocation, true);
for(Wall blocking : allWalls){
Point intersection = movement.intersects(blocking);
if(intersection != null){
return;
}
}
// skip horizontal and vertical values so we don't get the bug ¯\_(ツ)_/¯. Those 20+ hours on ray casting isn't worth it
for(Wall wall : allWalls){
if(Math.abs(wall.getStart().getX() - newX) <= .1F){
skipX = true;
}
if(Math.abs(wall.getStart().getY() - newY) <= .1F){
skipY = true;
}
}
}
locationInMatrix = new Point(locationInMatrix.getX()+scalar*dx + (skipX ? .5F : 0), locationInMatrix.getY()+scalar*dy + (skipY ? .5F : 0));
int[] currentCell = Maze.getMatrixPoint(locationInMatrix);
int[] previousCell = maze.getPathKeeper();
if(Math.abs(currentCell[0]/2-previousCell[0])==1 ^ Math.abs(currentCell[1]/2-previousCell[1])==1){
maze.updateCell(currentCell[0]/2,currentCell[1]/2);
}
}
public Point getPos(){
return locationInMatrix;
}
public void setVelocity(int key, boolean toggle){
switch(key){
case 'W':
case 'w':
case KeyEvent.VK_UP:
movingUp = toggle;
break;
case 'S':
case 's':
case KeyEvent.VK_DOWN:
movingDown = toggle;
break;
case 'A':
case 'a':
case KeyEvent.VK_LEFT:
movingLeft = toggle;
break;
case 'D':
case 'd':
case KeyEvent.VK_RIGHT:
movingRight = toggle;
break;
}
}
@Override
public void render(){
// find offset for the maze, which is the distance to the center
float offsetX = centerOfScreen.getX() - locationInMatrix.getX();
float offsetY = centerOfScreen.getY() - locationInMatrix.getY();
maze.setOffsetX(offsetX);
maze.setOffsetY(offsetY);
maze.refresh(offsetX, offsetY, Maze.WALL_SCALE, Maze.WALL_SCALE);
this.verticies = maze.verticies(offsetX, offsetY);
this.allWalls = maze.getWalls();
this.allWalls = maze.getWalls();
if(!sketch.isGodMode()){
drawVision(getRays());
}
sketch.fill(255,0,0);
sketch.getInstance().noStroke();
sketch.ellipse(centerOfScreen.getX(), centerOfScreen.getY(), 20, 20);
if(didWin()){
sketch.endGame(EndScreen.EndType.WIN);
}else if(didLose()){
sketch.endGame(EndScreen.EndType.LOSE);
}
}
private List<Ray> getRays(){
List<Ray> rays = new ArrayList<>();
// Draw a ray to each endpoint if there is not a wall blocking the way
for(Point vertex : verticies){
Ray mainRay = new Ray(centerOfScreen, vertex, true);
if(!isMainRayBlocked(mainRay)){
mainRay.setAuxiliaryRay(createAuxiliaryRay(mainRay));
rays.add(mainRay);
}
}
// counter clockwise sorting
Collections.sort(rays);
return rays;
}
private void drawVision(List<Ray> rays){
sketch.stroke(255);
for(int i = 0; i < rays.size(); i++){
Main.getInstance().fill(255);
Ray current = rays.get(i);
Ray next = rays.get((i + 1) % rays.size());
boolean shareWall = false;
for(Wall wall : allWalls){
if(wall.isPointOnWall(current.getEnd()) && wall.isPointOnWall(next.getEnd())){
shareWall = true;
}
}
if(shareWall){
// if the ray is drawing to the same wall, then use the main lines to connect
Main.getInstance().triangle(centerOfScreen.getX(), centerOfScreen.getY(),
current.getEnd().getX(), current.getEnd().getY(),
next.getEnd().getX(), next.getEnd().getY());
}else{
Ray currentAuxiliary = current.getAuxiliaryRay();
Ray nextAuxiliary = next.getAuxiliaryRay();
// The rays are not drawn to the same point
// If a ray cannot be drawn to the midpoint between the two auxiliary rays, we must connect
// auxiliary to main
Point midpointAuxAux = Point.midpoint(currentAuxiliary.getEnd(), nextAuxiliary.getEnd());
boolean isMidpointBlocked = false;
Ray toMidPoint = new Ray(centerOfScreen, midpointAuxAux, true);
for(Wall blockCheck : allWalls){
Point intersection = toMidPoint.intersects(blockCheck);
// make sure the wall is not between the two line segment
if(intersection != null &&
!intersection.equals(midpointAuxAux)){
isMidpointBlocked = true;
break;
}
}
if(isMidpointBlocked){
// Cannot connect the aux
boolean canDrawMainAux = true;
// if the main cannot be drawn to the aux midpoint without intersecting a wall at another point other
// than the start of the main ray or the aux endpoint, it is not valid
Ray checkBlockingMainAux = new Ray(current.getEnd(), nextAuxiliary.getEnd(), true);
for(Wall block : allWalls){
Point intersection = checkBlockingMainAux.intersects(block);
if(intersection != null && !intersection.equals(current.getEnd()) && !intersection.equals(nextAuxiliary.getEnd())){
canDrawMainAux = false;
break;
}
}
if(canDrawMainAux){
Main.getInstance().triangle(centerOfScreen.getX(), centerOfScreen.getY(),
current.getEnd().getX(), current.getEnd().getY(),
nextAuxiliary.getEnd().getX(), nextAuxiliary.getEnd().getY());
}else{
// make sure the auxiliary to main can be drawn
boolean canDrawAuxMain = true;
Ray checkBlockingAuxMain = new Ray(currentAuxiliary.getEnd(), next.getEnd(), true);
for(Wall block : allWalls){
Point intersection = checkBlockingAuxMain.intersects(block);
if(intersection != null && !intersection.equals(currentAuxiliary.getEnd()) && !intersection.equals(next.getEnd())){
canDrawAuxMain = false;
break;
}
}
if(canDrawAuxMain){
Main.getInstance().triangle(centerOfScreen.getX(), centerOfScreen.getY(),
currentAuxiliary.getEnd().getX(), currentAuxiliary.getEnd().getY(),
next.getEnd().getX(), next.getEnd().getY());
}else{
Main.getInstance().triangle(centerOfScreen.getX(), centerOfScreen.getY(),
currentAuxiliary.getEnd().getX(), currentAuxiliary.getEnd().getY(),
nextAuxiliary.getEnd().getX(), nextAuxiliary.getEnd().getY());
}
}
}else{
//connecting auxiliary does work
Main.getInstance().triangle(centerOfScreen.getX(), centerOfScreen.getY(),
currentAuxiliary.getEnd().getX(), currentAuxiliary.getEnd().getY(),
nextAuxiliary.getEnd().getX(), nextAuxiliary.getEnd().getY());
}
}
}
}
/**
* Checks if the ray is blocked by a wall between the start of the ray and the end of the ray.
* @param ray Ray to check for collision with a wall other than the one it is aiming for
* @return True if the ray is blocked, false otherwise
*/
private boolean isMainRayBlocked(Ray ray){
for(Wall wall : allWalls){
// The ray will always touch the wall it is aiming to touch
Point intersect = ray.intersects(wall);
// Make sure there is an intersection and
// the intersection is not the point the ray ends at
if(intersect != null && !intersect.equals(ray.getEnd())){
return true;
}
}
return false;
}
private Ray createAuxiliaryRay(Ray mainRay){
// mainRay.getEnd() gives direction of the geometric ray
Ray auxiliary = new Ray(mainRay.getStart(), mainRay.getEnd(), false);
for(Wall collideWall : allWalls){
Point collisionPoint = auxiliary.intersects(collideWall);
// make sure there is an intersection and the collision point is not the directional point (since directional
// point is a collision point itself, but a trivial one)
if(collisionPoint == null || collisionPoint.equals(auxiliary.getEnd())){
continue;
}
// Make sure there is no wall blocking the auxiliary to its destination point
boolean isAuxBlocked = false;
for(Wall blockingWall : allWalls){
Point blockingPoint = auxiliary.intersects(blockingWall);
// make sure there is an intersection and
// the intersection is not the directional part of the aux ray (which is a trivial intersection) and
// the intersection is not the intersection from the wall we want to collide with and
// the intersection is not where the aux ray starts
if(blockingPoint == null || blockingPoint.equals(auxiliary.getEnd()) ||
blockingPoint.equals(collisionPoint) || blockingPoint.equals(auxiliary.getStart())){
continue;
}
// make sure the blocking wall is not blocked by the collision wall
Ray startToCollision = new Ray(auxiliary.getStart(), collisionPoint, true);
if(startToCollision.intersects(blockingWall) == null){
continue;
}
isAuxBlocked = true;
break;
}
if(!isAuxBlocked){
return new Ray(mainRay.getStart(), collisionPoint, true);
}
}
return mainRay;
}
public boolean canSeeCharacter(Character character1, Character character2){
Point thisLocation;
Point otherLocation;
if(character1 instanceof Monster){
thisLocation = ((Monster) character1).location();
}else{
thisLocation = character1.centerOfScreen;
}
if(character2 instanceof Monster){
otherLocation = ((Monster) character2).location();
}else{
otherLocation = character2.centerOfScreen;
}
Ray vision = new Ray(thisLocation, otherLocation, true);
sketch.line(thisLocation.getX(), thisLocation.getY(), otherLocation.getX(), otherLocation.getY());
for(Wall wall : allWalls){
Point intersection = vision.intersects(wall);
if(intersection != null){
return false;
}
}
return true;
}
public boolean didWin(){
int[] curPos = Maze.getMatrixPoint(locationInMatrix);
return curPos[1]/2==maze.getLength()-1 && curPos[0]/2==maze.getWidth()-1;
}
public boolean onTopOfPlayer(){
return true;
}
public boolean didLose(){
if(!sketch.isGodMode()){
int[] curPos = Maze.getMatrixPoint(locationInMatrix);
List<Monster> monsters = new ArrayList<Monster>();
for(int i = 1; i<sketch.getMovables().size(); i++){
if(sketch.getMovables().get(i).onTopOfPlayer()){
return true;
}
}
return false;
} else {
return false;
}
}
}