从零开始手写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;                         // 角色id
    protected 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 法术攻击 1
    private 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;
    }

    @Override
    public 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;

    // TODO
    private 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;
    }

    @Override
    public 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

        

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值