From b4b9ac582bc2ce5974e37f24863e1d951dec13f2 Mon Sep 17 00:00:00 2001 From: bingor Date: Wed, 12 Oct 2022 15:51:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9D=A6=E5=85=8B=E5=A4=A7=E6=88=98=EF=BC=88?= =?UTF-8?q?=E4=B8=80=E6=9C=9F=EF=BC=89-=E8=AE=BE=E8=AE=A1=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F-=E6=B7=BB=E5=8A=A0=E5=AD=90=E5=BC=B9=E4=B8=8E?= =?UTF-8?q?=E5=A2=99=E3=80=81=E5=9D=A6=E5=85=8B=E4=B8=8E=E5=A2=99=E7=9A=84?= =?UTF-8?q?=E7=A2=B0=E6=92=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/msb/chain/ColliderChain.java | 4 ++ .../msb/inter/impl/BulletWallCollider.java | 37 +++++++++++++++++++ .../com/msb/inter/impl/TankWallCollider.java | 36 ++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 src/main/java/com/msb/inter/impl/BulletWallCollider.java create mode 100644 src/main/java/com/msb/inter/impl/TankWallCollider.java diff --git a/src/main/java/com/msb/chain/ColliderChain.java b/src/main/java/com/msb/chain/ColliderChain.java index 693063f..fd1396a 100644 --- a/src/main/java/com/msb/chain/ColliderChain.java +++ b/src/main/java/com/msb/chain/ColliderChain.java @@ -7,7 +7,9 @@ package com.msb.chain;/** import com.msb.inter.Collider; import com.msb.inter.impl.BulletTankCollider; +import com.msb.inter.impl.BulletWallCollider; import com.msb.inter.impl.TankTankCollider; +import com.msb.inter.impl.TankWallCollider; import com.msb.model.abstracts.GameObject; import java.util.LinkedList; @@ -26,6 +28,8 @@ public class ColliderChain implements Collider { public ColliderChain() { add(new BulletTankCollider()); add(new TankTankCollider()); + add(new TankWallCollider()); + add(new BulletWallCollider()); } public void add(Collider collider) { diff --git a/src/main/java/com/msb/inter/impl/BulletWallCollider.java b/src/main/java/com/msb/inter/impl/BulletWallCollider.java new file mode 100644 index 0000000..d1640bc --- /dev/null +++ b/src/main/java/com/msb/inter/impl/BulletWallCollider.java @@ -0,0 +1,37 @@ +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.Wall; +import com.msb.model.abstracts.GameObject; + +/** + *@ClassName BulletWallCollider + *@Description TODO + *@Author bingor + *@Date 2022/10/11 9:41 + *@Version 3.0 + */ +public class BulletWallCollider implements Collider { + @Override + public boolean collide(GameObject o1, GameObject o2) { + if(o1 instanceof Bullet && o2 instanceof Wall) { + Bullet bullet = (Bullet) o1; + Wall wall = (Wall) o2; + + if(bullet.getRectangle().intersects(wall.getRectangle())) { + bullet.die(); + } + return true; //子弹碰墙,只有子弹消亡,但是墙还是好的,所以责任链还是需要继续 + } else if (o1 instanceof Wall && o2 instanceof Bullet) { + return collide(o2, o1); + } else { + return true; + } + } +} diff --git a/src/main/java/com/msb/inter/impl/TankWallCollider.java b/src/main/java/com/msb/inter/impl/TankWallCollider.java new file mode 100644 index 0000000..bb847fe --- /dev/null +++ b/src/main/java/com/msb/inter/impl/TankWallCollider.java @@ -0,0 +1,36 @@ +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.Wall; +import com.msb.model.abstracts.GameObject; + +/** + *@ClassName BulletTankCollider + *@Description TODO + *@Author bingor + *@Date 2022/10/11 9:41 + *@Version 3.0 + */ +public class TankWallCollider implements Collider { + @Override + public boolean collide(GameObject o1, GameObject o2) { + if(o1 instanceof Tank && o2 instanceof Wall) { + Tank tank = (Tank) o1; + Wall wall = (Wall) o2; + if(tank.getRectangle().intersects(wall.getRectangle())) { + tank.goBack(); + } + return true; //坦克与墙相撞,坦克和墙不会消失,所以责任链应该继续 + } else if((o1 instanceof Wall && o2 instanceof Tank)) { + return collide(o2, o1); + } else { + return true; + } + } +}