C# XNA - 库存/可破坏地形和收藏

发布于 2024-12-09 23:16:03 字数 293 浏览 1 评论 0原文

我无法在标题中很好地描述问题。如果有人拥有编辑权限并且可以更好地表达它,请随意。

所以我有一个不错的小地形,它是可破坏的,但是如果有人,比如说,打破了一棵树,我怎样才能让它在他们的库存中添加一棵树呢?

我不是在寻找代码,只是在寻找方法。

每个方块都有自己的类,全部继承自基类 Block,当方块被破坏时,它会掉落一种可拾取的婴儿方块。

我可以做到这一点,但我很困惑,因为这个想法是你可以打破方块并将它们放置在其他地方(是的,有点像《我的世界》),那么我该如何让这个库存系统工作呢?我想了两天,却没有任何收获。

I can't describe the problem well enough in the title. If somone has edit permissions and can phrase it better, feel free to.

So I've got a nice little terrain going, it's destructable and all, but if somone, say, breaks a tree, how can I make it add a tree to their inventory?

I'm not looking for code, just a method.

Each block has it's own class all inheriting from the base class Block, and when the block is destroyed, it drops a sort of pickup-able baby block.

I'm able to do this, but I'm confused because the idea is you can break blocks and place them somewhere else (Yeah, kinda like Minecraft), so how would I make this inventory system work? I've been thinking for two days and I got nothing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

楠木可依 2024-12-16 23:16:03

物品需要某种 ID 代码。显然你已经有了一个来存储世界本身。

当你砍倒它时,该块有一种“树”。

“树”+砍=> “砍伐的树”

当它被拾取时,您可以从世界中删除砍伐的树图形,并将砍伐的树项目放入玩家的库存中。

Items need some sort of ID code. You obviously already have one to store the world itself.

The block had a type of "tree" when you chopped it.

"Tree" + Chop => "Chopped tree"

When it's picked up you remove the chopped tree graphic from the world and put a chopped-tree item in the player's inventory.

旧情勿念 2024-12-16 23:16:03

我认为使用一些伪代码会更容易,而不是用文字解释它:

public enum MaterialType
{
    Wood,
    Stone,
    Count // this should always be the last one
}

public class Block
{
    MaterialType m_type;
}

public class Player
{
    MaterialType m_inventory[MaterialType.Count];
}

// call this when you break a block
public class World
{
    public void OnBlockDestroyed()
    {
        player.m_inventory[block.m_type]++;
    }
}

Instead of explaining it with words I figured it will be easier with some pseudo code:

public enum MaterialType
{
    Wood,
    Stone,
    Count // this should always be the last one
}

public class Block
{
    MaterialType m_type;
}

public class Player
{
    MaterialType m_inventory[MaterialType.Count];
}

// call this when you break a block
public class World
{
    public void OnBlockDestroyed()
    {
        player.m_inventory[block.m_type]++;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文