从零开始手写mmo游戏从框架到爆炸(十三)— 地图与野怪模板

本文主要是介绍从零开始手写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游戏从框架到爆炸(十三)— 地图与野怪模板的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/725694

相关文章

无人叉车3d激光slam多房间建图定位异常处理方案-墙体画线地图切分方案

墙体画线地图切分方案 针对问题:墙体两侧特征混淆误匹配,导致建图和定位偏差,表现为过门跳变、外月台走歪等 ·解决思路:预期的根治方案IGICP需要较长时间完成上线,先使用切分地图的工程化方案,即墙体两侧切分为不同地图,在某一侧只使用该侧地图进行定位 方案思路 切分原理:切分地图基于关键帧位置,而非点云。 理论基础:光照是直线的,一帧点云必定只能照射到墙的一侧,无法同时照到两侧实践考虑:关

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

C++11第三弹:lambda表达式 | 新的类功能 | 模板的可变参数

🌈个人主页: 南桥几晴秋 🌈C++专栏: 南桥谈C++ 🌈C语言专栏: C语言学习系列 🌈Linux学习专栏: 南桥谈Linux 🌈数据结构学习专栏: 数据结构杂谈 🌈数据库学习专栏: 南桥谈MySQL 🌈Qt学习专栏: 南桥谈Qt 🌈菜鸡代码练习: 练习随想记录 🌈git学习: 南桥谈Git 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈�

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

uva 1342 欧拉定理(计算几何模板)

题意: 给几个点,把这几个点用直线连起来,求这些直线把平面分成了几个。 解析: 欧拉定理: 顶点数 + 面数 - 边数= 2。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#inc

uva 11178 计算集合模板题

题意: 求三角形行三个角三等分点射线交出的内三角形坐标。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <

poj 2104 and hdu 2665 划分树模板入门题

题意: 给一个数组n(1e5)个数,给一个范围(fr, to, k),求这个范围中第k大的数。 解析: 划分树入门。 bing神的模板。 坑爹的地方是把-l 看成了-1........ 一直re。 代码: poj 2104: #include <iostream>#include <cstdio>#include <cstdlib>#include <al

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

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

最大流、 最小费用最大流终极版模板

最大流  const int inf = 1000000000 ;const int maxn = 20000 , maxm = 500000 ;struct Edge{int v , f ,next ;Edge(){}Edge(int _v , int _f , int _next):v(_v) ,f(_f),next(_next){}};int sourse , mee

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

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