C++ 我的世界2D块类型

C++ Minecraft2D Block Type?

本文关键字:类型 2D 我的世界 C++      更新时间:2023-10-16

我正在用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;
    }
}

我会用类似于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,因此您可以改用数组。

一种可能性是使用一个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);

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

我会在这里结合这两个答案,并制作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"

为什么要使用数组并std::map whan ?(可以在编译时初始化)

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