前文有提到,我放飞机可以吃到三种技能,也就是有三种不同的状态。
- 无敌技能
- 三射技能
- 变弱技能
- 获取范围,以进行碰撞检测
- 死亡
- 移动
- 减血
- 碰撞
- 获取攻击力
在这里,比如无敌技能不会死,但是没有无敌技能的话,一撞就死。
所以,在不同的状态下,碰撞检测之后的操作就是不一样的。
可以用if else之类的语句去判断,但是如果后续修改游戏,比如多出一种,无敌+三射技能,要加的判断会比较多。
所以这里用到一个设计模式中的状态模式,但是状态较少的时候,代码量反而会交简单的逻辑判断语句多。
还是用一个接口来表示我方飞机的状态。
具体代码:
public interface MainPlaneState {
// 吃到无敌技能
public void eatStrong();
// 吃到三弹齐发技能
public void eatTriple();
// 吃到变弱技能
public void eatWeek();
// 发射子弹
public void fire();
// 和敌方飞机撞击
public void hit(GameRole gameRole);
}
正常状态:
import com.mybeatplane.helper.GameConstant;
import com.mybeatplane.interfase.GameObserver;
import com.mybeatplane.interfase.GameRole;
import com.mybeatplane.role.Bullet;
import com.mybeatplane.role.MainPlane;
import com.mybeatplane.role.NormalPcPlane;
public class NormalState implements MainPlaneState {
private MainPlane mainPlane;
public MainPlane getMainPlane() {
return mainPlane;
}
public void setMainPlane(MainPlane mainPlane) {
this.mainPlane = mainPlane;
}
public NormalState(MainPlane mainPlane) {
this.mainPlane = mainPlane;
}
// 正常状态下吃到无敌技能会变无敌
public void eatStrong() {
mainPlane.setState(mainPlane.getStrongState());
}
// 正常状态下吃到三射技能会变成三射状态
public void eatTriple() {
mainPlane.setState(mainPlane.getTripleShootState());
}
// 正常状态下吃到变弱技能 不起作用
public void eatWeek() {
}
// 正常状态下子弹是单发的
public void fire() {
int currentX = mainPlane.getCurrentX()
+ (GameConstant.MainPlaneData.RADIUS - GameConstant.BulletData.RADIUS)
/ 2;
int currentY = mainPlane.getCurrentY();
GameObserver observer = mainPlane.getObserver();
Bullet bullet = new Bullet(currentX, currentY, observer);
observer.onBulletAppear(bullet);
}
// 正常状态下,如果我方飞机撞上对方飞机,我方飞机死亡
public void hit(GameRole gameRole) {
if (mainPlane.getRect().intersects(gameRole.getRect())) {
mainPlane.godie();
}
}
}
无敌状态:
import com.mybeatplane.helper.GameConstant;
import com.mybeatplane.interfase.GameObserver;
import com.mybeatplane.interfase.GameRole;
import com.mybeatplane.role.Bullet;
import com.mybeatplane.role.MainPlane;
import com.mybeatplane.role.NormalPcPlane;
public class StrongState implements MainPlaneState {
private MainPlane mainPlane;
public MainPlane getMainPlane() {
return mainPlane;
}
public void setMainPlane(MainPlane mainPlane) {
this.mainPlane = mainPlane;
}
public StrongState(MainPlane mainPlane) {
this.mainPlane = mainPlane;
}
// 无敌状态下吃到无敌技能不起作用
public void eatStrong() {
}
// 无敌状态下吃到三射技能不起作用
public void eatTriple() {
}
// 无敌状态下吃到变弱技能,会变成正常状态
public void eatWeek() {
mainPlane.setState(mainPlane.getNormalState());
}
// 无敌状态下发射子弹是单发的
public void fire() {
int currentX = mainPlane.getCurrentX()
+ (GameConstant.MainPlaneData.RADIUS - GameConstant.BulletData.RADIUS)
/ 2;
int currentY = mainPlane.getCurrentY();
GameObserver observer = mainPlane.getObserver();
Bullet bullet = new Bullet(currentX, currentY, observer);
observer.onBulletAppear(bullet);
}
// 无敌状态下撞击敌方飞机不受到伤害,敌方飞机死亡
public void hit(GameRole gameRole) {
gameRole.godie();
}
}
三射状态:
import java.util.ArrayList;
import java.util.List;
import com.mybeatplane.helper.GameConstant;
import com.mybeatplane.interfase.GameObserver;
import com.mybeatplane.interfase.GameRole;
import com.mybeatplane.role.Bullet;
import com.mybeatplane.role.MainPlane;
import com.mybeatplane.role.NormalPcPlane;
public class TripleShootState implements MainPlaneState {
private MainPlane mainPlane;
public TripleShootState(MainPlane mainPlane) {
this.mainPlane = mainPlane;
}
public MainPlane getMainPlane() {
return mainPlane;
}
public void setMainPlane(MainPlane mainPlane) {
this.mainPlane = mainPlane;
}
// 三射状态吃到无敌技能不起作用
public void eatStrong() {
}
// 三射状态吃到三射技能不起作用
public void eatTriple() {
}
// 三射状态吃到变弱技能 变成正常状态
public void eatWeek() {
mainPlane.setState(mainPlane.getNormalState());
}
// 三射状态下子弹一次产生三个子弹发射
public void fire() {
List<Bullet> newBullets = new ArrayList<Bullet>();
int currentXOne = mainPlane.getCurrentX()
+ (GameConstant.MainPlaneData.RADIUS - GameConstant.BulletData.RADIUS)
/ 2;
int currentXTwo = mainPlane.getCurrentX()
- GameConstant.BulletData.RADIUS;
int currentXThree = mainPlane.getCurrentX()
+ GameConstant.MainPlaneData.RADIUS;
int currentY = mainPlane.getCurrentY();
GameObserver observer = mainPlane.getObserver();
Bullet bulletOne = new Bullet(currentXOne, currentY, observer);
Bullet bulletTwo = new Bullet(currentXTwo, currentY, observer);
Bullet bulletThree = new Bullet(currentXThree, currentY, observer);
newBullets.add(bulletOne);
newBullets.add(bulletTwo);
newBullets.add(bulletThree);
observer.onBulletsAppear(newBullets);
}
// 三射状态下我方飞机撞击到敌方飞机,我方飞机死亡
public void hit(GameRole gameRole) {
if (mainPlane.getRect().intersects(gameRole.getRect())) {
mainPlane.godie();
}
}
}