Commit 06746c8f authored by 9831111's avatar 9831111 🙂

Add verticalWallCollision method.

parent d875d70a
...@@ -32,10 +32,6 @@ public class GameState { ...@@ -32,10 +32,6 @@ public class GameState {
private boolean one = false; private boolean one = false;
private boolean two = false; private boolean two = false;
private boolean three = false; private boolean three = false;
private int step = 0;
boolean PermissionUp = true;
boolean PermissionDown = true;
boolean check = true;
public GameState(int num) { public GameState(int num) {
...@@ -84,64 +80,11 @@ public class GameState { ...@@ -84,64 +80,11 @@ public class GameState {
if (keyUP) { if (keyUP) {
PermissionUp = true; move(+5);
for (Wall wall : Controller.walls) {
if (wall.getWidth() == 5 && wall.getHeight() == 50) {
if ((getBounds(locX, locY).intersects(new Rectangle((int) wall.getX(), (int) wall.getY(), 5, 50)))) {
//&&
// ((rotateAmount == 0 && locX < wall.getX()) || ((rotateAmount == 180 || rotateAmount == -180) && locX > wall.getX()))
PermissionUp = false;
break;
}
}
if (wall.getWidth() == 50 && wall.getHeight() == 5) {
if ((getBounds(locX, locY).intersects(new Rectangle((int) wall.getX(), (int) wall.getY(), 50, 5)))) {
// &&
// (((rotateAmount == +90 || rotateAmount == -270) && locY < wall.getY()) || (((rotateAmount == -90 || rotateAmount == -270) && locY > wall.getY())))
PermissionUp = false;
break;
}
}
}
if (PermissionUp)
move(+5);
for (Wall wall : Controller.walls)
if (!PermissionDown && (!(getBounds(locX, locY).intersects(new Rectangle((int) wall.getX(), (int) wall.getY(), 50, 5))) ||
!(getBounds(locX, locY).intersects(new Rectangle((int) wall.getX(), (int) wall.getY(), 5, 50))))) {
move(+5);
break;
}
} }
if (keyDOWN) { if (keyDOWN) {
PermissionDown = true; move(-5);
for (Wall wall : Controller.walls) {
if (wall.getWidth() == 5 && wall.getHeight() == 50) {
if ((getBounds(locX, locY).intersects(new Rectangle((int) wall.getX(), (int) wall.getY(), 5, 50)))) {
//&&
// ((((rotateAmount == 0) || locX >= wall.getX())) || (((rotateAmount == -180 || rotateAmount == 180) && locX <= wall.getX())))
PermissionDown = false;
break;
}
}
if (wall.getWidth() == 50 && wall.getHeight() == 5) {
if ((getBounds(locX, locY).intersects(new Rectangle((int) wall.getX(), (int) wall.getY(), 50, 5)))) {
// &&
// (((rotateAmount == -90 || rotateAmount == 270) && locY<wall.getY()))|| (((rotateAmount==90||rotateAmount==-270)&& locY>wall.getY()))
PermissionDown = false;
break;
}
}
}
if (PermissionDown)
move(-5);
for (Wall wall : Controller.walls)
if (!PermissionUp && (!(getBounds(locX, locY).intersects(new Rectangle((int) wall.getX(), (int) wall.getY(), 50, 5))) ||
!(getBounds(locX, locY).intersects(new Rectangle((int) wall.getX(), (int) wall.getY(), 5, 50))))) {
move(-5);
break;
}
// }
} }
...@@ -158,9 +101,78 @@ public class GameState { ...@@ -158,9 +101,78 @@ public class GameState {
locY = Math.min(locY, 50 + ((Controller.row - 1) / 2) * 50 + (((Controller.row - 1) / 2) + 1) * 5 - 25); locY = Math.min(locY, 50 + ((Controller.row - 1) / 2) * 50 + (((Controller.row - 1) / 2) + 1) * 5 - 25);
} }
public Wall verticalWallCollision() {
for (Wall wall : Controller.walls) {
if (wall.getWidth() == 5 && wall.getHeight() == 50) {
if ((getBounds(locX, locY).intersects(new Rectangle((int) wall.getX(), (int) wall.getY(), 5, 50)))) {
return wall;
}
}
}
return null;
}
// public Wall horizontalWallCollision() {
// for (Wall wall : Controller.walls) {
// if (wall.getWidth() == 50 && wall.getHeight() == 5) {
// if ((getBounds(locX, locY).intersects(new Rectangle((int) wall.getX(), (int) wall.getY(), 50, 5)))) {
// return wall;
// }
// }
// }
// return null;
// }
public boolean isMoveAllowed(int px) {
if (verticalWallCollision() != null) {
if (verticalWallCollision().getX() > locX && px > 0) {
if ((rotateAmount <= 90 && rotateAmount >= -90) || (rotateAmount <= -270 && rotateAmount > -360) || (rotateAmount >= 270 && rotateAmount < 360)) {
System.out.println("1");
return false;
} else return true;
}
if (verticalWallCollision().getX() < locX && px < 0) {
if ((rotateAmount <= 90 && rotateAmount >= -90) || (rotateAmount <= -270 && rotateAmount > -360) || (rotateAmount >= 270 && rotateAmount < 360)) {
System.out.println("2");
return false;
} else return true;
}
if (verticalWallCollision().getX() > locX && px < 0) {
if ((rotateAmount >= 90 && rotateAmount <= 270) || (rotateAmount <= -90 && rotateAmount >= -270)) {
System.out.println("3");
return false;
} else return true;
}
if (verticalWallCollision().getX() < locX && px > 0) {
if ((rotateAmount >= 90 || rotateAmount <= 270) || (rotateAmount <= -90 && rotateAmount >= -270)) {
System.out.println("4");
return false;
} else return true;
}
}
// if (horizontalWallCollision() != null) {
// if (horizontalWallCollision().getY() > locY && px > 0 && ((rotateAmount <= 180 && rotateAmount >= 0) || (rotateAmount <= -180 && rotateAmount > -360)))
// return false;
// if (horizontalWallCollision().getY() < locY && px < 0 && ((rotateAmount <= 180 && rotateAmount >= 0) || (rotateAmount <= -180 && rotateAmount > -360)))
// return false;
// if (horizontalWallCollision().getY() < locY && px > 0 && ((rotateAmount >= -180 && rotateAmount <= 0) || (rotateAmount >= 180 && rotateAmount < 360)))
// return false;
// if (horizontalWallCollision().getY() > locY && px < 0 && ((rotateAmount >= -180 && rotateAmount <= 0) || (rotateAmount >= 180 && rotateAmount < 360)))
// return false;
//
// }
return true;
}
public void move(int px) { public void move(int px) {
double d; double d;
if (!isMoveAllowed(px)) px = 0;
if (rotateAmount == 0) locX += px; if (rotateAmount == 0) locX += px;
if (rotateAmount == 90 || rotateAmount == -270) locY += px; if (rotateAmount == 90 || rotateAmount == -270) locY += px;
if (rotateAmount == -90 || rotateAmount == 270) locY -= px; if (rotateAmount == -90 || rotateAmount == 270) locY -= px;
...@@ -214,15 +226,6 @@ public class GameState { ...@@ -214,15 +226,6 @@ public class GameState {
} }
public void checkWall() {
for (Wall wall : Controller.walls) {
if (wall.getX() == locX + 5 || wall.getY() == locY + 5) {
keyDOWN = false;
} else if (wall.getX() == locX - 5 || wall.getY() == locY - 5) {
keyUP = false;
}
}
}
public KeyListener getKeyListener() { public KeyListener getKeyListener() {
return keyHandler; return keyHandler;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment