2012-08-09 32 views
0

我正在使用Java编写Minecraft2D类游戏,并决定使用C++创建相同的游戏以增强我的C++能力。但我有一个问题。我在Java中有一个BlockType枚举,它包含BlockType的图像位置和硬度(需要多长时间来挖掘它)。我发现在C++中,枚举与Java中的不同。我如何在C++中实现这一点?C++ Minecraft2D块类型?

BlockType.java:

public enum BlockType { 
    STONE("res/blocks/stone.png",3), 
    COAL("res/blocks/coal.png", 2), 
    AIR("res/blocks/air.png",0), 
    GRASS("res/blocks/grass.png",1), 
    DIRT("res/blocks/dirt.png",1), 
    DIAMOND("res/blocks/diamond.png",5), 
    REDSTONE("res/blocks/redstone.png",3), 
    COBBLE("res/blocks/cobble.png",3), 
    BRICK("res/blocks/brick.png",4), 
    IRON("res/blocks/iron.png",4), 
    GOLD("res/blocks/gold.png",5); 
    public final String location; 
    public final int hardness; 
    BlockType(String location, int hardness){ 
    this.location = location; 
    this.hardness = hardness; 
    } 
} 

回答

1

我会用类似SingerOfTheFall答案的东西去:

enum blocks 
{ 
    STONE, 
    COAL, 
    GOLD 
}; 

struct BlockType { 
    BlockType(std::string loc, int h): location(loc), hardness(h) {} 
    std::string location; 
    int hardness; 
}; 

BlockType blockTypes[] = { 
    BlockType("res/blocks/stone.png", 3), // STONE 
    BlockType("res/blocks/coal.png", 2), // COAL 
    BlockType("res/blocks/gold.png", 5) // GOLD 
}; 

// use: 
cout << "Location: " << blockTypes[STONE].location << endl; 

std::map是一个很好的容器,但它使用的二进制搜索每次需要获得价值的时间。指数将从0到n,所以你可以使用数组。

+0

谢谢,我会用这个。但是,如果您不介意,还有另一个问题:如何将这个文件用于多个文件?我的意思是我该如何声明结构或枚举?在头文件中?对不起,我是C++的noob,所以如果你回答我会很开心。 – 2012-08-09 13:08:10

+0

我会使用类似这样的东西:http://ideone.com/zd4XI – 2012-08-11 10:30:22

0

C++枚举工作确实是另一种方式。

enum eMyEnum 
{ 
    ONE = 15, 
    TWO = 22 
}; 

是所有你可以从他们那里得到的,基本上他们只是允许你创建INT值“名字”。

对于你的情况,我会做一个枚举的块名称:

enum blocks 
{ 
    STONE, 
    SAND, 
    <...> 
}; 

,然后做一个地图:

< blocks, pair< string, int > > 
    ^ ^ ^ ^
    |  |  |  | 
    |  |  |  hardness 
    |  | path to picture 
    |  |  
    |  the block's attributes: the picture path and hardness 
    | 
    the block type from the enum (e.g. SAND) 

或者只是做一个结构来保持三个值:

struct block 
{ 
    string type;//or int, or your enum type, depending on how do you want to store it. 
    string picture; 
    int hardness; 
} 
1

可能会使用std::map,键入enum v ALUE并与std::pair<sd::string, int>值:

#include <string> 
#include <map> 
#include <utility> 

enum BlockType 
{ 
    STONE, 
    COAL, 
    GOLD 
}; 

std::map<BlockType, std::pair<std::string, int>> BlockTypes; 

BlockTypes[STONE] = std::make_pair(std::string("res/blocks/stone.png"), 3); 
BlockTypes[COAL] = std::make_pair(std::string("res/blocks/coal.png"), 2); 
BlockTypes[GOLD] = std::make_pair(std::string("res/blocks/gold.png"), 5); 
0

为什么使用和std::map焕数组会做什么? (可在编译时初始化)

using namespace std; 

struct BlockType { 
    enum { 
     STONE = 0, 
     COAL, 
     LAST 
    }; 
    BlockType(string location, int hardness) : location(location), hardness(hardness) {} 
    const string location; 
    const int hardness; 
    static const BlockType Blocks[LAST]; 
}; 

const BlockType BlockType::Blocks[] = { 
    BlockType("res/blocks/stone.png", 3), 
    BlockType("res/blocks/coal.png", 2) 
}; 

int main() { 
    cout << BlockType::Blocks[BlockType::STONE].location << `\n`; 
    return 0; 
} 
0

我会在这里结合了答案,使enum的映射来阻止struct

struct Block 
{ 
    block(path, str) : strength(str), path(path) {} 
    int str; 
    std::string path; 
}; 

enum BlockType 
{ 
    STONE, 
    COAL, 
    ETC 
} 

std::map<BlockType, Block> blocks; 
blocks[STONE] = Block("c:/block/bla.png", 1); 
blocks[STONE].str; // 1 
blocks[STONE].path; // "c:/block/bla.png"