From c905803fc5fe875e325c8e01b06fbee3462a2b13 Mon Sep 17 00:00:00 2001 From: Askou Date: Tue, 1 Jun 2021 15:08:21 +0200 Subject: [PATCH] Add hole block and separate load/create map --- assets/wall/hole.mtl | 12 +++++ assets/wall/hole.obj | 61 ++++++++++++++++++++++++++ assets/wall/hole.png | Bin 0 -> 1508 bytes sources/Map/Map.cpp | 89 ++++++++++++++++++++++---------------- sources/Map/Map.hpp | 16 ++++++- sources/Runner/Runner.cpp | 3 +- 6 files changed, 140 insertions(+), 41 deletions(-) create mode 100644 assets/wall/hole.mtl create mode 100644 assets/wall/hole.obj create mode 100644 assets/wall/hole.png diff --git a/assets/wall/hole.mtl b/assets/wall/hole.mtl new file mode 100644 index 00000000..7925a729 --- /dev/null +++ b/assets/wall/hole.mtl @@ -0,0 +1,12 @@ +# Blender MTL File: 'None' +# Material Count: 1 + +newmtl Cube.001 +Ns 225.000000 +Ka 1.000000 1.000000 1.000000 +Kd 0.800000 0.800000 0.800000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 diff --git a/assets/wall/hole.obj b/assets/wall/hole.obj new file mode 100644 index 00000000..519fef7e --- /dev/null +++ b/assets/wall/hole.obj @@ -0,0 +1,61 @@ +# hole.obj +# + +mtllib hole.mtl +o hole + +v -0.500000 -0.500000 0.500000 +v 0.500000 -0.500000 0.500000 +v -0.500000 0.500000 0.500000 +v 0.500000 0.500000 0.500000 +v -0.500000 0.500000 -0.500000 +v 0.500000 0.500000 -0.500000 +v -0.500000 -0.500000 -0.500000 +v 0.500000 -0.500000 -0.500000 + +vt 0.000000 0.000000 +vt 0.333333 0.000000 +vt 0.666666 0.000000 +vt 1.000000 0.000000 +vt 0.000000 0.500000 +vt 0.333333 0.500000 +vt 0.666666 0.500000 +vt 1.000000 0.500000 +vt 0.000000 1.000000 +vt 0.333333 1.000000 +vt 0.666666 1.000000 +vt 1.000000 1.000000 + +vn 0.000000 0.000000 1.000000 +vn 0.000000 1.000000 0.000000 +vn 0.000000 0.000000 -1.000000 +vn 0.000000 -1.000000 0.000000 +vn 1.000000 0.000000 0.000000 +vn -1.000000 0.000000 0.000000 + +g floor +usemtl floor +# Back +s 1 +f 1/1/1 2/4/1 3/9/1 +f 3/9/1 2/4/1 4/12/1 +# Top +s 2 +f 3/1/2 4/4/2 5/9/2 +f 5/9/2 4/4/2 6/12/2 +# Front +s 3 +f 5/1/3 6/4/3 7/9/3 +f 7/9/3 6/4/3 8/12/3 +# Bottom +s 4 +f 7/1/4 8/4/4 1/9/4 +f 1/9/4 8/4/4 2/12/4 +# Right +s 5 +f 2/1/5 8/4/5 4/9/5 +f 4/9/5 8/4/5 6/12/5 +# Left +s 6 +f 7/1/6 1/4/6 5/9/6 +f 5/9/6 1/4/6 3/12/6 \ No newline at end of file diff --git a/assets/wall/hole.png b/assets/wall/hole.png new file mode 100644 index 0000000000000000000000000000000000000000..fe9ee288cdbedc7595e723150cb42fa83e6ee7bf GIT binary patch literal 1508 zcmeAS@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7xzrV0uv%5>XPASgue|l%JNFld4cs zS&*ubT9KK?z)*4P?ey45ceHp~??<{$Yfx!9Qe^o; zv!&|n`VFts6|Lgl9$YW@&ATJ-VeQGN%CEmSPu}o3b9o8p)WWi&!s@cGJ6dj;7Va*( z`?BnH&b8tSLy1LKM7cRX3ryOPZEriFbaKd+%IhtaDx9?wg~}dJmtgwzsj}bf*mH)x zEI%)D%P+3~ZpSkJb?S8fjc3ApF8qAR@REJ^ozovaA1bOovYuIuf8}|{d7FRFdHiSB zEF+~^>w9h;T-MH`CdaY!&2^)$hhblGr#C&?c#Q3d?Y4>qCR#@p7%{893dy{5BX=>I zxG=xjmNUEyrky>yqhRu6VJ=@Y%}YlQZu-cPFs&s&K4X3Kx2j$l?eaq*Y8`@cKd%e@ z%l=UAHDmKyfxs`;VIR7~;|}+_=3Sk^s$im@w~P6YhWNi9Q|vuL1+K`Z9cRB*F-NHV z()T-)&PE#eUz``+(e%}uLGjQnu5=Fvg*2h1J{M>7auk*@tokrV^&=zGl2fvNMM4KF zQ{L>kcFfwXT2DJYE^Ur4!@?VpY-X-n2}_rpkoWGBS*0~A#B}vacHOAWPo|hY Cx zx_V^N%2eZG?XXyzomFMU-ydx)5S-H(RkZQZ(kW4vAvz%oLk;&ID*B-4ZuR9vyKLQ* z#b!(Vy?vweuI-r`W?lZ$LaplEwY}5h*jpPXCoNg1%r9+sYK8Dlv(#lPjs0iaonB+U zwfoibPtsRjEIKy(%B*yA(}?Dq&P~?}7j;W^6ey{8$=D22ZUfKPVe}%R$_~(7+NBWaPPT$Q>urBz0<}Le`U%%#N&%UvfJKSe+ z!uQ0Q3f?!Re^*?!&%N)Izi|77=oh+Q#HzUe^0%n{t2ki4@uBqhu(@q<>l9}&Ffg`c zIy(n=Iy=MCJTRrtshw!+aX3Jv)jxRaB2jkh4vUl(eqt`JGF!P^R|sUahROQG{bKi( znj|9besJr92j`Qn9y}Tu&K~Y4@Q1OmxHza}qR@M9F5RR(A1dUZ-z~0qZ|AUL`pmS_ z9_Cqjvsb#roJk2kdO>@afP+Nfms4kqo~dYZ-v700dEBbq66Zg?|GoLwl{rTn8a!-H z^ITTDr!eo{7T$Bej|hfM34K)j$Z)cr$Db3WM<1J?W1Y@APtx%6%F0I^8zVB3m-a?p zywb6}WAUnG!TTnBjCi`|Xry*{(R{~fRu_bxC zyDx`7I;J!Gca%qgD@k*tT`ZM>?NMQuIx|Q7 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(Vector3f(i, 0, j)) + //.addComponent(1) .addComponent>(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(Vector3f(i,0,height + 1)) + .addComponent(Vector3f(i, 0, height + 1)) + //.addComponent(1) .addComponent>(UnbreakableObj, std::make_pair(MAP_DIFFUSE, UnbreakablePnj)); scene->addEntity("Vertical Wall") - .addComponent(Vector3f(i,0,-1)) + .addComponent(Vector3f(i, 0, -1)) + //.addComponent(1) .addComponent>(UnbreakableObj, std::make_pair(MAP_DIFFUSE, UnbreakablePnj)); } for (int i = -1; i < height + 1; i++) { scene->addEntity("Horizontal Wall") - .addComponent(Vector3f(width + 1,0,i)) + .addComponent(Vector3f(width + 1, 0, i)) + //.addComponent(1) .addComponent>(UnbreakableObj, std::make_pair(MAP_DIFFUSE, UnbreakablePnj)); scene->addEntity("Horizontal Wall") - .addComponent(Vector3f(-1,0,i)) + .addComponent(Vector3f(-1, 0, i)) + //.addComponent(1) .addComponent>(UnbreakableObj, std::make_pair(MAP_DIFFUSE, UnbreakablePnj)); } scene->addEntity("Vertical Wall") .addComponent(Vector3f(width + 1, 0, height + 1)) + //.addComponent(1) .addComponent>(UnbreakableObj, std::make_pair(MAP_DIFFUSE, UnbreakablePnj)); } - void MapGenerator::generateFloor(int width, int height, std::shared_ptr scene) + void MapGenerator::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")); @@ -60,6 +66,7 @@ namespace BBM for (int j = 0; j < height + 1; j++) { scene->addEntity("Floor") .addComponent(Vector3f(i,-1,j)) + //.addComponent(1) .addComponent>("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 scene, BlockType blockType) { - if (blockType == BREAKABLE) { - createBreakable(coords, scene); - } else if (blockType == UNBREAKABLE) { - createUnbreakable(coords, scene); - } - /* std::map> elements = { + std::map 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 scene) @@ -91,6 +95,7 @@ namespace BBM scene->addEntity("Breakable Block") .addComponent(coords) .addComponent(1) + //.addComponent(1) .addComponent>("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(coords) + //.addComponent(1) .addComponent>("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png")); } void MapGenerator::createHole(Vector3f coords, 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(Vector3f(coords.x, coords.y - 1, coords.z)) + //.addComponent(1) + .addComponent>("assets/wall/hole.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/hole.png")); /* .addComponent([](const WAL::Entity &entity, WAL::Entity &other) { if (other.hasComponent()) { auto &health = other.getComponent(); @@ -118,6 +125,7 @@ namespace BBM { scene->addEntity("Bumper Block") .addComponent(coords) + //.addComponent(1) .addComponent>("assets/wall/bumper_block.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/bumper_block.png")); /* .addComponent([](const WAL::Entity &entity, WAL::Entity &other) { if (other.hasComponent()) { @@ -131,6 +139,7 @@ namespace BBM { scene->addEntity("Stairs Block") .addComponent(coords) + //.addComponent(1) .addComponent>("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(static_cast(normal_dist(e)) % (HOLE)); + { + double rnd = static_cast(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 scene) + std::map, MapGenerator::BlockType> MapGenerator::createMap(int width, int height) { std::map, 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, \ +BlockType> map, std::shared_ptr 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 \ No newline at end of file diff --git a/sources/Map/Map.hpp b/sources/Map/Map.hpp index d519c52d..3f97c7c3 100644 --- a/sources/Map/Map.hpp +++ b/sources/Map/Map.hpp @@ -7,6 +7,8 @@ #pragma once #include +#include +#include #include #include #include @@ -26,7 +28,7 @@ namespace BBM class MapGenerator { private: - using MapElem = void (Vector3f coords, std::shared_ptr scene); + using MapElem = std::function scene)>; //! @brief Enum of the block available. enum BlockType { NOTHING, @@ -52,6 +54,7 @@ namespace BBM //! @param scene Scene where the map is instanced //! @brief Generate the unbreakable block of the map static void generateUnbreakableBlock(int width, int height, std::shared_ptr scene); + //! @param width Width of the map //! @param height Height of the map //! @param scene Scene where the map is instanced @@ -93,12 +96,21 @@ namespace BBM //! @param scene Scene where the map is instanced //! @brief Create stair of the map static void createStairs(Vector3f coords, std::shared_ptr scene); + public: + //! @param width Width of the map //! @param height Height of the map + //! @brief Generate map of block to be loaded + static std::map, BlockType> 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 generateMap(int width, int height, int seed, std::shared_ptr scene); + static void loadMap(int width, int height, std::map, \ +BlockType> map, std::shared_ptr scene); }; } // namespace BBM \ No newline at end of file diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 8c14ad31..a02a51a0 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -77,7 +77,8 @@ namespace BBM scene->addEntity("camera") .addComponent(0, 20, -5) .addComponent(); - MapGenerator::generateMap(15, 15, rand(), scene); + std::srand(std::time(NULL)); + MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene); return scene; }