mirror of
https://github.com/zoriya/Bomberman.git
synced 2025-12-20 13:25:10 +00:00
Add hole block and separate load/create map
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
namespace RAY3D = RAY::Drawables::Drawables3D;
|
||||
|
||||
namespace BBM
|
||||
{
|
||||
{
|
||||
void MapGenerator::generateUnbreakableBlock(int width, int height, std::shared_ptr<WAL::Scene> scene)
|
||||
{
|
||||
std::string UnbreakableObj = "assets/wall/unbreakable_wall.obj";
|
||||
@@ -19,6 +19,7 @@ namespace BBM
|
||||
if (!(i % 2) && !(j % 2)) {
|
||||
scene->addEntity("Unbreakable Wall")
|
||||
.addComponent<PositionComponent>(Vector3f(i, 0, j))
|
||||
//.addComponent<CollisionComponent>(1)
|
||||
.addComponent<Drawable3DComponent<RAY3D::Model>>(UnbreakableObj, std::make_pair(MAP_DIFFUSE, UnbreakablePnj));
|
||||
}
|
||||
}
|
||||
@@ -32,26 +33,31 @@ namespace BBM
|
||||
|
||||
for (int i = -1; i < width + 1; i++) {
|
||||
scene->addEntity("Vertical Wall")
|
||||
.addComponent<PositionComponent>(Vector3f(i,0,height + 1))
|
||||
.addComponent<PositionComponent>(Vector3f(i, 0, height + 1))
|
||||
//.addComponent<CollisionComponent>(1)
|
||||
.addComponent<Drawable3DComponent<RAY3D::Model>>(UnbreakableObj, std::make_pair(MAP_DIFFUSE, UnbreakablePnj));
|
||||
scene->addEntity("Vertical Wall")
|
||||
.addComponent<PositionComponent>(Vector3f(i,0,-1))
|
||||
.addComponent<PositionComponent>(Vector3f(i, 0, -1))
|
||||
//.addComponent<CollisionComponent>(1)
|
||||
.addComponent<Drawable3DComponent<RAY3D::Model>>(UnbreakableObj, std::make_pair(MAP_DIFFUSE, UnbreakablePnj));
|
||||
}
|
||||
for (int i = -1; i < height + 1; i++) {
|
||||
scene->addEntity("Horizontal Wall")
|
||||
.addComponent<PositionComponent>(Vector3f(width + 1,0,i))
|
||||
.addComponent<PositionComponent>(Vector3f(width + 1, 0, i))
|
||||
//.addComponent<CollisionComponent>(1)
|
||||
.addComponent<Drawable3DComponent<RAY3D::Model>>(UnbreakableObj, std::make_pair(MAP_DIFFUSE, UnbreakablePnj));
|
||||
scene->addEntity("Horizontal Wall")
|
||||
.addComponent<PositionComponent>(Vector3f(-1,0,i))
|
||||
.addComponent<PositionComponent>(Vector3f(-1, 0, i))
|
||||
//.addComponent<CollisionComponent>(1)
|
||||
.addComponent<Drawable3DComponent<RAY3D::Model>>(UnbreakableObj, std::make_pair(MAP_DIFFUSE, UnbreakablePnj));
|
||||
}
|
||||
scene->addEntity("Vertical Wall")
|
||||
.addComponent<PositionComponent>(Vector3f(width + 1, 0, height + 1))
|
||||
//.addComponent<CollisionComponent>(1)
|
||||
.addComponent<Drawable3DComponent<RAY3D::Model>>(UnbreakableObj, std::make_pair(MAP_DIFFUSE, UnbreakablePnj));
|
||||
}
|
||||
|
||||
void MapGenerator::generateFloor(int width, int height, std::shared_ptr<WAL::Scene> scene)
|
||||
void MapGenerator::generateFloor(int width, int height, std::shared_ptr<WAL::Scene> scene)
|
||||
{
|
||||
/* RAY3D::Model model = RAY3D::Model("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png"));
|
||||
|
||||
@@ -60,6 +66,7 @@ namespace BBM
|
||||
for (int j = 0; j < height + 1; j++) {
|
||||
scene->addEntity("Floor")
|
||||
.addComponent<PositionComponent>(Vector3f(i,-1,j))
|
||||
//.addComponent<CollisionComponent>(1)
|
||||
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/wall/floor.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/floor.png"));
|
||||
}
|
||||
}
|
||||
@@ -67,23 +74,20 @@ namespace BBM
|
||||
|
||||
void MapGenerator::createElement(Vector3f coords, std::shared_ptr<WAL::Scene> scene, BlockType blockType)
|
||||
{
|
||||
if (blockType == BREAKABLE) {
|
||||
createBreakable(coords, scene);
|
||||
} else if (blockType == UNBREAKABLE) {
|
||||
createUnbreakable(coords, scene);
|
||||
}
|
||||
/* std::map<BlockType, std::function<MapElem>> elements = {
|
||||
std::map<BlockType, MapElem> elements = {
|
||||
{BREAKABLE, &createBreakable},
|
||||
{UNBREAKABLE, &createUnbreakable},
|
||||
/* {HOLE, &Map::createHole},
|
||||
{BUMPER, &Map::createBumper},
|
||||
{STAIRS, &Map::createStairs}
|
||||
{HOLE, &createHole},
|
||||
/* {BUMPER, &Map::createBumper},
|
||||
{STAIRS, &Map::createStairs} */
|
||||
};
|
||||
auto element = std::find(elements.begin(), elements.end(), blockType);
|
||||
|
||||
if (element == elements.end())
|
||||
try {
|
||||
auto element = elements.at(blockType);
|
||||
element(coords, scene);
|
||||
} catch (std::exception const &err) {
|
||||
return;
|
||||
element->second(coords, scene); */
|
||||
}
|
||||
}
|
||||
|
||||
void MapGenerator::createBreakable(Vector3f coords, std::shared_ptr<WAL::Scene> scene)
|
||||
@@ -91,6 +95,7 @@ namespace BBM
|
||||
scene->addEntity("Breakable Block")
|
||||
.addComponent<PositionComponent>(coords)
|
||||
.addComponent<HealthComponent>(1)
|
||||
//.addComponent<CollisionComponent>(1)
|
||||
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/wall/breakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/breakable_wall.png"));
|
||||
}
|
||||
|
||||
@@ -98,14 +103,16 @@ namespace BBM
|
||||
{
|
||||
scene->addEntity("Unbreakable Block")
|
||||
.addComponent<PositionComponent>(coords)
|
||||
//.addComponent<CollisionComponent>(1)
|
||||
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png"));
|
||||
}
|
||||
|
||||
void MapGenerator::createHole(Vector3f coords, std::shared_ptr<WAL::Scene> scene)
|
||||
{
|
||||
scene->addEntity("Hole Block")
|
||||
.addComponent<PositionComponent>(coords)
|
||||
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/wall/hole_block.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/hole_block.png"));
|
||||
.addComponent<PositionComponent>(Vector3f(coords.x, coords.y - 1, coords.z))
|
||||
//.addComponent<CollisionComponent>(1)
|
||||
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/wall/hole.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/hole.png"));
|
||||
/* .addComponent<ColliderComponent>([](const WAL::Entity &entity, WAL::Entity &other) {
|
||||
if (other.hasComponent<HealthComponent>()) {
|
||||
auto &health = other.getComponent<HealthComponent>();
|
||||
@@ -118,6 +125,7 @@ namespace BBM
|
||||
{
|
||||
scene->addEntity("Bumper Block")
|
||||
.addComponent<PositionComponent>(coords)
|
||||
//.addComponent<CollisionComponent>(1)
|
||||
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/wall/bumper_block.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/bumper_block.png"));
|
||||
/* .addComponent<ColliderComponent>([](const WAL::Entity &entity, WAL::Entity &other) {
|
||||
if (other.hasComponent<MovableComponent>()) {
|
||||
@@ -131,6 +139,7 @@ namespace BBM
|
||||
{
|
||||
scene->addEntity("Stairs Block")
|
||||
.addComponent<PositionComponent>(coords)
|
||||
//.addComponent<CollisionComponent>(1)
|
||||
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/wall/stairs_block.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/stairs_block.png"));
|
||||
}
|
||||
|
||||
@@ -143,18 +152,21 @@ namespace BBM
|
||||
}
|
||||
|
||||
MapGenerator::BlockType MapGenerator::getRandomBlockType()
|
||||
{
|
||||
std::random_device r;
|
||||
std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
|
||||
std::mt19937 e(seed);
|
||||
std::normal_distribution<> normal_dist(3, 0.2);
|
||||
return static_cast<BlockType>(static_cast<int>(normal_dist(e)) % (HOLE));
|
||||
{
|
||||
double rnd = static_cast<double>(std::rand())/RAND_MAX;
|
||||
|
||||
std::printf("%f\n", rnd);
|
||||
if (rnd > 0.95)
|
||||
return HOLE;
|
||||
if (rnd > 0.10)
|
||||
return BREAKABLE;
|
||||
return NOTHING;
|
||||
}
|
||||
|
||||
void MapGenerator::generateMap(int width, int height, int seed, std::shared_ptr<WAL::Scene> scene)
|
||||
std::map<std::tuple<int, int>, MapGenerator::BlockType> MapGenerator::createMap(int width, int height)
|
||||
{
|
||||
std::map<std::tuple<int, int>, BlockType> map;
|
||||
|
||||
|
||||
width = width % 2 ? width + 1 : width;
|
||||
height = height % 2 ? height + 1 : height;
|
||||
for (int i = 0; i < width; i++)
|
||||
@@ -177,20 +189,21 @@ namespace BBM
|
||||
map[std::make_tuple(i, j)] = BREAKABLE;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < width + 1; i++) {
|
||||
for (int j = 0; j < height + 1; j++) {
|
||||
if (!((i + 1) % 2) && !((j + 1) % 2)) {
|
||||
for (int i = 0; i < width + 1; i++)
|
||||
for (int j = 0; j < height + 1; j++)
|
||||
if (!((i + 1) % 2) && !((j + 1) % 2))
|
||||
map[std::make_tuple(i, j)] = UNBREAKABLE;
|
||||
}
|
||||
}
|
||||
}
|
||||
return (map);
|
||||
}
|
||||
|
||||
void MapGenerator::loadMap(int width, int height, std::map<std::tuple<int, int>, \
|
||||
BlockType> map, std::shared_ptr<WAL::Scene> scene)
|
||||
{
|
||||
generateWall(width, height, scene);
|
||||
generateFloor(width, height, scene);
|
||||
for (int i = 0; i < width + 1; i++) {
|
||||
for (int j = 0; j < height + 1; j++) {
|
||||
for (int i = 0; i < width + 1; i++)
|
||||
for (int j = 0; j < height + 1; j++)
|
||||
createElement(Vector3f(i, 0, j), scene, map[std::make_tuple(i, j)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace BBM
|
||||
Reference in New Issue
Block a user