2013-12-19 83 views
1

我建立我的第一个盘点,我希望它像任何其他库存基本就是工作:如何处理项目库存

  • 当你拿起东西就会把它添加到您的库存的你以后可以装备。
  • 它的外观和功能类似于疯狂神的库存领域。

我得到了一些基本的东西,如做:

  • 绘图库存插槽,当你鼠标移到他们改变的质感。
  • 用鼠标移动项目/对象。

就是这样。

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

如果任何人都可以指引我走向正确的方向,我会非常感激,它会为我节省很多时间!

下面的代码BTW:

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 
    } 
} 

回答

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的想法是,在你的代码一切都表达从字面上看,基类包含封装所有派生子类的属性和函数。所以剑会继承位置,边界,纹理,等等。

这很聪明,因为它允许您编程一次,然后重新使用每个项目的代码。

我不会写代码的你,而是通过定义你这样的项目启动:

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

我要保持这种在最低限度,因为我没有很多的时间,但如果我能指出你的方向,那就是在C#计算机编程语言中学习更多关于面向对象的原则。谷歌,你是黄金。

+0

这样做更有意义,并希望能让我走上正轨,谢谢! – Iskalder

0

库存是相当庞大的游戏的一部分。你可以列出项目,例如从头开始写。

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 

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

当你了解它的工作原理时,这并不复杂。

+0

嗨,谢谢你的回复!你编码的方式看起来不像我之前见过的任何一种代码。我很抱歉,但是我的编码知识并没有达到Xna和半基本C#之外的那么远。实际上我发现很难阅读和理解它们如何一起工作。 – Iskalder

+0

这看起来像是视觉基础,在我看来这并不是解决这个问题的最佳方法。定义一个物品类很好,但是在需要许多不同物品的游戏中,您希望为游戏中的每件物品创建一个子类。 –

相关问题