Commit 7ef46f02 authored by nargessalehi98's avatar nargessalehi98

Merge remote-tracking branch 'origin/master'

parents 51f1d0d5 f5e45af1
......@@ -339,21 +339,7 @@ public class GameFrame extends JFrame {
}
}
public boolean checkWallAdded(int currentX, int currentY) {
String str = currentX + " " + currentY;
for (Wall wall : walls) {
if (wall.toString().equals(str)) return false;
else return true;
}
return true;
}
public void getColor(int x, int y) throws AWTException {
Robot robot = new Robot();
System.out.println(robot.getPixelColor(x, y));
}
/**
* Draw prize randomly
......
......@@ -11,6 +11,7 @@ import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
/**
......@@ -33,9 +34,9 @@ public class GameState {
private boolean two = false;
private boolean three = false;
private int step = 0;
boolean PermissionUp = true;
boolean PermissionDown = true;
boolean check = true;
public double rotateAmountTank;
public double rotateAmountBullet;
private int bulletTime = 0;
public GameState(int num) {
......@@ -84,87 +85,114 @@ public class GameState {
if (keyUP) {
PermissionUp = true;
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) {
PermissionDown = true;
move(-5);
}
if (keyLEFT)
rotateAmount -= 15;
if (keyRIGHT)
rotateAmount += 15;
locX = Math.max(locX, 40);
locX = Math.min(locX, 20 + (((Controller.col - 1) / 2) * 50) + (((Controller.col - 1) / 2) + 1) * 5 - 25);
locY = Math.max(locY, 70);
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)))) {
//&&
// ((((rotateAmount == 0) || locX >= wall.getX())) || (((rotateAmount == -180 || rotateAmount == 180) && locX <= wall.getX())))
PermissionDown = false;
break;
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)))) {
// &&
// (((rotateAmount == -90 || rotateAmount == 270) && locY<wall.getY()))|| (((rotateAmount==90||rotateAmount==-270)&& locY>wall.getY()))
PermissionDown = false;
break;
return wall;
}
}
}
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;
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)) {
return false;
} else return true;
}
if (verticalWallCollision().getX() < locX && px < 0) {
if ((rotateAmount <= 90 && rotateAmount >= -90) || (rotateAmount <= -270 && rotateAmount > -360) || (rotateAmount >= 270 && rotateAmount < 360)) {
return false;
} else return true;
}
for(Prize prize:Controller.prizes){
if((getBounds(locX, locY).intersects(new Rectangle((int) prize.getX(), (int) prize.getY(), prize.getWidth(), prize.getHeight())))){
Controller.getPrize=true;
Controller.tanks.get(0).setPrize(prize.getName());
if (verticalWallCollision().getX() > locX && px < 0) {
if ((rotateAmount >= 90 && rotateAmount <= 270) || (rotateAmount <= -90 && rotateAmount >= -270)) {
return false;
} else return true;
}
if (verticalWallCollision().getX() < locX && px > 0) {
if ((rotateAmount >= 90 || rotateAmount <= 270) || (rotateAmount <= -90 && rotateAmount >= -270)) {
return false;
} else return true;
}
if (keyLEFT)
rotateAmount -= 15;
}
if (horizontalWallCollision() != null) {
if (horizontalWallCollision().getY() > locY && px > 0) {
if ((rotateAmount <= 180 && rotateAmount >= 0) || (rotateAmount <= -180 && rotateAmount > -360)) {
System.out.println("1");
return false;
} else return true;
}
if (horizontalWallCollision().getY() < locY && px < 0) {
if ((rotateAmount <= 180 && rotateAmount >= 0) || (rotateAmount <= -180 && rotateAmount > -360)) {
System.out.println("2");
if (keyRIGHT)
rotateAmount += 15;
return false;
} else return true;
}
if (horizontalWallCollision().getY() < locY && px > 0) {
if ((rotateAmount >= -180 && rotateAmount <= 0) || (rotateAmount >= 180 && rotateAmount < 360)) {
System.out.println("3");
locX = Math.max(locX, 40);
locX = Math.min(locX, 20 + (((Controller.col - 1) / 2) * 50) + (((Controller.col - 1) / 2) + 1) * 5 - 25);
return false;
} else return true;
}
if (horizontalWallCollision().getY() > locY && px < 0) {
if ((rotateAmount >= -180 && rotateAmount <= 0) || (rotateAmount >= 180 && rotateAmount < 360)) {
System.out.println("4");
locY = Math.max(locY, 70);
locY = Math.min(locY, 50 + ((Controller.row - 1) / 2) * 50 + (((Controller.row - 1) / 2) + 1) * 5 - 25);
return false;
} else return true;
}
}
return true;
}
public void move(int px) {
double d;
if (!isMoveAllowed(px)) px = 0;
if (rotateAmount == 0) locX += px;
if (rotateAmount == 90 || rotateAmount == -270) locY += px;
if (rotateAmount == -90 || rotateAmount == 270) locY -= px;
......@@ -217,16 +245,202 @@ public class GameState {
}
}
public int calculateBullet(String xy) {
int x = locX;
int y = locY;
if (rotateAmountTank == 0) {
if (xy.equals("x")) {
x += 20;
return x;
} else {
y += 10;
return y;
}
}
if (rotateAmountTank == 90 || rotateAmountTank == -270) {
if (xy.equals("x")) {
x += 9;
return x;
} else {
return y + 20;
}
}
if (rotateAmountTank == -90 || rotateAmountTank == 270) {
if (xy.equals("x")) {
x += 9;
return x;
} else {
return y - 20;
}
}
if (rotateAmountTank == 180 || rotateAmountTank == -180) {
if (xy.equals("x")) {
return x;
} else {
y += 10;
return y;
}
}
if ((rotateAmountTank > 0 && rotateAmountTank < 90) || (rotateAmountTank < -270 && rotateAmountTank > -360)) {
x += 10 * Math.sin(rotateAmountTank * Math.PI / 180);
y += 10 * Math.cos(rotateAmountTank * Math.PI / 180);
if (xy.equals("x")) return x;
else return y;
}
if ((rotateAmountTank > 180 && rotateAmountTank < 270) || (rotateAmountTank < -90 && rotateAmountTank > -180)) {
x -= 10 * Math.sin(rotateAmountTank * Math.PI / 180);
y -= 10 * Math.cos(rotateAmountTank * Math.PI / 180);
if (xy.equals("x")) return x;
else return y;
}
if ((rotateAmountTank > 90 && rotateAmountTank < 180) || (rotateAmountTank < -180 && rotateAmountTank > -270)) {
x += 25 * Math.sin(rotateAmountTank * Math.PI / 180);
y -= 20 * Math.cos(rotateAmountTank * Math.PI / 180);
if (xy.equals("x")) return x;
else return y;
}
if ((rotateAmountTank > 270 && rotateAmountTank < 360) || (rotateAmountTank < 0 && rotateAmountTank > -90)) {
x += 20 * Math.cos(rotateAmountTank * Math.PI / 180);
y -= 25 * Math.sin(rotateAmountTank * Math.PI / 180);
if (xy.equals("x")) return x;
else return y;
}
return 0;
}
public void shotBullet(int px, Bullet bullet) {
double d;
double r = rotateAmountBullet;
if (r == 0) bullet.x += px;
if (r == 90 || r == -270) bullet.y += px;
if (r == -90 || r == 270) bullet.y -= px;
if (r == 180 || r == -180) bullet.x -= px;
if ((r > 0 && r < 90) || (r < -270 && r > -360)) {
if (px > 0) {
bullet.x += px * Math.cos(r * Math.PI / 180);
bullet.y += px * Math.sin(r * Math.PI / 180);
} else {
bullet.x += px * Math.cos(r * Math.PI / 180);
bullet.y += px * Math.sin(r * Math.PI / 180);
}
}
if ((r > 90 && r < 180) || (r < -180 && r > -270)) {
if (px > 0) {
d = r - 90;
bullet.x -= px * Math.sin(d * Math.PI / 180);
bullet.y += px * Math.cos(d * Math.PI / 180);
} else {
d = r + 270;
bullet.x -= px * Math.sin(d * Math.PI / 180);
bullet.y += px * Math.cos(d * Math.PI / 180);
}
}
public void checkWall() {
if ((r > 180 && r < 270) || (r < -90 && r > -180)) {
if (px > 0) {
d = r - 180;
bullet.x -= px * Math.cos(d * Math.PI / 180);
bullet.y -= px * Math.sin(d * Math.PI / 180);
} else {
d = r + 180;
bullet.x -= px * Math.cos(d * Math.PI / 180);
bullet.y -= px * Math.sin(d * Math.PI / 180);
}
}
if ((r > 270 && r < 360) || (r < 0 && r > -90)) {
if (px > 0) {
d = r - 270;
bullet.x += px * Math.sin(d * Math.PI / 180);
bullet.y -= px * Math.cos(d * Math.PI / 180);
} else {
d = r + 90;
bullet.x += px * Math.sin(d * Math.PI / 180);
bullet.y -= px * Math.cos(d * Math.PI / 180);
}
}
}
public void bulletCollision(Bullet bullet) {
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;
Rectangle rectangle = new Rectangle(wall.getX(), wall.getY(), wall.getWidth(), wall.getHeight());
Ellipse2D ellipse2D = new Ellipse2D.Double(bullet.x, bullet.y, 7, 7);
if (ellipse2D.intersects(rectangle)) {
new AudioPlayer("sound effects/select.wav", 0);
if (wall.getWidth() == 5 && wall.getHeight() == 50) {
if ((rotateAmountBullet >= 0 && rotateAmountBullet <= 180) || (rotateAmountBullet <= -180 && rotateAmountBullet > -360)) {
rotateAmountBullet = -rotateAmountBullet - 180;
break;
} else {
rotateAmountBullet = 180 - rotateAmountBullet;
break;
}
} else if (wall.getWidth() == 50 && wall.getHeight() == 5) {
rotateAmountBullet = -rotateAmountBullet;
break;
} else if (wall.getWidth() == 5 && wall.getHeight() == 5) {
if ((rotateAmountBullet >= 0 && rotateAmountBullet <= 180) || (rotateAmountBullet <= -180 && rotateAmountBullet > -360)) {
rotateAmountBullet = 180 - rotateAmountBullet;
break;
} else {
rotateAmountBullet = -rotateAmountBullet;
break;
}
}
}
}
}
public void checkTankDestroyed(Tank tankToCheck, Bullet bullet) {
Rectangle rectangle = new Rectangle(tankToCheck.getX(), tankToCheck.getY(), 25, 25);
Ellipse2D ellipse2D = new Ellipse2D.Double(bullet.x, bullet.y, 7, 7);
if (ellipse2D.intersects(rectangle)) {
new AudioPlayer("sound effects/enemydestroyed.wav", 0);
Controller.tanks.remove(tankToCheck);
tankToCheck.alive = false;
bullet.alive = false;
rotateAmountTank = 0;
bulletTime = 61;
}
}
public void fire(Graphics2D g2d) {
if (keyM && Controller.tanks.size() > 0) {
if (rotateAmountBullet >= 360 || rotateAmountBullet <= -360) rotateAmountBullet = 0;
Bullet bullet = new Bullet(calculateBullet("x"), calculateBullet("y"), rotateAmountBullet, 30);
bullets.add(bullet);
bulletCollision(bullets.get(0));
shotBullet(8, bullets.get(0));
g2d.fillOval(bullets.get(0).x, bullets.get(0).y, 7, 7);
if (bullets.size() > 5 && Controller.tanks.size() > 0)
checkTankDestroyed(Controller.tanks.get(0), bullets.get(0));
if (bulletTime > 60) {
bullets.get(0).x = calculateBullet("x");
bullets.get(0).y = calculateBullet("y");
bullets.get(0).rotateAmountBullet = rotateAmountTank;
bullets.clear();
keyM = false;
bulletTime = 0;
}
bulletTime++;
}
}
public KeyListener getKeyListener() {
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