UNITY->(width*height)style Inventory
项目过后对项目功能进行记录,(width*height)风格背包实现细节,包含对物体的存放,装备,替换,对未知装备的鉴定,物体前缀的获取,项目类型为tcg+rpg,背包的作用主要为游戏中的物品的获取存放,卡牌的获取管理,对可叠加物品的存放,(width*height)的目的为对物品的存放管理,其效果如下
- 基础物品 (width*height)
物品的创建通过ScriptObject进行创建,根据物品类型设计相关的尺寸(width*height),利用deepcopy创建实体存放如背包,项目的物品管理通过一个独立的类
ItemDatabase进行管理,在玩家获取一个新物品(未鉴定),通过一个随机生成类来产生词缀和属性

本项目物品的生成通过deepcopy生成物品并添加进itemdatabse,Itemdatabase的作用对游戏物品资产的管理以及生成,有利于未知装备的生成,在对商品商管理上,添加未知随机的装备可以为游戏的物品提高游戏性
public static ItemClass DeepCopy(ItemClass obj) {
GameObject oj = obj.worldObject;
//ItemClass is item base class
ItemClass i = (ItemClass)Process(obj);
i.worldObject = oj;
return i;
}
static object Process(object obj) {
if(obj==null)
return null;
Type type=obj.GetType();
if(type.IsValueType || type==typeof(string)) {
return obj;
}
else if(type.IsArray) {
Type elementType=Type.GetType(
type.FullName.Replace("[]",string.Empty));
var array=obj as Array;
Array copied=Array.CreateInstance(elementType,array.Length);
for(int i=; i<array.Length; i++) {
copied.SetValue(Process(array.GetValue(i)),i);
}
return Convert.ChangeType(copied,obj.GetType());
}
else if(type.IsClass) {
object toret=Activator.CreateInstance(obj.GetType());
FieldInfo[] fields=type.GetFields(BindingFlags.Public|
BindingFlags.NonPublic|BindingFlags.Instance);
foreach(FieldInfo field in fields) {
object fieldValue=field.GetValue(obj);
if(fieldValue==null)
continue;
field.SetValue(toret, Process(fieldValue));
}
return toret;
}
else
throw new ArgumentException("Unknown type");
}
2.生成slot并将物品存放进slot(width*height)
在物品添加进背包时,根据几个条件进行判断
1.slot是否能填充进去
在物品添加进背包栏,需要检测物品尺寸是否越过背包限制并物品之间是否存在重叠,重叠状态一般在物品编辑器中设置(通常为药水...),当不可叠加物品存在重叠状态中,获取状态颜色并提示状态
//检测边界以及叠加状态
public bool CheckItemFit(ItemClass item, InventorySlot slot, bool skipLastCheck) {
//Run through all the slots that the item occupies
for(int i = ; i < item.height; i++) {
for(int j = ; j < item.width; j++) {
//Check if the slot exists
if(slot.itemStartNumber + inventoryWidth * i + j >= items.Count) {
return false;
}
//Check to see if the first slot is located at the edge of the inventory
for(int k = ; k < item.height; k++) {
if(slot.itemStartNumber + inventoryWidth * k + j != slot.itemStartNumber + inventoryWidth * k) {
if(((slot.itemStartNumber + inventoryWidth * i + j ) % inventoryWidth == ) && item.width != ) {
return false;
}
}
}
//Last check is only used sometimes
//Checks to see if there's already something in the slots
if(!skipLastCheck) {
if(items[slot.itemStartNumber + inventoryWidth * i + j].itemStartNumber != slot.itemStartNumber + inventoryWidth * i + j) {
return false;
}
}
else {
List<int> counter = new List<int>();
for(int l = ; l < item.height; l++) {
for(int m = ; m < item.width; m++) {
if((slot.itemStartNumber + inventoryWidth * (item.height - ) + (item.width - )) < items.Count - && items[slot.itemStartNumber + inventoryWidth * l + m].itemStartNumber != slot.itemStartNumber && items[slot.itemStartNumber + inventoryWidth * l + m].item.itemName != "" && !counter.Contains(items[slot.itemStartNumber + inventoryWidth * l + m].itemStartNumber)) {
counter.Add(items[slot.itemStartNumber + inventoryWidth * l + m].itemStartNumber);
}
}
}
if(counter.Count > ) {
//return false if there's more than one item
return false;
}
else if(counter.Count == ) {
return true;
}
}
}
}
return true;
}
当物品状态为可添加时,添加进slot,根据width*height尺寸进行添加
for(int l = ; l < item.height; l++) {
for(int m = ; m < item.width; m++) {
//First we add the items to the slots the it fills and set their slots to clear
items[i + inventoryWidth * l + m].item = DeepCopy(item);
items[i + inventoryWidth * l + m].itemStartNumber = i;
items[i + inventoryWidth * l + m].GetComponent<Image>().color = Color.clear;
items[i + inventoryWidth * l + m].stacksizeText.gameObject.SetActive(false);
//If it's the first index of the added item
if(items.IndexOf(items[i + inventoryWidth * l + m]) == i) {
SetSlotImageSprite(items[i + inventoryWidth * l + m], item.icon);
items[i + inventoryWidth * l + m].itemFrame.gameObject.SetActive(true);
items[i + inventoryWidth * l + m].itemFrame.GetComponent<CanvasGroup>().interactable = true;
items[i + inventoryWidth * l + m].itemFrame.GetComponent<CanvasGroup>().blocksRaycasts = true;
items[i + inventoryWidth * l + m].GetComponent<CanvasGroup>().blocksRaycasts = true;
items[i + inventoryWidth * l + m].itemFrame.rectTransform.sizeDelta = new Vector2(item.width * slotIconSize, item.height * slotIconSize);
//If the item is stackable
if(item.stackable) {
items[i + inventoryWidth * l + m].stacksizeText.gameObject.SetActive(true);
items[i + inventoryWidth * l + m].stacksizeText.text = item.stackSize.ToString();
}
//The item is unidentified
if(item.unidentified) {
items[i + inventoryWidth * l + m].itemImage.color = Color.red;
items[i + inventoryWidth * l + m].unidentified.gameObject.SetActive(true);
}
}
}
}
2.物品是否存在物品
当物品存在物品时,需要对存在物品的占用尺寸进行判断(int slot=indexof(item)),当slot存在物品时,遍历slot.count 获取空栏位并存放,另外一种情况是当玩家处于dragging状态时,可以对物品进行替换,存放在物品的方式一般为
Items[slot.itemStartNumber + inventoryWidth * l + m].item.variables
UNITY->(width*height)style Inventory的更多相关文章
- offset[Parent/Width/Height/Top/Left] 、 client[Width/Height/Top/Left] 、 Element.getBoundingClientRect()
开篇提示:以下内容都经个人测试,参考API文档总结,但还是不能保证完全正确,若有错误,还请留言指出___________________________________________________ ...
- client/scroll/offset width/height/top/left ---记第一篇博客
client/scroll/offset width/height/top/left (盒模型为contentBox,定位原点是元素左上角边框最外层的交点) clientWidth width+左p ...
- as3:sprite作为容器使用时,最好不要指定width,height
除 TextField 和 Video 对象以外,没有内容的显示对象(如一个空的 Sprite)的高度为 0,即使您尝试将 height 设置为其它值,也是这样. 如果您设置了 height 属性,则 ...
- 正则:img的url,width,height 和 a标签的url以及替换
代码:// 内容:$detail['content'] //img的url,width,height $img = array(); $matches = array(); $regeImg = '/ ...
- ffmpeg按比例缩放--"width / height not divisible by 2" 解决方法
最近在处理视频的时候,有这么一个需求 如果视频的分辨率宽度大于960的话,就把宽度设为960,而高度按其比例进行缩放 如果视频的分辨率高度大于540的话,就把高度设为540,而宽度按其比例进行缩放 之 ...
- 如何理解VB窗体中的scale类属性及width height属性之间的关系
如何理解VB窗体中的scale类属性及width height属性之间的关系 VB中的SCALEHIEGT,SCALEWIDTH,与窗体中的WIDTH,HEIGHT的区别及关系是许多VB初学者难以理解 ...
- css 行内元素 块元素 替换元素 非替换元素 以及这些元素的width height margin padding 特性
一.各种元素的width height margin padding 特性(具体css元素的分来参看二) 1.块级元素 width. height. margin的四个方向. padding的四个方向 ...
- 转:Canvas标签的width和height以及style.width和style.height的区别
转自:http://www.cnblogs.com/artwl/archive/2012/02/28/2372042.html 作者:Artwl 背景 今天在博问中看到一个问题:用canvas 的 l ...
- [WPF系列]- Style - Specify width/height as resource in WPF
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys=" ...
随机推荐
- mysql 查询当天、昨天、本周、上周、本月、上月、今年、去年数据
mysql查询今天.昨天.7天.近30天.本月.上一月 数据 今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT ...
- oracle函数 REPLACE(c1,c2[,c3])
[功能]将字符表达式值中,部分相同字符串,替换成新的字符串 [参数] c1 希望被替换的字符或变量 c2 被替换的字符串 c3 要替换的字符串,默认为空(即删除之意,不是空格) [返回]字 ...
- hdu 2312 Cliff Climbing (pfs)
Problem - 2312 一条很暴力,有点恶心的搜索.题意其实很简单,主要是pfs的时候拓展结点会有种麻烦的感觉.注意的是,这里的n和m跟平常见到的有所不同,交换过来了.我的代码就是在因为这个长宽 ...
- 手写call,apply方法实现
call Function.prototype.myCall = function(){ var object = arguments[0]; var arr = []; for(var i = 1; ...
- artTemplate模版引擎的使用
artTemplate: template.js 一款 JavaScript 模板引擎,简单,好用.提供一套模板语法,用户可以写一个模板区块,每次根据传入的数据,生成对应数据产生的HTML片段,渲染不 ...
- Python--day45--pymysql操作数据库详细
1.一个pymysql往数据库提交数据的简单例子: import pymysql # user = "eric" # pwd = '123123' #增加 conn = pymys ...
- SpringBoot2.0 使用AOP统一处理Web请求日志(完整版)
一,加入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- 2018-8-10-win10-uwp-绘图--Line-控件使用
title author date CreateTime categories win10 uwp 绘图 Line 控件使用 lindexi 2018-08-10 19:16:51 +0800 201 ...
- Vue-axios 在vue cli中封装
common/post.js import axios from 'axios' // 引入axios import qs from 'qs' // 引入qs axios.defaults.baseU ...
- 深度学习——GAN
整理自: https://blog.csdn.net/woaidapaopao/article/details/77806273?locationnum=9&fps=1 思想 表达式 实际计算 ...