From c7ab528e86c187952517edd882d8bd2c2b1400a5 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Thu, 3 Jun 2021 16:40:12 +0200 Subject: [PATCH] map generator: use variable to avoir strings repetition --- sources/Map/Map.cpp | 58 +++++++++++++++++++++++++++++++++++---------- sources/Map/Map.hpp | 21 ++++++++++++++++ wasm-python.py | 24 +++++++++++++++++++ 3 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 wasm-python.py diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index d46b247b..401aba30 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -9,10 +9,22 @@ namespace RAY3D = RAY::Drawables::Drawables3D; namespace BBM { + const std::string MapGenerator::assetsPath = "./assets/"; + const std::string MapGenerator::wallAssetsPath = MapGenerator::assetsPath + "wall/"; + const std::string MapGenerator::imageExtension = ".png"; + const std::string MapGenerator::objExtension = ".obj"; + const std::string MapGenerator::breakableWallPath = MapGenerator::wallAssetsPath + "breakable_wall"; + const std::string MapGenerator::unbreakableWallPath = MapGenerator::wallAssetsPath + "unbreakable_wall"; + const std::string MapGenerator::floorPath = MapGenerator::wallAssetsPath + "floor"; + const std::string MapGenerator::secondFloorPath = MapGenerator::wallAssetsPath + "upper_floor"; + const std::string MapGenerator::stairsPath = MapGenerator::wallAssetsPath + "stairs"; + const std::string MapGenerator::bumperPath = MapGenerator::wallAssetsPath + "bumper"; + const std::string MapGenerator::holePath = MapGenerator::wallAssetsPath + "hole"; + void MapGenerator::generateUnbreakableBlock(int width, int height, std::shared_ptr scene) { - std::string UnbreakableObj = "assets/wall/unbreakable_wall.obj"; - std::string UnbreakablePnj = "assets/wall/unbreakable_wall.png"; + static const std::string UnbreakableObj = unbreakableWallPath + objExtension; + static const std::string UnbreakablePng = unbreakableWallPath + imageExtension; for (int i = 0; i < width + 1; i++) { for (int j = 0; j < height + 1; j++) { @@ -20,7 +32,7 @@ namespace BBM scene->addEntity("Unbreakable Wall") .addComponent(Vector3f(i, 0, j)) //.addComponent(1) - .addComponent>(UnbreakableObj, std::make_pair(MAP_DIFFUSE, UnbreakablePnj)); + .addComponent>(UnbreakableObj, std::make_pair(MAP_DIFFUSE, UnbreakablePng)); } } } @@ -28,8 +40,8 @@ namespace BBM void MapGenerator::generateWall(int width, int height, std::shared_ptr scene) { - std::string UnbreakableObj = "assets/wall/unbreakable_wall.obj"; - std::string UnbreakablePnj = "assets/wall/unbreakable_wall.png"; + static const std::string UnbreakableObj = unbreakableWallPath + objExtension; + static const std::string UnbreakablePnj = unbreakableWallPath + imageExtension; scene->addEntity("Bottom Wall") .addComponent(Vector3f((width + 1) / 2, 0, -1)) @@ -51,13 +63,16 @@ namespace BBM void MapGenerator::generateFloor(MapBlock map, int width, int height, std::shared_ptr scene) { + static const std::string floorObj = floorPath + objExtension; + static const std::string floorPng = floorPath + imageExtension; + for (int i = 0; i < width + 1; i++) { for (int j = 0; j < height + 1; j++) { if (map[std::make_tuple(i, 0, j)] != HOLE && map[std::make_tuple(i, 0, j)] != BUMPER) scene->addEntity("Unbreakable Wall") .addComponent(Vector3f(i, -1, j)) //.addComponent(1) - .addComponent>("assets/wall/floor.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/floor.png")); + .addComponent>(floorObj, std::make_pair(MAP_DIFFUSE, floorPng)); } } } @@ -84,43 +99,56 @@ namespace BBM void MapGenerator::createBreakable(Vector3f coords, std::shared_ptr scene) { + static const std::string breakableObj = breakableWallPath + objExtension; + static const std::string breakablePng = breakableWallPath + imageExtension; 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")); + .addComponent>(breakableObj, std::make_pair(MAP_DIFFUSE, breakablePng)); } void MapGenerator::createFloor(Vector3f coords, std::shared_ptr scene) { + static const std::string floorObj = floorPath + objExtension; + static const std::string floorPng = floorPath + imageExtension; + scene->addEntity("Floor") .addComponent(Vector3f(coords)) //.addComponent(1) - .addComponent>("assets/wall/floor.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/floor.png")); + .addComponent>(floorObj, std::make_pair(MAP_DIFFUSE, floorPng)); } void MapGenerator::createUpperFloor(Vector3f coords, std::shared_ptr scene) { + static const std::string floorObj = secondFloorPath + objExtension; + static const std::string floorPng = secondFloorPath + imageExtension; + scene->addEntity("Upper Floor") .addComponent(Vector3f(coords)) //.addComponent(1) - .addComponent>("assets/wall/upper_floor.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/upper_floor.png")); + .addComponent>(floorObj, std::make_pair(MAP_DIFFUSE, floorPng)); } void MapGenerator::createUnbreakable(Vector3f coords, std::shared_ptr scene) { + static const std::string UnbreakableObj = unbreakableWallPath + objExtension; + static const std::string UnbreakablePng = unbreakableWallPath + imageExtension; + scene->addEntity("Unbreakable Block") .addComponent(coords) //.addComponent(1) - .addComponent>("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png")); + .addComponent>(UnbreakableObj, std::make_pair(MAP_DIFFUSE, UnbreakablePng)); } void MapGenerator::createHole(Vector3f coords, std::shared_ptr scene) { + static const std::string holeObj = holePath + objExtension; + static const std::string holePng = holePath + imageExtension; scene->addEntity("Hole Block") .addComponent(Vector3f(coords.x, coords.y - 1, coords.z)) - .addComponent>("assets/wall/hole.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/hole.png")); + .addComponent>(holeObj, std::make_pair(MAP_DIFFUSE, holePng)); /* .addComponent([](WAL::Entity &other, const WAL::Entity &entity) { if (other.hasComponent()) { auto &health = other.getComponent(); @@ -131,10 +159,12 @@ namespace BBM void MapGenerator::createBumper(Vector3f coords, std::shared_ptr scene) { + static const std::string bumperObj = bumperPath + objExtension; + static const std::string bumperPng = bumperPath + imageExtension; std::cout << "Bumper Created" << std::endl; scene->addEntity("Bumper Block") .addComponent(Vector3f(coords.x, coords.y, coords.z)) - .addComponent>("assets/wall/bumper.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/bumper.png")); + .addComponent>(bumperObj, std::make_pair(MAP_DIFFUSE, bumperPng)); /* .addComponent([](const WAL::Entity &entity, WAL::Entity &other) { if (other.hasComponent()) { auto &movable = other.getComponent(); @@ -145,11 +175,13 @@ namespace BBM void MapGenerator::createStairs(Vector3f coords, std::shared_ptr scene) { + static const std::string stairsObj = stairsPath + objExtension; + static const std::string stairsPng = stairsPath + imageExtension; std::cout << "Stairs Created" << std::endl; scene->addEntity("Stairs Block") .addComponent(coords) //.addComponent(1) - .addComponent>("assets/wall/stairs.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/stairs.png")); + .addComponent>(stairsObj, std::make_pair(MAP_DIFFUSE, stairsPng)); } bool MapGenerator::isCloseToBlockType(std::map, BlockType> map, int x, int y, int z, BlockType blockType) diff --git a/sources/Map/Map.hpp b/sources/Map/Map.hpp index 865e6563..46f9fc06 100644 --- a/sources/Map/Map.hpp +++ b/sources/Map/Map.hpp @@ -132,6 +132,27 @@ namespace BBM static MapBlock cleanBreakable(MapBlock map, int width, int height); + static const std::string assetsPath; + + static const std::string wallAssetsPath; + + static const std::string imageExtension; + + static const std::string objExtension; + + static const std::string unbreakableWallPath; + + static const std::string breakableWallPath; + + static const std::string floorPath; + + static const std::string stairsPath; + + static const std::string bumperPath; + + static const std::string secondFloorPath; + + static const std::string holePath; public: diff --git a/wasm-python.py b/wasm-python.py new file mode 100644 index 00000000..77aad1db --- /dev/null +++ b/wasm-python.py @@ -0,0 +1,24 @@ +# Python 3 + +import sys +import socketserver +from http.server import SimpleHTTPRequestHandler + +class WasmHandler(SimpleHTTPRequestHandler): + def end_headers(self): + # Include additional response headers here. CORS for example: + #self.send_header('Access-Control-Allow-Origin', '*') + SimpleHTTPRequestHandler.end_headers(self) + + +# Python 3.7.5 adds in the WebAssembly Media Type. If this is an older +# version, add in the Media Type. +if sys.version_info < (3, 7, 5): + WasmHandler.extensions_map['.wasm'] = 'application/wasm' + + +if __name__ == '__main__': + PORT = 8080 + with socketserver.TCPServer(("", PORT), WasmHandler) as httpd: + print("Listening on port {}. Press Ctrl+C to stop.".format(PORT)) + httpd.serve_forever()