本文主要是介绍从零开始手写mmo游戏从框架到爆炸(十三)— 地图与野怪模板,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
导航:从零开始手写mmo游戏从框架到爆炸(零)—— 导航-CSDN博客
首先祝大家龙年大吉,大吉大利,财源滚滚,事事如意!
角色有了,那么基本的功能应该包括选择地图,打怪,掉落装备,升级等等,我们一一来完成。首先我们设计野怪和地图。
野怪模板
和上一章一样,我们首先创建野怪的模板-MonsterTemplate。
/*** @version 1.0.0* @description: 野怪模版* @author: eric* @date: 2024-02-18 13:50**/
public class MonsterTemplate extends CharacterTemplate{private int quality; // 作为野怪的级别 普通 进阶 史诗 传奇 神话public int getQuality() {return quality;}public void setQuality(int quality) {this.quality = quality;}
}
我们看到野怪有了品质的划分,野怪根据品质可以分为普通-进阶-史诗-传奇-神话。这点在后面创建野怪的时候可以看到效果。
创建怪物模板json,有些参数是后面慢慢需要用到的,大家主要关注的就是基本属性和升级属性。
[{"id":101,"name":"地精战士","strength":15,"armature":10,"constitution":15,"magic":0,"technique":0,"speed":10,"quality":1,"levelUpStrength":3,"levelUpArmature":1,"levelUpConstitution":3,"levelUpMagic":0,"levelUpTechnique":3,"levelUpSpeed":3,"skills":"","skillQuality":"E","extSkills":"","attackType":0,"sum":12,"init":55,"poisonResistance":5,"flameResistance":5,"thunderResistance":5,"iceResistance":5,"exp":1.1,"position":1},{"id":120,"name":"地精头目","strength":25,"armature":20,"constitution":25,"magic":0,"technique":0,"speed":15,"quality":2,"levelUpStrength":5,"levelUpArmature":2,"levelUpConstitution":6,"levelUpMagic":0,"levelUpTechnique":6,"levelUpSpeed":5,"skills":"","skillQuality":"E","extSkills":"","attackType":0,"sum":22.5,"init":97.5,"poisonResistance":5,"flameResistance":5,"thunderResistance":5,"iceResistance":5,"exp":1.1,"position":1},{"id":140,"name":"地精精英","strength":45,"armature":30,"constitution":120,"magic":10,"technique":10,"speed":20,"quality":3,"levelUpStrength":10,"levelUpArmature":3,"levelUpConstitution":12,"levelUpMagic":0,"levelUpTechnique":8,"levelUpSpeed":8,"skills":"","skillQuality":"E","extSkills":"","attackType":0,"sum":38.5,"init":255,"poisonResistance":5,"flameResistance":5,"thunderResistance":5,"iceResistance":5,"exp":1.1,"position":1}
]
创建怪物工厂-MonsterFactory
package com.loveprogrammer.factory;import com.alibaba.fastjson2.JSONArray;
import com.loveprogrammer.factory.template.MonsterTemplate;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;/*** @ClassName MonsterFactory* @Description 怪物工厂* @Author admin* @Date 2024/2/18 9:53* @Version 1.0*/
public class MonsterFactory {private static List<MonsterTemplate> monsterTemplates;private static Map<Integer,MonsterTemplate> monsterTemplateMap;// 初始化野怪模版static {try {// 读取配置文件,然后加载到weaponTemplates中ClassLoader classLoader = MonsterFactory.class.getClassLoader();InputStream inputStream = classLoader.getResourceAsStream("template/monster.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);monsterTemplates = JSONArray.parseArray(responseStrBuilder.toString(), MonsterTemplate.class);monsterTemplateMap = monsterTemplates.stream().collect(Collectors.toMap(MonsterTemplate::getId, e->e));} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {for (MonsterTemplate monsterTemplate : monsterTemplates) {System.out.println(monsterTemplate.toString());}}
}
运行怪物工厂的main方法可以看到已经载入了怪物模板
CharacterTemplate{角色 ='地精战士', 说明='null', 力量=15, 护甲=10, 体力=15, 魔力=0, 技巧=0, 速度=10}
CharacterTemplate{角色 ='地精头目', 说明='null', 力量=25, 护甲=20, 体力=25, 魔力=0, 技巧=0, 速度=15}
CharacterTemplate{角色 ='地精精英', 说明='null', 力量=45, 护甲=30, 体力=120, 魔力=10, 技巧=10, 速度=20}
地图
地图要包括野怪的id,等级以及每次和玩家打架的数量。同时我们为了后续扩展,还增加普通野怪和精英野怪的区分。地图的模板代码如下,大家可以自行查看-MapTemplate
package com.loveprogrammer.factory.template;import java.io.Serializable;
import java.util.List;/*** @version 1.0.0* @description: 地图模版* @author: eric* @date: 2024-02-18 11:29**/
public class MapTemplate implements Serializable{private String id;private String name;private int level;private String desc;private List<FloorMap> floors;private List<MonsterData> monsters;private String teamSize;private List<String> npcIds;private List<BossData> bossList;private Integer resistanceReduce;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getLevel() {return level;}public void setLevel(int level) {this.level = level;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc = desc;}public List<MonsterData> getMonsters() {return monsters;}public void setMonsters(List<MonsterData> monsters) {this.monsters = monsters;}@Overridepublic String toString() {return "MapTemplate{" +"id='" + id + '\'' +", 名称='" + name + '\'' +", 级别='" + level + '\'' +", 说明='" + desc + '\'' +'}';}public static class FloorMap implements Serializable {private String id;private String name;private String desc;private List<MonsterData> monsters;private String teamSize;public String getId() {return id;}public void setId(String 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 List<MonsterData> getMonsters() {return monsters;}public void setMonsters(List<MonsterData> monsters) {this.monsters = monsters;}public String getTeamSize() {return teamSize;}public void setTeamSize(String teamSize) {this.teamSize = teamSize;}}public static class MonsterData implements Serializable {// 出现的概率private int percents;// 级别最高private int levelMin;// 级别最低private int levelMax;//private int monsterId;// 掉落的列表private List<Drop> dropList;public int getPercents() {return percents;}public void setPercents(int percents) {this.percents = percents;}public int getLevelMin() {return levelMin;}public void setLevelMin(int levelMin) {this.levelMin = levelMin;}public int getLevelMax() {return levelMax;}public void setLevelMax(int levelMax) {this.levelMax = levelMax;}public int getMonsterId() {return monsterId;}public void setMonsterId(int monsterId) {this.monsterId = monsterId;}public List<Drop> getDropList() {return dropList;}public void setDropList(List<Drop> dropList) {this.dropList = dropList;}}public static class BossData implements Serializable {// 级别private int level;//private int monsterId;private List<String> teamMembers;// 掉落的列表private List<Drop> dropList;public int getLevel() {return level;}public void setLevel(int level) {this.level = level;}public int getMonsterId() {return monsterId;}public void setMonsterId(int monsterId) {this.monsterId = monsterId;}public List<String> getTeamMembers() {return teamMembers;}public void setTeamMembers(List<String> teamMembers) {this.teamMembers = teamMembers;}public List<Drop> getDropList() {return dropList;}public void setDropList(List<Drop> dropList) {this.dropList = dropList;}}public static class Drop implements Serializable {// 出现的概率private int percents;//private int goodsId;private String goodsType;public int getPercents() {return percents;}public void setPercents(int percents) {this.percents = percents;}public int getGoodsId() {return goodsId;}public void setGoodsId(int goodsId) {this.goodsId = goodsId;}public String getGoodsType() {return goodsType;}public void setGoodsType(String goodsType) {this.goodsType = goodsType;}}public String getTeamSize() {return teamSize;}public void setTeamSize(String teamSize) {this.teamSize = teamSize;}public List<String> getNpcIds() {return npcIds;}public void setNpcIds(List<String> npcIds) {this.npcIds = npcIds;}public List<BossData> getBossList() {return bossList;}public void setBossList(List<BossData> bossList) {this.bossList = bossList;}public List<FloorMap> getFloors() {return floors;}public void setFloors(List<FloorMap> floors) {this.floors = floors;}public Integer getResistanceReduce() {return resistanceReduce;}public void setResistanceReduce(Integer resistanceReduce) {this.resistanceReduce = resistanceReduce;}
}
我们创建一个地图的json
[{"id": 1,"name": "新手村","level": 1,"desc": "新手练级的地方,适合等级1-10级","teamSize": "2-3","monsters": [{"percents": 45,"levelMin": 1,"levelMax": 3,"monsterId": 101},{"percents": 45,"levelMin": 4,"levelMax": 6,"monsterId": 120},{"percents": 10,"levelMin": 7,"levelMax": 9,"monsterId": 140},{"percents": 10,"levelMin": 5,"levelMax": 9,"monsterId": 141}],"npcIds": [],"bossList": []}
]
同样我们来看下地图工厂的代码
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.loveprogrammer.factory.template.MapTemplate;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;/*** @version 1.0.0* @description: 地图工厂* @author: eric* @date: 2022-08-08 16:44**/
public class MapFactory {private static List<MapTemplate> mapTemplates;// 初始化地图模版static {try {// 读取配置文件,然后加载到weaponTemplates中ClassLoader classLoader = MapFactory.class.getClassLoader();InputStream inputStream = classLoader.getResourceAsStream("template/map.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);mapTemplates = JSONArray.parseArray(responseStrBuilder.toString(), MapTemplate.class);} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {for (MapTemplate mapTemplate : mapTemplates) {System.out.println(JSON.toJSONString(mapTemplate));}}
}
全部源码详见:
gitee : eternity-online: 多人在线mmo游戏 - Gitee.com
分支:step-08
这篇关于从零开始手写mmo游戏从框架到爆炸(十三)— 地图与野怪模板的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!