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

相关文章

cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个?

跨平台系列 cross-plateform 跨平台应用程序-01-概览 cross-plateform 跨平台应用程序-02-有哪些主流技术栈? cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个? cross-plateform 跨平台应用程序-04-React Native 介绍 cross-plateform 跨平台应用程序-05-Flutte

国产游戏崛起:技术革新与文化自信的双重推动

近年来,国产游戏行业发展迅猛,技术水平和作品质量均得到了显著提升。特别是以《黑神话:悟空》为代表的一系列优秀作品,成功打破了过去中国游戏市场以手游和网游为主的局限,向全球玩家展示了中国在单机游戏领域的实力与潜力。随着中国开发者在画面渲染、物理引擎、AI 技术和服务器架构等方面取得了显著进展,国产游戏正逐步赢得国际市场的认可。然而,面对全球游戏行业的激烈竞争,国产游戏技术依然面临诸多挑战,未来的

Spring框架5 - 容器的扩展功能 (ApplicationContext)

private static ApplicationContext applicationContext;static {applicationContext = new ClassPathXmlApplicationContext("bean.xml");} BeanFactory的功能扩展类ApplicationContext进行深度的分析。ApplicationConext与 BeanF

数据治理框架-ISO数据治理标准

引言 "数据治理"并不是一个新的概念,国内外有很多组织专注于数据治理理论和实践的研究。目前国际上,主要的数据治理框架有ISO数据治理标准、GDI数据治理框架、DAMA数据治理管理框架等。 ISO数据治理标准 改标准阐述了数据治理的标准、基本原则和数据治理模型,是一套完整的数据治理方法论。 ISO/IEC 38505标准的数据治理方法论的核心内容如下: 数据治理的目标:促进组织高效、合理地

ZooKeeper 中的 Curator 框架解析

Apache ZooKeeper 是一个为分布式应用提供一致性服务的软件。它提供了诸如配置管理、分布式同步、组服务等功能。在使用 ZooKeeper 时,Curator 是一个非常流行的客户端库,它简化了 ZooKeeper 的使用,提供了高级的抽象和丰富的工具。本文将详细介绍 Curator 框架,包括它的设计哲学、核心组件以及如何使用 Curator 来简化 ZooKeeper 的操作。 1

【Kubernetes】K8s 的安全框架和用户认证

K8s 的安全框架和用户认证 1.Kubernetes 的安全框架1.1 认证:Authentication1.2 鉴权:Authorization1.3 准入控制:Admission Control 2.Kubernetes 的用户认证2.1 Kubernetes 的用户认证方式2.2 配置 Kubernetes 集群使用密码认证 Kubernetes 作为一个分布式的虚拟

Spring Framework系统框架

序号表示的是学习顺序 IoC(控制反转)/DI(依赖注入): ioc:思想上是控制反转,spring提供了一个容器,称为IOC容器,用它来充当IOC思想中的外部。 我的理解就是spring把这些对象集中管理,放在容器中,这个容器就叫Ioc这些对象统称为Bean 用对象的时候不用new,直接外部提供(bean) 当外部的对象有关系的时候,IOC给它俩绑好(DI) DI和IO

Sentinel 高可用流量管理框架

Sentinel 是面向分布式服务架构的高可用流量防护组件,主要以流量为切入点,从限流、流量整形、熔断降级、系统负载保护、热点防护等多个维度来帮助开发者保障微服务的稳定性。 Sentinel 具有以下特性: 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应

火柴游戏java版

代码 /*** 火柴游戏* <p>* <li>有24根火柴</li>* <li>组成 A + B = C 等式</li>* <li>总共有多少种适合方式?</li>* <br>* <h>分析:</h>* <li>除去"+"、"="四根,最多可用火柴根数20根。</li>* <li>全部用两根组合成"1",最大数值为1111。使用枚举法,A和B范围在0~1111,C为A+B。判断</li>** @

生信代码入门:从零开始掌握生物信息学编程技能

少走弯路,高效分析;了解生信云,访问 【生信圆桌x生信专用云服务器】 : www.tebteb.cc 介绍 生物信息学是一个高度跨学科的领域,结合了生物学、计算机科学和统计学。随着高通量测序技术的发展,海量的生物数据需要通过编程来进行处理和分析。因此,掌握生信编程技能,成为每一个生物信息学研究者的必备能力。 生信代码入门,旨在帮助初学者从零开始学习生物信息学中的编程基础。通过学习常用