坦克大战(一期)-设计模式-责任链应用1.1

DesignPatterns
bingor 2 years ago
parent 09d84ecaec
commit 20e075db3c

@ -0,0 +1,44 @@
package com.msb.chain;/**
* @Author bingor
* @Date 2022/10/11 17:55
* @Description: com.msb.chain
* @Version: 1.0
*/
import com.msb.inter.Collider;
import com.msb.inter.impl.BulletTankCollider;
import com.msb.inter.impl.TankTankCollider;
import com.msb.model.abstracts.GameObject;
import java.util.LinkedList;
import java.util.List;
/**
*@ClassName ColliderChain
*@Description TODO
*@Author bingor
*@Date 2022/10/11 17:55
*@Version 3.0
*/
public class ColliderChain {
List<Collider> colliders = new LinkedList<>();
public ColliderChain() {
add(new BulletTankCollider());
add(new TankTankCollider());
}
public void add(Collider collider) {
colliders.add(collider);
}
public List<Collider> getColliders() {
return colliders;
}
public void collide(GameObject o1, GameObject o2) {
for (Collider collider : colliders) {
collider.collide(o1, o2);
}
}
}

@ -6,6 +6,7 @@ package com.msb.model;/**
*/
import com.msb.base.PropertyMgr;
import com.msb.chain.ColliderChain;
import com.msb.enums.DirEnum;
import com.msb.enums.GroupEnum;
import com.msb.factorys.CircleFactory;
@ -36,8 +37,7 @@ public class GameModel {
public List<Explode> explodes = new ArrayList<>(); //爆炸效果
*/
public List<GameObject> gameObjects = new ArrayList<>();
private BulletTankCollider bulletTankCollider = new BulletTankCollider();
private TankTankCollider tankTankCollider = new TankTankCollider();
private ColliderChain colliderChain = new ColliderChain();
// public GameFactory gameFactory = new DefaultFactory();
public GameModel() {
@ -88,8 +88,7 @@ public class GameModel {
// bullets.get(i).collide(tanks.get(j));
GameObject o1 = gameObjects.get(i);
GameObject o2 = gameObjects.get(j);
bulletTankCollider.collide(o1, o2);
tankTankCollider.collide(o1, o2);
colliderChain.collide(o1, o2);
}
}

Loading…
Cancel
Save