从零开始手写mmo游戏从框架到爆炸(二十七)— 英雄属性展示及穿戴装备-1

本文主要是介绍从零开始手写mmo游戏从框架到爆炸(二十七)— 英雄属性展示及穿戴装备-1,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

        本章我们把命令行客户端与后台串起来,并完成穿戴装备的功能。

服务端改造

        首先改造角色抽象类

         增加穿戴的装备抽象(野怪也可以穿戴装备)

public abstract class Character implements Attackable, Serializable {// 装备的装备private Equips equips;public Equips getEquips() {if(equips == null){equips = new Equips();}return equips;}public void setEquips(Equips equips) {this.equips = equips;}

        穿戴装备的抽象

@Data
public class Equips implements Serializable {public  String string ="\t\t\t\t\t\t\t\t\t";public  String tt ="\t\t";private List<Equipment> all;public Equips(){all = new ArrayList<>();}public Equips(String string, Equipment weapon, Equipment leftRing, Equipment rightRing, Equipment breastplate, Equipment shoes,Equipment helmet, Equipment glove, Equipment necklace, Equipment legArmor, Equipment belt, Equipment barcer,Equipment shoulder, Equipment shield) {all = new ArrayList<>();this.string = string;this.weapon = weapon;this.leftRing = leftRing;this.rightRing = rightRing;this.breastplate = breastplate;this.shoes = shoes;this.helmet = helmet;this.glove = glove;this.necklace = necklace;this.legArmor = legArmor;this.belt = belt;this.barcer = barcer;this.shoulder = shoulder;this.shield = shield;initAll();}private void initAll() {if(weapon != null){all.add(weapon);}if(leftRing != null){all.add(leftRing);}if(leftRing != null){all.add(leftRing);}if(breastplate != null)all.add(breastplate);if(shoes != null)all.add(shoes);if(helmet != null)all.add(helmet);if(glove != null)all.add(glove);if(necklace != null)all.add(necklace);if(legArmor != null)all.add(legArmor);if(belt != null)all.add(belt);if(barcer != null)all.add(barcer);if(shoulder != null)all.add(shoulder);if(shield != null)all.add(shield);}// 装备的武器private Equipment weapon;// 装备的戒指 左戒private Equipment leftRing;// 装备的戒指 右戒private Equipment rightRing;// 装备的护甲private Equipment breastplate;// 装备的鞋private Equipment shoes;// 装备的头盔private Equipment helmet;// 装备的手套private Equipment glove;// 装备的项链private Equipment necklace;// 装备的腿甲private Equipment legArmor;// 装备的腰带private Equipment belt;// 装备的护壁private Equipment barcer;// 装备的肩甲private Equipment shoulder;// 装备的盾牌private Equipment shield;public String prettyPrint() {return  string+"****装备信息**********************************************************\n"+tt + "武器:" + (getWeapon() == null ? "无" : getWeapon().toString()) +"\n"+tt + "盾牌:" + (getShield() == null ? "无" : getShield().toString()) +"\n"+tt + "胸甲:" + (getBreastplate() == null ? "无" : getBreastplate().toString()) +"\n"+tt + "头盔:" + (getHelmet() == null ? "无" : getHelmet().toString()) +"\n"+tt + "肩甲:" + (getShoulder() == null ? "无" : getShoulder().toString()) +"\n"+tt + "护壁:" + (getBarcer() == null ? "无" : getBarcer().toString()) +"\n"+tt + "手套:" + (getGlove() == null ? "无" : getGlove().toString()) +"\n"+tt + "腰带:" + (getBelt() == null ? "无" : getBelt().toString()) +"\n"+tt + "腿甲:" + (getLegArmor() == null ? "无" : getLegArmor().toString()) +"\n"+tt + "鞋子:" + (getShoes() == null ? "无" : getShoes().toString()) +"\n"+tt + "左戒:" + (getLeftRing() == null ? "无" : getLeftRing().toString()) +"\n"+tt + "右戒:" + (getRightRing() == null ? "无" : getRightRing().toString()) +"\n"+tt + "项链:" + (getNecklace() == null ? "无" : getNecklace().toString()) +"\n"+string+""+"************************************************************";}public Equipment findByPosition(EquipmentEnum equipmentEnum){if(equipmentEnum.equals(EquipmentEnum.WEAPON))return this.getWeapon();if(equipmentEnum.equals(EquipmentEnum.BARCER))return this.getBarcer();if(equipmentEnum.equals(EquipmentEnum.BELT))return this.getBelt();if(equipmentEnum.equals(EquipmentEnum.BREASTPLATE))return this.getBreastplate();if(equipmentEnum.equals(EquipmentEnum.GLOVES))return this.getGlove();if(equipmentEnum.equals(EquipmentEnum.HELMET))return this.getHelmet();if(equipmentEnum.equals(EquipmentEnum.LEFTRING))return this.getLeftRing();if(equipmentEnum.equals(EquipmentEnum.LEGARMOR))return this.getLegArmor();if(equipmentEnum.equals(EquipmentEnum.NECKLACE))return this.getNecklace();if(equipmentEnum.equals(EquipmentEnum.RIGHTRING))return this.getRightRing();if(equipmentEnum.equals(EquipmentEnum.SHIELD))return this.getShield();if(equipmentEnum.equals(EquipmentEnum.SHOES))return this.getShoes();if(equipmentEnum.equals(EquipmentEnum.SHOULDER))return this.getShoulder();return null;}public List<Equipment> getAll() {return all;}
}

       穿脱装备的功能开发

        新增一个接口 WearEquipment

public interface WearEquipment {String wear(Equipment equipment, int weaponType);String takeOff(Equipment equipment);
}

        在角色抽象类Character 中来实现:

 @Overridepublic Tuple<String, List<String>> wear(Equipment equipment, int weaponType) {Tuple<String, List<String>> result = new Tuple<>();result.setValue(new ArrayList<>());String takeOffEquipmentId = null;// 装备级别要小于等于英雄的等级if(equipment.getLevel() > 1 && equipment.getLevel() > getLevel()){result.getValue().add("对不起,装备等级高于英雄,无法穿戴");result.setKey(null);return result;}// 先看同类的是否存在,如果存在,先脱再穿EquipmentEnum equipmentPosition = equipment.getEquipmentPosition();switch (equipmentPosition.getCode().toLowerCase()) {case "weapon":if(weaponType != equipment.getWeaponTypeEnum().getCode()){result.getValue().add("对不起,该武器不适合英雄,无法穿戴");result.setKey(null);return result;}if (equips.getWeapon() != null) {takeOffEquipmentId = takeOff(equips.getWeapon());}equips.setWeapon(equipment);break;case "breastplate":if (equips.getBreastplate() != null) {takeOffEquipmentId = takeOff(equips.getBreastplate());}equips.setBreastplate(equipment);break;case "shield":if (equips.getShield() != null) {takeOffEquipmentId = takeOff(equips.getShield());}equips.setShield(equipment);break;case "shoes":if (equips.getShoes() != null) {takeOffEquipmentId = takeOff(equips.getShoes());}equips.setShoes(equipment);break;case "helmet":if (equips.getHelmet() != null) {takeOffEquipmentId = takeOff(equips.getHelmet());}equips.setHelmet(equipment);break;case "gloves":if (equips.getGlove() != null) {takeOffEquipmentId = takeOff(equips.getGlove());}equips.setGlove(equipment);break;case "necklace":if (equips.getNecklace() != null) {takeOffEquipmentId = takeOff(equips.getNecklace());}equips.setNecklace(equipment);break;case "leftring":if (equips.getLeftRing() != null) {takeOffEquipmentId = takeOff(equips.getLeftRing());}equips.setLeftRing(equipment);break;case "rightring":if (equips.getRightRing() != null) {takeOffEquipmentId = takeOff(equips.getRightRing());}equips.setRightRing(equipment);break;case "legarmor":if (equips.getLegArmor() != null) {takeOffEquipmentId = takeOff(equips.getLegArmor());}equips.setLegArmor(equipment);break;case "belt":if (equips.getBelt() != null) {takeOffEquipmentId =  takeOff(equips.getBelt());}equips.setBelt(equipment);break;case "barcer":if (equips.getBarcer() != null) {takeOffEquipmentId = takeOff(equips.getBarcer());}equips.setBarcer(equipment);break;case "shoulder":if (equips.getShoulder() != null) {takeOffEquipmentId = takeOff(equips.getShoulder());}equips.setShoulder(equipment);break;default:result.getValue().add("穿戴装备失败" + equipment.getName());result.setKey(null);return result;}List<String> lines = wearEquipment(equipment);result.getValue().addAll(lines);result.setKey(takeOffEquipmentId);return result;}@Overridepublic String takeOff(Equipment equipment) {// 脱装备就是整体属性下降this.setStrength(this.strength - equipment.getStrength());this.setArmature(this.armature - equipment.getArmature());this.setConstitution(this.constitution - equipment.getConstitution());this.hp.addAndGet(-equipment.getHp());this.setMagic(this.magic - equipment.getMagic());this.setTechnique(this.technique - equipment.getTechnique());this.setSpeed(this.speed - equipment.getSpeed());this.setPoisonResistance(this.poisonResistance - equipment.getPoisonResistance());this.setFlameResistance(this.flameResistance - equipment.getFlameResistance());this.setThunderResistance(this.thunderResistance - equipment.getThunderResistance());this.setIceResistance(this.iceResistance - equipment.getIceResistance());equipment.setUsed(false);return equipment.getId();}private List<String> wearEquipment(Equipment equipment) {List<String> result = new ArrayList<>();result.add("穿戴装备" + equipment.getName() + " 成功");// 穿装备就是整体属性提升this.setStrength(this.strength + equipment.getStrength());this.setArmature(this.armature + equipment.getArmature());this.setConstitution(this.constitution + equipment.getConstitution());this.hp.addAndGet(equipment.getConstitution() * 10 + equipment.getHp());this.hpMax = this.hp.get();this.setMagic(this.magic + equipment.getMagic());this.setTechnique(this.technique + equipment.getTechnique());this.setSpeed(this.speed + equipment.getSpeed());// 装备提升的抗性this.setThunderResistance(this.thunderResistance + equipment.getThunderResistance());this.setFlameResistance(this.flameResistance + equipment.getFlameResistance());this.setPoisonResistance(this.poisonResistance + equipment.getPoisonResistance());this.setIceResistance(this.iceResistance + equipment.getIceResistance());equipment.setUsed(true);return result;}

         新增接口

          新增装备列表和穿戴装备接口,以及获得个人属性接口- ServerHeroTag

package com.loveprogrammer.command.server;public interface ServerHeroTag {/**** 创建英雄*/int TAG_HERO_CREATE = 1;/**** 英雄的详情*/int TAG_HERO_INFO = 2;/**** 英雄装备列表*/int TAG_HERO_EQUIP_LIST = 3;/**** 英雄穿戴装备*/int TAG_HERO_EQUIP_WEAR = 4;
}

          英雄详情查询

           英雄查询返回体:

HeroInfo

@Data
public class HeroInfo extends RequestBase {public HeroInfo() {}public String string = "\t\t\t\t\t\t\t\t\t";private int id;                         // 角色idprivate String name;                   // 角色信息模板private int strength;                  // 力量 影响物理输出 物理技能输出private int armature;                 // 护甲值 影响物理防御和法术防御private int constitution;               // 体质 影响生命值 一点体质增加10点生命值private int magic;                       // 魔力 影响法术输出 法术技能输出private int technique;                   // 技巧 影响闪避率、暴击率private int speed;                         // 攻击速度private int hpMax;                        // 最大生命值private int level;                           // 级别private Integer currentExp;/*** 下一级所需经验值**/private Integer nextLevelNeedExp;// 毒抗private int poisonResistance;// 火抗private int flameResistance;// 电抗private int thunderResistance;// 冰抗private int iceResistance;//角色打印信息的重写public String prettyPrint() {return string + "*****信息****\n" +string + "姓名:" + this.name + "\n" +string + "级别" + this.level + "\n" +string + "血量:" + this.hpMax + "\n" +string + "力量:" + this.strength + "\n" +string + "法力:" + this.magic + "\n" +string + "技巧:" + this.technique + "\n" +string + "护甲值" + this.armature + "\n" +string + "速度" + this.speed + "\n" +string + "毒抗" + this.poisonResistance + "\n" +string + "火抗" + this.flameResistance + "\n" +string + "雷抗" + this.thunderResistance + "\n" +string + "冰抗" + this.iceResistance + "\n" +string + "当前经验" + this.currentExp + "\n" +string + "下一级经验" + this.nextLevelNeedExp + "\n" +string + "" + "*************";}//角色打印信息的重写public String toString() {return "**** 名称:" + this.name + " " +"级" + this.level + " " +"血:" + this.hpMax + " " +"甲: " + this.armature + " " +"力:" + this.strength + " " +"法:" + this.magic + " " +"技:" + this.technique + " " +"速:" + this.speed + " " +"抗性[毒:" + this.poisonResistance + " " +"火:" + this.flameResistance + " " +"雷:" + this.thunderResistance + " " +"冰:" + this.iceResistance + "] " +"经验(" + this.currentExp + "/" + this.nextLevelNeedExp + ") ****";}
}

        

HeroHandler

@TagListener(tag = ServerHeroTag.TAG_HERO_INFO,messageClass = StringRequest.class)public StringMessage getHeroInfo(ChannelHandlerContext ctx, StringRequest data) throws Exception {int sessionId = getSessionId(ctx.channel());UserGameWrapper gameWrapper = SessionGameContext.USER_GAME_MAP.get(sessionId);HeroWrapper currentHero = gameWrapper.getCurrentHero();// 转成英雄详情HeroInfo heroInfo = HeroConvert.INSTANCE.toHeroInfo(currentHero.getHero());StringMessage message = StringMessage.create(data.getSuccessCallbackTopic(), data.getSuccessCallbackTag());message.setStatusCode(CommonValue.MSG_STATUS_CODE_SUCCESS);message.setBody(JSON.toJSONString(heroInfo));return message;}

客户端改造

        补充展示命令 ClientSceneTag

public interface ClientSceneTag {/**** 战斗场景*/int TAG_SCENE_BATTLE = 1;/**** 英雄属性查看*/int TAG_SCENE_HERO = 2;
}

        展示功能- 改造portalMenu

@TagListener(tag = ClientMenuTag.TAG_MENU_PORTAL,messageClass = StringRequest.class)public void portalMenu(ChannelHandlerContext ctx, StringRequest msg){ConsolePrint.publishMessage(msg.getContent());// 展示首页数据ConsolePrint.publishMessage("请选择您要进行的操作");ConsolePrint.publishMessage("【1.打怪】  【2.属性及装备】  【3.战兽】");ConsolePrint.publishMessage("【4.冒险者工会】   【5.副本】  【6.工会】 ");ConsolePrint.publishMessage("【8.配置】  【9.退出】");ConsolePrint.publishMessage("请选择:");int choose = ScannerInput.inputInt(1, 9, 9);if (choose != 9) {switch (choose) {case 1:// 请求地图列表,同时跳转到地图菜单StringMessage message = StringMessage.create(ServerTopic.TOPIC_BATTLE, ServerBattleTag.TAG_BATTLE_MAPS);message.setStatusCode(CommonValue.MSG_STATUS_CODE_SUCCESS);StringRequest request = new StringRequest(ClientTopic.TOPIC_MENU,ClientMenuTag.TAG_MENU_MAP_CHOOSE, ClientTopic.TOPIC_MENU,ClientMenuTag.TAG_MENU_PORTAL);message.setBody(JSON.toJSONString(request));ChannelUtils.pushToServer(ctx.channel(), message);break;case 2:// 请求英雄数据message = StringMessage.create(ServerTopic.TOPIC_HERO, ServerHeroTag.TAG_HERO_INFO);message.setStatusCode(CommonValue.MSG_STATUS_CODE_SUCCESS);request = new StringRequest(ClientTopic.TOPIC_SCENE,ClientSceneTag.TAG_SCENE_HERO, ClientTopic.TOPIC_MENU,ClientMenuTag.TAG_MENU_PORTAL);message.setBody(JSON.toJSONString(request));ChannelUtils.pushToServer(ctx.channel(), message);...

        增加新的handler方法

@TagListener(tag = ClientSceneTag.TAG_SCENE_HERO, messageClass = HeroInfo.class)public void heroInfo(ChannelHandlerContext ctx, HeroInfo data) {ConsolePrint.publishMessage("英雄属性如下");ConsolePrint.publishMessage(data.prettyPrint());ConsolePrint.publishMessage("英雄装备如下");ConsolePrint.publishMessage("请选择操作");ConsolePrint.publishMessage("【1.根据系统推荐更换装备】  【2.查看已有装备,自选装备更换】 【3.卖掉装备】", 1);ConsolePrint.publishMessage("【4.合成装备】   【5.查看共享装备】  【6.返回】", 1);ConsolePrint.publishMessage("请选择:");int choose = ScannerInput.inputInt(1, 6, 6);if (choose != 6) {switch (choose) {case 1:ConsolePrint.publishMessage("暂未开放");break;case 2:ConsolePrint.publishMessage("暂未开放");break;case 3:ConsolePrint.publishMessage("暂未开放");break;case 4:ConsolePrint.publishMessage("暂未开放");break;case 5:ConsolePrint.publishMessage("暂未开放");break;default:ConsolePrint.publishMessage("暂未开放");break;}}// 返回上一级菜单 主菜单StringRequest msg = new StringRequest();NetworkClientListener.getInstance().forward(ctx,ClientTopic.TOPIC_MENU, ClientMenuTag.TAG_MENU_PORTAL, JSON.toJSONString(msg));}

效果

【1.登录已有账户】  【2.注册新账户】  【3.退出】
请选择:
1
请输入账户名:
eric
请输入密码:
111111
234:234
请选择要创建的职业
1:JobChoose.JobItem(id=1, name=战士, desc=剑类武器, strength=60, armature=5, constitution=80, magic=0, technique=15, speed=130)
1
选择的职业为战士
请输入角色的名称:
haha
124:124
英雄创建成功
请选择您要进行的操作
【1.打怪】  【2.属性及装备】  【3.战兽】
【4.冒险者工会】   【5.副本】  【6.工会】 
【8.配置】  【9.退出】
请选择:
2
暂未开放,敬请期待
367:367
英雄属性如下*****信息****姓名:haha级别0血量:800力量:60法力:0技巧:15护甲值5速度130毒抗0火抗0雷抗0冰抗0当前经验0下一级经验100*************
英雄装备如下
请选择操作
【1.根据系统推荐更换装备】  【2.查看已有装备,自选装备更换】 【3.卖掉装备】
【4.合成装备】   【5.查看共享装备】  【6.返回】
请选择:

全部源码详见:

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

分支:step-14

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

这篇关于从零开始手写mmo游戏从框架到爆炸(二十七)— 英雄属性展示及穿戴装备-1的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1

基于Flask框架添加多个AI模型的API并进行交互

《基于Flask框架添加多个AI模型的API并进行交互》:本文主要介绍如何基于Flask框架开发AI模型API管理系统,允许用户添加、删除不同AI模型的API密钥,感兴趣的可以了解下... 目录1. 概述2. 后端代码说明2.1 依赖库导入2.2 应用初始化2.3 API 存储字典2.4 路由函数2.5 应

Python GUI框架中的PyQt详解

《PythonGUI框架中的PyQt详解》PyQt是Python语言中最强大且广泛应用的GUI框架之一,基于Qt库的Python绑定实现,本文将深入解析PyQt的核心模块,并通过代码示例展示其应用场... 目录一、PyQt核心模块概览二、核心模块详解与示例1. QtCore - 核心基础模块2. QtWid

使用PyTorch实现手写数字识别功能

《使用PyTorch实现手写数字识别功能》在人工智能的世界里,计算机视觉是最具魅力的领域之一,通过PyTorch这一强大的深度学习框架,我们将在经典的MNIST数据集上,见证一个神经网络从零开始学会识... 目录当计算机学会“看”数字搭建开发环境MNIST数据集解析1. 认识手写数字数据库2. 数据预处理的

浅析CSS 中z - index属性的作用及在什么情况下会失效

《浅析CSS中z-index属性的作用及在什么情况下会失效》z-index属性用于控制元素的堆叠顺序,值越大,元素越显示在上层,它需要元素具有定位属性(如relative、absolute、fi... 目录1. z-index 属性的作用2. z-index 失效的情况2.1 元素没有定位属性2.2 元素处

最新Spring Security实战教程之Spring Security安全框架指南

《最新SpringSecurity实战教程之SpringSecurity安全框架指南》SpringSecurity是Spring生态系统中的核心组件,提供认证、授权和防护机制,以保护应用免受各种安... 目录前言什么是Spring Security?同类框架对比Spring Security典型应用场景传统

Python结合Flask框架构建一个简易的远程控制系统

《Python结合Flask框架构建一个简易的远程控制系统》这篇文章主要为大家详细介绍了如何使用Python与Flask框架构建一个简易的远程控制系统,能够远程执行操作命令(如关机、重启、锁屏等),还... 目录1.概述2.功能使用系统命令执行实时屏幕监控3. BUG修复过程1. Authorization

HTML5 data-*自定义数据属性的示例代码

《HTML5data-*自定义数据属性的示例代码》HTML5的自定义数据属性(data-*)提供了一种标准化的方法在HTML元素上存储额外信息,可以通过JavaScript访问、修改和在CSS中使用... 目录引言基本概念使用自定义数据属性1. 在 html 中定义2. 通过 JavaScript 访问3.

CSS模拟 html 的 title 属性(鼠标悬浮显示提示文字效果)

《CSS模拟html的title属性(鼠标悬浮显示提示文字效果)》:本文主要介绍了如何使用CSS模拟HTML的title属性,通过鼠标悬浮显示提示文字效果,通过设置`.tipBox`和`.tipBox.tipContent`的样式,实现了提示内容的隐藏和显示,详细内容请阅读本文,希望能对你有所帮助... 效

SpringBoot集成图片验证码框架easy-captcha的详细过程

《SpringBoot集成图片验证码框架easy-captcha的详细过程》本文介绍了如何将Easy-Captcha框架集成到SpringBoot项目中,实现图片验证码功能,Easy-Captcha是... 目录SpringBoot集成图片验证码框架easy-captcha一、引言二、依赖三、代码1. Ea