坦克大战(一期)-设计模式-坦克大战子弹发射用策略模式

DesignPatterns
bingor 2 years ago
parent ee1a314d2a
commit bbd1a0eb5d

@ -3,4 +3,8 @@ initTankCount=10
tankSpeed=5 tankSpeed=5
bulletSpeed=10 bulletSpeed=10
gameWidth=1080 gameWidth=1080
gameHeight=720 gameHeight=720
#fireStrategy
goodFS=com.msb.FourFireStrategy
badFS=com.msb.DefaultFireStrategy

@ -31,6 +31,8 @@ public class Bullet {
rectangle.x = this.y; rectangle.x = this.y;
rectangle.width = WIDTH; rectangle.width = WIDTH;
rectangle.height = HEIGHT; rectangle.height = HEIGHT;
tankFrame.bullets.add(this);
} }
public void paint(Graphics g) { public void paint(Graphics g) {

@ -0,0 +1,22 @@
package com.msb;/**
* @Author bingor
* @Date 2022/10/8 11:21
* @Description: com.msb
* @Version: 1.0
*/
/**
*@ClassName DefaultFireStrategy
*@Description TODO
*@Author bingor
*@Date 2022/10/8 11:21
*@Version 3.0
*/
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());
}
}

@ -0,0 +1,11 @@
package com.msb;
/**
* @Author bingor
* @Date 2022/10/8 11:21
* @Description: com.msb
* @Version: 1.0
*/
public interface FireStrategy {
public void fire(Tank tank);
}

@ -0,0 +1,24 @@
package com.msb;/**
* @Author bingor
* @Date 2022/10/8 11:21
* @Description: com.msb
* @Version: 1.0
*/
/**
*@ClassName DefaultFireStrategy
*@Description TODO
*@Author bingor
*@Date 2022/10/8 11:21
*@Version 3.0
*/
public class FourFireStrategy 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;
for (DirEnum value : DirEnum.values()) {
new Bullet(bX, bY, value, tank.getGroup(), tank.getTankFrame());
}
}
}

@ -23,6 +23,7 @@ public class Tank {
private Random random = new Random(); private Random random = new Random();
private GroupEnum group = GroupEnum.BAD; private GroupEnum group = GroupEnum.BAD;
private Rectangle rectangle = new Rectangle(); private Rectangle rectangle = new Rectangle();
private FireStrategy fireStrategy;
public Tank(int x, int y, DirEnum dir, GroupEnum group, TankFrame tankFrame) { public Tank(int x, int y, DirEnum dir, GroupEnum group, TankFrame tankFrame) {
this.x = x; this.x = x;
@ -35,6 +36,20 @@ public class Tank {
rectangle.x = this.y; rectangle.x = this.y;
rectangle.width = WIDTH; rectangle.width = WIDTH;
rectangle.height = HEIGHT; rectangle.height = HEIGHT;
try {
if(group == GroupEnum.GOOD) {
fireStrategy = (FireStrategy) Class.forName(PropertyMgr.getString("goodFS")).newInstance();
} else {
fireStrategy = (FireStrategy) Class.forName(PropertyMgr.getString("badFS")).newInstance();
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} }
public void paint(Graphics g) { public void paint(Graphics g) {
@ -96,9 +111,10 @@ public class Tank {
public void fire() { public void fire() {
//子弹在坦克每个方向上的中心点应该是不一样的. TODO //子弹在坦克每个方向上的中心点应该是不一样的. TODO
int bX = this.x + WIDTH/2 - Bullet.WIDTH/2; /*int bX = this.x + WIDTH/2 - Bullet.WIDTH/2;
int bY = this.y+HEIGHT/2-Bullet.HEIGHT/2; int bY = this.y+HEIGHT/2-Bullet.HEIGHT/2;
tankFrame.bullets.add(new Bullet(bX, bY, this.dir, this.group, tankFrame)); tankFrame.bullets.add(new Bullet(bX, bY, this.dir, this.group, tankFrame));*/ //换成策略模式
fireStrategy.fire(this);
} }
public void boundsCheck() { public void boundsCheck() {
@ -131,4 +147,7 @@ public class Tank {
return rectangle; return rectangle;
} }
public TankFrame getTankFrame() {
return tankFrame;
}
} }

Loading…
Cancel
Save