坦克大战(一期)-设计模式-添加子弹与墙、坦克与墙的碰撞

DesignPatterns
bingor 2 years ago
parent 761d0c5c75
commit b4b9ac582b

@ -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) {

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

@ -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;
}
}
}
Loading…
Cancel
Save