为什么我的自动RPG库存网格不起作用?

本文关键字:网格 不起作用 RPG 我的 为什么 | 更新日期: 2023-09-27 18:08:47

我的情况是这样的。我有四个类:Inventory.cs, inventorytable .cs, ItemDatabase.cs, BaseItem.cs(以及一些从BaseItem派生的项目,如武器,消耗品等)

Inventory创建了新的InventoryTab(消耗品,武器,护甲),并且在每个InventoryTab中都有一个BaseItems列表。

然后调用

Inventory.AddItemByID(int ID, int Amount),它检查ItemDatabase.cs的id,并在游戏开始时将项目预加载到列表中。

好的,现在你有了关于我的库存如何运行的基本信息,这是我的问题:

在InventoryTab中:

int Column, Row; //Declared at the top of the class
for (int i = 0; i < ItemList.Count; i++)
{
    Column++;
    if (Column > gridSize.X)
    {
        Column = 0; Row++; //Row is not limited because my inventory will be unlimited in height
    }
    ItemList[i].gridLocation = new Point(column, row);
}

虽然我认为这将工作,相反,它在顶部创建一个项目,并迅速跳到右边,然后跳过一行,并重复。如果我通过KeyboardState添加更多的项目,它会闪烁一个巨大的项目列表,然后消失。

我确信这是因为它在循环中分配gridLocation的值,但我又不知道如何以其他方式处理它。

我需要它做的是分配gridLocation每个BaseItem在ItemList只有一次。

编辑:

InventoryTab.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace InventoryEngine
{
    public class InventoryTab
    {
        public List<BaseItem> ItemList = new List<BaseItem>();
        int itemSize; //In Pixels
        Point gridSize = new Point(5, 4);
        int LocY, Column, Row;
        public float Scale;
        //Region Clipping
        Rectangle scissorRect;
        RasterizerState rState = new RasterizerState()
        {
            ScissorTestEnable = true
        };
        Vector2 gridOffset;
        Texture2D slot;
        SpriteFont sf;
        MouseState ms;
        public void LoadContent(ContentManager c, GraphicsDevice g)
        {
            ItemDatabase.LoadItemData(c);
            slot = c.Load<Texture2D>("inventory_slot");
            sf = c.Load<SpriteFont>("SpriteFont1");
            Scale = 4.0f;
            itemSize = 32 * (int)Scale;
            gridOffset = new Vector2(g.Viewport.Width / 2 - (itemSize * gridSize.X / 2), g.Viewport.Height / 2 - (itemSize * gridSize.Y / 2));
            LocY = g.Viewport.Height / 2;
        }
        public void Update(GameTime gt, GraphicsDevice g)
        {
            ms = Mouse.GetState();
            LocY += ms.ScrollWheelValue / 10;
            if (LocY >= g.Viewport.Height / 2 - 252)
                LocY = g.Viewport.Height / 2 - 252;
            for (int i = 0; i < ItemList.Count / 1; i++)
            {
                Column++;
                if (Column > gridSize.X)
                {
                    Column = 0; Row++;
                }
                ItemList[i].gridLocation = new Point(Column, Row);
            }
            foreach (BaseItem item in ItemList)
            {
                item.UpdateValues(gridSize, itemSize, gridOffset, LocY);
            }
        }
        public void DrawTab(SpriteBatch sb, GraphicsDevice g)
        {
            scissorRect = new Rectangle((int)gridOffset.X, g.Viewport.Height / 2 - 256, gridSize.X * itemSize, gridSize.Y * itemSize);
            sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, rState);
            //g.ScissorRectangle = scissorRect;
            foreach (BaseItem i in ItemList)
            {
                sb.Draw(slot, new Vector2(i.itemRect.X, i.itemRect.Y), new Rectangle(0, 0, 32, 32), Color.White, 0, Vector2.Zero, Scale, SpriteEffects.None, .95f);
                sb.Draw(i.Icon, new Vector2(i.itemRect.X, i.itemRect.Y), new Rectangle(0, 0, i.Icon.Width, i.Icon.Height), Color.White, 0, Vector2.Zero, Scale, SpriteEffects.None, .95f);
                //if (i.currentAmount > 1)
                    sb.DrawString(sf, "" + i.currentAmount, new Vector2(i.itemRect.X, i.itemRect.Y), Color.White, 0f, Vector2.Zero, Scale, SpriteEffects.None, .95f);
            }
            sb.End();
        }
    }
}

BaseItem.cs:

using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
namespace InventoryEngine
{
    public class BaseItem
    {
        public Texture2D Icon;
        public string name;
        string description;
        public int id, currentAmount, maxAmount;
        public enum TabType { Consumable, Weapon, Armor, Ammo, Jewellery, Resources, Misc }
        public TabType tabType;
        public bool isSelected, isUsable;
        public Vector2 positionOffset;
        public Point gridLocation;
        public Rectangle itemRect;
        public BaseItem(Texture2D IconName, int ID, string Name, string Description, int MaxAmount, TabType TabType)
        {
            Icon = IconName;
            id = ID;
            name = Name;
            description = Description;
            maxAmount = MaxAmount;
            tabType = TabType;
        }
        public void UpdateValues(Point GridSize, int itemSize, Vector2 GridOffset, int OffsetY)
        {
            currentAmount = (int)MathHelper.Clamp(currentAmount, 0, maxAmount);
            itemRect = new Rectangle(gridLocation.X * itemSize + (int)GridOffset.X, gridLocation.Y * itemSize + OffsetY, itemSize, itemSize);
        }
    }
}

为什么我的自动RPG库存网格不起作用?

对于@deathismyfriend,你可以运行一个嵌套的for循环遍历每个位置:

int pos; //Declared at the top of the class
pos = 0;
for (int x = 0; x < gridsize.X; x++) // x represents column
{
    for (int y = 0; y < ItemList.Count / gridsize.X; y++) // y represents row
    {
        // In the above (ItemList.count / gridsize.X) = number of rows needed
        ItemList[pos].gridLocation = new Point(x, y);
        pos++;
    }
}

应该做你想让它做的。

莫娜