0

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

块类型.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;
    }
}
4

5 回答 5

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,因此您可以改用数组。

于 2012-08-09T09:03:44.870 回答
1

一种可能性是使用 a std::map,由一个enum值作为键,并且值为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);
于 2012-08-09T08:46:13.417 回答
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;
}
于 2012-08-09T08:55:20.730 回答
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"
于 2012-08-09T08:57:16.207 回答
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;
}
于 2012-08-09T08:45:23.287 回答