diff --git a/src/main/java/com/msb/TankDemo.java b/src/main/java/com/msb/TankDemo.java index 4e41ec8..257b79e 100644 --- a/src/main/java/com/msb/TankDemo.java +++ b/src/main/java/com/msb/TankDemo.java @@ -10,7 +10,6 @@ public class TankDemo { public static void main(String[] args) throws InterruptedException { TankFrame tankFrame = new TankFrame(); - //现实当中,虽然可以过按键来改变方块的坐标并且重新刷新画板来实现移动,但是敌方的坦克应该是自动在跑 while (true) { Thread.sleep(50); diff --git a/src/main/java/com/msb/TankFrame.java b/src/main/java/com/msb/TankFrame.java index 288ccd8..34227b5 100644 --- a/src/main/java/com/msb/TankFrame.java +++ b/src/main/java/com/msb/TankFrame.java @@ -19,8 +19,8 @@ import java.util.Objects; public class TankFrame extends Frame { GameModel gameModel = new GameModel(); - public static final int GAME_WIDTH = 1000; - public static final int GAME_HEIGHT = 800; + public static final int GAME_WIDTH = 1400; + public static final int GAME_HEIGHT = 1050; public TankFrame() { diff --git a/src/main/java/com/msb/factorys/DefaultFactory.java b/src/main/java/com/msb/factorys/DefaultFactory.java index 97b798c..853e3fe 100644 --- a/src/main/java/com/msb/factorys/DefaultFactory.java +++ b/src/main/java/com/msb/factorys/DefaultFactory.java @@ -32,6 +32,7 @@ public class DefaultFactory extends GameFactory { @Override public BaseExplode createExplode(int x, int y, GameModel gameModel) { - return new Explode(x, y, gameModel); +// return new Explode(x, y, gameModel); + return null; } } diff --git a/src/main/java/com/msb/inter/Collider.java b/src/main/java/com/msb/inter/Collider.java new file mode 100644 index 0000000..1929f64 --- /dev/null +++ b/src/main/java/com/msb/inter/Collider.java @@ -0,0 +1,13 @@ +package com.msb.inter; + +import com.msb.model.abstracts.GameObject; + +/** + * @Author bingor + * @Date 2022/10/11 9:39 + * @Description: com.msb.inter + * @Version: 1.0 + */ +public interface Collider { + public void collide(GameObject o1, GameObject o2); +} diff --git a/src/main/java/com/msb/inter/impl/BulletTankCollider.java b/src/main/java/com/msb/inter/impl/BulletTankCollider.java new file mode 100644 index 0000000..6b1d087 --- /dev/null +++ b/src/main/java/com/msb/inter/impl/BulletTankCollider.java @@ -0,0 +1,38 @@ +package com.msb.inter.impl;/** + * @Author bingor + * @Date 2022/10/11 9:41 + * @Description: com.msb.inter.impl + * @Version: 1.0 + */ + +import com.msb.inter.Collider; +import com.msb.model.Bullet; +import com.msb.model.Tank; +import com.msb.model.abstracts.GameObject; + +/** + *@ClassName BulletTankCollider + *@Description TODO + *@Author bingor + *@Date 2022/10/11 9:41 + *@Version 3.0 + */ +public class BulletTankCollider implements Collider { + @Override + public void collide(GameObject o1, GameObject o2) { + if(o1 instanceof Bullet && o2 instanceof Tank) { + Bullet bullet = (Bullet) o1; + Tank tank = (Tank) o2; + if(bullet.getGroup() == tank.getGroup()) return; + + if(bullet.getRectangle().intersects(tank.getRectangle())) { + bullet.die(); + tank.die(); + } + } else if (o1 instanceof Tank && o2 instanceof Bullet) { + collide(o2, o1); + } else { + return; + } + } +} diff --git a/src/main/java/com/msb/inter/impl/TankTankCollider.java b/src/main/java/com/msb/inter/impl/TankTankCollider.java new file mode 100644 index 0000000..5479399 --- /dev/null +++ b/src/main/java/com/msb/inter/impl/TankTankCollider.java @@ -0,0 +1,33 @@ +package com.msb.inter.impl;/** + * @Author bingor + * @Date 2022/10/11 9:41 + * @Description: com.msb.inter.impl + * @Version: 1.0 + */ + +import com.msb.inter.Collider; +import com.msb.model.Tank; +import com.msb.model.abstracts.GameObject; + +/** + *@ClassName BulletTankCollider + *@Description TODO + *@Author bingor + *@Date 2022/10/11 9:41 + *@Version 3.0 + */ +public class TankTankCollider implements Collider { + @Override + public void collide(GameObject o1, GameObject o2) { + if(o1 instanceof Tank && o2 instanceof Tank) { + Tank tank1 = (Tank) o1; + Tank tank2 = (Tank) o2; + if(tank1.getRectangle().intersects(tank2.getRectangle())) { + tank1.goBack(); + tank2.goBack(); + } + } else { + return; + } + } +} diff --git a/src/main/java/com/msb/model/Bullet.java b/src/main/java/com/msb/model/Bullet.java index 4f33d75..6366ee1 100644 --- a/src/main/java/com/msb/model/Bullet.java +++ b/src/main/java/com/msb/model/Bullet.java @@ -4,7 +4,7 @@ import com.msb.TankFrame; import com.msb.base.ResourcesMgr; import com.msb.enums.DirEnum; import com.msb.enums.GroupEnum; -import com.msb.model.abstracts.BaseBullet; +import com.msb.model.abstracts.GameObject; import java.awt.*; @@ -14,9 +14,10 @@ import java.awt.*; * @Description: 子弹类 * @Version: 1.0 */ -public class Bullet extends BaseBullet { +//public class Bullet extends BaseBullet { +public class Bullet extends GameObject { - private int x, y; +// private int x, y; private DirEnum dir; public static final int WIDTH = ResourcesMgr.bulletD.getWidth(); public static final int HEIGHT = ResourcesMgr.bulletD.getHeight(); @@ -38,13 +39,13 @@ public class Bullet extends BaseBullet { rectangle.width = WIDTH; rectangle.height = HEIGHT; - gameModel.bullets.add(this); + gameModel.gameObjects.add(this); } @Override public void paint(Graphics g) { if( ! this.live) { - gameModel.bullets.remove(this); + gameModel.gameObjects.remove(this); return; } @@ -109,7 +110,15 @@ public class Bullet extends BaseBullet { } - private void die() { + public void die() { this.live = false; } + + public GroupEnum getGroup() { + return group; + } + + public Rectangle getRectangle() { + return rectangle; + } } diff --git a/src/main/java/com/msb/model/CircleExplode.java b/src/main/java/com/msb/model/CircleExplode.java index 8f2500c..b234337 100644 --- a/src/main/java/com/msb/model/CircleExplode.java +++ b/src/main/java/com/msb/model/CircleExplode.java @@ -36,7 +36,8 @@ public class CircleExplode extends BaseExplode { step++; if(step >= 10) { step = 0; - gameModel.explodes.remove(this); +// gameModel.explodes.remove(this); + gameModel.gameObjects.remove(this); } g.setColor(color); diff --git a/src/main/java/com/msb/model/Explode.java b/src/main/java/com/msb/model/Explode.java index 76584b1..ae8628a 100644 --- a/src/main/java/com/msb/model/Explode.java +++ b/src/main/java/com/msb/model/Explode.java @@ -2,6 +2,7 @@ package com.msb.model; import com.msb.base.ResourcesMgr; import com.msb.model.abstracts.BaseExplode; +import com.msb.model.abstracts.GameObject; import java.awt.*; @@ -11,9 +12,10 @@ import java.awt.*; * @Description: com.msb * @Version: 1.0 */ -public class Explode extends BaseExplode { +//public class Explode extends BaseExplode { +public class Explode extends GameObject { - private int x,y; +// private int x,y; private GameModel gameModel; public static final int WIDTH = ResourcesMgr.explodeImages[0].getWidth(); public static final int HEIGHT = ResourcesMgr.explodeImages[0].getHeight(); @@ -32,7 +34,8 @@ public class Explode extends BaseExplode { g.drawImage(ResourcesMgr.explodeImages[step++], x, y, null); if(step >= ResourcesMgr.explodeImages.length) { step = 0; - gameModel.explodes.remove(this); +// gameModel.explodes.remove(this); + gameModel.gameObjects.remove(this); } } diff --git a/src/main/java/com/msb/model/GameModel.java b/src/main/java/com/msb/model/GameModel.java index 9a360f1..a42816a 100644 --- a/src/main/java/com/msb/model/GameModel.java +++ b/src/main/java/com/msb/model/GameModel.java @@ -11,7 +11,10 @@ import com.msb.enums.GroupEnum; import com.msb.factorys.CircleFactory; import com.msb.factorys.DefaultFactory; import com.msb.factorys.abstracts.GameFactory; +import com.msb.inter.impl.BulletTankCollider; +import com.msb.inter.impl.TankTankCollider; import com.msb.model.abstracts.BaseExplode; +import com.msb.model.abstracts.GameObject; import java.awt.*; import java.util.ArrayList; @@ -27,54 +30,70 @@ import java.util.List; public class GameModel { Tank myTank = new Tank(100, 500, DirEnum.RIGHT, GroupEnum.GOOD, this); - // Explode explode = new Explode(100, 100, this); -// Bullet bullet = new Bullet(200, 200, DirEnum.DOWN); + /* public List bullets = new ArrayList<>(); public List tanks = new ArrayList<>(); //敌方坦克 - public List explodes = new ArrayList<>(); //爆炸效果 + public List explodes = new ArrayList<>(); //爆炸效果 + */ + public List gameObjects = new ArrayList<>(); + private BulletTankCollider bulletTankCollider = new BulletTankCollider(); + private TankTankCollider tankTankCollider = new TankTankCollider(); // public GameFactory gameFactory = new DefaultFactory(); public GameModel() { -// Random random = new Random(); int initTankCount = Integer.parseInt(PropertyMgr.getString("initTankCount")); - //创建5个敌方坦克 for (int i=0; i