导航:从零开始手写mmo游戏从框架到爆炸(零)—— 导航-优快云博客
目录
文接上一章。我们把战斗系统demo应用到实际的项目中来。在第十九章(从零开始手写mmo游戏从框架到爆炸(十九)— 用户数据封装与存储-优快云博客)中,客户端已经可以创建英雄并且进入游戏主界面了,下一步,我们需要选择地图,并且和地图中的野怪来一张遭遇战。
首先我们把上一章中的test路径下的demo移动到正式路径下并进行修改。新增的几个类如下:
其中大部分上一章已经基本讲过了,大家对着gitee上的分支源码来看即可,我们重点关注地图已经搜寻野怪的代码。
地图设定
地图:TownMap
@Data
public class TownMap implements Serializable {
private String id;
private FindMonstersEngine findMonsterEngine;
// 级别 1 就是 0-9级 9 就是 90-99级
private int level;
private int floorId;
private String name;
private String desc;
private String teamSize;
// 可能出现的野怪
private List<MonsterInMap> monsters;
public TownMap(MapTemplate mapTemplate) {
this.findMonsterEngine = new DefaultFindMonsters();
this.name = mapTemplate.getName();
this.level = mapTemplate.getLevel();
this.monsters = MapFactory.createMonsters(mapTemplate);
this.desc = mapTemplate.getDesc();
this.teamSize = mapTemplate.getTeamSize();
this.id = mapTemplate.getId();
}
}
地图中存储了野怪的等级已经出现的概率。同时增加了一个接口-FindMonstersEngine。
我们来看下这个接口:
public interface FindMonstersEngine {
List<Monster> findMonsters(HeroWrapper hero, TownMap map) throws IOException, ClassNotFoundException, Exception;
}
这个接口就一个方法,寻找野怪。我们写一个默认的实现类:
public class DefaultFindMonsters extends AbstractFindMonsters{
@Override
public List<Monster> findMonsters(HeroWrapper heroWrapper, TownMap map) throws Exception {
List<Monster> result = new ArrayList<>();
// 根据
List<MonsterInMap> monsters = map.getMonsters();
String[] teamSize = map.getTeamSize().split("-");
int min = Integer.parseInt(teamSize[0]);
int max = Integer.parseInt(teamSize[1]);
int num;
if(min == max){
num = max;
}else {
num = min + new Random().nextInt(max - min);
}
for (int i = 0; i < num; i++) {
Monster monster = RandomBalance.getMonsters(monsters);
result.add(monster);
}
return BeanCopyUtils.deepCopy(result);
}
}
战斗引擎
我们再看下战斗引擎 BattleEngine
public class BattleEngine {
// 队列不变
private final LinkedList<Attack> actions = new LinkedList<>();
private int addAction(Attack action){
actions.offer(action);
return actions.size();
}
public BattleFightResult fight(List<? extends Character> listA, List<? extends Character&g