我认为战斗序列是您游戏中的迷你游戏。更新滴答声(或转折滴答声)被定向到处理这些事件的组件。这种方法将战斗序列逻辑封装在一个单独的类中,使您的主要游戏循环可以自由在游戏状态之间进行转换。
void gameLoop() {
while(gameRunning) {
if (state == EXPLORATION) {
// Perform actions for when player is simply walking around
// ...
}
else if (state == IN_BATTLE) {
// Perform actions for when player is in battle
currentBattle.HandleTurn()
}
else if (state == IN_DIALOGUE) {
// Perform actions for when player is talking with npcs
// ...
}
}
}
战斗序列类如下所示:
class BattleSequence {
public:
BattleSequence(Entity player, Entity enemy);
void HandleTurn();
bool battleFinished();
private:
Entity currentlyAttacking;
Entity currentlyReceiving;
bool finished;
}
您的Troll和Warrior都继承自称为Entity的公共超类。在HandleTurn中,攻击实体可以移动。这等效于AI思维例程。
void HandleTurn() {
// Perform turn actions
currentlyAttacking.fight(currentlyReceiving);
// Switch sides
Entity temp = currentlyAttacking;
currentlyAttacking = currentlyReceiving;
currentlyReceiving = temp;
// Battle end condition
if (currentlyReceiving.isDead() || currentlyAttacking.hasFled()) {
finished = true;
}
}
战斗方法决定实体将要做什么。请注意,这不需要涉及对方实体,例如喝药水或逃跑。
更新:为了支持多个怪物和一个玩家聚会,您引入了一个Group类:
class Group {
public:
void fight(Group opponents) {
// Loop through all group members so everyone gets
// a shot at the opponents
for (int i = 0; i < memberCount; i++) {
Entity attacker = members[i];
attacker.fight(opponents);
}
}
Entity get(int targetID) {
// TODO: Bounds checking
return members[targetID];
}
bool isDead() {
bool dead = true;
for (int i = 0; i < memberCount; i++) {
dead = dead && members[i].isDead();
}
return dead;
}
bool hasFled() {
bool fled = true;
for (int i = 0; i < memberCount; i++) {
fled = fled && members[i].hasFled();
}
return fled;
}
private:
Entity[] members;
int memberCount;
}
Group类将替换BattleSequence类中所有出现的Entity。选择和攻击将由Entity类本身处理,因此AI在选择最佳操作方案时可以考虑整个团队。
class Entity {
public:
void fight(Group opponents) {
// Algorithm for selecting an entity from the group
// ...
int targetID = 0; // Or just pick the first one
Entity target = opponents.get(targetID);
// Fighting algorithm
target.applyDamage(10);
}
}