导航:从零开始手写mmo游戏从框架到爆炸(零)—— 导航-CSDN博客
上一章我们完成了装备金币和经验掉落的计算,这一章我们来完成装备、金币与经验的获取。这里我们先改造英雄封装 - HeroWrapper
@Data
public class HeroWrapper {
/***
* 英雄
*/
private Hero hero;
// 仓库 或者 背包
private Warehouse warehouse;
public HeroWrapper(Hero hero) {
this.hero = hero;
}
}
仓库的代码:
public class Warehouse implements Serializable {
/**
* 最多200件装备
**/
private static final int MAX_EQUIPMENT_NUM = 200;
/**
* 金币
**/
private int goldCoin;
/**
* 钻石
**/
private int diamonds;
/**
* 装备
**/
private List<Equipment> equipmentList = new ArrayList<>();
public Warehouse() {
}
public Warehouse(int goldCoin, int diamonds, List<Equipment> equipmentList) {
this.goldCoin = goldCoin;
this.diamonds = diamonds;
this.equipmentList = equipmentList;
}
public void pickUp(List<Goods> fallingGoods) {
for (Goods good : fallingGoods) {
if(good instanceof Equipment){
equipmentList.add((Equipment) good);
}
}
}
public void pickUp(Coin fallingCoin) {
if(fallingCoin != null && (fallingCoin.getGoldCoin()>0 || fallingCoin.getDiamonds()>0)){
this.goldCoin += fallingCoin.getGoldCoin();
this.diamonds += fallingCoin.getDiamonds();
}
}
public int getGoldCoin() {
return goldCoin;
}
public void setGoldCoin(int goldCoin) {
this.goldCoin = goldCoin;
}
public int getDiamonds() {
return diamonds;
}
public void setDiamonds(int diamonds) {
this.diamonds = diamonds;
}
public List<Equipment> getEquipmentList() {
return equipmentList;
}
public void setEquipmentList(List<Equipment> equipmentList) {
this.equipmentList = equipmentList;
}
}
HeroWrapper中增加获取经验的方法:
@Data
public class HeroWrapper {
/***
* 英雄
*/
private Hero hero;
// 仓库 或者 背包
private Warehouse warehouse;
public HeroWrapper(Hero hero) {
this.hero = hero;
warehouse = new Warehouse();
}
public void addExp(int fallingExp, int monsterLevel, BattleFightResult result) {
// 找到当前职业的最高等级
int highestLevel = getHero().getTemplate().getHighestLevel();
if(getHero().getLevel() >= highestLevel){
result.addConsole("已经达到当前职业模板的最高级,请转职");
return;
}
// 更新英雄的经验
int upExp = fallingExp;
if(hero.getLevel() > (monsterLevel + 5)){
upExp = 1;
}else if(hero.getLevel() > (monsterLevel + 2)){
upExp = (int) (fallingExp * 0.5);
}
Integer currentExp = this.hero.getCurrentExp();
int exp = currentExp + upExp;
this.hero.setCurrentExp(exp);
if(this.hero.getNextLevelNeedExp() < exp){
// 升级了
result.addConsole("恭喜你升级了");
hero.levelUp(exp);
}
}
}
改造BattleEngine中获取经验、金币和物品的方法
if (fallingExp > 0) {
result.addConsole("获得经验" + fallingExp + "点");
// 获取经验经验
getHeroWrapper().addExp(fallingExp, max_monster_level,result);
}
if (fallingCoin.getGoldCoin() > 0) {
result.addConsole("获得金币" + fallingCoin.getGoldCoin());
getHeroWrapper().getWarehouse().pickUp(fallingCoin);
}
if (fallingGoods.size() > 0) {
for (Goods fallingGood : fallingGoods) {
result.addConsole("获得物品" + fallingGood.toString());
}
getHeroWrapper().getWarehouse().pickUp(fallingGoods);
}
main方法修改:
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Hero heroA = JobFactory.createHero(JobFactory.getTemplates().get(0), "haha");
Monster monster = MonsterFactory.createMonster(101, 1);
HeroWrapper wrapper = new HeroWrapper(heroA);
BattleEngine engine = new BattleEngine(wrapper);
BattleFightResult fight = engine.fight(Arrays.asList(heroA), Arrays.asList(monster));
for (String fightLog : fight.getFightLogs()) {
System.out.println(fightLog);
}
System.out.println("当前英雄经验" + heroA.getCurrentExp());
System.out.println("当前英雄金币" + wrapper.getWarehouse().getGoldCoin());
System.out.println("当前英雄装备");
for (Equipment equipment : wrapper.getWarehouse().getEquipmentList()) {
System.out.println(equipment.prettyPrint());
}
}
我们来看下改造后的效果:
haha[800/800] 攻击了地精战士,造成伤害60点
地精战士[140/200] 攻击了haha,造成伤害20点
haha[780/800] 攻击了地精战士,造成伤害60点
地精战士[80/200] 攻击了haha,造成伤害20点
haha[760/800] 攻击了地精战士,造成伤害60点
地精战士[20/200] 攻击了haha,造成伤害20点
haha[740/800] 攻击了地精战士,造成伤害60点,地精战士阵亡
获得经验1点
获得金币1
获得物品*物品信息* 名称:木剑 级别:1 品质:普通 力量:4 ;
当前英雄经验1
当前英雄金币1
当前英雄装备
****物品信息****
名称:木剑
品质:普通
位置:武器
说明:最普通的剑,无任何附加属性
力量:4
***************
Process finished with exit code 0
全部源码详见:
gitee : eternity-online: 多人在线mmo游戏 - Gitee.com
分支:step-13
请各位帅哥靓女帮忙去gitee上点个星星,谢谢!