You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
5.2 KiB

package com.demo.tank.course14;
import com.demo.tank.enums.Direction;
import com.demo.tank.enums.Group;
import com.demo.tank.util.ResourceManager;
import java.awt.*;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.Arrays;
import java.util.Random;
public class Tank extends GameObject {
int oldX, oldY;
private Direction dir;
private static final int SPEED = 8;
private boolean moving = true;
private boolean living = true;
private Group group = Group.BAD;
public static final int WIDTH = ResourceManager.tankD.getWidth();
public static final int HEIGHT = ResourceManager.tankD.getHeight();
private Random random = new Random();
Rectangle rect = new Rectangle();
FireStrategy fireStrategy;
public Tank(int x, int y, Direction dir, Group group) {
this.x = x;
this.y = y;
this.dir = dir;
this.group = group;
rect.x = this.x;
rect.y = this.y;
rect.width = Tank.WIDTH;
rect.height = Tank.HEIGHT;
if (this.group == Group.GOOD) {
// String className = PropertyManager.getString("good.tank.fire.strategy");
try {
fireStrategy = (FireStrategy) Class.forName("com.demo.tank.course14.FourDirectionFireStrategy").newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else if (this.group == Group.BAD) fireStrategy = new DefaultFireStrategy();
}
@Override
public void paint(Graphics g) {
if (!living) GameModel.getInstance().remove(this);
//根据方向绘制坦克
switch (dir) {
case UP:
g.drawImage(this.group == Group.GOOD ? ResourceManager.tankU : ResourceManager.badTankU, x, y, null);
break;
case DOWN:
g.drawImage(this.group == Group.GOOD ? ResourceManager.tankD : ResourceManager.badTankD, x, y, null);
break;
case LEFT:
g.drawImage(this.group == Group.GOOD ? ResourceManager.tankL : ResourceManager.badTankL, x, y, null);
break;
case RIGHT:
g.drawImage(this.group == Group.GOOD ? ResourceManager.tankR : ResourceManager.badTankR, x, y, null);
break;
}
move();
}
public void move() {
oldX = x;
oldY = y;
//如果没有移动 return
if (!moving) 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 == Group.BAD) {
if (random.nextInt(10) == 5) {
this.fire();
}
if (random.nextInt(100) > 95) {
this.randomDirection();
}
}
//边界检测
boundsCheck();
rect.x = this.x;
rect.y = this.y;
}
public void back() {
this.x = oldX;
this.y = oldY;
}
private void boundsCheck() {
if (x < 0) x = 0;
if (x > TankFrameV14.GAME_WIDTH - Tank.WIDTH) x = TankFrameV14.GAME_WIDTH - Tank.WIDTH;
if (y < 30) y = 30; //算上菜单条
if (y > TankFrameV14.GAME_HEIGHT - Tank.HEIGHT) y = TankFrameV14.GAME_HEIGHT - Tank.HEIGHT;
}
private void randomDirection() {
this.dir = Direction.values()[random.nextInt(4)];
}
public void fire() {
fireStrategy.fire(this);
}
public void handleFireKey(){
java.util.List<TankFireObserver> observers = Arrays.asList(new TankFireHandler());
TankEvent event = new TankEvent(Timestamp.from(Instant.now()), this);
for (TankFireObserver o : observers){
o.actionOnFire(event);
}
}
public void die() {
this.living = false;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Direction getDir() {
return dir;
}
public void setDir(Direction dir) {
this.dir = dir;
}
public boolean isMoving() {
return moving;
}
public void setMoving(boolean moving) {
this.moving = moving;
}
public boolean isLiving() {
return living;
}
public void setLiving(boolean living) {
this.living = living;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public Rectangle getRect() {
return rect;
}
public void setRect(Rectangle rect) {
this.rect = rect;
}
public int getWidth() {
return WIDTH;
}
public int getHeight() {
return HEIGHT;
}
}