导航:从零开始手写mmo游戏从框架到爆炸(零)—— 导航-CSDN博客
本章主要是补充装备工厂,增加根据野怪等级和品质获得装备的方法:
增加的代码如下- EquipmentFactory:
public static Equipment createEquipment(Monster monster,
QualityEnum qualityEnum){
Equipment result;
// 根据怪物等级,找到对应的装备; 去掉等级过高的装备
List<EquipmentTemplate> templates = allEquipmentTemplates.stream().filter(e -> e.getLevel() <= monster.getLevel()).collect(Collectors.toList());
// 随机找到一个装备模板
EquipmentTemplate equipmentTemplate = templates.get(new Random().nextInt(templates.size()));
// 找到装备的品质
if(equipmentTemplate.getType().equalsIgnoreCase(EquipmentEnum.WEAPON.getCode())){
result = getWeapon(equipmentTemplate,qualityEnum);
}else{
// 不是武器的话都一样
result = getOtherEquipment(equipmentTemplate,qualityEnum);
}
// 添加词缀
return result;
}
private static Weapon getWeapon(EquipmentTemplate equipmentTemplate, QualityEnum qualityEnum) {
Weapon weapon = new Weapon(equipmentTemplate.getName(),equipmentTemplate.getType(),
equipmentTemplate.getDesc(), EquipmentEnum.WEAPON,
WeaponTypeEnum.getByCode(equipmentTemplate.getWeaponType()),qualityEnum,equipmentTemplate.getId());
// 品质越高,浮动越小
weapon.setGoods_id(equipmentTemplate.getId());
weapon.setLevel(equipmentTemplate.getLevel());
weapon.setArmature(calculate(equipmentTemplate.getArmature(),qualityEnum));
weapon.setStrength(calculate(equipmentTemplate.getStrength(),qualityEnum));
weapon.setSpeed(calculate(equipmentTemplate.getSpeed(),qualityEnum));
weapon.setConstitution(calculate(equipmentTemplate.getConstitution(),qualityEnum));
weapon.setTechnique(calculate(equipmentTemplate.getTechnique(),qualityEnum));
weapon.setHp(calculate(equipmentTemplate.getHp(),qualityEnum));
weapon.setMagic(calculate(equipmentTemplate.getMagic(),qualityEnum));
weapon.setSpeed(calculate(equipmentTemplate.getSpeed(),qualityEnum));
weapon.setEvasion(calculate(equipmentTemplate.getEvasion(),qualityEnum));
weapon.setFortune(calculate(equipmentTemplate.getFortune(),qualityEnum));
weapon.setPoisonResistance(calculate(equipmentTemplate.getPoisonResistance(),qualityEnum));
weapon.setFlameResistance(calculate(equipmentTemplate.getFlameResistance(),qualityEnum));
weapon.setThunderResistance(calculate(equipmentTemplate.getThunderResistance(),qualityEnum));
weapon.setIceResistance(calculate(equipmentTemplate.getIceResistance(),qualityEnum));
return weapon;
}
public static Equipment getOtherEquipment(EquipmentTemplate equipmentTemplate, QualityEnum qualityEnum) {
Equipment equipment = new Equipment(equipmentTemplate.getName(),equipmentTemplate.getType(),
equipmentTemplate.getDesc(),EquipmentEnum.getByCode(equipmentTemplate.getType()),qualityEnum,equipmentTemplate.getId());
// 品质越高,浮动越小
equipment.setGoods_id(equipmentTemplate.getId());
equipment.setLevel(equipmentTemplate.getLevel());
equipment.setArmature(calculate(equipmentTemplate.getArmature(),qualityEnum));
equipment.setStrength(calculate(equipmentTemplate.getStrength(),qualityEnum));
equipment.setSpeed(calculate(equipmentTemplate.getSpeed(),qualityEnum));
equipment.setConstitution(calculate(equipmentTemplate.getConstitution(),qualityEnum));
equipment.setTechnique(calculate(equipmentTemplate.getTechnique(),qualityEnum));
equipment.setHp(calculate(equipmentTemplate.getHp(),qualityEnum));
equipment.setMagic(calculate(equipmentTemplate.getMagic(),qualityEnum));
equipment.setSpeed(calculate(equipmentTemplate.getSpeed(),qualityEnum));
equipment.setEvasion(calculate(equipmentTemplate.getEvasion(),qualityEnum));
equipment.setFortune(calculate(equipmentTemplate.getFortune(),qualityEnum));
// 四种元素的抗性
equipment.setPoisonResistance(calculate(equipmentTemplate.getPoisonResistance(),qualityEnum));
equipment.setFlameResistance(calculate(equipmentTemplate.getFlameResistance(),qualityEnum));
equipment.setThunderResistance(calculate(equipmentTemplate.getThunderResistance(),qualityEnum));
equipment.setIceResistance(calculate(equipmentTemplate.getIceResistance(),qualityEnum));
return equipment;
}
private static int calculate(String data, QualityEnum qualityEnum) {
if(StringUtils.isEmpty(data)){
return 0;
}
String[] split = data.split("-");
int min = Integer.parseInt(split[0]);
int max = Integer.parseInt(split[1]);
int add = max - min;
if(add > 1){
switch (qualityEnum){
case 黄色:
min = min + add/5;
break;
case 紫色:
min = min + add/4;
break;
case 红色:
min = min + add/3;
break;
case 暗金:
min = min + add/2;
break;
default:
break;
}
}
if(max > min) {
// 根据品质改变最大最小值
return min + new Random().nextInt(max - min);
}else if (max == min){
return max;
}
return 0;
}
大概解释一下就是根据野怪的等级找到可获得的装备模板列表,再根据品质去获得属性的浮动,品质越高浮动越小,这样品质高的武器将有机会获得更高的属性加成。
我们来测试一下-测试代码:
public static void main(String[] args) {
// for (EquipmentTemplate template : allEquipmentTemplates) {
// System.out.println(template.toString());
// }
Equipment equipment = createEquipment(MonsterFactory.createMonster(101, 10), QualityEnum.白色);
System.out.println("获得装备");
System.out.println(equipment.prettyPrint());
}
结果:
获得装备
****物品信息****
名称:布甲
品质:普通
位置:胸甲
说明:布甲
护甲:6
体力:13
***************
全部源码详见:
gitee : eternity-online: 多人在线mmo游戏 - Gitee.com
分支:step-12
请各位帅哥靓女帮忙去gitee上点个星星,谢谢!