Commit 3c1f24f1 authored by nargessalehi98's avatar nargessalehi98

Add Computer Player logic.

parent 7362786b
/*** In The Name of Allah ***/ /*** In The Name of Allah ***/
import org.w3c.dom.css.RGBColor; import javax.imageio.ImageIO;
import java.awt.*; import java.awt.*;
import java.awt.event.KeyAdapter; import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
...@@ -12,7 +11,11 @@ import java.awt.event.MouseListener; ...@@ -12,7 +11,11 @@ import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener; import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform; import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D; import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
/** /**
* This class holds the state of game and all of its elements. * This class holds the state of game and all of its elements.
...@@ -23,38 +26,64 @@ import java.awt.image.BufferedImage; ...@@ -23,38 +26,64 @@ import java.awt.image.BufferedImage;
public class GameState { public class GameState {
public int locX, locY, diam; public int locX, locY, diam;
public boolean gameOver; public int pcX, pcY;
private boolean keyUP, keyDOWN, keyRIGHT, keyLEFT; public boolean gameOver, keyM;
public boolean keyUP, keyDOWN, keyRIGHT, keyLEFT;
private boolean mousePress; private boolean mousePress;
private int mouseX, mouseY; private int mouseX, mouseY;
private KeyHandler keyHandler; private KeyHandler keyHandler;
private MouseHandler mouseHandler; private MouseHandler mouseHandler;
public double rotateAmount; public double rotateAmountTank;
public double rotateAmountPC;
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; private boolean left;
public double rotateAmountTank; private boolean right;
private boolean up;
private boolean down;
boolean PermissionUp = true;
boolean PermissionDown = true;
public double rotateAmountBullet; public double rotateAmountBullet;
private int bulletTime = 0; private int bulletTime = 0;
private int tankTime = 0;
public ArrayList<Bullet> bullets;
public GameState(int num) { public GameState(int num) {
locX = 100; locX = 100;
locY = 100; locY = 100;
diam = 42; diam = 32;
rotateAmount = 0; pcX = 40;
pcY = 100;
rotateAmountTank = 0;
rotateAmountBullet = 0;
gameOver = false; gameOver = false;
// //
keyUP = false; keyUP = false;
keyDOWN = false; keyDOWN = false;
keyRIGHT = false; keyRIGHT = false;
keyLEFT = false; keyLEFT = false;
keyM = false;
//
up = true;
down = true;
left = true;
right = true;
// //
mousePress = false; mousePress = false;
mouseX = 0; mouseX = 0;
mouseY = 0; mouseY = 0;
// //
keyHandler = new KeyHandler();
mouseHandler = new MouseHandler();
if (num == 1) { if (num == 1) {
one = true; one = true;
} }
...@@ -64,48 +93,14 @@ public class GameState { ...@@ -64,48 +93,14 @@ public class GameState {
if (num == 3) { if (num == 3) {
three = true; three = true;
} }
keyHandler = new KeyHandler();
mouseHandler = new MouseHandler();
}
public Rectangle getBounds(int locX, int locY) { bullets = new ArrayList<>();
return new Rectangle(locX, locY, 25, 25);
} }
/** public Rectangle getBounds(int locX, int locY) {
* The method which updates the game state. return new Rectangle(locX, locY, 25, 25);
*/
public void update() throws AWTException {
if (rotateAmount >= 360 || rotateAmount <= -360) rotateAmount = 0;
if (mousePress) {
locY = mouseY - diam / 2;
locX = mouseX - diam / 2;
}
if (keyUP) {
move(+5);
}
if (keyDOWN) {
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() { public Wall verticalWallCollision() {
for (Wall wall : Controller.walls) { for (Wall wall : Controller.walls) {
if (wall.getWidth() == 5 && wall.getHeight() == 50) { if (wall.getWidth() == 5 && wall.getHeight() == 50) {
...@@ -132,24 +127,24 @@ public class GameState { ...@@ -132,24 +127,24 @@ public class GameState {
if (verticalWallCollision() != null) { if (verticalWallCollision() != null) {
if (verticalWallCollision().getX() > locX && px > 0) { if (verticalWallCollision().getX() > locX && px > 0) {
if ((rotateAmount <= 90 && rotateAmount >= -90) || (rotateAmount <= -270 && rotateAmount > -360) || (rotateAmount >= 270 && rotateAmount < 360)) { if ((rotateAmountTank <= 90 && rotateAmountTank >= -90) || (rotateAmountTank <= -270 && rotateAmountTank > -360) || (rotateAmountTank >= 270 && rotateAmountTank < 360)) {
return false; return false;
} else return true; } else return true;
} }
if (verticalWallCollision().getX() < locX && px < 0) { if (verticalWallCollision().getX() < locX && px < 0) {
if ((rotateAmount <= 90 && rotateAmount >= -90) || (rotateAmount <= -270 && rotateAmount > -360) || (rotateAmount >= 270 && rotateAmount < 360)) { if ((rotateAmountTank <= 90 && rotateAmountTank >= -90) || (rotateAmountTank <= -270 && rotateAmountTank > -360) || (rotateAmountTank >= 270 && rotateAmountTank < 360)) {
return false; return false;
} else return true; } else return true;
} }
if (verticalWallCollision().getX() > locX && px < 0) { if (verticalWallCollision().getX() > locX && px < 0) {
if ((rotateAmount >= 90 && rotateAmount <= 270) || (rotateAmount <= -90 && rotateAmount >= -270)) { if ((rotateAmountTank >= 90 && rotateAmountTank <= 270) || (rotateAmountTank <= -90 && rotateAmountTank >= -270)) {
return false; return false;
} else return true; } else return true;
} }
if (verticalWallCollision().getX() < locX && px > 0) { if (verticalWallCollision().getX() < locX && px > 0) {
if ((rotateAmount >= 90 || rotateAmount <= 270) || (rotateAmount <= -90 && rotateAmount >= -270)) { if ((rotateAmountTank >= 90 || rotateAmountTank <= 270) || (rotateAmountTank <= -90 && rotateAmountTank >= -270)) {
return false; return false;
} else return true; } else return true;
} }
...@@ -157,29 +152,22 @@ public class GameState { ...@@ -157,29 +152,22 @@ public class GameState {
} }
if (horizontalWallCollision() != null) { if (horizontalWallCollision() != null) {
if (horizontalWallCollision().getY() > locY && px > 0) { if (horizontalWallCollision().getY() > locY && px > 0) {
if ((rotateAmount <= 180 && rotateAmount >= 0) || (rotateAmount <= -180 && rotateAmount > -360)) { if ((rotateAmountTank <= 180 && rotateAmountTank >= 0) || (rotateAmountTank <= -180 && rotateAmountTank > -360)) {
System.out.println("1");
return false; return false;
} else return true; } else return true;
} }
if (horizontalWallCollision().getY() < locY && px < 0) { if (horizontalWallCollision().getY() < locY && px < 0) {
if ((rotateAmount <= 180 && rotateAmount >= 0) || (rotateAmount <= -180 && rotateAmount > -360)) { if ((rotateAmountTank <= 180 && rotateAmountTank >= 0) || (rotateAmountTank <= -180 && rotateAmountTank > -360)) {
System.out.println("2");
return false; return false;
} else return true; } else return true;
} }
if (horizontalWallCollision().getY() < locY && px > 0) { if (horizontalWallCollision().getY() < locY && px > 0) {
if ((rotateAmount >= -180 && rotateAmount <= 0) || (rotateAmount >= 180 && rotateAmount < 360)) { if ((rotateAmountTank >= -180 && rotateAmountTank <= 0) || (rotateAmountTank >= 180 && rotateAmountTank < 360)) {
System.out.println("3");
return false; return false;
} else return true; } else return true;
} }
if (horizontalWallCollision().getY() > locY && px < 0) { if (horizontalWallCollision().getY() > locY && px < 0) {
if ((rotateAmount >= -180 && rotateAmount <= 0) || (rotateAmount >= 180 && rotateAmount < 360)) { if ((rotateAmountTank >= -180 && rotateAmountTank <= 0) || (rotateAmountTank >= 180 && rotateAmountTank < 360)) {
System.out.println("4");
return false; return false;
} else return true; } else return true;
} }
...@@ -188,63 +176,220 @@ public class GameState { ...@@ -188,63 +176,220 @@ public class GameState {
return true; return true;
} }
public void move(int px) { public void pcUpdate() {
double d;
if (!isMoveAllowed(px)) px = 0; for (Wall wall : Controller.walls) {
if (rotateAmount == 0) locX += px; if (wall.getWidth() == 5 && wall.getHeight() == 50) {
if (rotateAmount == 90 || rotateAmount == -270) locY += px; if (getBounds(pcX, pcY).intersects(new Rectangle((int) wall.getX(), (int) wall.getY(), (int) wall.getWidth(), (int) wall.getHeight())) && wall.getX() < pcX) {
if (rotateAmount == -90 || rotateAmount == 270) locY -= px; left = false;
if (rotateAmount == 180 || rotateAmount == -180) locX -= px; System.out.println("3");
break;
}
if (getBounds(pcX, pcY).intersects(new Rectangle((int) wall.getX(), (int) wall.getY(), (int) wall.getWidth(), (int) wall.getHeight())) && wall.getX() > pcX) {
right = false;
System.out.println("4");
break;
}
if ((rotateAmount > 0 && rotateAmount < 90) || (rotateAmount < -270 && rotateAmount > -360)) {
if (px > 0) {
locX += px * Math.cos(rotateAmount * Math.PI / 180);
locY += px * Math.sin(rotateAmount * Math.PI / 180);
} else {
locX += px * Math.cos(rotateAmount * Math.PI / 180);
locY += px * Math.sin(rotateAmount * Math.PI / 180);
} }
if (wall.getWidth() == 50 && wall.getHeight() == 5) {
if (getBounds(pcX, pcY).intersects(new Rectangle((int) wall.getX(), (int) wall.getY(), (int) wall.getWidth(), (int) wall.getHeight())) && wall.getY() > pcY) {
down = false;
System.out.println("2");
break;
}
if (getBounds(pcX, pcY).intersects(new Rectangle((int) wall.getX(), (int) wall.getY(), (int) wall.getWidth(), (int) wall.getHeight())) && wall.getY() < pcY) {
System.out.println("1");
up = false;
break;
} }
if ((rotateAmount > 90 && rotateAmount < 180) || (rotateAmount < -180 && rotateAmount > -270)) {
if (px > 0) { }
d = rotateAmount - 90; if (!up && !down && !left && !right) {
locX -= px * Math.sin(d * Math.PI / 180); if (getBounds(pcX, pcY).intersects(new Rectangle((int) wall.getX(), (int) wall.getY(), (int) wall.getWidth(), (int) wall.getHeight())) && wall.getX() < pcX) {
locY += px * Math.cos(d * Math.PI / 180); left = false;
System.out.println("33");
} else { } else {
d = rotateAmount + 270; left = true;
locX -= px * Math.sin(d * Math.PI / 180);
locY += px * Math.cos(d * Math.PI / 180); }
if (getBounds(pcX, pcY).intersects(new Rectangle((int) wall.getX(), (int) wall.getY(), (int) wall.getWidth(), (int) wall.getHeight())) && wall.getX() > pcX) {
right = false;
System.out.println("44");
} else
right = true;
if (wall.getWidth() == 50 && wall.getHeight() == 5) {
if (getBounds(pcX, pcY).intersects(new Rectangle((int) wall.getX(), (int) wall.getY(), (int) wall.getWidth(), (int) wall.getHeight())) && wall.getY() > pcY) {
down = false;
System.out.println("22");
break;
} else
down = true;
if (getBounds(pcX, pcY).intersects(new Rectangle((int) wall.getX(), (int) wall.getY(), (int) wall.getWidth(), (int) wall.getHeight())) && wall.getY() < pcY) {
System.out.println("11");
up = false;
break;
} else
up = true;
} }
} }
if ((rotateAmount > 180 && rotateAmount < 270) || (rotateAmount < -90 && rotateAmount > -180)) {
if (px > 0) {
d = rotateAmount - 180;
locX -= px * Math.cos(d * Math.PI / 180);
locY -= px * Math.sin(d * Math.PI / 180);
} else {
d = rotateAmount + 180;
locX -= px * Math.cos(d * Math.PI / 180);
locY -= px * Math.sin(d * Math.PI / 180);
} }
if (up) {
pcY -= 2;
rotateAmountPC = 270;
} else if (down) {
pcY += 2;
rotateAmountPC = 90;
} else if (right) {
pcX -= 2;
rotateAmountPC = 180;
} else if (left) {
pcX += 2;
rotateAmountPC = 0;
} }
if ((rotateAmount > 270 && rotateAmount < 360) || (rotateAmount < 0 && rotateAmount > -90)) {
if (px > 0) { // pcX = Math.max(pcX, 40);
d = rotateAmount - 270; // pcX = Math.min(pcX, 20 + (((Controller.col - 1) / 2) * 50) + (((Controller.col - 1) / 2) + 1) * 5 - 25);
locX += px * Math.sin(d * Math.PI / 180); //
locY -= px * Math.cos(d * Math.PI / 180); // pcY = Math.max(pcY, 70);
} else { // pcY = Math.min(pcY, 50 + ((Controller.row - 1) / 2) * 50 + (((Controller.row - 1) / 2) + 1) * 5 - 25);
d = rotateAmount + 90;
locX += px * Math.sin(d * Math.PI / 180);
locY -= px * Math.cos(d * Math.PI / 180);
} }
/**
* The method which updates the game state.
*/
public void update() {
if (rotateAmountTank >= 360 || rotateAmountTank <= -360) rotateAmountTank = 0;
if (mousePress) {
locY = mouseY - diam / 2;
locX = mouseX - diam / 2;
} }
if (keyUP) {
calculateBullet("x");
calculateBullet("y");
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) {
calculateBullet("x");
calculateBullet("y");
PermissionDown = 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())))
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;
}
}
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 (keyLEFT)
rotateAmountTank -= 15;
if (keyRIGHT)
rotateAmountTank += 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 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 int calculateBullet(String xy) { public int calculateBullet(String xy) {
int x = locX; int x = locX;
int y = locY; int y = locY;
...@@ -309,64 +454,62 @@ public class GameState { ...@@ -309,64 +454,62 @@ public class GameState {
} }
return 0; return 0;
} }
public void shotBullet(int px, Bullet bullet) {
public void move(int px) {
double d; double d;
double r = rotateAmountBullet; if (!isMoveAllowed(px)) px = 0;
if (r == 0) bullet.x += px; if (rotateAmountTank == 0) locX += px;
if (r == 90 || r == -270) bullet.y += px; if (rotateAmountTank == 90 || rotateAmountTank == -270) locY += px;
if (r == -90 || r == 270) bullet.y -= px; if (rotateAmountTank == -90 || rotateAmountTank == 270) locY -= px;
if (r == 180 || r == -180) bullet.x -= px; if (rotateAmountTank == 180 || rotateAmountTank == -180) locX -= px;
if ((r > 0 && r < 90) || (r < -270 && r > -360)) { if ((rotateAmountTank > 0 && rotateAmountTank < 90) || (rotateAmountTank < -270 && rotateAmountTank > -360)) {
if (px > 0) { if (px > 0) {
bullet.x += px * Math.cos(r * Math.PI / 180); locX += px * Math.cos(rotateAmountTank * Math.PI / 180);
bullet.y += px * Math.sin(r * Math.PI / 180); locY += px * Math.sin(rotateAmountTank * Math.PI / 180);
} else { } else {
bullet.x += px * Math.cos(r * Math.PI / 180); locX += px * Math.cos(rotateAmountTank * Math.PI / 180);
bullet.y += px * Math.sin(r * Math.PI / 180); locY += px * Math.sin(rotateAmountTank * Math.PI / 180);
} }
} }
if ((rotateAmountTank > 90 && rotateAmountTank < 180) || (rotateAmountTank < -180 && rotateAmountTank > -270)) {
if ((r > 90 && r < 180) || (r < -180 && r > -270)) {
if (px > 0) { if (px > 0) {
d = r - 90; d = rotateAmountTank - 90;
bullet.x -= px * Math.sin(d * Math.PI / 180); locX -= px * Math.sin(d * Math.PI / 180);
bullet.y += px * Math.cos(d * Math.PI / 180); locY += px * Math.cos(d * Math.PI / 180);
} else { } else {
d = r + 270; d = rotateAmountTank + 270;
bullet.x -= px * Math.sin(d * Math.PI / 180); locX -= px * Math.sin(d * Math.PI / 180);
bullet.y += px * Math.cos(d * Math.PI / 180); locY += px * Math.cos(d * Math.PI / 180);
} }
} }
if ((rotateAmountTank > 180 && rotateAmountTank < 270) || (rotateAmountTank < -90 && rotateAmountTank > -180)) {
if ((r > 180 && r < 270) || (r < -90 && r > -180)) {
if (px > 0) { if (px > 0) {
d = r - 180; d = rotateAmountTank - 180;
bullet.x -= px * Math.cos(d * Math.PI / 180); locX -= px * Math.cos(d * Math.PI / 180);
bullet.y -= px * Math.sin(d * Math.PI / 180); locY -= px * Math.sin(d * Math.PI / 180);
} else { } else {
d = r + 180; d = rotateAmountTank + 180;
bullet.x -= px * Math.cos(d * Math.PI / 180); locX -= px * Math.cos(d * Math.PI / 180);
bullet.y -= px * Math.sin(d * Math.PI / 180); locY -= px * Math.sin(d * Math.PI / 180);
} }
} }
if ((rotateAmountTank > 270 && rotateAmountTank < 360) || (rotateAmountTank < 0 && rotateAmountTank > -90)) {
if ((r > 270 && r < 360) || (r < 0 && r > -90)) {
if (px > 0) { if (px > 0) {
d = r - 270; d = rotateAmountTank - 270;
bullet.x += px * Math.sin(d * Math.PI / 180); locX += px * Math.sin(d * Math.PI / 180);
bullet.y -= px * Math.cos(d * Math.PI / 180); locY -= px * Math.cos(d * Math.PI / 180);
} else { } else {
d = r + 90; d = rotateAmountTank + 90;
bullet.x += px * Math.sin(d * Math.PI / 180); locX += px * Math.sin(d * Math.PI / 180);
bullet.y -= px * Math.cos(d * Math.PI / 180); locY -= px * Math.cos(d * Math.PI / 180);
} }
} }
} }
public void bulletCollision(Bullet bullet) { public void bulletCollision(Bullet bullet) {
for (Wall wall : Controller.walls) { for (Wall wall : Controller.walls) {
Rectangle rectangle = new Rectangle(wall.getX(), wall.getY(), wall.getWidth(), wall.getHeight()); Rectangle rectangle = new Rectangle(wall.getX(), wall.getY(), wall.getWidth(), wall.getHeight());
...@@ -396,6 +539,7 @@ public class GameState { ...@@ -396,6 +539,7 @@ public class GameState {
} }
} }
} }
public void checkTankDestroyed(Tank tankToCheck, Bullet bullet) { public void checkTankDestroyed(Tank tankToCheck, Bullet bullet) {
Rectangle rectangle = new Rectangle(tankToCheck.getX(), tankToCheck.getY(), 25, 25); Rectangle rectangle = new Rectangle(tankToCheck.getX(), tankToCheck.getY(), 25, 25);
Ellipse2D ellipse2D = new Ellipse2D.Double(bullet.x, bullet.y, 7, 7); Ellipse2D ellipse2D = new Ellipse2D.Double(bullet.x, bullet.y, 7, 7);
...@@ -411,35 +555,64 @@ public class GameState { ...@@ -411,35 +555,64 @@ public class GameState {
} }
} }
public void fire(Graphics2D g2d) { public void shotBullet(int px, Bullet bullet) {
if (keyM && Controller.tanks.size() > 0) {
if (rotateAmountBullet >= 360 || rotateAmountBullet <= -360) rotateAmountBullet = 0;
Bullet bullet = new Bullet(calculateBullet("x"), calculateBullet("y"), rotateAmountBullet, 30); double d;
bullets.add(bullet); double r = rotateAmountBullet;
bulletCollision(bullets.get(0));
shotBullet(8, bullets.get(0)); if (r == 0) bullet.x += px;
g2d.fillOval(bullets.get(0).x, bullets.get(0).y, 7, 7); 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 (bullets.size() > 5 && Controller.tanks.size() > 0) if ((r > 0 && r < 90) || (r < -270 && r > -360)) {
checkTankDestroyed(Controller.tanks.get(0), bullets.get(0)); 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 (bulletTime > 60) { 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);
}
}
bullets.get(0).x = calculateBullet("x"); if ((r > 180 && r < 270) || (r < -90 && r > -180)) {
bullets.get(0).y = calculateBullet("y");
bullets.get(0).rotateAmountBullet = rotateAmountTank;
bullets.clear();
keyM = false;
bulletTime = 0;
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);
} }
bulletTime++; }
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 KeyListener getKeyListener() { public KeyListener getKeyListener() {
...@@ -479,6 +652,11 @@ public class GameState { ...@@ -479,6 +652,11 @@ public class GameState {
case KeyEvent.VK_ESCAPE: case KeyEvent.VK_ESCAPE:
gameOver = true; gameOver = true;
break; break;
case KeyEvent.VK_M:
rotateAmountBullet = rotateAmountTank;
new AudioPlayer("sound effects/cannon.wav", 0);
keyM = true;
break;
} }
} }
if (two) { if (two) {
...@@ -519,6 +697,8 @@ public class GameState { ...@@ -519,6 +697,8 @@ public class GameState {
break; break;
} }
} }
} }
@Override @Override
...@@ -537,6 +717,8 @@ public class GameState { ...@@ -537,6 +717,8 @@ public class GameState {
case KeyEvent.VK_RIGHT: case KeyEvent.VK_RIGHT:
keyRIGHT = false; keyRIGHT = false;
break; break;
case KeyEvent.VK_M:
break;
} }
} }
if (two) { if (two) {
...@@ -572,7 +754,6 @@ public class GameState { ...@@ -572,7 +754,6 @@ public class GameState {
} }
} }
} }
} }
/** /**
......
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