导航:从零开始手写mmo游戏从框架到爆炸(零)—— 导航-CSDN博客
写了这么多的框架,说好的mmo游戏呢?所以我们暂时按下框架不表,这几篇我们设计英雄角色、怪物、技能和地图。本篇我们来对游戏角色进行设定。
用户在注册成功之后,可以登录,登录好像还没有说,这个不重要,就需要选择存档或者新开游戏,就好像暗黑2一样。存档!好的这个也不重要。我们先说选英雄,英雄就有职业,职业就有技能,英雄升级之后属性要提升。野怪也是一样的,有技能有等级,而且还要区分品质,不如白银怪,黄金怪等等。
英雄或者说职业,野怪等等需要有模板,数据来源一种是存在数据库里面的,一种是存在文件中的。我更倾向于存在文件中,为什么呢,因为我开心。现在我们正式开始职业的设定。
首先我们先创建游戏引擎模块:
定义角色模板
角色模板是英雄和野怪通用的
CharacterTemplate.java
package com.loveprogrammer.factory.template;
import java.io.Serializable;
/**
* @version 1.0.0
* @description: 角色模版
* @author: eric
* @date: 2024-02-7 13:54
**/
public class CharacterTemplate implements Serializable {
private int id;
private String name;
private String desc;
private int strength; // 力量 影响物理输出 物理技能输出
private int armature; // 护甲值 影响物理防御和法术防御
private int constitution; // 体质 影响生命值 一点体质增加10点生命值
private int magic; // 魔力 影响法术输出 法术技能输出
private int technique; // 技巧 影响闪避率、暴击率
private int speed; // 攻击速度
private int levelUpStrength; // 每升一级力量增加值
private int levelUpArmature; // 每升一级护甲增加值
private int levelUpConstitution; // 每升一级生命力增加值
private int levelUpMagic; // 每升一级魔法增加值
private int levelUpTechnique; // 每升一级技巧增加值
private int levelUpSpeed; // 每升一级速度增加值
private int highestLevel; // 转职前的最高级别
private String skills; // 技能id字符串,逗号隔开
// 技能品级
private String skillQuality; // 技能品级
private String acquiredLevel; // 技能习得的等级
private String extSkills;
private int attackType; // 是物理攻击还是魔法攻击
private String states; // 附带状态,比如眩晕免疫,毒免疫等
// 毒抗
private int poisonResistance;
// 火抗
private int flameResistance;
// 电抗
private int thunderResistance;
// 冰抗
private int iceResistance;
// 经验
private double exp = 1.0;
// 武器类型
private int weaponType;
// 可转职的职业
private String nextLevelJobList;
// 战斗位置,数字越小越靠近前排
private int position;
@Override
public String toString() {
return "HeroTemplate{" +
"角色 ='" + name + '\'' +
", 说明='" + desc + '\'' +
", 力量=" + strength +
", 护甲=" + armature +
", 体力=" + constitution +
", 魔力=" + magic +
", 技巧=" + technique +
", 速度=" + speed +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public int getStrength() {
return strength;
}
public void setStrength(int strength) {
this.strength = strength;
}
public int getArmature() {
return armature;
}
public void setArmature(int armature) {
this.armature = armature;
}
public int getConstitution() {
return constitution;
}
public void setConstitution(int constitution) {
this.constitution = constitution;
}
public int getMagic() {
return magic;
}
public void setMagic(int magic) {
this.magic = magic;
}
public int getTechnique() {
return technique;
}
public void setTechnique(int technique) {
this.technique = technique;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getLevelUpStrength() {
return levelUpStrength;
}
public void setLevelUpStrength(int levelUpStrength) {
this.levelUpStrength = levelUpStrength;
}
public int getLevelUpArmature() {
return levelUpArmature;
}
public void setLevelUpArmature(int levelUpArmature) {
this.levelUpArmature = levelUpArmature;
}
public int getLevelUpConstitution() {
return levelUpConstitution;
}
public void setLevelUpConstitution(int levelUpConstitution) {
this.levelUpConstitution = levelUpConstitution;
}
public int getLevelUpMagic() {
return levelUpMagic;
}
public void setLevelUpMagic(int levelUpMagic) {
this.levelUpMagic = levelUpMagic;
}
public int getLevelUpTechnique() {
return levelUpTechnique;
}
public void setLevelUpTechnique(int levelUpTechnique) {
this.levelUpTechnique = levelUpTechnique;
}
public int getLevelUpSpeed() {
return levelUpSpeed;
}
public void setLevelUpSpeed(int levelUpSpeed) {
this.levelUpSpeed = levelUpSpeed;
}
public String getSkills() {
if(skills == null){
skills = "";
}
return skills;
}
public void setSkills(String skills) {
this.skills = skills;
}
public String getExtSkills() {
return extSkills;
}
public void setExtSkills(String extSkills) {
this.extSkills = extSkills;
}
public int getAttackType() {
return attackType;
}
public void setAttackType(int attackType) {
this.attackType = attackType;
}
public String getStates() {
if(states == null){
states = "";
}
return states;
}
public void setStates(String states) {
this.states = states;
}
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 double getExp() {
return exp;
}
public void setExp(double exp) {
this.exp = exp;
}
public int getIceResistance() {
return iceResistance;
}
public void setIceResistance(int iceResistance) {
this.iceResistance = iceResistance;
}
public int getWeaponType() {
return weaponType;
}
public void setWeaponType(int weaponType) {
this.weaponType = weaponType;
}
public String getAcquiredLevel() {
return acquiredLevel;
}
public void setAcquiredLevel(String acquiredLevel) {
this.acquiredLevel = acquiredLevel;
}
public String getSkillQuality() {
return skillQuality;
}
public void setSkillQuality(String skillQuality) {
this.skillQuality = skillQuality;
}
public int getHighestLevel() {
return highestLevel;
}
public void setHighestLevel(int highestLevel) {
this.highestLevel = highestLevel;
}
public String getNextLevelJobList() {
return nextLevelJobList;
}
public void setNextLevelJobList(String nextLevelJobList) {
this.nextLevelJobList = nextLevelJobList;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
}
职业模板集成通用模板。JobTemplate中的newSkills是后期做转职的时候用的,先忽略。
JobTemplate.java
package com.loveprogrammer.factory.template;
/**
* @ClassName JobTemplate
* @Description 职业模板
* @Author admin
* @Date 2024-02-7 13:54
* @Version 1.0
*/
public class JobTemplate extends CharacterTemplate{
private String newSkills;
public String getNewSkills() {
return newSkills;
}
public void setNewSkills(String newSkills) {
this.newSkills = newSkills;
}
}
创建模板文件
job.json
[
{
"id":1,
"name":"战士",
"strength":60,
"magic":0,
"constitution":80,
"technique":15,
"speed":130,
"armature":5,
"levelUpStrength":45,
"levelUpMagic":0,
"levelUpConstitution":35,
"levelUpTechnique":25,
"levelUpSpeed":25,
"levelUpArmature":15,
"attackType":0,
"weaponType":1,
"totalA":230,
"totalB":152.5,
"skills":"1,11,19,32",
"newSkills":"1,11,19,32",
"skillQuality":"E,E,E,E",
"acquiredLevel":"0,5,10,15",
"extSkills":0,
"nextLevelJobList":100101,
"position":1,
"desc":"剑类武器",
"highestLevel":15,
"init":0
}
]
创建职业工厂
JobFactory
package com.loveprogrammer.factory;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.loveprogrammer.factory.template.JobTemplate;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @ClassName JobFactory
* @Description 英雄职业工厂
* @Author admin
* @Date 2024/2/7 15:49
* @Version 1.0
*/
public class JobFactory {
private static List<JobTemplate> templates;
private static Map<Integer,JobTemplate> jobMap;
static {
try {
// 读取配置文件,然后加载到weaponTemplates中
ClassLoader classLoader = JobFactory.class.getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("template/job.json");
assert inputStream != null;
BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null)
responseStrBuilder.append(inputStr);
templates = JSONArray.parseArray(responseStrBuilder.toString(),JobTemplate.class);
jobMap = templates.stream().collect(Collectors.toMap(JobTemplate::getId, e ->e));
} catch (IOException e) {
e.printStackTrace();
}
}
public static List<JobTemplate> getTemplates(){
return templates;
}
public static void main(String[] args) {
System.out.println("加载的职业有:" + JSON.toJSONString(templates));
}
}
运行一下工厂的main方法:
加载的职业有:[{"acquiredLevel":"0,5,10,15","armature":5,"attackType":0,"constitution":80,"desc":"剑类武器","exp":1.0,"extSkills":"0","flameResistance":0,"highestLevel":15,"iceResistance":0,"id":1,"levelUpArmature":15,"levelUpConstitution":35,"levelUpMagic":0,"levelUpSpeed":25,"levelUpStrength":45,"levelUpTechnique":25,"magic":0,"name":"战士","newSkills":"1,11,19,32","nextLevelJobList":"100101","poisonResistance":0,"position":1,"skillQuality":"E,E,E,E","skills":"1,11,19,32","speed":130,"states":"","strength":60,"technique":15,"thunderResistance":0,"weaponType":1}]
这样我们就有了职业的模板了。
全部源码详见:
gitee : eternity-online: 多人在线mmo游戏 - Gitee.com
分支:step-08