从零开始手写mmo游戏从框架到爆炸(二十三)— 装备系统一

导航:从零开始手写mmo游戏从框架到爆炸(零)—— 导航-CSDN博客

目录

装备模板

装备模型

装备模板配置

加载装备模板


        下一步,就是要考虑经验、金币、和装备掉落的问题。经验金币都好说,装备系统是目前需要考虑的问题。

       为了简化我们把装备分成武器、胸甲两个部位,后期可以慢慢扩展。此时我们还是需要装备

模板。这个功能和之前的野怪模板、地图模板差不多。

装备模板

        物品模板类- GoodsTemplate :

@Data
public class GoodsTemplate implements Serializable {private int id;private String name;private String type;private int level;private String desc;
}

       装备模板类:

/*** @version 1.0.0* @description: 装备的模板类* @author: eric* @date: 2024-02-23 10:33**/
@Data
public class EquipmentTemplate extends GoodsTemplate {private String strength;                  // 力量 影响物理输出 物理技能输出private String armature;                 // 护甲值 影响物理防御和法术防御private String constitution;               // 体质 影响生命值 一点体质增加10点生命值private String magic;                       // 魔力 影响法术输出 法术技能输出private String technique;                   // 技巧 影响闪避率、暴击率private String speed;                         // 攻击速度private String hp;                            // 生命值private String evasion;private String fortune;private int weaponType;private int suitId;// 毒抗private String poisonResistance;// 火抗private String flameResistance;// 电抗private String thunderResistance;// 冰抗private String iceResistance;// 特殊效果private String effects;@Overridepublic String toString() {return "EquipmentTemplate{" +"id='" + getId() + '\'' +", name='" + getName() + '\'' +", type='" + getType() + '\'' +", level=" + getLevel() +", desc='" + getDesc() + '\'' +", strength=" + strength +", armature=" + armature +", constitution=" + constitution +", magic=" + magic +", technique=" + technique +", speed=" + speed +", hp=" + hp +", evasion='" + evasion + '\'' +", fortune='" + fortune + '\'' +", weaponType=" + weaponType +'}';}
}

装备模型

          装备是物品的一种,物品应该包括装备、耗材、任务物品、宠物蛋等等。

          所以首先定义一个物品父类 Goods:

@Data
public abstract class Goods implements Serializable {private String id;private Integer goods_id = 0;private String name;private String type;private int level;private String desc;public  String string ="\t\t\t\t\t\t\t\t\t";// 品质public QualityEnum quality;// 毒抗private int poisonResistance;// 火抗private int flameResistance;// 电抗private int thunderResistance;// 冰抗private int iceResistance;public Goods(){}// 没有等级的物品public Goods(String name, String type, String desc, QualityEnum quality, int goodsId) {this.name = name;this.type = type;this.desc = desc;this.quality = quality;this.goods_id = goodsId;this.id = UUID.randomUUID().toString().replace("-","");}// 有等级的物品public Goods(String name, String type, String desc, QualityEnum quality, int level,int poisonResistance, int flameResistance, int thunderResistance, int iceResistance,int goodsId) {this(name,type,desc,quality,goodsId);this.level = level;this.poisonResistance = poisonResistance;this.flameResistance = flameResistance;this.thunderResistance = thunderResistance;this.iceResistance = iceResistance;}// 物品打印信息的重写public String toString() {return  string+"****物品信息****\n"+string+"名称:"+this.name+"\n"+string+"类型:"+this.type +"\n"+string+"说明:"+this.desc +"\n"+string+""+"***************";}// 物品打印信息的重写public String prettyPrint() {return  string+"****物品信息****\n"+string+"名称:"+ this.name+"\n"+string+"品质:"+ this.quality.getDesc() +"\n"+string+"说明:"+ this.desc +"\n"+string+""+"***************";}
}

        接着对装备进行定义,装备就要有位置,武器就要有类型,这里我们定义两个枚举类 EquipmentEnum 和 WeaponTypeEnum

/*** 装备的位置** @author eric* @version 1.0.0*/
public enum EquipmentEnum {WEAPON("weapon", "武器", 1),SHIELD("shield", "盾牌",2),SHOES("shoes", "鞋子", 3),HELMET("helmet", "头盔", 4),GLOVES("gloves", "手套", 5),NECKLACE("necklace", "项链", 6),TALISMAN("talisman", "护身符", 7),LEFTRING("leftRing", "左戒", 8),RIGHTRING("rightRing", "右戒", 9),LEGARMOR("legArmor", "腿甲", 10),BELT("belt", "腰带", 11),BARCER("barcer", "护臂", 12),SHOULDER("shoulder", "肩甲", 13),BREASTPLATE("breastplate", "胸甲",14),UNKOWN("unkown", "其他", 15);public final static int total = 14;private String code;private String desc;private int posNum;EquipmentEnum(String code, String desc, int posNum) {this.code = code;this.desc = desc;this.posNum = posNum;}public static EquipmentEnum getByCode(String code){Optional<EquipmentEnum> optional =Arrays.stream(EquipmentEnum.values()).filter(v -> Objects.equals(v.getCode(), code)).findFirst();return optional.orElse(EquipmentEnum.UNKOWN);}public static EquipmentEnum getByPosNum(int posNum){Optional<EquipmentEnum> optional =Arrays.stream(EquipmentEnum.values()).filter(v -> v.getPosNum() == posNum).findFirst();return optional.orElse(EquipmentEnum.UNKOWN);}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 int getPosNum() {return posNum;}
}
/*** 武器类型** @author eric* @version 1.0.0*/
public enum WeaponTypeEnum {SWORD(1, "长剑"),WAND(2, "魔杖"),DAGGER(3, "匕首"),GUN(4, "枪类"),HAMMER(5, "锤类"),AXE(6, "斧类"),STICK(7, "棍类");private Integer code;private String desc;WeaponTypeEnum(Integer code, String desc) {this.code = code;this.desc = desc;}public static WeaponTypeEnum getByCode(int code){Optional<WeaponTypeEnum> optional =Arrays.stream(WeaponTypeEnum.values()).filter(v -> Objects.equals(v.getCode(), code)).findFirst();return optional.orElse(WeaponTypeEnum.SWORD);}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;}
}

        武器的类型其实很有用处,比如不同的职业只能使用一种武器类型。

       我们来定义装备模型 - Equipment:

@Data
public class Equipment extends Goods {private int suitId;                   // 如果是套装,套装的idprivate int strength;                  // 力量 影响物理输出 物理技能输出private int armature;                 // 护甲值 影响物理防御和法术防御private int constitution;               // 体质 影响生命值 一点体质增加10点生命值private int magic;                       // 魔力 影响法术输出 法术技能输出private int technique;                   // 技巧 影响闪避率、暴击率private int speed;                         // 攻击速度private int hp;                            // 生命值private int evasion;private int fortune;private boolean used = false;   // 是否被使用// 如果constant == true,不能增加前缀,属于固定属性装备private boolean constant = false;// 如果specifyDrop == true,只有特定的副本或者特定的野怪才能掉落private boolean specifyDrop = false;// 武器位置public EquipmentEnum equipmentPosition;// 武器类型private WeaponTypeEnum weaponTypeEnum;private int difference;public Equipment(String name, String type, String desc, QualityEnum quality, int strength,int armature, int constitution, int magic, int technique, int speed, int hp,int evasion, int fortune, boolean used, boolean constant, boolean specifyDrop,EquipmentEnum equipmentPosition, List<String> affixList,WeaponTypeEnum weaponTypeEnum, int suitId, int level, int poisonResistance,int flameResistance, int thunderResistance, int iceResistance,  int goodsId) {this(name, type, desc, quality, strength, armature, constitution, magic, technique, speed,hp, evasion, fortune, used, equipmentPosition, weaponTypeEnum, affixList, level,poisonResistance,flameResistance,thunderResistance,iceResistance,goodsId);this.constant = constant;this.specifyDrop = specifyDrop;this.suitId = suitId;}public Equipment(String name, String type, String desc, QualityEnum quality, int strength,int armature, int constitution, int magic, int technique, int speed, int hp,int evasion, int fortune, boolean used, EquipmentEnum equipmentPosition,WeaponTypeEnum weaponTypeEnum, List<String> affixList, int level,int poisonResistance, int flameResistance, int thunderResistance, int iceResistance,int goodsId) {super(name, type, desc, quality, level,poisonResistance,flameResistance,thunderResistance,iceResistance,goodsId);this.strength = strength;this.armature = armature;this.constitution = constitution;this.magic = magic;this.technique = technique;this.speed = speed;this.hp = hp;this.evasion = evasion;this.fortune = fortune;this.equipmentPosition = equipmentPosition;this.weaponTypeEnum = weaponTypeEnum;this.used = used;if(affixList == null){affixList = new ArrayList<>();}}public Equipment(String name, String type, String desc, EquipmentEnum equipmentEnum,QualityEnum quality, int goodsId) {super(name, type, desc, quality,goodsId);this.equipmentPosition = equipmentEnum;}// 物品打印信息的重写@Overridepublic String toString() {return "*物品信息* " +"名称:" + getName() + " " + "级别:" + getLevel() + " " +"品质:" + this.quality.getColor() + this.quality.getDesc() + ConsoleColors.RESET + " " +(strength > 0 ? ("力量:" + strength + " ") : "") +(armature > 0 ? ("护甲:" + armature + " ") : "") +(constitution > 0 ? ("体力:" + constitution + " ") : "") +(magic > 0 ? ("魔力:" + magic + " ") : "") +(technique > 0 ? ("技巧:" + technique + " ") : "") +(speed > 0 ? ("速度:" + speed + " ") : "") +(getPoisonResistance() > 0 ? ("毒抗:" + getPoisonResistance() + " ") : "") +(getFlameResistance() > 0 ? ("火抗:" + getFlameResistance() + " ") : "") +(getThunderResistance() > 0 ? ("雷抗:" + getThunderResistance() + " ") : "") +(getIceResistance() > 0 ? ("冰抗:" + getIceResistance() + " ") : "") +";";}// 物品打印信息的重写public String prettyPrint() {return string + "****物品信息****\n" +string + "名称:" + getName() + "\n" +string + "品质:" + this.quality.getColor() + this.quality.getDesc() + ConsoleColors.RESET + "\n" +string + "位置:" + Optional.ofNullable(this.getEquipmentPosition()).orElse(EquipmentEnum.UNKOWN).getDesc() + "\n" +string + "说明:" + getDesc() + "\n" +(strength > 0 ? (string + "力量:" + strength + "\n") : "") +(armature > 0 ? (string + "护甲:" + armature + "\n") : "") +(constitution > 0 ? (string + "体力:" + constitution + "\n") : "") +(magic > 0 ? (string + "魔力:" + magic + "\n") : "") +(technique > 0 ? (string + "技巧:" + technique + "\n") : "") +(speed > 0 ? (string + "速度:" + speed + "\n") : "") +(getPoisonResistance() > 0 ? (string + "毒抗:" + getPoisonResistance() + "\n") : "") +(getFlameResistance() > 0 ? (string + "火抗:" + getFlameResistance() + "\n") : "") +(getThunderResistance() > 0 ? (string + "雷抗:" + getThunderResistance() + "\n") : "") +(getIceResistance() > 0 ? (string + "冰抗:" + getIceResistance() + "\n") : "") +string + "" + "***************";}}

针对武器,我们再定义武器的模型:

public class Weapon extends Equipment {public Weapon(String name, String type, String desc, EquipmentEnum equipmentEnum,WeaponTypeEnum weaponTypeEnum,QualityEnum quality, int goodsId) {super(name, type, desc, equipmentEnum, quality,goodsId);this.setWeaponTypeEnum(weaponTypeEnum);}}

装备模板配置

       在resource路径下补充装备的json,我们不同部位的装备,使用不同的json来区分。

内容如下:

 weapon.json

[{"id":1,"name":"木剑","type":"weapon","level":1,"desc":"最普通的剑,无任何附加属性","strength":"1-10","armature":"0-0","constitution":"0-0","technique":"0-0","speed":"0-0","hp":"0-0","magic":"0-0","evasion":"0-0","fortune":"0-0","weaponType":1,"total":10},{"id":10,"name":"短剑","type":"weapon","level":5,"desc":"最普通的剑,无任何附加属性","strength":"10-20","armature":"0-0","constitution":"10-20","technique":"10-20","speed":"0-0","hp":"0-0","magic":"0-0","evasion":"0-0","fortune":"0-0","weaponType":1,"total":"60"}
]

breastplate.json

[{"id":301,"name":"布甲","type":"breastplate","level": 1,"desc":"布甲","strength": "0-0","armature": "5-10","constitution": "5-20","technique": "0-0","speed": "0-0","hp": "0-0","magic": "0-0","evasion": "0-0","fortune": "0-0"}
]

加载装备模板

        我们创建一个装备工厂,用于加载装备模板,同时生产装备 - EquipmentFactory

/*** @version 1.0.0* @description: 装备工厂* @author: eric* @date: 2023-02-23 10:34**/
public class EquipmentFactory {private static List<EquipmentTemplate> allEquipmentTemplates;private static List<EquipmentTemplate> weaponTemplates;private static Map<Integer,EquipmentTemplate> weaponTemplateIdMap;static {try {// 读取配置文件,然后加载到weaponTemplates中ClassLoader classLoader = EquipmentFactory.class.getClassLoader();InputStream inputStream = classLoader.getResourceAsStream("template/equipments/weapon.json");BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));StringBuilder responseStrBuilder = new StringBuilder();String inputStr;while ((inputStr = streamReader.readLine()) != null)responseStrBuilder.append(inputStr);weaponTemplates = JSONArray.parseArray(responseStrBuilder.toString(),EquipmentTemplate.class);weaponTemplateIdMap = weaponTemplates.stream().collect(Collectors.toMap(EquipmentTemplate::getId, e->e));if(allEquipmentTemplates == null){allEquipmentTemplates = new ArrayList<>();}allEquipmentTemplates.addAll(weaponTemplates);// 加载其他的普通装备String path = classLoader.getResource("template/equipments").getFile();//注意getResource("")里面是空字符串File file = new File(path);File[] files = file.listFiles();for (File f : files) {if(f.getName().equals("weapon.json")){continue;}String fileName = "template/equipments/" + f.getName();inputStream = classLoader.getResourceAsStream(fileName);streamReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));responseStrBuilder = new StringBuilder();String str;while ((str = streamReader.readLine()) != null)responseStrBuilder.append(str);List<EquipmentTemplate> equipmentTemplates = JSONArray.parseArray(responseStrBuilder.toString(),EquipmentTemplate.class);allEquipmentTemplates.addAll(equipmentTemplates);}} catch (IOException e) {e.printStackTrace();} finally {}}public static List<EquipmentTemplate> getWeaponTemplates() {return weaponTemplates;}public static void main(String[] args) {for (EquipmentTemplate template : allEquipmentTemplates) {System.out.println(template.toString());}}}

        好,下一章我们来完成装备的生成

全部源码详见:

gitee : eternity-online: 多人在线mmo游戏 - Gitee.com

分支:step-12

请各位帅哥靓女帮忙去gitee上点个星星,谢谢! 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/493604.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

LeetCode_Java_动态规划系列(1)(题目+思路+代码)

目录 斐波那契类型 746.使用最小花费爬楼梯 矩阵 120. 三角形最小路径和 斐波那契类型 746.使用最小花费爬楼梯 给你一个整数数组 cost &#xff0c;其中 cost[i] 是从楼梯第 i 个台阶向上爬需要支付的费用。一旦你支付此费用&#xff0c;即可选择向上爬一个或者两个台阶。…

Pyglet控件的批处理参数batch和分组参数group简析

先来复习一下之前写的两个例程&#xff1a; 1. 绘制网格线 import pygletwindow pyglet.window.Window(800, 600) color (255, 255, 255, 255) # 白色 lines []for y in range(0, window.height, 40):lines.append(pyglet.shapes.Line(0, y, window.width, y, colorcolo…

【AIGC大模型】跑通wonder3D (windows)

这两天看了AI大神李某舟被封杀&#xff0c;课程被下架的新闻&#xff0c;TU商 认为&#xff1a;现在这种玩概念、徒具高大上外表却无实质内容的东西太多了&#xff0c;已经形成一种趋势和风潮&#xff0c;各行各业各圈层都在做大做强这种势&#xff0c;对了&#xff0c;这种行为…

C++ 之LeetCode刷题记录(三十四)

&#x1f604;&#x1f60a;&#x1f606;&#x1f603;&#x1f604;&#x1f60a;&#x1f606;&#x1f603; 开始cpp刷题之旅。 目标&#xff1a;执行用时击败90%以上使用 C 的用户。 12. 整数转罗马数字 罗马数字包含以下七种字符&#xff1a; I&#xff0c; V&#xf…

力扣382.链表随机节点

Problem: 382. 链表随机节点 文章目录 题目描述思路复杂度Code 题目描述 思路 由水塘抽样易得&#xff0c;当遇到i个元素&#xff0c;有 1 / i 1/i 1/i的概率选择该元素&#xff1b;则在实际操作中我们定义一个下标i从1开始遍历每次判断rand() % i 0&#xff08;该操作就是判断…

空指针和Void指针的基本概念和用法

前言&#xff1a;本文只是限于说明空指针与void指针的基本性质和用法&#xff0c;关于更深层次的用法&#xff0c;则不介绍&#xff0c;因为本人自己还没有搞懂&#xff01;&#xff01;&#xff01; 1&#xff1a;空指针 1.1空指针的基本定义 定义:在C语言中&#xff0c;如…

beego代理前端web的bug

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 一、beego代理前端web的bug总结 一、beego代理前端web的bug *报错&#xff0c;为web压缩包index.html里面的注释被错误解析&#xff0c;删掉就行 2024/02/22 10:2…

MySQL数据库进阶第五篇(锁)

文章目录 一、锁的概述二、全局锁三、表级锁四、元数据锁&#xff08;meta data lock, MDL&#xff09;五、意向锁六、行级锁七、行锁&#xff08;Record Lock&#xff09;八、间隙锁&#xff08;Gap Lock&#xff09;九、临键锁&#xff08;Next-Key Lock&#xff09;十、锁总…

【GameFramework框架内置模块】4、内置模块之调试器(Debugger)

推荐阅读 CSDN主页GitHub开源地址Unity3D插件分享简书地址QQ群&#xff1a;398291828 大家好&#xff0c;我是佛系工程师☆恬静的小魔龙☆&#xff0c;不定时更新Unity开发技巧&#xff0c;觉得有用记得一键三连哦。 一、前言 【GameFramework框架】系列教程目录&#xff1a;…

【原创教程】汇川H5U入门教案

一、软元件介绍 1、位软元件 PLC编程支持位软元件,位软元件具体类型、范围、点数和相关说明如下表所示: 2、字软元件 ● 掉电保持范围不可更改。 ● 字软元件作为整数使用时,根据指令参数,作为16位或32位数据使用。作为16位数据使用时,占用1个软元件;作为32位数据使…

揭密字节跳动薪资职级,资深测试居然能拿......

曾经的互联网是PC的时代&#xff0c;随着智能手机的普及&#xff0c;移动互联网开始飞速崛起。而字节跳动抓住了这波机遇&#xff0c;2015年&#xff0c;字节跳动全面加码短视频&#xff0c;从那以后&#xff0c;抖音成为了字节跳动用户、收入和估值的最大增长引擎。 自从字节逐…

前端解析后端返回文件流格式数据

当后端接口返回数据是一个文件流数据时&#xff0c;如下后端返回给我的是一个pdf文件流数据 methods: {gotoPri() {protocolApi().then(res > {this.createPdf(res.data,XXX协议)})},createPdf(res, name) {// Blob构造函数返回一个新的 Blob 对象并指定type类型。let blob …