Search code examples
c#.netxnainventory

C# XNA - Inventory/destructable terrain & collection


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.


Solution

  • 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]++;
        }
    }