从零开始手写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

相关文章

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 应

Python GUI框架中的PyQt详解

《PythonGUI框架中的PyQt详解》PyQt是Python语言中最强大且广泛应用的GUI框架之一,基于Qt库的Python绑定实现,本文将深入解析PyQt的核心模块,并通过代码示例展示其应用场... 目录一、PyQt核心模块概览二、核心模块详解与示例1. QtCore - 核心基础模块2. QtWid

使用PyTorch实现手写数字识别功能

《使用PyTorch实现手写数字识别功能》在人工智能的世界里,计算机视觉是最具魅力的领域之一,通过PyTorch这一强大的深度学习框架,我们将在经典的MNIST数据集上,见证一个神经网络从零开始学会识... 目录当计算机学会“看”数字搭建开发环境MNIST数据集解析1. 认识手写数字数据库2. 数据预处理的

最新Spring Security实战教程之Spring Security安全框架指南

《最新SpringSecurity实战教程之SpringSecurity安全框架指南》SpringSecurity是Spring生态系统中的核心组件,提供认证、授权和防护机制,以保护应用免受各种安... 目录前言什么是Spring Security?同类框架对比Spring Security典型应用场景传统

Python结合Flask框架构建一个简易的远程控制系统

《Python结合Flask框架构建一个简易的远程控制系统》这篇文章主要为大家详细介绍了如何使用Python与Flask框架构建一个简易的远程控制系统,能够远程执行操作命令(如关机、重启、锁屏等),还... 目录1.概述2.功能使用系统命令执行实时屏幕监控3. BUG修复过程1. Authorization

SpringBoot集成图片验证码框架easy-captcha的详细过程

《SpringBoot集成图片验证码框架easy-captcha的详细过程》本文介绍了如何将Easy-Captcha框架集成到SpringBoot项目中,实现图片验证码功能,Easy-Captcha是... 目录SpringBoot集成图片验证码框架easy-captcha一、引言二、依赖三、代码1. Ea

Gin框架中的GET和POST表单处理的实现

《Gin框架中的GET和POST表单处理的实现》Gin框架提供了简单而强大的机制来处理GET和POST表单提交的数据,通过c.Query、c.PostForm、c.Bind和c.Request.For... 目录一、GET表单处理二、POST表单处理1. 使用c.PostForm获取表单字段:2. 绑定到结

修改若依框架Token的过期时间问题

《修改若依框架Token的过期时间问题》本文介绍了如何修改若依框架中Token的过期时间,通过修改`application.yml`文件中的配置来实现,默认单位为分钟,希望此经验对大家有所帮助,也欢迎... 目录修改若依框架Token的过期时间修改Token的过期时间关闭Token的过期时js间总结修改若依

MyBatis框架实现一个简单的数据查询操作

《MyBatis框架实现一个简单的数据查询操作》本文介绍了MyBatis框架下进行数据查询操作的详细步骤,括创建实体类、编写SQL标签、配置Mapper、开启驼峰命名映射以及执行SQL语句等,感兴趣的... 基于在前面几章我们已经学习了对MyBATis进行环境配置,并利用SqlSessionFactory核