RPG游戏黑暗之光Part4:物品模块

(由于电脑出了点问题重装以后好多写好的文档没有了,所以说各位一定要好好备份啊!这里博主微(ku)笑(B)着回来填坑啦~)

RPG游戏主要组成中道具的构成是很大一部分的内容,其中从属性分类包括药品,武器(上装,饰品,宝石等),药品等。

这里RPG游戏游戏人物自身需要有个背包系统,商店(武器,药品)也要有自己的商品系统,这里我们先分后和的来介绍。

1. 人物背包系统

不同类型物品的使用不同,这里给大家看下我的物品内容,简单的介绍一下

道具id,道具名称,道具在图集中的名称,属性,增加的hp,mp,speed,attack,defense,售价,回收价,简介(鼠标停留在道具ui显示的内容)

自定义一个类ObjectInfo包含这些内容,用另一个类读取txt文本保存到字典中,主要代码如下。

public class ObjectsText : MonoBehaviour {public static ObjectsText _instance;
//这里的这一行就是unity默认txt的类,官方文档有介绍public TextAsset objectsText;public Dictionary<int, ObjectInfo> objectDictionary = new Dictionary<int, ObjectInfo>();private void Awake(){_instance = this;ReadText();ObjectInfo info = new ObjectInfo();}public ObjectInfo GetObjectById(int id){ObjectInfo info = new ObjectInfo();objectDictionary.TryGetValue(id, out info);return info;}public void ReadText(){string text = objectsText.text;string[] objectsInfo = text.Split('\n');foreach(string x in objectsInfo){string[] objectInfo = x.Split(';');ObjectInfo Info = new ObjectInfo();Info.id = int.Parse(objectInfo[0]);Info.obj_name = objectInfo[1];Info.icon_name = objectInfo[2];switch (objectInfo[3]){case "Drug":Info.type = ObjectType.Drug;break;case "Headgear":Info.type = ObjectType.Headgear;break;case "Armor":Info.type = ObjectType.Armor;break;case "Right_Hand":Info.type = ObjectType.Right_Hand;break;case "Left_Hand":Info.type = ObjectType.Left_Hand;break;case "Shoe":Info.type = ObjectType.Shoe;break;case "Accessory":Info.type = ObjectType.Accessory;break;case "Material":Info.type = ObjectType.Material;break;}Info.hp = int.Parse(objectInfo[4]);Info.mp = int.Parse(objectInfo[5]);Info.attack = int.Parse(objectInfo[6]);Info.defense = int.Parse(objectInfo[7]);Info.speed = int.Parse(objectInfo[8]);Info.buy_price = int.Parse(objectInfo[9]);Info.sell_price = int.Parse(objectInfo[10]);Info.obj_info = objectInfo[11];objectDictionary.Add(Info.id, Info);}}
}

在其他脚本中直接可以利用ObjectsText._instance.objectDictionary获取字典内容。

通过导入的ngui包可以轻易拖拽出一个ui背包界面,分为背包,背包框,背包框内物品,这里我们利用物品id的唯一性来设置背包框属性和物品属性,用以之后的背包内物品的增减。

这里注意ui控件 脚本中写onmouseover有问题,可以在视图窗添加eventtrigger来解决这一问题。

最上层背包系统的代码

public bool EarnGoods(int id){ObjectInfo info = ObjectsText._instance.GetObjectById(id);print("生成id为:" + id);//增加要考虑全空的情况,有的情况,满的情况foreach (InventoryItemGird x in ItemGirds){if (x.item.GetComponent<InventoryItem>().ItemCount == 0){x.SetGird(info);return true;}else{if (x.id == id){x.SetGird(info);return true;}else{continue;}}}return false;
}

接下来的背包框的代码最为重要,不仅仅需要涉及到物品使用,物品拖拽,还要考虑使用情况。

public int id = 0;public GameObject item;public GameObject originalItem;private bool flag = false;
public GameObject preItem;public void Start(){originalItem = item;}public void SetGird(ObjectInfo info){id = info.id;//这里不是一个初始化//说到底还是prefab这里初始化的问题//这里获取一次物品以后GetItemCount永远不为0,相当于修改prefab内容if (item.GetComponent<InventoryItem>().ItemCount == 0){//这里增加了老的但是内容没有修改//一开始没有在等号左边加上item导致获取的内容不正确item = NGUITools.AddChild(gameObject, item);}item.GetComponent<InventoryItem>().AddItem(info);}public void MoveItem(GameObject surface){ObjectInfo info = item.GetComponent<InventoryItem>().objectInfo;surface.GetComponent<InventoryItemGird>().SetGird(info);Destroy(item);item = originalItem;id = 0;}

这里的背包框的内容属性设置是通过上层的背包系统获取的。而其中最主要的设置就是对下一层物品的新建,这里默认在视图窗拖拽一个物品,但是并不在物品中显示,且id设置为0,该背包框有物品在的时候替换,其他时间为该物品。

物品模块如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class InventoryItem : UIDragDropItem
{private UILabel label;//private int itemCount = 0;private UISprite sprite;private Vector3 originalPos;//public GameObject gb;//放的是显示info的内容public ObjectInfo objectInfo;public string SpriteName{get { return sprite.spriteName; }set { sprite.spriteName = value; }}public int ItemCount{get { return itemCount; }set { itemCount = value; }}//这里还要修正 接下来是武器的修改public void UseItem(){GameObject player = GameObject.FindGameObjectWithTag("Player");if (objectInfo.type == ObjectType.Drug){PlayerStatus status = player.GetComponent<PlayerStatus>();status.hp += objectInfo.hp;if (status.hp > status.hpMax)status.hp = status.hpMax;status.mp += objectInfo.mp;if (status.mp > status.mpMax)status.mp = status.mpMax;status.attack += objectInfo.attack;status.defense += objectInfo.defense;status.speed += objectInfo.speed;//如果药品为1destroy不然只是-1if (ItemCount == 1){
GameObject item = GetComponentInParent<InventoryItemGird> ().preItem;Destroy(GetComponentInParent<InventoryItemGird>().item);GetComponentInParent<InventoryItemGird>().item = item;GetComponentInParent<InventoryItemGird>().id = 0;}else{ItemCount--;label.text = itemCount + "";}}else if(objectInfo.type == ObjectType.Material){}else{string type = objectInfo.type.ToString();GameObject gb2 = GameObject.Find("UI Root").transform.Find("Equip").Find(type).gameObject;if (gb2.transform.childCount == 0){GameObject object1 = NGUITools.AddChild(gb2, gameObject);object1.GetComponent<InventoryItem>().AddItem(objectInfo);//删除原来的道具Doneif (ItemCount == 1){
GameObject item = GetComponentInParent<InventoryItemGird> ().preItem;
Destroy(GetComponentInParent<InventoryItemGird>().item);
GetComponentInParent<InventoryItemGird>().item = item;
GetComponentInParent<InventoryItemGird>().id = 0;}else{ItemCount--;label.text = itemCount + "";}}else{ObjectInfo info = gb2.GetComponentInChildren<InventoryItem>().objectInfo;player.GetComponent<PlayerStatus>().hpMax -= info.hp;player.GetComponent<PlayerStatus>().mpMax -= info.mp;player.GetComponent<PlayerStatus>().attack -= info.attack;player.GetComponent<PlayerStatus>().defense -= info.defense;player.GetComponent<PlayerStatus>().speed -= info.speed;Destroy(gb2.GetComponentInChildren<InventoryItem>().gameObject);print(info.obj_info);GameObject object1 = NGUITools.AddChild(gb2, gameObject);object1.GetComponent<InventoryItem>().AddItem(objectInfo);if (ItemCount == 1){
GameObject item = GetComponentInParent<InventoryItemGird> ().preItem;
Destroy(GetComponentInParent<InventoryItemGird>().item);
GetComponentInParent<InventoryItemGird>().item = item;
GetComponentInParent<InventoryItemGird>().id = 0;}else{ItemCount--;label.text = itemCount + "";}GameObject.Find("UI Root").transform.Find("Inventory").GetComponent<Inventory>().EarnGoods(info.id);}player.GetComponent<PlayerStatus>().hpMax += objectInfo.hp;player.GetComponent<PlayerStatus>().mpMax += objectInfo.mp;player.GetComponent<PlayerStatus>().attack += objectInfo.attack;player.GetComponent<PlayerStatus>().defense += objectInfo.defense;player.GetComponent<PlayerStatus>().speed += objectInfo.speed;}}protected override void OnDragDropRelease(GameObject surface){gb.SetActive(false);base.OnDragDropRelease(surface);if (surface == null){transform.position = originalPos;}else if (surface.tag == "InventoryItemGird"){if (surface.transform.childCount == 0){GetComponentInParent<InventoryItemGird>().MoveItem(surface);}else{InventoryItem itm = surface.GetComponentInChildren<InventoryItem>();int count1 = itm.ItemCount;string str1 = itm.SpriteName;ObjectInfo info1 = itm.objectInfo;string text1 = itm.gb.GetComponentInChildren<UILabel>().text;itm.SetItem(ItemCount, SpriteName, objectInfo, gb.GetComponentInChildren<UILabel>().text);SetItem(count1, str1, info1, text1);}}else if (surface.tag == "InventoryItem" && surface.GetComponentInParent<InventoryItemGird>()!=null){print(surface.tag);InventoryItem itm = surface.GetComponent<InventoryItem>();int count1 = itm.ItemCount;string str1 = itm.SpriteName;ObjectInfo info1 = itm.objectInfo;string text1 = itm.gb.GetComponentInChildren<UILabel>().text;itm.SetItem(ItemCount, SpriteName, objectInfo, gb.GetComponentInChildren<UILabel>().text);SetItem(count1, str1, info1, text1);}else{transform.position = originalPos;}}public void SetItem(int Count, string str, ObjectInfo info, string text){itemCount = Count;sprite.spriteName = str;objectInfo = info;label.text = itemCount + "";GetComponentInParent<InventoryItemGird>().id = info.id;gb.GetComponentInChildren<UILabel>().text = text;transform.position = originalPos;}public void AddItem(ObjectInfo info){objectInfo = info;label = GetComponentInChildren<UILabel>();itemCount++;label.text = itemCount + "";sprite = GetComponent<UISprite>();sprite.spriteName = info.icon_name;originalPos = transform.position;}}

每个物品包含一个显示info的label子对象,还有拖拽以后不在背包框中返回原位置,其余位置有物品还要将两个物品替换等操作,当然其中涉及到的主要是该类继承自UIDragDropItem类,只有这样才可以实现ui的拖拽,在里面protected override void OnDragDropRelease(GameObject surface),这就是拖拽结束鼠标松开调用的代码,在其中的修改就是对物品的一系列操作。

这里本章节结束啦,你问我还有药品装备商店,用户自己的装备装备呢,少年郎这些东西都是举一反三的没必要照搬教条自己摸索出来的错了也没事这只会加深自己的印象,反正博主后来涉及的道具操作就不按照学习案例中照搬喽,相信自己你行的

【RPG黑暗之光】第四章· 物品模块相关推荐

  1. android movie 资源释放,Android 资讯类App项目实战 第四章 电影模块

    前言: 正在做一个资讯类app,打算一边做一边整理,供自己学习与巩固.用到的知识复杂度不高,仅适于新手.经验不多,如果写出来的代码有不好的地方欢迎讨论. 以往的内容 第四章 电影模块 本章内容最终效果 ...

  2. 【RPG黑暗之光】第二章· 角色创建

    RPG黑暗之光Part2:角色创建 1.Part1创建完成继续进行角色创建过程中出现一点问题,在角色导入的时候贴图丢失了.这直接导致强迫症的我跑去学习了3dmax的相关知识,结果一波三折最终还是用下面 ...

  3. 第四章 Lua模块开发

    在实际开发中,不可能把所有代码写到一个大而全的lua文件中,需要进行分模块开发:而且模块化是高性能Lua应用的关键.使用require第一次导入模块后,所有Nginx 进程全局共享模块的数据和代码,每 ...

  4. box2d 碰撞检测_Box2d新系列 第四章 碰撞模块

    注:此文章翻译自Box2D v2.2.0用户手册,仅供学习参考. 4.1  关于 碰撞模块包括了形状以及操作形状的方法.模块也包括了动态树(dynamic tree)和broad-phase算法来提高 ...

  5. python全栈开发中级班全程笔记(第二模块、第四章)(常用模块导入)

    python全栈开发笔记第二模块 第四章 :常用模块(第二部分)     一.os 模块的 详解 1.os.getcwd()    :得到当前工作目录,即当前python解释器所在目录路径 impor ...

  6. 第四章 Python常用模块

    第四章 常用模块 4.1 模块介绍 4.1.1 模块及其好处 随着我们代码写的越来越多,功能越来越复杂,我们发在一个文件里维护就比较麻烦.所以我们就把不同的代码放在不同的py文件里,比如我们把连接数据 ...

  7. Box2D v2.3.0 用户指南(第四章)

     第四章 碰撞模块(Collision Module) 4.1简介 碰撞模块包含形状(shape)以及操作它们的函数.此外,碰撞模块还包括dynamictree和broad-phase来加快大型系 ...

  8. Unity 游戏黑暗之光笔记第四章 任务系统的实现

    Unity 游戏黑暗之光笔记 第四章 任务系统的实现 具体步骤可以看RPG游戏<黑暗之光>流程介绍与代码分析之(四) 注意要点 在把鼠标放在任务面板上点击时,为了让角色不移动,在Playe ...

  9. 读书笔记--项亮《推荐系统实践》第四章

    第四章 利用用户标签数据 背景: 推荐系统的目的是联系用户的兴趣和物品,这种联系需要依赖不同的媒介.目前流行的推荐系统基本上通过3种方式联系用户兴趣和物品. 基于物品的算法: 基于用户的算法: 通过一 ...

最新文章

  1. gdb+gdbserver
  2. excel 导入mysql_如何将Excel文件导入MySQL数据库
  3. 为一月份开设的组队学习课程投票啦
  4. ubuntu 中修改用户名后sudo无法解析主机
  5. python的程序异常类型,Python3.4学习笔记之类型判断,异常处理,终止程序操作小结...
  6. HTTP GET与POST区别
  7. AbsoluteLayout(绝对布局)
  8. 『数据可视化』基于Python的数据可视化工具
  9. dsu on tree(Educational Codeforces Round 2: E. Lomsat gelral)
  10. win10远程桌面查看对方计算机名,详细教你win10远程桌面连接命令
  11. Python爬虫实战之爬取链家广州房价_03存储
  12. “天鹅”类谜解大全!-
  13. 机器人协同工作,RobotArt是怎么做到的呢?
  14. plc通过无线通讯连接服务器,PLC无线通讯
  15. form中action属性
  16. python制作微信个人二维码_一个python自动生成微信二维码海报的轮子
  17. 数字正交下变频(多相滤波法)
  18. 11.12. ACLs
  19. scrapy常用设置参考手册 1
  20. 解决夜神模拟器连接eclipse的问题

热门文章

  1. Python-阿里云地图的爬取
  2. Python的pep8(代码规范)
  3. 飞书报表自动化推送设置步骤
  4. 可追踪评估模型TAM的评估
  5. Turbo C 2.0的下载地址
  6. 下载的turbo c 3.0 怎样安装
  7. android:绘图
  8. 查看电脑电池损耗状况
  9. Firefox火狐浏览器主页被360篡改了
  10. logistic函数,sigma函数性质