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; + } + } +}