parent
a7d85de1d5
commit
796b2c6863
Binary file not shown.
@ -1,14 +1,11 @@
|
||||
package com.demo.tank.course7;
|
||||
|
||||
import com.demo.tank.enums.Group;
|
||||
import com.demo.tank.util.Audio;
|
||||
|
||||
public class DefaultFireStrategy implements FireStrategy {
|
||||
@Override
|
||||
public void fire(Tank tank) {
|
||||
int bx = tank.getX() + Tank.WIDTH/2 - Bullet.WIDTH/2;
|
||||
int by = tank.getY() + Tank.HEIGHT/2 - Bullet.HEIGHT/2;
|
||||
new Bullet(bx, by, tank.getDir(), tank.getGroup(),tank.getTankFrame());
|
||||
if (tank.getGroup() == Group.GOOD) new Thread(() -> new Audio("audio/tank_fire.wav").play()).start();
|
||||
// if (tank.getGroup() == Group.GOOD) new Thread(() -> new Audio("audio/tank_fire.wav").play()).start();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,8 @@
|
||||
package com.demo.tank.course7.abstractFactory;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public abstract class BaseBullet {
|
||||
public abstract void paint(Graphics g);
|
||||
public abstract void collideWith(BaseTank tank);
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.demo.tank.course7.abstractFactory;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public abstract class BaseExplode {
|
||||
public abstract void paint(Graphics g);
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.demo.tank.course7.abstractFactory;
|
||||
|
||||
import com.demo.tank.enums.Group;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public abstract class BaseTank {
|
||||
public Rectangle rect = new Rectangle();
|
||||
|
||||
public abstract void paint(Graphics g);
|
||||
public abstract void die();
|
||||
|
||||
public abstract Group getGroup();
|
||||
|
||||
public abstract int getX();
|
||||
|
||||
public abstract int getY();
|
||||
}
|
@ -0,0 +1,128 @@
|
||||
package com.demo.tank.course7.abstractFactory;
|
||||
|
||||
import com.demo.tank.course7.Bullet;
|
||||
import com.demo.tank.course7.Explode;
|
||||
import com.demo.tank.course7.Tank;
|
||||
import com.demo.tank.course7.TankFrameV7;
|
||||
import com.demo.tank.enums.Direction;
|
||||
import com.demo.tank.enums.Group;
|
||||
import com.demo.tank.util.ResourceManager;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class CircleBullet extends BaseBullet{
|
||||
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 TankFrameV7 tf;
|
||||
Rectangle rect = new Rectangle();
|
||||
|
||||
public CircleBullet(int x, int y, Direction direction, Group group, TankFrameV7 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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g){
|
||||
if(!live){
|
||||
tf.bullets.remove(this);
|
||||
}
|
||||
Color c = g.getColor();
|
||||
g.setColor(Color.YELLOW);
|
||||
g.fillOval(x, y, 10, 10);
|
||||
g.setColor(c);
|
||||
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 > TankFrameV7.GAME_WIDTH || y > TankFrameV7.GAME_HEIGHT){
|
||||
live = false;
|
||||
}
|
||||
rect.x = this.x;
|
||||
rect.y = this.y;
|
||||
}
|
||||
|
||||
@Override
|
||||
//检测是否跟坦克碰撞
|
||||
public void collideWith(BaseTank 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(tf.gameFactory.createExplode(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;
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package com.demo.tank.course7.abstractFactory;
|
||||
|
||||
import com.demo.tank.course7.TankFrameV7;
|
||||
import com.demo.tank.util.ResourceManager;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class CircleExplode extends BaseExplode{
|
||||
private int x, y;
|
||||
public static final int WIDTH = ResourceManager.explodes[0].getWidth();
|
||||
public static final int HEIGHT = ResourceManager.explodes[0].getHeight();
|
||||
private TankFrameV7 tf;
|
||||
|
||||
private int step = 0;
|
||||
|
||||
public CircleExplode(int x, int y, TankFrameV7 tf) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.tf = tf;
|
||||
// new Thread(() -> new Audio("audio/explode.wav").play()).start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g){
|
||||
Color c = g.getColor();
|
||||
g.setColor(Color.RED);
|
||||
g.fillOval(x, y, 5*step, 5*step);
|
||||
step++;
|
||||
if(step >= 10){
|
||||
//播放完爆炸效果图片, remove
|
||||
tf.explodes.remove(this);
|
||||
}
|
||||
g.setColor(c);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.demo.tank.course7.abstractFactory;
|
||||
|
||||
import com.demo.tank.course7.TankFrameV7;
|
||||
import com.demo.tank.enums.Direction;
|
||||
import com.demo.tank.enums.Group;
|
||||
|
||||
public class CircleFactory extends GameFactory{
|
||||
@Override
|
||||
public BaseTank createTank(int x, int y, Direction dir, Group group, TankFrameV7 tf) {
|
||||
return new CircleTank(x,y,dir,group,tf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseBullet createBullet(int x, int y, Direction dir, Group group, TankFrameV7 tf) {
|
||||
return new CircleBullet(x, y, dir, group, tf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseExplode createExplode(int x, int y, TankFrameV7 tf) {
|
||||
return new CircleExplode(x, y, tf);
|
||||
}
|
||||
}
|
@ -0,0 +1,193 @@
|
||||
package com.demo.tank.course7.abstractFactory;
|
||||
|
||||
import com.demo.tank.course7.Bullet;
|
||||
import com.demo.tank.course7.DefaultFireStrategy;
|
||||
import com.demo.tank.course7.FireStrategy;
|
||||
import com.demo.tank.course7.Tank;
|
||||
import com.demo.tank.course7.TankFrameV7;
|
||||
import com.demo.tank.enums.Direction;
|
||||
import com.demo.tank.enums.Group;
|
||||
import com.demo.tank.util.ResourceManager;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class CircleTank extends BaseTank{
|
||||
private int x,y;
|
||||
private Direction dir;
|
||||
private static final int SPEED = 8;
|
||||
private boolean moving = true;
|
||||
private boolean living = true;
|
||||
private Group group = Group.BAD;
|
||||
TankFrameV7 tankFrame = null;
|
||||
public static final int WIDTH = ResourceManager.tankD.getWidth();
|
||||
public static final int HEIGHT = ResourceManager.tankD.getHeight();
|
||||
private Random random = new Random();
|
||||
public Rectangle rect = new Rectangle();
|
||||
FireStrategy fireStrategy;
|
||||
|
||||
|
||||
public CircleTank(int x, int y, Direction dir, Group group, TankFrameV7 tankFrame) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.dir = dir;
|
||||
this.group = group;
|
||||
this.tankFrame = tankFrame;
|
||||
|
||||
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.course7.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) tankFrame.enemyTanks.remove(this);
|
||||
//根据方向绘制坦克
|
||||
Color c = g.getColor();
|
||||
g.setColor(this.group == Group.BAD ? Color.RED : Color.YELLOW);
|
||||
g.fillOval(x,y, 40, 40);
|
||||
g.setColor(c);
|
||||
move();
|
||||
}
|
||||
|
||||
public void move(){
|
||||
//如果没有移动 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;
|
||||
}
|
||||
|
||||
private void boundsCheck() {
|
||||
if(x < 0) x = 0;
|
||||
if(x > TankFrameV7.GAME_WIDTH - Tank.WIDTH) x = TankFrameV7.GAME_WIDTH - Tank.WIDTH;
|
||||
if(y < 30) y = 30; //算上菜单条
|
||||
if(y > TankFrameV7.GAME_HEIGHT - Tank.HEIGHT) y = TankFrameV7.GAME_HEIGHT - Tank.HEIGHT;
|
||||
}
|
||||
|
||||
private void randomDirection() {
|
||||
this.dir = Direction.values()[random.nextInt(4)];
|
||||
}
|
||||
|
||||
public void fire() {
|
||||
// fireStrategy.fire(this);
|
||||
int bx = x + Tank.WIDTH/2 - Bullet.WIDTH/2;
|
||||
int by = y + Tank.HEIGHT/2 - Bullet.HEIGHT/2;
|
||||
if(this.group == Group.BAD){
|
||||
this.tankFrame.gameFactory.createBullet(bx, by, this.dir, this.group,this.tankFrame);
|
||||
}else{
|
||||
for(Direction direction : Direction.values()){
|
||||
this.tankFrame.gameFactory.createBullet(bx, by, direction, this.group,this.tankFrame);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void die(){
|
||||
this.living = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
@Override
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Group getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
public void setGroup(Group group) {
|
||||
this.group = group;
|
||||
}
|
||||
|
||||
public TankFrameV7 getTankFrame() {
|
||||
return tankFrame;
|
||||
}
|
||||
|
||||
public void setTankFrame(TankFrameV7 tankFrame) {
|
||||
this.tankFrame = tankFrame;
|
||||
}
|
||||
|
||||
public Rectangle getRect() {
|
||||
return rect;
|
||||
}
|
||||
|
||||
public void setRect(Rectangle rect) {
|
||||
this.rect = rect;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.demo.tank.course7.abstractFactory;
|
||||
|
||||
import com.demo.tank.course7.Bullet;
|
||||
import com.demo.tank.course7.Explode;
|
||||
import com.demo.tank.course7.Tank;
|
||||
import com.demo.tank.course7.TankFrameV7;
|
||||
import com.demo.tank.enums.Direction;
|
||||
import com.demo.tank.enums.Group;
|
||||
|
||||
public class DefaultFactory extends GameFactory{
|
||||
@Override
|
||||
public BaseTank createTank(int x, int y, Direction dir, Group group, TankFrameV7 tf) {
|
||||
return new Tank(x,y,dir,group,tf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseBullet createBullet(int x, int y, Direction dir, Group group, TankFrameV7 tf) {
|
||||
return new Bullet(x, y, dir, group, tf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseExplode createExplode(int x, int y, TankFrameV7 tf) {
|
||||
return new Explode(x, y, tf);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.demo.tank.course7.abstractFactory;
|
||||
|
||||
import com.demo.tank.course7.TankFrameV7;
|
||||
import com.demo.tank.enums.Direction;
|
||||
import com.demo.tank.enums.Group;
|
||||
|
||||
public abstract class GameFactory {
|
||||
public abstract BaseTank createTank(int x, int y, Direction dir, Group group, TankFrameV7 tf);
|
||||
public abstract BaseBullet createBullet(int x, int y, Direction dir, Group group, TankFrameV7 tf);
|
||||
public abstract BaseExplode createExplode(int x, int y, TankFrameV7 tf);
|
||||
}
|
Loading…
Reference in new issue