java - 当所有敌人在电子游戏中死亡时如何改变关卡?
问题描述
我正在实现一个 2d 视频游戏。当你杀死一个级别的所有敌人时,你应该进入下一个级别,但我不知道如何实现它。您对我可以使用的可能设计模式有什么建议吗?
public class GameWorld {
int aliveEnemies = 3;
void setAliveEnemies() {
//omitted by OP
}
int getAliveEnemies() {
if (aliveEnemies == 0) {
goToNextLevel();
}
}
}
public class Level {
void spawnEntities(){
// omitted by OP
}
}
解决方案
我想你Gameworld
有一个Level
属性。当你进入下一个关卡时,你应该调用关卡的 spawnEntity。
public class GameWorld {
Level currentLevel = null; //Level attribute
int aliveEnemies = 0;
//Gameworld constructor
GameWorld(Level lev) {
this.currentLevel = lev;
}
void setAliveEnemies() {
this.aliveEnemies = this.currentLevel.spawnEntities(); //Load next enemies
}
int getAliveEnemies() {
if (aliveEnemies == 0) {
goToNextLevel();
}
}
//goToNextLevel implementation
private void goToNextLevel() {
this.level = new Level(); //Load new Level
this.setAliveEnemies(); // Load enemies of that level
}
}
public class Level {
int spawnEntities() //should return number of enemis spawned
// omitted by OP
return 3;
}
}
推荐阅读
- javascript - 如何在 jest/enzyme 测试中测试 axios 获取请求功能?
- c++ - 使用 Scatter 和 Gather 的 MPI 矩阵乘法
- xamarin.forms - 鼠标悬停在背景颜色上的图像按钮
- python - 为什么我的模型总是精确地得到 0.5 AUC?
- javascript - 使用按钮(在我的中继器中)使用 javascript 显示隐藏信息,但似乎不起作用
- node.js - OTFUEL_APP_TOKEN' 未被识别为内部或外部命令,
- javascript - 如何隐藏图像链接,即从获取它的位置屏蔽存储库
- c++ - 如何在模板类中添加常用功能?
- mysql - 选择 SQL 查询不适用于简单的 Where clouse
- html - 文本对齐不适用于特定长度的 div