mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-06-08 03:50:47 +00:00
basic map with hole
This commit is contained in:
+76
-38
@@ -78,8 +78,9 @@ namespace BBM
|
||||
{BREAKABLE, &createBreakable},
|
||||
{UNBREAKABLE, &createUnbreakable},
|
||||
{HOLE, &createHole},
|
||||
/* {BUMPER, &Map::createBumper},
|
||||
{STAIRS, &Map::createStairs} */
|
||||
{FLOOR, &createFloor},
|
||||
/* {BUMPER, &createBumper},
|
||||
{STAIRS, &createStairs} */
|
||||
};
|
||||
|
||||
try {
|
||||
@@ -99,6 +100,14 @@ namespace BBM
|
||||
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/wall/breakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/breakable_wall.png"));
|
||||
}
|
||||
|
||||
void MapGenerator::createFloor(Vector3f coords, std::shared_ptr<WAL::Scene> scene)
|
||||
{
|
||||
scene->addEntity("Floor")
|
||||
.addComponent<PositionComponent>(Vector3f(coords))
|
||||
//.addComponent<CollisionComponent>(1)
|
||||
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/wall/floor.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/floor.png"));
|
||||
}
|
||||
|
||||
void MapGenerator::createUnbreakable(Vector3f coords, std::shared_ptr<WAL::Scene> scene)
|
||||
{
|
||||
scene->addEntity("Unbreakable Block")
|
||||
@@ -111,9 +120,8 @@ namespace BBM
|
||||
{
|
||||
scene->addEntity("Hole Block")
|
||||
.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) {
|
||||
/* .addComponent<CollisionComponent>([](const WAL::Entity &entity, WAL::Entity &other) {
|
||||
if (other.hasComponent<HealthComponent>()) {
|
||||
auto &health = other.getComponent<HealthComponent>();
|
||||
health.takeDmg(health.getHealthPoint());
|
||||
@@ -124,15 +132,14 @@ namespace BBM
|
||||
void MapGenerator::createBumper(Vector3f coords, std::shared_ptr<WAL::Scene> scene)
|
||||
{
|
||||
scene->addEntity("Bumper Block")
|
||||
.addComponent<PositionComponent>(coords)
|
||||
//.addComponent<CollisionComponent>(1)
|
||||
.addComponent<PositionComponent>(Vector3f(coords.x, coords.y - 1, coords.z))
|
||||
.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>()) {
|
||||
auto &movable = other.getComponent<MovableComponent>();
|
||||
movable.addForce(Vector3f(0, 5, 0));
|
||||
}
|
||||
} */
|
||||
/* .addComponent<CollisionComponent>([](const WAL::Entity &entity, WAL::Entity &other) {
|
||||
if (other.hasComponent<MovableComponent>()) {
|
||||
auto &movable = other.getComponent<MovableComponent>();
|
||||
movable.addForce(Vector3f(0, 5, 0));
|
||||
}
|
||||
} */
|
||||
}
|
||||
|
||||
void MapGenerator::createStairs(Vector3f coords, std::shared_ptr<WAL::Scene> scene)
|
||||
@@ -143,19 +150,18 @@ namespace BBM
|
||||
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/wall/stairs_block.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/stairs_block.png"));
|
||||
}
|
||||
|
||||
bool MapGenerator::isCloseToBlockType(std::map<std::tuple<int, int>, BlockType> map, int x, int z, BlockType blockType)
|
||||
bool MapGenerator::isCloseToBlockType(std::map<std::tuple<int, int, int>, BlockType> map, int x, int y, int z, BlockType blockType)
|
||||
{
|
||||
return (map[std::make_tuple(x - 1, z)] == blockType ||
|
||||
map[std::make_tuple(x + 1, z)] == blockType ||
|
||||
map[std::make_tuple(x, z + 1)] == blockType ||
|
||||
map[std::make_tuple(x, z - 1)] == blockType);
|
||||
return (map[std::make_tuple(x - 1, y, z)] == blockType ||
|
||||
map[std::make_tuple(x + 1, y, z)] == blockType ||
|
||||
map[std::make_tuple(x, y, z + 1)] == blockType ||
|
||||
map[std::make_tuple(x, y, z - 1)] == blockType);
|
||||
}
|
||||
|
||||
|
||||
MapGenerator::BlockType MapGenerator::getRandomBlockType()
|
||||
{
|
||||
double rnd = static_cast<double>(std::rand())/RAND_MAX;
|
||||
|
||||
std::printf("%f\n", rnd);
|
||||
if (rnd > 0.95)
|
||||
return HOLE;
|
||||
if (rnd > 0.10)
|
||||
@@ -163,47 +169,79 @@ namespace BBM
|
||||
return NOTHING;
|
||||
}
|
||||
|
||||
std::map<std::tuple<int, int>, MapGenerator::BlockType> MapGenerator::createMap(int width, int height)
|
||||
MapGenerator::MapBlock MapGenerator::createHeight(MapBlock map, int width, int height)
|
||||
{
|
||||
std::map<std::tuple<int, int>, BlockType> map;
|
||||
double rnd = static_cast<double>(std::rand())/RAND_MAX;
|
||||
|
||||
if (rnd > 0.60)
|
||||
for (int i = 0; i < width; i++) {
|
||||
map[std::make_tuple(i, 1, height)] = map[std::make_tuple(i, 0, height)];
|
||||
map[std::make_tuple(i, 0, height)] = NOTHING;
|
||||
}
|
||||
for (int j = 0; j < height; j++) {
|
||||
map[std::make_tuple(width, 1, j)] = map[std::make_tuple(width, 0, j)];
|
||||
map[std::make_tuple(width, 0, j)] = NOTHING;
|
||||
}
|
||||
if (rnd > 0.30)
|
||||
for (int i = width - width/4; i < width + width/4 + 1; i++) {
|
||||
for (int j = height - height/4; j < height + height/4 + 1; j++) {
|
||||
map[std::make_tuple(i, 1, j)] = map[std::make_tuple(i, 0, j)];
|
||||
map[std::make_tuple(i, 0, j)] = NOTHING;
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
MapGenerator::MapBlock MapGenerator::createSpawner(MapBlock map, int width, int height)
|
||||
{
|
||||
map[std::make_tuple(0, 0, 0)] = SPAWNER;
|
||||
map[std::make_tuple(width, 0, 0)] = SPAWNER;
|
||||
map[std::make_tuple(0, 0, height)] = SPAWNER;
|
||||
map[std::make_tuple(width, 0, height)] = SPAWNER;
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
MapGenerator::MapBlock MapGenerator::createMap(int width, int height)
|
||||
{
|
||||
MapBlock map;
|
||||
|
||||
width = width % 2 ? width + 1 : width;
|
||||
height = height % 2 ? height + 1 : height;
|
||||
for (int i = 0; i < width; i++)
|
||||
for (int j = 0; j < height; j++)
|
||||
map[std::make_tuple(i, j)] = NOTHING;
|
||||
map[std::make_tuple(0, 0)] = SPAWNER;
|
||||
map[std::make_tuple(width, 0)] = SPAWNER;
|
||||
map[std::make_tuple(0, height)] = SPAWNER;
|
||||
map[std::make_tuple(width, height)] = SPAWNER;
|
||||
map[std::make_tuple(i, 0, j)] = NOTHING;
|
||||
map = createSpawner(map, width, height);
|
||||
for (int i = 0; i < width + 1; i++) {
|
||||
for (int j = 0; j < height + 1; j++) {
|
||||
if (map[std::make_tuple(i, j)] == SPAWNER)
|
||||
if (map[std::make_tuple(i, 0, j)] == SPAWNER)
|
||||
continue;
|
||||
if (isCloseToBlockType(map, i , j, SPAWNER)) {
|
||||
map[std::make_tuple(i, j)] = NOTHING;
|
||||
if (isCloseToBlockType(map, i , 0, j, SPAWNER)) {
|
||||
map[std::make_tuple(i, 0, j)] = NOTHING;
|
||||
} else {
|
||||
map[std::make_tuple(i, j)] = getRandomBlockType();
|
||||
map[std::make_tuple(i, 0, j)] = getRandomBlockType();
|
||||
}
|
||||
if (map[std::make_tuple(i, j)] == UNBREAKABLE && isCloseToBlockType(map, i, j, UNBREAKABLE))
|
||||
map[std::make_tuple(i, j)] = BREAKABLE;
|
||||
if (map[std::make_tuple(i, 0, j)] == UNBREAKABLE && isCloseToBlockType(map, i, 0, j, UNBREAKABLE))
|
||||
map[std::make_tuple(i, 0, 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))
|
||||
map[std::make_tuple(i, j)] = UNBREAKABLE;
|
||||
map[std::make_tuple(i, 0, j)] = UNBREAKABLE;
|
||||
//map = createHeight(map, width, height);
|
||||
return (map);
|
||||
}
|
||||
|
||||
void MapGenerator::loadMap(int width, int height, std::map<std::tuple<int, int>, \
|
||||
|
||||
void MapGenerator::loadMap(int width, int height, std::map<std::tuple<int, 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++)
|
||||
createElement(Vector3f(i, 0, j), scene, map[std::make_tuple(i, j)]);
|
||||
for (int x = 0; x < width + 1; x++)
|
||||
for (int z = 0; z < height + 1; z++)
|
||||
for (int y = 0; y < 1 + 1; y++)
|
||||
createElement(Vector3f(x, y, z), scene, map[std::make_tuple(x, y, z)]);
|
||||
}
|
||||
|
||||
} // namespace BBM
|
||||
+25
-4
@@ -34,12 +34,15 @@ namespace BBM
|
||||
NOTHING,
|
||||
BREAKABLE,
|
||||
HOLE,
|
||||
FLOOR,
|
||||
BUMPER,
|
||||
STAIRS,
|
||||
SPAWNER,
|
||||
UNBREAKABLE
|
||||
};
|
||||
|
||||
using MapBlock = std::map<std::tuple<int, int, int>, BlockType>;
|
||||
|
||||
//! @brief Generate random block type
|
||||
static BlockType getRandomBlockType();
|
||||
|
||||
@@ -47,7 +50,7 @@ namespace BBM
|
||||
//! @param x x index on the block
|
||||
//! @param z z index on the block
|
||||
//! @param blockType blockType to compare with position
|
||||
static bool isCloseToBlockType(std::map<std::tuple<int, int>, BlockType> map, int x, int z, BlockType blockType);
|
||||
static bool isCloseToBlockType(std::map<std::tuple<int, int, int>, BlockType> map, int x, int y, int z, BlockType blockType);
|
||||
|
||||
//! @param width Width of the map
|
||||
//! @param height Height of the map
|
||||
@@ -92,25 +95,43 @@ namespace BBM
|
||||
//! @brief Create bumper of the map
|
||||
static void createBumper(Vector3f coords, std::shared_ptr<WAL::Scene> scene);
|
||||
|
||||
//! @param coords coords of the element
|
||||
//! @param scene Scene where the map is instanced
|
||||
//! @brief Create bumper of the map
|
||||
static void createFloor(Vector3f coords, std::shared_ptr<WAL::Scene> scene);
|
||||
|
||||
|
||||
//! @param coords coords of the element
|
||||
//! @param scene Scene where the map is instanced
|
||||
//! @brief Create stair of the map
|
||||
static void createStairs(Vector3f coords, std::shared_ptr<WAL::Scene> scene);
|
||||
|
||||
//! @param map Map to load with block declared inside
|
||||
//! @param width Width of the map
|
||||
//! @param height Height of the map
|
||||
//! @brief Generate map of block to be loaded
|
||||
static MapBlock createSpawner(MapBlock map, int width, int height);
|
||||
|
||||
//! @param map Map to load with block declared inside
|
||||
//! @param width Width of the map
|
||||
//! @param height Height of the map
|
||||
//! @brief Generate height for the map
|
||||
static MapBlock createHeight(MapBlock map, int width, int height);
|
||||
|
||||
|
||||
public:
|
||||
|
||||
//! @param width Width of the map
|
||||
//! @param height Height of the map
|
||||
//! @brief Generate map of block to be loaded
|
||||
static std::map<std::tuple<int, int>, BlockType> createMap(int width, int height);
|
||||
static MapBlock createMap(int width, int height);
|
||||
|
||||
//! @param width Width of the map
|
||||
//! @param height Height of the map
|
||||
//! @param map Map to load with block declared inside
|
||||
//! @param scene Scene where the map is instanced
|
||||
//! @brief Generate the map
|
||||
static void loadMap(int width, int height, std::map<std::tuple<int, int>, \
|
||||
BlockType> map, std::shared_ptr<WAL::Scene> scene);
|
||||
static void loadMap(int width, int height, MapBlock map, std::shared_ptr<WAL::Scene> scene);
|
||||
|
||||
};
|
||||
} // namespace BBM
|
||||
@@ -75,8 +75,8 @@ namespace BBM
|
||||
.addComponent<KeyboardComponent>()
|
||||
.addComponent<MovableComponent>();
|
||||
scene->addEntity("camera")
|
||||
.addComponent<PositionComponent>(0, 20, -5)
|
||||
.addComponent<CameraComponent>();
|
||||
.addComponent<PositionComponent>(8, 20, 7)
|
||||
.addComponent<CameraComponent>(Vector3f(8, 0, 8));
|
||||
std::srand(std::time(NULL));
|
||||
MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene);
|
||||
return scene;
|
||||
|
||||
Reference in New Issue
Block a user