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

相关文章

GSON框架下将百度天气JSON数据转JavaBean

《GSON框架下将百度天气JSON数据转JavaBean》这篇文章主要为大家详细介绍了如何在GSON框架下实现将百度天气JSON数据转JavaBean,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言一、百度天气jsON1、请求参数2、返回参数3、属性映射二、GSON属性映射实战1、类对象映

Python利用GeoPandas打造一个交互式中国地图选择器

《Python利用GeoPandas打造一个交互式中国地图选择器》在数据分析和可视化领域,地图是展示地理信息的强大工具,被将使用Python、wxPython和GeoPandas构建的交互式中国地图行... 目录技术栈概览代码结构分析1. __init__ 方法:初始化与状态管理2. init_ui 方法:

解决若依微服务框架启动报错的问题

《解决若依微服务框架启动报错的问题》Invalidboundstatement错误通常由MyBatis映射文件未正确加载或Nacos配置未读取导致,需检查XML的namespace与方法ID是否匹配,... 目录ruoyi-system模块报错报错详情nacos文件目录总结ruoyi-systnGLNYpe

Python38个游戏开发库整理汇总

《Python38个游戏开发库整理汇总》文章介绍了多种Python游戏开发库,涵盖2D/3D游戏开发、多人游戏框架及视觉小说引擎,适合不同需求的开发者入门,强调跨平台支持与易用性,并鼓励读者交流反馈以... 目录PyGameCocos2dPySoyPyOgrepygletPanda3DBlenderFife

Python Web框架Flask、Streamlit、FastAPI示例详解

《PythonWeb框架Flask、Streamlit、FastAPI示例详解》本文对比分析了Flask、Streamlit和FastAPI三大PythonWeb框架:Flask轻量灵活适合传统应用... 目录概述Flask详解Flask简介安装和基础配置核心概念路由和视图模板系统数据库集成实际示例Stre

游戏闪退弹窗提示找不到storm.dll文件怎么办? Stormdll文件损坏修复技巧

《游戏闪退弹窗提示找不到storm.dll文件怎么办?Stormdll文件损坏修复技巧》DLL文件丢失或损坏会导致软件无法正常运行,例如我们在电脑上运行软件或游戏时会得到以下提示:storm.dll... 很多玩家在打开游戏时,突然弹出“找不到storm.dll文件”的提示框,随后游戏直接闪退,这通常是由于

Olingo分析和实践之OData框架核心组件初始化(关键步骤)

《Olingo分析和实践之OData框架核心组件初始化(关键步骤)》ODataSpringBootService通过初始化OData实例和服务元数据,构建框架核心能力与数据模型结构,实现序列化、URI... 目录概述第一步:OData实例创建1.1 OData.newInstance() 详细分析1.1.1

SpringBoot集成EasyPoi实现Excel模板导出成PDF文件

《SpringBoot集成EasyPoi实现Excel模板导出成PDF文件》在日常工作中,我们经常需要将数据导出成Excel表格或PDF文件,本文将介绍如何在SpringBoot项目中集成EasyPo... 目录前言摘要简介源代码解析应用场景案例优缺点分析类代码方法介绍测试用例小结前言在日常工作中,我们经

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

Python的端到端测试框架SeleniumBase使用解读

《Python的端到端测试框架SeleniumBase使用解读》:本文主要介绍Python的端到端测试框架SeleniumBase使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全... 目录SeleniumBase详细介绍及用法指南什么是 SeleniumBase?SeleniumBase