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.

133 lines
3.2 KiB

package com.demo.tank.course6;
import com.demo.tank.enums.Direction;
import com.demo.tank.enums.Group;
import com.demo.tank.util.ResourceManager;
import java.awt.*;
public class Bullet {
private int x, y;
private Direction direction;
private static final int SPEED = 10;
public static final int WIDTH = ResourceManager.bulletD.getWidth();
public static final int HEIGHT = ResourceManager.bulletD.getHeight();
private boolean live = true;
private Group group = Group.BAD;
private TankFrameV6 tf;
Rectangle rect = new Rectangle();
public Bullet(int x, int y, Direction direction, Group group, TankFrameV6 tf) {
this.x = x;
this.y = y;
this.direction = direction;
this.group = group;
this.tf = tf;
rect.x = this.x;
rect.y = this.y;
rect.width = Bullet.WIDTH;
rect.height = Bullet.HEIGHT;
tf.bullets.add(this);
}
public void paint(Graphics g){
if(!live){
tf.bullets.remove(this);
}
switch (direction){
case UP:
g.drawImage(ResourceManager.bulletU, x, y, null);
break;
case DOWN:
g.drawImage(ResourceManager.bulletD, x, y, null);
break;
case LEFT:
g.drawImage(ResourceManager.bulletL, x, y, null);
break;
case RIGHT:
g.drawImage(ResourceManager.bulletR, x, y, null);
break;
}
move();
}
private void move() {
switch (direction){
case UP: y -= SPEED;
break;
case DOWN: y += SPEED;
break;
case LEFT: x -= SPEED;
break;
case RIGHT: x += SPEED;
break;
default:
break;
}
if(x < 0 || y < 0 || x > TankFrameV6.GAME_WIDTH || y > TankFrameV6.GAME_HEIGHT){
live = false;
}
rect.x = this.x;
rect.y = this.y;
}
//检测是否跟坦克碰撞
public void collideWith(Tank tank){
//关闭队友伤害
if(this.group == tank.getGroup()) return;
if(rect.intersects(tank.rect)){
tank.die();
this.die();
//爆炸
int ex = tank.getX() + Tank.WIDTH/2 - Explode.WIDTH/2;
int ey = tank.getY() + Tank.HEIGHT/2 - Explode.HEIGHT/2;
tf.explodes.add(new Explode(ex, ey , tf));
}
}
private void die() {
this.live = 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 getDirection() {
return direction;
}
public void setDirection(Direction direction) {
this.direction = direction;
}
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
}