C++ 我的世界2D块类型
C++ Minecraft2D Block Type?
我正在用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;
}
相关文章:
- 字符串类型的 2d 数组(C++;)
- 指向 2D 数组的指针的类型是什么?
- C++ 2D shared_ptr数组使用抽象多态类型初始化
- 将 2D 数组作为函数参数,并返回 2D 数组作为函数的返回类型
- (对于C++)读取类型从文件转换为 2D 数组
- 将 2D 矢量转换为 C 类型的最佳方法(适用于 SGX 飞地)
- 在类中创建向量(2D 数组)的向量 - Error:C++ 需要所有声明的类型说明符
- 我们可以在整数类型的双指针中分配2D整数数组的地址吗?怎么可能
- 为什么要在将 2d 数组传递给函数时将其声明为类型,为什么要在类型中包含行和列
- 具有不同类型的元素和使用类的 2D 数组
- 如何在 int 类型的 2D 向量中push_back数据
- (C++)用于实例化新对象并将其分配给指向相同对象类型的指针的 2D 向量的语法
- 2D数组中数组下标的类型"int[int]"无效
- 使用类型转换在 C++ 中返回 2D 数组
- 具有两种不同数据类型的2d阵列
- 我不能在不使用指针的情况下返回 2d 数组!我正在制作一个井字游戏类型游戏,这是我的向上移动功能
- C++ 我的世界2D块类型
- 用派生类填充基类类型的2D矢量
- 如何为不同类型的2d数组生成随机数
- 在c++中推回基类类型2d vector中的子类对象