从零开始手写mmo游戏从框架到爆炸(十四)— 英雄和野怪

2024-02-20 07:36

本文主要是介绍从零开始手写mmo游戏从框架到爆炸(十四)— 英雄和野怪,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

导航:从零开始手写mmo游戏从框架到爆炸(零)—— 导航-CSDN博客 

       之前都是模板的创建,现在我们来创建英雄和野怪。

枚举类       

        首先创建几个枚举类,定义野怪的品质和血统等。

 LevelExpEnum -  每升一级需要的经验

/*** Copyright 2017-2018. All rights reserved.** This copyrighted material is made available to anyone wishing to use, modify,* copy, or redistribute it subject to the terms and conditions of the GNU* Lesser General Public License, as published by the Free Software Foundation.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY* or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License* for more details.** You should have received a copy of the GNU Lesser General Public License* along with this distribution; if not, see <http://www.gnu.org/licenses/>.**/
package com.loveprogrammer.enums;import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;/*** 每一级所需要的经验** @author eric* @version 1.0.0*/
public enum LevelExpEnum {一级(1, 100),二级(2, 500),三级(3, 1300),四级(4, 2900),五级(5, 6100),六级(6, 12500),七级(7, 20000),八级(8, 30000),九级(9, 43000),十级(10, 60000),十一级(11, 78000),十二级(12, 100000),十三级(13, 130000),十四级(14, 170000),十五级(15, 220000),十六级(16, 300000),十七级(17, 400000),十八级(18, 510000),十九级(19, 630000),二十级(20, 760000),二十一级(21, 960000),二十二级(22, 1200000),二十三级(23, 1480000),二十四级(24, 1780000),二十五级(25, 2150000),二十六级(26, 2600000),二十七级(27, 3150000),二十八级(28, 3800000),二十九级(29, 4500000),三十级(30, 5300000),三十一级(31, 6200000),三十二级(32, 7200000),三十三级(33, 8300000),三十四级(34, 9500000),三十五级(35, 10800000),三十六级(36, 12200000),三十七级(37, 13700000),三十八级(38, 15300000),三十九级(39, 17000000),四十级(40, 19000000),四十一级(41, 21100000),四十二级(42, 23500000),四十三级(43, 26200000),四十四级(44, 29200000),四十五级(45, 32400000),四十六级(46, 36000000),四十七级(47, 40000000),四十八级(48, 44500000),四十九级(49, 49500000),五十级(50, 55500000),五十一级(51, 100375000);private int level;private int exp;LevelExpEnum(int level, int exp) {this.level = level;this.exp = exp;}public int getLevel() {return level;}public void setLevel(int level) {this.level = level;}public int getExp() {return exp;}public void setExp(int exp) {this.exp = exp;}public static LevelExpEnum getByLevel(int level){Optional<LevelExpEnum> optional =Arrays.stream(LevelExpEnum.values()).filter(v -> Objects.equals(v.getLevel(), level)).findFirst();return optional.orElse(LevelExpEnum.二十七级);}
}

 MonsterQualityEnum - 野怪的血统,血统也影响掉落的装备的品质

/*** Copyright 2017-2018. All rights reserved.** This copyrighted material is made available to anyone wishing to use, modify,* copy, or redistribute it subject to the terms and conditions of the GNU* Lesser General Public License, as published by the Free Software Foundation.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY* or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License* for more details.** You should have received a copy of the GNU Lesser General Public License* along with this distribution; if not, see <http://www.gnu.org/licenses/>.**/
package com.loveprogrammer.enums;import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;/*** 野怪的血统** @author eric* @version 1.0.0*/
public enum MonsterQualityEnum {Normal(1, "奴仆级",30,10),SENIOR(2, "战将级",50,10),EPIC(3, "领主级",100,20),LEGEND(4, "君主级",150,25),MYTH(5, "帝王级",200,28);private Integer code;private String desc;// 千分之几private int percent;private int percentEgg;MonsterQualityEnum(Integer code, String desc, int percent, int percentEgg) {this.code = code;this.desc = desc;this.percent = percent;this.percentEgg = percentEgg;}public static MonsterQualityEnum getByLevel(int code){Optional<MonsterQualityEnum> optional =Arrays.stream(MonsterQualityEnum.values()).filter(v -> Objects.equals(v.getCode(), code)).findFirst();return optional.orElse(MonsterQualityEnum.Normal);}public Integer getCode() {return code;}public void setCode(Integer code) {this.code = code;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc = desc;}public int getPercent() {return percent;}public int getPercentEgg() {return percentEgg;}
}

 QualityEnum - 通用品质,野怪有品质,装备也有品质

/*** Copyright 2017-2018. All rights reserved.** This copyrighted material is made available to anyone wishing to use, modify,* copy, or redistribute it subject to the terms and conditions of the GNU* Lesser General Public License, as published by the Free Software Foundation.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY* or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License* for more details.** You should have received a copy of the GNU Lesser General Public License* along with this distribution; if not, see <http://www.gnu.org/licenses/>.**/
package com.loveprogrammer.enums;import com.loveprogrammer.utils.ConsoleColors;import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;/*** 品质** @author eric* @version 1.0.0*/
public enum QualityEnum {白色("white", "普通",1d, ConsoleColors.RESET, 1),蓝色("blue", "进阶",1.3d, ConsoleColors.BLUE, 2),黄色("yellow", "优质",1.6d, ConsoleColors.YELLOW_BRIGHT,3),紫色("purple", "卓越",1.95d, ConsoleColors.PURPLE,4),红色("red", "传奇",2.4d, ConsoleColors.RED,5),暗金("gold", "史诗",2.9d, ConsoleColors.YELLOW,6),绿色("green", "套装",3.1d, ConsoleColors.GREEN_BOLD_BRIGHT,7);private String code;private String desc;private Double ratio;private String color;private int id;QualityEnum(String code, String desc, Double ratio, String color, int id) {this.code = code;this.desc = desc;this.ratio = ratio;this.color = color;this.id = id;}public static QualityEnum getById(int id){Optional<QualityEnum> optional =Arrays.stream(QualityEnum.values()).filter(v -> Objects.equals(v.getId(), id)).findFirst();return optional.orElse(QualityEnum.白色);}public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc = desc;}public Double getRatio() {return ratio;}public String getColor() {return color;}public int getId() {return id;}
}

创建实体       

然后创建英雄及野怪的model对象。首先创建父类,提取公共的属性

package com.loveprogrammer.model;import com.loveprogrammer.enums.LevelExpEnum;
import com.loveprogrammer.enums.MonsterQualityEnum;
import com.loveprogrammer.factory.template.JobTemplate;import java.io.IOException;
import java.io.Serializable;
import java.util.concurrent.atomic.AtomicInteger;
public abstract class Character implements Serializable {protected int id;                         // 角色idprotected String name;                   // 角色信息模板// protected int attack;                    // 攻击力protected int strength;                  // 力量 影响物理输出 物理技能输出protected int armature;                 // 护甲值 影响物理防御和法术防御protected int constitution;               // 体质 影响生命值 一点体质增加10点生命值protected int magic;                       // 魔力 影响法术输出 法术技能输出protected int technique;                   // 技巧 影响闪避率、暴击率protected int speed;                         // 攻击速度protected AtomicInteger hp;                            // 生命值 必须是一个线程安全的变量protected int hpMax;                            // 最大生命值protected int baseStrength;                  // 基础力量protected int baseArmature;                 // 基础护甲protected int baseConstitution;               // 基础体质protected int baseMagic;                       // 基础魔力protected int baseTechnique;                   // 基础技巧protected int baseSpeed;                         // 基础速度protected int baseHp;                            // 基础生命值protected int level;                           // 级别protected int star = 1;                            // 星级 初始1星 最大可强化到10星protected MonsterQualityEnum monsterQualityEnum;  // 作为野怪的级别/*** 当前经验值**/protected Integer currentExp;/*** 下一级所需经验值**/protected Integer nextLevelNeedExp;// 物理攻击 0 法术攻击 1private int attackType;// 毒抗private int poisonResistance;// 火抗private int flameResistance;// 电抗private int thunderResistance;// 冰抗private int iceResistance;// 暴击率加成private int criticalStrikeRateAdd;// 更好装备获得加成private int betterEquipmentRateAdd;// 闪避率加成private int dodgeChanceAdd;// 命中率 一般只有武器 或者套装才增加命中率private int hitRateAdd;// 1 前排 2 后排private int position;public Character() {}public Character(int id,String name, int level, int strength, int armature,int constitution, int magic, int technique, int speed, int attackType, int poisonResistance,int flameResistance, int thunderResistance, int iceResistance, int position) {this.id = id;this.name = name;this.level = level;this.strength = strength;this.armature = armature;this.constitution = constitution;this.hp = new AtomicInteger(this.constitution * 10);this.hpMax = this.hp.get();this.speed = speed;this.magic = magic;this.technique = technique;
//        this.skills = new ArrayList<>();
//        this.equips = new Equips();
//        this.states = new ArrayList<>();// 基础属性用于计算增幅型的技能this.baseStrength = strength;this.baseArmature = armature;this.baseConstitution = constitution;this.baseHp = this.constitution * 10;this.baseSpeed = speed;this.baseMagic = magic;this.baseTechnique = technique;this.attackType = attackType;this.currentExp = 0;this.nextLevelNeedExp = LevelExpEnum.getByLevel(level+1).getExp();this.poisonResistance = poisonResistance;this.flameResistance = flameResistance;this.thunderResistance = thunderResistance;this.iceResistance = iceResistance;this.position = position;}public Character(int id,String name, int strength, int armature, int constitution, int magic, int technique, int speed,AtomicInteger hp, int hpMax, int baseStrength, int baseArmature, int baseConstitution, int baseMagic,int baseTechnique, int baseSpeed, int baseHp, int level, MonsterQualityEnum monsterQualityEnum,Integer currentExp, Integer nextLevelNeedExp, int attackType,int poisonResistance, int flameResistance, int thunderResistance, int iceResistance, int position) {this.id = id;this.name = name;this.strength = strength;this.armature = armature;this.constitution = constitution;this.magic = magic;this.technique = technique;this.speed = speed;this.hp = hp;this.hpMax = hpMax;this.baseStrength = baseStrength;this.baseArmature = baseArmature;this.baseConstitution = baseConstitution;this.baseMagic = baseMagic;this.baseTechnique = baseTechnique;this.baseSpeed = baseSpeed;this.baseHp = baseHp;this.level = level;this.monsterQualityEnum = monsterQualityEnum;this.currentExp = currentExp;this.nextLevelNeedExp = nextLevelNeedExp;this.attackType = attackType;this.poisonResistance = poisonResistance;this.flameResistance = flameResistance;this.thunderResistance = thunderResistance;this.iceResistance = iceResistance;this.position = position;}public String string = "\t\t\t\t\t\t\t\t\t";//角色打印信息的重写public String prettyPrint() {return string + "*****信息****\n" +string + "姓名:" + this.name + "\n" +string + "级别" + this.level + "\n" +string + "血量:" + this.hp + "\n" +string + "力量:" + this.strength + "\n" +string + "法力:" + this.magic + "\n" +string + "技巧:" + this.technique + "\n" +string + "护甲值" + this.armature + "\n" +string + "速度" + this.speed + "\n" +string + "毒抗" + this.poisonResistance + "\n" +string + "火抗" + this.flameResistance + "\n" +string + "雷抗" + this.thunderResistance + "\n" +string + "冰抗" + this.iceResistance + "\n" +string + "当前经验" + this.currentExp + "\n" +string + "下一级经验" + this.nextLevelNeedExp + "\n" +string + "" + "*************";}//角色打印信息的重写public String toString() {return "**** 名称:" + this.name + " " +"级" + this.level + " " +"血:" + this.hp + " " +"甲: " + this.armature + " " +"力:" + this.strength + " " +"法:" + this.magic + " " +"技:" + this.technique + " " +"速:" + this.speed + " " +"抗性[毒:" + this.poisonResistance + " " +"火:" + this.flameResistance + " " +"雷:" + this.thunderResistance + " " +"冰:" + this.iceResistance + "] " +"经验(" + this.currentExp + "/" + this.nextLevelNeedExp + ") ****";}public String simplePrint() {return "**** 名称:" + this.name + " " +"级别" + this.level + " " +"血量:" + this.hp + " " +"护甲值:" + this.armature + " " +"力量:" + this.strength + " " +"法力:" + this.magic + " " +"技巧:" + this.technique + " " +"速度:" + this.speed + "****";}// 排版的方法:让内容都显示在屏幕的中间public static void typeset() {                 //排版的方法:让内容都显示在屏幕的中间System.out.print("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");}public void setName(String name) {this.name = name;}public void setStrength(int strength) {this.strength = strength;}public void setConstitution(int constitution) {this.constitution = constitution;}public void setMagic(int magic) {this.magic = magic;}public void setTechnique(int technique) {this.technique = technique;}public void setArmature(int armature) {this.armature = armature;}public int getArmature() {return armature;}//    public void setHp(int hp) {
//        this.hp = new AtomicInteger(hp);
//    }public String getName() {return name;}public int getStrength() {return strength;}public int getConstitution() {return constitution;}public int getMagic() {return magic;}public int getTechnique() {return technique;}public AtomicInteger hp() {return hp;}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}public int getLevel() {return level;}public void setLevel(int level) {this.level = level;}public Integer getCurrentExp() {return currentExp;}public void setCurrentExp(Integer currentExp) {this.currentExp = currentExp;}public Integer getNextLevelNeedExp() {return nextLevelNeedExp;}public int setNextLevelNeedExp() {// 需要的经验值越来越高this.nextLevelNeedExp = LevelExpEnum.getByLevel(level).getExp();return this.nextLevelNeedExp;}public MonsterQualityEnum getMonsterQualityEnum() {return monsterQualityEnum;}public void setMonsterQualityEnum(MonsterQualityEnum monsterQualityEnum) {this.monsterQualityEnum = monsterQualityEnum;}public abstract void levelUp(int exp);public int getBaseStrength() {return baseStrength;}public int getBaseArmature() {return baseArmature;}public int getBaseConstitution() {return baseConstitution;}public int getBaseMagic() {return baseMagic;}public int getBaseTechnique() {return baseTechnique;}public int getBaseSpeed() {return baseSpeed;}public int getBaseHp() {return baseHp;}public void setBaseStrength(int baseStrength) {this.baseStrength = baseStrength;}public void setBaseArmature(int baseArmature) {this.baseArmature = baseArmature;}public void setBaseConstitution(int baseConstitution) {this.baseConstitution = baseConstitution;}public void setBaseMagic(int baseMagic) {this.baseMagic = baseMagic;}public void setBaseTechnique(int baseTechnique) {this.baseTechnique = baseTechnique;}public void setBaseSpeed(int baseSpeed) {this.baseSpeed = baseSpeed;}public void setBaseHp(int baseHp) {this.baseHp = baseHp;}public int getHpMax() {return hpMax;}public int getCurrentHp() {return hp.get();}public int getAttackType() {return attackType;}public void setAttackType(int attackType) {this.attackType = attackType;}public AtomicInteger getHp() {return hp;}public void setHp(AtomicInteger hp) {this.hp = hp;}public void setHpMax(int hpMax) {this.hpMax = hpMax;}public void setNextLevelNeedExp(Integer nextLevelNeedExp) {this.nextLevelNeedExp = nextLevelNeedExp;}public int getStar() {return star;}public void setStar(int star) {this.star = star;}public int getPoisonResistance() {return poisonResistance;}public void setPoisonResistance(int poisonResistance) {this.poisonResistance = poisonResistance;}public int getFlameResistance() {return flameResistance;}public void setFlameResistance(int flameResistance) {this.flameResistance = flameResistance;}public int getThunderResistance() {return thunderResistance;}public void setThunderResistance(int thunderResistance) {this.thunderResistance = thunderResistance;}public int getIceResistance() {return iceResistance;}public void setIceResistance(int iceResistance) {this.iceResistance = iceResistance;}public int getCriticalStrikeRateAdd() {return criticalStrikeRateAdd;}public void setCriticalStrikeRateAdd(int criticalStrikeRateAdd) {this.criticalStrikeRateAdd = criticalStrikeRateAdd;}public int getBetterEquipmentRateAdd() {return betterEquipmentRateAdd;}public void setBetterEquipmentRateAdd(int betterEquipmentRateAdd) {this.betterEquipmentRateAdd = betterEquipmentRateAdd;}public int getDodgeChanceAdd() {return dodgeChanceAdd;}public void setDodgeChanceAdd(int dodgeChanceAdd) {this.dodgeChanceAdd = dodgeChanceAdd;}public int getHitRateAdd() {return hitRateAdd;}public void setHitRateAdd(int hitRateAdd) {this.hitRateAdd = hitRateAdd;}public int getId() {return id;}// 转职public void transfer(JobTemplate jobTemplate) throws IOException, ClassNotFoundException {// 首先要替换到自己的模板}public int getPosition() {return position;}public void setPosition(int position) {this.position = position;}
}

      英雄- Hero 

package com.loveprogrammer.model;import com.loveprogrammer.enums.MonsterQualityEnum;
import com.loveprogrammer.factory.template.JobTemplate;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;/*** @version 1.0.0* @description:* @author: eric* @date: 2022-08-02 16:41**/
public class Hero extends Character{private JobTemplate template;public Hero(String name, int strength, int armature, int constitution, int magic, int technique, int speed,AtomicInteger hp, int hpMax, int baseStrength, int baseArmature, int baseConstitution, int baseMagic,int baseTechnique, int baseSpeed, int baseHp, int level, MonsterQualityEnum monsterQualityEnum,Integer currentExp, Integer nextLevelNeedExp, int attackType, JobTemplate template, int poisonResistance,int flameResistance, int thunderResistance,int iceResistance,int position) {super(template.getId(),name,strength,armature,constitution,magic,technique,speed,hp,hpMax,baseStrength,baseArmature,baseConstitution,baseMagic,baseTechnique,baseSpeed,baseHp,level,monsterQualityEnum,currentExp,nextLevelNeedExp,attackType,poisonResistance,flameResistance,thunderResistance,iceResistance,position);this.template = template;}public Hero(String name, JobTemplate template) {super(template.getId(),name,0,template.getStrength(),template.getArmature(),template.getConstitution(),template.getMagic(),template.getTechnique(),template.getSpeed(),template.getAttackType(),template.getPoisonResistance(),template.getFlameResistance(), template.getThunderResistance(),template.getIceResistance(),template.getPosition());this.template = template;}@Overridepublic void levelUp(int exp) {this.setLevel(this.level + 1);// 属性增加this.setStrength(this.strength + template.getLevelUpStrength());this.setTechnique(this.technique + template.getLevelUpTechnique());this.setArmature(this.armature + template.getLevelUpArmature());this.setConstitution(this.constitution + template.getLevelUpConstitution());this.hp.addAndGet(10 * template.getLevelUpConstitution());this.hpMax = this.hp.get();this.setMagic(this.magic + template.getLevelUpMagic());// this.setTechnique(this.technique + template.getLevelUpTechnique());this.setSpeed(this.speed + template.getLevelUpSpeed());// 基础属性增加this.setBaseStrength(this.getBaseStrength() + template.getLevelUpStrength());this.setBaseTechnique(this.getBaseTechnique() + template.getLevelUpTechnique());this.setBaseArmature(this.getBaseArmature() + template.getLevelUpArmature());this.setBaseConstitution(this.getBaseConstitution() + template.getLevelUpConstitution());this.setBaseMagic(this.getBaseMagic() + template.getLevelUpMagic());// this.setBaseTechnique(this.technique);this.setBaseSpeed(this.getBaseSpeed() + template.getLevelUpSpeed());// 计算下一级需要的经验值int needExp = this.setNextLevelNeedExp();if(exp > needExp){levelUp(exp);}}
}

野怪 -  Monster

package com.loveprogrammer.model;import com.loveprogrammer.utils.ConsoleColors;
import com.loveprogrammer.enums.MonsterQualityEnum;
import com.loveprogrammer.enums.QualityEnum;
import com.loveprogrammer.factory.template.MapTemplate;
import com.loveprogrammer.factory.template.MonsterTemplate;import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;/*** @version 1.0.0* @description:* @author: eric* @date: 2022-08-02 18:18**/
public class Monster extends Character {private MonsterTemplate template;// 品质private QualityEnum qualityEnum;// TODOprivate List<MapTemplate.Drop> dropList;// 读档的时候用public Monster(String name, int strength, int armature, int constitution, int magic, int technique, int speed,AtomicInteger hp, int hpMax, int baseStrength, int baseArmature, int baseConstitution, int baseMagic,int baseTechnique, int baseSpeed, int baseHp, int level, MonsterQualityEnum monsterQualityEnum,Integer currentExp, Integer nextLevelNeedExp, int attackType, MonsterTemplate template,QualityEnum qualityEnum, int poisonResistance,int flameResistance, int thunderResistance,int iceResistance,int position) {super(template.getId(),name,strength,armature,constitution,magic,technique,speed,hp,hpMax,baseStrength,baseArmature,baseConstitution,baseMagic,baseTechnique,baseSpeed,baseHp,level,monsterQualityEnum,currentExp,nextLevelNeedExp,attackType,poisonResistance,flameResistance,thunderResistance,iceResistance,position);this.template = template;this.qualityEnum = qualityEnum;}public Monster(String name, int level, int strength, int armature, int constitution,int magic, int technique ,int speed, int attackType,MonsterQualityEnum monsterQualityEnum,QualityEnum qualityEnum,MonsterTemplate monsterTemplate, int poisonResistance,int flameResistance, int thunderResistance, int iceResistance,int position) {super(monsterTemplate.getId(),name, level, strength, armature, constitution,magic,technique,speed,attackType,poisonResistance,flameResistance,thunderResistance,iceResistance,position);this.monsterQualityEnum = monsterQualityEnum;if(monsterTemplate == null){monsterTemplate = new MonsterTemplate();}this.template = monsterTemplate;this.qualityEnum = qualityEnum;}public Monster(MonsterTemplate template,QualityEnum qualityEnum){super(template.getId(),template.getName(), 1, template.getStrength(), template.getArmature(),template.getConstitution(),template.getMagic(),template.getTechnique(),template.getSpeed(),template.getAttackType(),template.getPoisonResistance(),template.getFlameResistance(),template.getThunderResistance(),template.getIceResistance(),template.getPosition());this.monsterQualityEnum = MonsterQualityEnum.getByLevel(template.getQuality());this.template = template;this.qualityEnum = qualityEnum;}@Overridepublic void levelUp(int exp) {this.setLevel(this.level + 1);
//        // 检查技能升级情况
//        if(!this.getSkills().isEmpty()){
//            this.getSkills().forEach(e -> e.levelUp(this.getLevel()));
//        }// 属性增加this.setStrength((int) (this.strength + template.getLevelUpStrength() * qualityEnum.getRatio()));this.setTechnique((int) (this.technique + template.getLevelUpTechnique()* qualityEnum.getRatio()));this.setArmature((int) (this.armature + template.getLevelUpArmature()* qualityEnum.getRatio()));this.setConstitution((int) (this.constitution + template.getLevelUpConstitution()* qualityEnum.getRatio()));this.hp.addAndGet((int) (10 * template.getLevelUpConstitution() * qualityEnum.getRatio()));this.hpMax = this.hp.get();this.setMagic((int) (this.magic + template.getLevelUpMagic()* qualityEnum.getRatio()));this.setSpeed((int) (this.speed + template.getLevelUpSpeed()* qualityEnum.getRatio()));// 基础属性增加this.setBaseStrength((int) (this.getBaseStrength() + template.getLevelUpStrength()* qualityEnum.getRatio()));this.setBaseMagic((int) (this.getBaseMagic() + template.getLevelUpMagic()* qualityEnum.getRatio()));this.setBaseTechnique((int) (this.getBaseTechnique() + template.getLevelUpTechnique()* qualityEnum.getRatio()));this.setBaseArmature((int) (this.getBaseArmature() + template.getLevelUpArmature()* qualityEnum.getRatio()));this.setBaseConstitution((int) (this.getBaseConstitution() + template.getLevelUpConstitution()* qualityEnum.getRatio()));this.setBaseSpeed((int) (this.getBaseSpeed() + template.getLevelUpSpeed()* qualityEnum.getRatio()));// 计算下一级需要的经验值int needExp = this.setNextLevelNeedExp();if(exp > needExp){levelUp(exp);}}public Monster upLevel(int level){this.strength = (int) (level * template.getLevelUpStrength() * qualityEnum.getRatio() + template.getStrength());this.armature = (int) (level * template.getLevelUpArmature() * qualityEnum.getRatio() + template.getArmature());this.constitution = (int) (level * template.getLevelUpConstitution() * qualityEnum.getRatio() + template.getConstitution());this.magic = (int) (level * template.getLevelUpMagic() * qualityEnum.getRatio() + template.getMagic());this.technique = (int) (level * template.getLevelUpTechnique() * qualityEnum.getRatio() + template.getTechnique());this.speed = (int) (level * template.getLevelUpSpeed() * qualityEnum.getRatio() + template.getSpeed());this.hp.set(Optional.of(this.constitution * 10).orElse(0));this.hpMax = this.hp.get();return this;}public MonsterTemplate getTemplate() {return template;}//角色打印信息的重写public String toString() {return "**** 名称:" + this.name + " " +"级别" + this.level + " " +"天赋" + this.qualityEnum.getColor() + this.qualityEnum.getDesc() + ConsoleColors.RESET + " " +"品质" + this.monsterQualityEnum.getDesc() + " " +"血量:" + this.hp + " " +"护甲值: " + this.armature + " " +"力量:" + this.strength + " " +"法力:" + this.magic + " " +"技巧:" + this.technique + " " +"速度:" + this.speed + " " +"抗性[毒:" + this.getPoisonResistance() + " 火:" + this.getFlameResistance()+ " 雷:" + this.getThunderResistance() + " 冰:" + this.getIceResistance() + "] " +"经验(" + this.currentExp + "/" + this.nextLevelNeedExp + ") ****";}public QualityEnum getQualityEnum() {return qualityEnum;}public void setQualityEnum(QualityEnum qualityEnum) {this.qualityEnum = qualityEnum;}public void setTemplate(MonsterTemplate template) {this.template = template;}public List<MapTemplate.Drop> getDropList() {return dropList;}public void setDropList(List<MapTemplate.Drop> dropList) {this.dropList = dropList;}public int score() {return strength + armature * 2 + constitution + magic + technique + speed / 2;}
}

创建英雄

     修改HeroFactory 

    public static Hero createHero(JobTemplate template, String name) throws IOException, ClassNotFoundException {Hero hero = new Hero(name, template);return hero;}

全部源码详见:

gitee : eternity-online: 多人在线mmo游戏 - Gitee.com

分支:step-09

        

这篇关于从零开始手写mmo游戏从框架到爆炸(十四)— 英雄和野怪的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/727466

相关文章

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

Python的端到端测试框架SeleniumBase使用解读

《Python的端到端测试框架SeleniumBase使用解读》:本文主要介绍Python的端到端测试框架SeleniumBase使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全... 目录SeleniumBase详细介绍及用法指南什么是 SeleniumBase?SeleniumBase

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

详解如何使用Python从零开始构建文本统计模型

《详解如何使用Python从零开始构建文本统计模型》在自然语言处理领域,词汇表构建是文本预处理的关键环节,本文通过Python代码实践,演示如何从原始文本中提取多尺度特征,并通过动态调整机制构建更精确... 目录一、项目背景与核心思想二、核心代码解析1. 数据加载与预处理2. 多尺度字符统计3. 统计结果可

C++ HTTP框架推荐(特点及优势)

《C++HTTP框架推荐(特点及优势)》:本文主要介绍C++HTTP框架推荐的相关资料,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Crow2. Drogon3. Pistache4. cpp-httplib5. Beast (Boos

SpringBoot基础框架详解

《SpringBoot基础框架详解》SpringBoot开发目的是为了简化Spring应用的创建、运行、调试和部署等,使用SpringBoot可以不用或者只需要很少的Spring配置就可以让企业项目快... 目录SpringBoot基础 – 框架介绍1.SpringBoot介绍1.1 概述1.2 核心功能2

Spring框架中@Lazy延迟加载原理和使用详解

《Spring框架中@Lazy延迟加载原理和使用详解》:本文主要介绍Spring框架中@Lazy延迟加载原理和使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、@Lazy延迟加载原理1.延迟加载原理1.1 @Lazy三种配置方法1.2 @Component

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1

基于Flask框架添加多个AI模型的API并进行交互

《基于Flask框架添加多个AI模型的API并进行交互》:本文主要介绍如何基于Flask框架开发AI模型API管理系统,允许用户添加、删除不同AI模型的API密钥,感兴趣的可以了解下... 目录1. 概述2. 后端代码说明2.1 依赖库导入2.2 应用初始化2.3 API 存储字典2.4 路由函数2.5 应