【问题标题】:How do I handle items in inventory如何处理库存中的物品
【发布时间】:2013-12-19 13:14:25
【问题描述】:

我正在构建我的第一个库存,我希望它能够像任何其他基本库存一样工作:

  • 当您捡起某样东西时,它会将其添加到您的库存中,供您以后装备。
  • 它的外观和工作方式类似于狂神领域的物品栏。

我完成了一些基本的工作,例如:

  • 绘制库存槽,当您将鼠标悬停时它们会改变纹理。
  • 用鼠标移动项目/对象。

就是这样。

所以现在我陷入了涉及如何处理项目的部分,我知道有几种处理项目的方法,但我不知道在我的当前代码中最简单的方法是什么我正在使用。

如果有人能指出我正确的方向,我将非常感激,这将为我节省大量时间!

这是代码顺便说一句:

class InventorySlots
{
    Texture2D box;
    Texture2D blackbox;
    Texture2D empty;
    const int offSet = 100;
    Rectangle[,] Inventoryslots = new Rectangle[6, 4];
    Rectangle testrect; // Rect for moving item
    bool isMoving;

    public void LoadContent(ContentManager Content)
    {
        box = Content.Load<Texture2D>("Box");
        blackbox = Content.Load<Texture2D>("boxselected");
        testrect = new Rectangle(10, 20, box.Width, box.Height);//Set up my test rect
        empty = box;

        for (int x = 0; x < 6; x++)
        {
            for (int y = 0; y < 4; y++)
            {
                Inventoryslots[x, y] = new Rectangle((x * box.Width) + offSet, // Setup my inventory slots
                     (y * box.Height) + offSet, box.Width, box.Height);
            }
        }
    }

    public void Update(GameTime gameTime)
    {
        if ((ButtonState.Pressed == Mouse.GetState().LeftButton))
        {
            if (testrect.Intersects(new Rectangle(Game1.mousePosition.X, Game1.mousePosition.Y, 0, 0)))
            {
                isMoving = true;
            }
        }
        else
        {
            isMoving = false;
        }
        if (isMoving)
        {
            testrect = new Rectangle(Game1.mousePosition.X - testrect.Width / 2, Game1.mousePosition.Y - testrect.Height / 2, testrect.Width, testrect.Height);
        }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        for (int x = 0; x < 6; x++)
        {
            for (int y = 0; y < 4; y++)
            {
                if (Inventoryslots[x, y].Contains(Game1.mousePosition))
                {
                    empty = box;
                }
                else if (!Inventoryslots[x, y].Contains(Game1.mousePosition))
                    empty = blackbox;

                spriteBatch.Draw(empty, Inventoryslots[x, y], Color.White);                   
            }
        }

        spriteBatch.Draw(box, testrect, Color.White);//Draw my test item that i can move around
    }
}

【问题讨论】:

    标签: c# xna inventory


    【解决方案1】:

    您的代码表明您缺乏对面向对象原则的理解,游戏中的一切都应该是对象或接口。

    public class Item 
    {
        private Texture2D texture; 
    
        private Vector2 position; 
        public Vector2 Position { get {return position; } } 
    
        private Rectangle Bounds; 
    
        public bool Collides(Vector2 position)
        {
             Bounds = new Rectangle(this.position.X, this.position.Y, 
                      this.texture.width, this.texture.height); 
    
             if (Bounds.Intersects(position))
                 return true; 
             else return false; 
        }
    }
    

    使用面向对象编程的原则,您可以从项目基类派生特定项目,如下所示:

    public class Sword : Item 
    {
         public Sword() { } 
    }
    

    OOP 背后的理念是代码中的所有内容都按字面意思表达,基类包含封装其所有派生子类的属性和函数。所以 Sword 会继承位置、边界、纹理等等。

    这很聪明,因为它允许您对事物进行一次编程,然后为每个项目重复使用该代码。

    我不会为你编写代码,但首先要像这样定义你的项目:

    private Item[,] Inventoryslots = new Item[6, 4]; 
    

    我将尽量减少这一点,因为我没有太多时间,但如果我能指出一个方向,那就是学习更多关于 C# 计算机编程语言中的面向对象原则的知识。谷歌,你是金子。

    【讨论】:

    • 这更有意义,希望能让我走上正轨谢谢!
    【解决方案2】:

    库存是游戏的重要组成部分。您可以列出项目,例如从头部编写。

    Public Class Item
         Public ID as integer // id of item (apple=1, hammer=2)
         Public Position as vector2 // position in your inventory
         Public Equiped as boolean = false;
    End Class
    
    Public Class Items
         Inherit List of(Item)
    
         Sub Add(id)
             dim Item as new Item
             Item.ID = id
             // find first free slot and apply position to Item.Position
             me.add(Item)
         End Sub
    
         Sub Remove(id)
             me.removeAll(function(c) c.id = id)
         End Sub
    
         Sub Relocate(id, newPostion)
             // example of looping
             For Each Item as Item in Me
                 If Item.ID = id Then
                     Item.Position = newPosition
                 End If
             Next
         End Sub
    
         Sub Equip(id)
             Dim Item as Item = me.find(function(c) c.id=id)
             Item.Equiped = true;
         End Sub
    
    End Class
    

    你可以像这样创建枚举器:

    Enum InventoryItems
        1 = Apple
        2 = Sword
    End
    

    您声明主要变量:Public Inventory as New Items。使用枚举器添加新项目:Inventory.Add(InventoryItems.Apple) 但添加函数必须接受枚举。

    一旦你了解它的工作原理,它就不会那么复杂了。

    【讨论】:

    • 您好,感谢您的回复!你编码的方式看起来不像我以前见过的任何类型的代码。很抱歉,我的编码知识在 Xna 和半基本 C# 之外没有那么远。实际上,我觉得有点难以阅读和理解它们是如何协同工作的。
    • 这看起来像visual basic,在我看来,它确实不是解决这个问题的最佳方法。定义一个项目类很好,但在需要许多不同项目的游戏中,您需要为游戏中的每个项目创建一个子类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 2014-04-23
    相关资源
    最近更新 更多