diff --git a/resources/config.properties b/resources/config.properties index fb36f7e..97becb4 100644 --- a/resources/config.properties +++ b/resources/config.properties @@ -3,4 +3,8 @@ initTankCount=10 tankSpeed=5 bulletSpeed=10 gameWidth=1080 -gameHeight=720 \ No newline at end of file +gameHeight=720 + +#fireStrategy +goodFS=com.msb.FourFireStrategy +badFS=com.msb.DefaultFireStrategy \ No newline at end of file diff --git a/src/com/msb/Bullet.java b/src/com/msb/Bullet.java index d23e419..ac3e934 100644 --- a/src/com/msb/Bullet.java +++ b/src/com/msb/Bullet.java @@ -31,6 +31,8 @@ public class Bullet { rectangle.x = this.y; rectangle.width = WIDTH; rectangle.height = HEIGHT; + + tankFrame.bullets.add(this); } public void paint(Graphics g) { diff --git a/src/com/msb/DefaultFireStrategy.java b/src/com/msb/DefaultFireStrategy.java new file mode 100644 index 0000000..4b8d031 --- /dev/null +++ b/src/com/msb/DefaultFireStrategy.java @@ -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()); + } +} diff --git a/src/com/msb/FireStrategy.java b/src/com/msb/FireStrategy.java new file mode 100644 index 0000000..68f579e --- /dev/null +++ b/src/com/msb/FireStrategy.java @@ -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); +} diff --git a/src/com/msb/FourFireStrategy.java b/src/com/msb/FourFireStrategy.java new file mode 100644 index 0000000..5bc88ef --- /dev/null +++ b/src/com/msb/FourFireStrategy.java @@ -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()); + } + } +} diff --git a/src/com/msb/Tank.java b/src/com/msb/Tank.java index 4b5dfd7..c74de42 100644 --- a/src/com/msb/Tank.java +++ b/src/com/msb/Tank.java @@ -23,6 +23,7 @@ public class Tank { private Random random = new Random(); private GroupEnum group = GroupEnum.BAD; private Rectangle rectangle = new Rectangle(); + private FireStrategy fireStrategy; public Tank(int x, int y, DirEnum dir, GroupEnum group, TankFrame tankFrame) { this.x = x; @@ -35,6 +36,20 @@ public class Tank { rectangle.x = this.y; rectangle.width = WIDTH; 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) { @@ -96,9 +111,10 @@ public class Tank { public void fire() { //子弹在坦克每个方向上的中心点应该是不一样的. 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; - 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() { @@ -131,4 +147,7 @@ public class Tank { return rectangle; } + public TankFrame getTankFrame() { + return tankFrame; + } }