C# 如何处理库存中的项目
我正在建立我的第一个库存,我希望它能像任何其他基本库存一样工作,即:C# 如何处理库存中的项目,c#,xna,inventory,C#,Xna,Inventory,我正在建立我的第一个库存,我希望它能像任何其他基本库存一样工作,即: 当你拿起东西时,它会将它添加到你的库存中,你以后可以装备它 它的外观和工作原理类似于《疯狂的上帝》的目录 我做了一些基本的事情,比如: 绘制资源清册插槽,并在鼠标悬停在其上时更改纹理 用鼠标移动项目/对象 就这样 所以现在我被困在了如何处理项目的部分,我知道有几种方法可以处理项目,但我不知道在我目前使用的代码中,最简单的方法是什么 如果有人能给我指出正确的方向,我将非常感谢,这将节省我很多时间 以下是代码,顺便说一句:
- 当你拿起东西时,它会将它添加到你的库存中,你以后可以装备它
- 它的外观和工作原理类似于《疯狂的上帝》的目录
- 绘制资源清册插槽,并在鼠标悬停在其上时更改纹理
- 用鼠标移动项目/对象
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
}
}
类资源清册插槽
{
纹理2D盒;
纹理2d黑盒;
纹理2d为空;
常数int偏移=100;
矩形[,]目录槽=新矩形[6,4];
矩形testrect;//用于移动项的Rect
布尔·伊斯莫温;
公共void加载内容(ContentManager内容)
{
框=内容。加载(“框”);
黑盒=Content.Load(“选择框”);
testrect=新矩形(10,20,box.Width,box.Height);//设置我的测试矩形
空=盒子;
对于(int x=0;x<6;x++)
{
对于(int y=0;y<4;y++)
{
Inventoryslots[x,y]=新矩形((x*box.Width)+偏移量,//设置我的库存插槽
(y*框高)+偏移,框宽,框高);
}
}
}
公开作废更新(游戏时间游戏时间)
{
if((ButtonState.Pressed==Mouse.GetState().LeftButton))
{
if(testrect.Intersects(新矩形(Game1.mousePosition.X,Game1.mousePosition.Y,0,0)))
{
isMoving=真;
}
}
其他的
{
isMoving=假;
}
如果(正在移动)
{
testrect=新矩形(Game1.mousePosition.X-testrect.Width/2,Game1.mousePosition.Y-testrect.Height/2,testrect.Width,testrect.Height);
}
}
公共作废抽签(SpriteBatch SpriteBatch)
{
对于(int x=0;x<6;x++)
{
对于(int y=0;y<4;y++)
{
if(Inventoryslots[x,y].Contains(Game1.mousePosition))
{
空=盒子;
}
如果(!Inventoryslots[x,y].包含(Game1.mousePosition))则为else
空=黑盒;
spriteBatch.Draw(空,库存槽[x,y],颜色.白色);
}
}
spriteBatch.Draw(box,testrect,Color.White);//绘制我可以移动的测试项
}
}
库存是游戏中相当重要的一部分。您可以选择要列出的项目,例如从头开始编写的项目
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
private Item[,] Inventoryslots = new Item[6, 4];
您将main vairable:公共库存声明为新项目
。要使用枚举器添加新项,请执行以下操作:Inventory.add(InventoryItems.Apple)
,但添加函数必须接受枚举
一旦你了解了它的工作原理,它就不再那么复杂了。你的代码表明你缺乏对面向对象原则的理解,游戏中的一切都应该是对象或界面
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背后的思想是,代码中的所有内容都是按字面表达的,基类包含封装其所有派生子类的属性和函数。所以剑会继承位置、边界、纹理等等
这是聪明的,因为它允许您对内容进行一次编程,然后对每个项目重复使用该代码
我不打算为您编写代码,但首先要定义如下项目:
Enum InventoryItems
1 = Apple
2 = Sword
End
private Item[,] Inventoryslots = new Item[6, 4];
我将把这个限制在最低限度,因为我没有太多时间,但如果我能为您指出一个方向,那就是学习更多关于C#计算机编程语言中面向对象的原则。用谷歌搜索一下,你就是黄金 嗨,谢谢你的回复!您编写代码的方式与我以前见过的任何代码都不一样。很抱歉,我对编码的了解远不止Xna和半基本C。事实上,我发现很难阅读和理解它们是如何协同工作的。这看起来像visual basic,在我看来,这并不是解决这个问题的最佳方法。定义一个项目类很好,但是在一个需要很多不同项目的游戏中,你需要为游戏中的每个项目创建一个子类。这更有意义,希望能让我走上正轨谢谢!