// // Created by Tom Augier on 5/26/21. // Edited by Benjamin Henry on 5/26/21. // #include "Map.hpp" #include #include namespace RAY3D = RAY::Drawables::Drawables3D; namespace BBM { void Map::generateWall(int width, int height, std::shared_ptr scene) { for (int i = 0; i < width; i++) { scene->addEntity("Width Wall") .addComponent(Vector3f(i,0,height)) .addComponent>("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png")); scene->addEntity("Width Wall") .addComponent(Vector3f(i,0,0)) .addComponent>("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png")); } for (int i = 0; i < height; i++) { scene->addEntity("Height Wall") .addComponent(Vector3f(width,0,i)) .addComponent>("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png")); scene->addEntity("Height Wall") .addComponent(Vector3f(0,0,i)) .addComponent>("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png")); } scene->addEntity("Width Wall") .addComponent(Vector3f(width, 0,height)) .addComponent>("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png")); } void Map::generateFloor(int width, int height, std::shared_ptr scene) { /* RAY3D::Model model = RAY3D::Model("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png")); model.setScale(RAY::Vector3(width, 0, height)); */ for (int i = 1; i < width; i++) { for (int j = 1; j < height; j++) { scene->addEntity("Floor") .addComponent(Vector3f(i,-1,j)) .addComponent>("assets/wall/floor.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/floor.png")); } } //.addComponent>(model); } void Map::createElement(Vector3f coords, Vector3f size, std::shared_ptr scene, BlockType blockType) { if (blockType == BREAKABLE) { createBreakable(coords, size, scene); } else if (blockType == UNBREAKABLE) { createUnbreakable(coords, size, scene); } /* std::map )>> elements = { {BREAKABLE, &Map::createBreakable}, {UNBREAKABLE, &Map::createUnbreakable}, {HOLE, &Map::createHole}, {BUMPER, &Map::createBumper}, {STAIRS, &Map::createStairs} }; auto element = std::find(elements.begin(), elements.end(), blockType); if (element == elements.end()) return; element->second(coords, size, scene); */ } void Map::createBreakable(Vector3f coords, Vector3f size, std::shared_ptr scene) { scene->addEntity("Breakable Block") .addComponent(coords) //.addComponent(1) .addComponent>("assets/wall/breakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/breakable_wall.png")); //.addComponent>("assets/wall/breakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/breakable_wall.png")); } void Map::createUnbreakable(Vector3f coords, Vector3f size, std::shared_ptr scene) { scene->addEntity("Unbreakable Block") .addComponent(coords) .addComponent>("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png")); } void Map::createHole(Vector3f coords, Vector3f size, std::shared_ptr scene) { scene->addEntity("Hole Block") .addComponent(coords) .addComponent>("assets/wall/hole_block.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/hole_block.png")); /* .addComponent([](const WAL::Entity &entity, WAL::Entity &other) { if (other.hasComponent()) { auto &health = other.getComponent(); health.takeDmg(health.getHealthPoint()); } }); */ WAL::Entity entity(""); } void Map::createBumper(Vector3f coords, Vector3f size, std::shared_ptr scene) { scene->addEntity("Bumper Block") .addComponent(coords); /* .addComponent([](const WAL::Entity &entity, WAL::Entity &other) { if (other.hasComponent()) { auto &movable = other.getComponent(); movable.addForce(Vector3f(0, 0, 5)); } } */ } void Map::createStairs(Vector3f coords, Vector3f size, std::shared_ptr scene) { scene->addEntity("Stairs Block") .addComponent(coords); } bool Map::isBlockCloseToBlockType(std::map, BlockType> map, int x, int y, BlockType blockType) { if (map[std::make_tuple(x - 1, y)] == blockType || map[std::make_tuple(x + 1, y)] == blockType || map[std::make_tuple(x, y + 1)] == blockType || map[std::make_tuple(x, y - 1)] == blockType) return (true); return (false); } Map::BlockType Map::getRandomBlockType(int seed, int blockCreated) { return static_cast((seed * blockCreated * rand()) % (HOLE)); } void Map::generateMap(int width, int height, int seed, std::shared_ptr scene) { std::map, BlockType> map; 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(1, 1)] = SPAWNER; map[std::make_tuple(width - 1, 1)] = SPAWNER; map[std::make_tuple(1, height - 1)] = SPAWNER; map[std::make_tuple(width - 1, height - 1)] = SPAWNER; for (int i = 1; i < width - 1; i++) { for (int j = 1; j < height - 1; j++) { if (isBlockCloseToBlockType(map, i , j, SPAWNER)) { map[std::make_tuple(i, j)] = NOTHING; } else { map[std::make_tuple(i, j)] = getRandomBlockType(seed, i * width + j); } if (map[std::make_tuple(i, j)] != NOTHING && isBlockCloseToBlockType(map, i , j, UNBREAKABLE) && !isBlockCloseToBlockType(map, i , j, SPAWNER)) { map[std::make_tuple(i, j)] = BREAKABLE; } } } generateWall(width, height, scene); generateFloor(width, height, scene); for (int i = 1; i < width - 1; i++) { for (int j = 1; j < height - 1; j++) { createElement(Vector3f(i, 0, j), Vector3f(50,50,50), scene, map[std::make_tuple(i, j)]); } } } } // namespace BBM