tank/src/main/java/com/msb/Tank.java

135 lines
3.8 KiB

package com.msb;
import java.awt.*;
import java.util.Random;
/**
* @Author bingor
* @Date 2022-09-29 21:53
* @Description: com.msb
* @Version: 1.0
*/
public class Tank {
private int x,y;
private DirEnum dir;
private static final int SPEED = 4;
private boolean move = true;
//为了解决能够在坦克中发射子弹,将创建的子弹通过坦克发射出来,那么需要在坦克类中持有游戏窗口的引用
private TankFrame tankFrame;
public static final int WIDTH = ResourcesMgr.goodTankU.getWidth();
public static final int HEIGHT = ResourcesMgr.goodTankU.getHeight();
private boolean live = true;
private Random random = new Random();
private GroupEnum group = GroupEnum.BAD;
private Rectangle rectangle = new Rectangle();
public Tank(int x, int y, DirEnum dir, GroupEnum group, TankFrame tankFrame) {
this.x = x;
this.y = y;
this.dir = dir;
this.group = group;
this.tankFrame = tankFrame;
rectangle.x = this.x;
rectangle.x = this.y;
rectangle.width = WIDTH;
rectangle.height = HEIGHT;
}
public void paint(Graphics g) {
if( ! live) {
tankFrame.tanks.remove(this);
return;
}
switch (dir) {
case UP: g.drawImage(this.group==GroupEnum.GOOD ? ResourcesMgr.goodTankU : ResourcesMgr.badTankU, x, y, null); break;
case DOWN: g.drawImage(this.group==GroupEnum.GOOD ? ResourcesMgr.goodTankD : ResourcesMgr.badTankD, x, y, null); break;
case LEFT: g.drawImage(this.group==GroupEnum.GOOD ? ResourcesMgr.goodTankL : ResourcesMgr.badTankL, x, y, null); break;
case RIGHT: g.drawImage(this.group==GroupEnum.GOOD ? ResourcesMgr.goodTankR : ResourcesMgr.badTankR, x, y, null); break;
default: break;
}
moving();
}
public void setDir(DirEnum dir) {
this.dir = dir;
}
public DirEnum getDir() {
return dir;
}
public void moving() {
if( ! move) return;
switch (dir) {
case UP: y -= SPEED; break;
case DOWN: y += SPEED; break;
case LEFT: x -= SPEED; break;
case RIGHT: x += SPEED; break;
default: break;
}
//敌方坦克随机发射炮弹
if(this.group == GroupEnum.BAD && random.nextInt(100) > 95) this.fire();
//敌方坦克随机改变方向进行
if(this.group == GroupEnum.BAD && random.nextInt(100) > 95) this.randomDir();
this.boundsCheck();
rectangle.x = this.x;
rectangle.y = this.y;
}
public void randomDir() {
this.dir = DirEnum.values()[random.nextInt(4)];
}
public void setMove(boolean move) {
this.move = move;
}
public void fire() {
//子弹在坦克每个方向上的中心点应该是不一样的. TODO
int bX = this.x + WIDTH/2 - Bullet.WIDTH/2;
int bY = this.y+HEIGHT/2-Bullet.HEIGHT/2;
tankFrame.bullets.add(new Bullet(bX, bY, this.dir, this.group, tankFrame));
}
public void boundsCheck() {
if(this.x < 2) this.x = 2;
if(this.x > TankFrame.GAME_WIDTH-Tank.WIDTH-2) this.x = TankFrame.GAME_WIDTH-Tank.WIDTH-2;
if(this.y < 28) this.y = 28; //因为窗口顶上有菜单栏的高度
if(this.y > TankFrame.GAME_HEIGHT-Tank.HEIGHT-2) this.y = TankFrame.GAME_HEIGHT-Tank.HEIGHT-2;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void die() {
this.live = false;
int eX = this.x + WIDTH/2 - Explode.WIDTH/2;
int eY = this.y + HEIGHT/2 - Explode.HEIGHT/2;
tankFrame.explodes.add(new Explode(eX, eY, tankFrame));
}
public GroupEnum getGroup() {
return group;
}
public Rectangle getRectangle() {
return rectangle;
}
}