|
|
|
@ -2,7 +2,7 @@ package com.example.tankbattle;
|
|
|
|
|
|
|
|
|
|
import com.example.tankbattle.abstractfactory.BaseTank;
|
|
|
|
|
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.Graphics;
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
|
|
public class Tank extends BaseTank {
|
|
|
|
@ -11,22 +11,18 @@ public class Tank extends BaseTank {
|
|
|
|
|
public static int WIDTH = ResourceMgr.goodTankU.getWidth();
|
|
|
|
|
public static int HEIGHT = ResourceMgr.goodTankU.getHeight();
|
|
|
|
|
|
|
|
|
|
Rectangle rect = new Rectangle();
|
|
|
|
|
|
|
|
|
|
private Random random = new Random();
|
|
|
|
|
|
|
|
|
|
public int x,y;
|
|
|
|
|
int x,y;
|
|
|
|
|
|
|
|
|
|
public Dir dir = Dir.DOWN;
|
|
|
|
|
Dir dir = Dir.DOWN;
|
|
|
|
|
|
|
|
|
|
private boolean moving = true;
|
|
|
|
|
|
|
|
|
|
public TankFrame tf = null;
|
|
|
|
|
TankFrame tf = null;
|
|
|
|
|
|
|
|
|
|
private boolean living = true;
|
|
|
|
|
|
|
|
|
|
public Group group = Group.BAD;
|
|
|
|
|
|
|
|
|
|
FireStrategy fs;
|
|
|
|
|
|
|
|
|
|
public Tank(int x, int y, Dir dir, Group group, TankFrame tf) {
|
|
|
|
@ -57,7 +53,16 @@ public class Tank extends BaseTank {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void fire(){
|
|
|
|
|
fs.fire(this);
|
|
|
|
|
// fs.fire(this);
|
|
|
|
|
int bX = this.x + Tank.WIDTH/2 - Bullet.WIDTH/2;
|
|
|
|
|
int bY = this.y + Tank.HEIGHT/2 - Bullet.HEIGHT/2;
|
|
|
|
|
|
|
|
|
|
Dir[] dirs = Dir.values();
|
|
|
|
|
for(Dir dir : dirs) {
|
|
|
|
|
tf.gf.createBullet(bX, bY, dir, group, tf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Dir getDir() {
|
|
|
|
@ -103,31 +108,24 @@ public class Tank extends BaseTank {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rect.x = this.x;
|
|
|
|
|
rect.y = this.y;
|
|
|
|
|
if (this.group == Group.BAD && random.nextInt(100) > 95) this.fire();
|
|
|
|
|
if (this.group == Group.BAD && random.nextInt(100) > 95)
|
|
|
|
|
this.fire();
|
|
|
|
|
|
|
|
|
|
if (this.group == Group.BAD && random.nextInt(100) > 95) {
|
|
|
|
|
if (this.group == Group.BAD && random.nextInt(100) > 95)
|
|
|
|
|
randomDir();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boundsCheck();
|
|
|
|
|
//update rect
|
|
|
|
|
rect.x = this.x;
|
|
|
|
|
rect.y = this.y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void boundsCheck() {
|
|
|
|
|
if (this.x < 0) {
|
|
|
|
|
this.x = 2;
|
|
|
|
|
}
|
|
|
|
|
if (this.y < 28) {
|
|
|
|
|
this.y = 28;
|
|
|
|
|
}
|
|
|
|
|
if (this.x < 2) x = 2;
|
|
|
|
|
if (this.y < 28) y = 28;
|
|
|
|
|
|
|
|
|
|
if (this.x > TankFrame.GAME_WIDTH - Tank.WIDTH) {
|
|
|
|
|
this.x = TankFrame.GAME_WIDTH - Tank.WIDTH;
|
|
|
|
|
}
|
|
|
|
|
if (this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT) {
|
|
|
|
|
this.y = TankFrame.GAME_HEIGHT - Tank.HEIGHT;
|
|
|
|
|
}
|
|
|
|
|
if (this.x > TankFrame.GAME_WIDTH - Tank.WIDTH-2) this.x = TankFrame.GAME_WIDTH - Tank.WIDTH - 2;
|
|
|
|
|
if (this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT-2) this.y = TankFrame.GAME_HEIGHT - Tank.HEIGHT-2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void randomDir() {
|
|
|
|
|