0

私はJavaでMinecraft2Dのようなゲームに取り組んでいましたが、C ++の機能を強化するために、同じゲームをC++で作成することにしました。しかし、私には問題があります。JavaにBlockType列挙型があり、そのBlockTypeの画像の場所と硬さ(マイニングにかかる​​時間)が含まれていました。C++の列挙型はJavaの列挙型とは異なることがわかりました。これをC++で実装するにはどうすればよいですか?

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;
    }
}
4

5 に答える 5

1

可能性としては、値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 に答える
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 に答える
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)

または、次の3つの値を保持する構造を作成します。

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 に答える
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 に答える