From 4942bd8e9974a0fd2ee8042400e3debb887284e5 Mon Sep 17 00:00:00 2001 From: TrueBabyChaise Date: Fri, 28 May 2021 10:10:18 +0200 Subject: [PATCH] Remove ColliderComponent and Fix issues on map --- .../Component/Collider/ColliderComponent.cpp | 23 --- .../Component/Collider/ColliderComponent.hpp | 38 ----- sources/Map/Map.cpp | 133 +++++++++--------- sources/Map/Map.hpp | 27 ++-- 4 files changed, 83 insertions(+), 138 deletions(-) delete mode 100644 sources/Component/Collider/ColliderComponent.cpp delete mode 100644 sources/Component/Collider/ColliderComponent.hpp diff --git a/sources/Component/Collider/ColliderComponent.cpp b/sources/Component/Collider/ColliderComponent.cpp deleted file mode 100644 index f50d7b7f..00000000 --- a/sources/Component/Collider/ColliderComponent.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// -// Created by Tom Augier on 5/26/21. -// Edited by Benjamin Henry on 5/26/21. -// - -#include "ColliderComponent.hpp" - -namespace BBM -{ - ColliderComponent::ColliderComponent(WAL::Entity &entity) - : Component(entity) - {} - - ColliderComponent::ColliderComponent(WAL::Entity &entity, std::function callback) - : Component(entity), - onCollide(callback) - {} - - WAL::Component *ColliderComponent::clone(WAL::Entity &entity) const - { - return new ColliderComponent(entity); - } -} // namespace WAL \ No newline at end of file diff --git a/sources/Component/Collider/ColliderComponent.hpp b/sources/Component/Collider/ColliderComponent.hpp deleted file mode 100644 index b1826268..00000000 --- a/sources/Component/Collider/ColliderComponent.hpp +++ /dev/null @@ -1,38 +0,0 @@ -// -// Created by Tom Augier on 5/26/21. -// Edited by Benjamin Henry on 5/26/21. -// - -#pragma once - -#include "Models/Vector3.hpp" -#include "Entity/Entity.hpp" -#include "Models/Callback.hpp" - -namespace BBM -{ - //! @brief A component to place on entities that can collide. - class ColliderComponent : public WAL::Component - { - public: - - WAL::Callback onCollide; - - //! @inherit - WAL::Component *clone(WAL::Entity &entity) const override; - - //! @brief Create a new collider component. - explicit ColliderComponent(WAL::Entity &entity); - - //! @brief Create a new collider component with a callback. - ColliderComponent::ColliderComponent(WAL::Entity &entity, std::function callback); - //! @brief A collider component is copy constructable. - ColliderComponent(const ColliderComponent &) = default; - //! @brief A default destructor - ~ColliderComponent() override = default; - //! @brief A collider component is not assignable. - ColliderComponent &operator=(const ColliderComponent &) = delete; - - friend class colliderSystem; - }; -} // namespace WAL \ No newline at end of file diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index 2f18897e..79018043 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -3,36 +3,44 @@ // Edited by Benjamin Henry on 5/26/21. // -#include "map.hpp" +#include "Map.hpp" +#include namespace BBM { - static void Map::generateWall(int width, int height, WAL::Scene &scene) - { - - } - - static void Map::generateFloor(int width, int height, WAL::Scene &scene) + void Map::generateWall(int width, int height, WAL::Scene &scene) { + WAL::Entity entity("Unbreakable Block"); + entity.addComponent(Vector3f(0,0,0)); + scene.addEntity(entity); } - static void Map::createElement(Vector3f coords, Vector3f size, WAL::Scene &scene, BlockType blockType) + void Map::generateFloor(int width, int height, WAL::Scene &scene) + { + WAL::Entity entity("Unbreakable Block"); + + entity.addComponent(Vector3f(0,0,0)); + scene.addEntity(entity); + } + + void Map::createElement(Vector3f coords, Vector3f size, WAL::Scene &scene, BlockType blockType) { std::map> elements = { {BREAKABLE, &createBreakable}, {UNBREAKABLE, &createUnbreakable}, - {HOLE, createHole}, - {BUMPER, createBumper}, - {STAIRS, createStairs} + {HOLE, &createHole}, + {BUMPER, &createBumper}, + {STAIRS, &createStairs} }; - - for (auto element : elements) - if (element.first == blockType) - element.second(coords, size, scene); + auto element = std::find(elements.begin(), elements.end(), blockType); + + if (element == elements.end()) + return; + element->second(coords, size, scene); } - static void Map::createBreakable(Vector3f coords, Vector3f size, WAL::Scene &scene) + void Map::createBreakable(Vector3f coords, Vector3f size, WAL::Scene &scene) { WAL::Entity entity("Breakable Block"); @@ -41,7 +49,7 @@ namespace BBM scene.addEntity(entity); } - static void Map::createUnbreakable(Vector3f coords, Vector3f size, WAL::Scene &scene) + void Map::createUnbreakable(Vector3f coords, Vector3f size, WAL::Scene &scene) { WAL::Entity entity("Unbreakable Block"); @@ -49,46 +57,46 @@ namespace BBM scene.addEntity(entity); } - static void Map::createHole(Vector3f coords, Vector3f size, WAL::Scene &scene) + void Map::createHole(Vector3f coords, Vector3f size, WAL::Scene &scene) { WAL::Entity entity("Hole Block"); entity.addComponent(coords); - entity.addComponent([](const WAL::Entity &entity, WAL::Entity &other) { + /* entity.addComponent([](const WAL::Entity &entity, WAL::Entity &other) { //En commentaire car manque le HealthComponent sur la branche (pour pas gĂȘner au niveau des erreurs) - /* if (other.hasComponent()) { + if (other.hasComponent()) { auto &health = other.getComponent(); health.takeDmg(health.getHealthPoint()); - } */ - }); + } + }); */ scene.addEntity(entity); } - static void Map::createBumper(Vector3f coords, Vector3f size, WAL::Scene &scene) + void Map::createBumper(Vector3f coords, Vector3f size, WAL::Scene &scene) { WAL::Entity entity("Bumper Block"); entity.addComponent(coords); - entity.addComponent([](const WAL::Entity &entity, WAL::Entity &other) { + /* entity.addComponent([](const WAL::Entity &entity, WAL::Entity &other) { if (other.hasComponent()) { auto &movable = other.getComponent(); movable.addForce(Vector3f(0, 0, 5)); } - } + } */ scene.addEntity(entity); } - static bool Map::isBlockCloseToPlayer(std::map, char> map, int x, int y) + bool Map::isBlockCloseToPlayer(std::map, BlockType> map, int x, int y) { - if (map[x - 1, y] == '*' || - map[x + 1, y] == '*' || - map[x, y + 1] == '*' || - map[x, y - 1] == '*' ||) - return (true) - return (false) + if (map[std::make_tuple(x - 1, y)] == '*' || + map[std::make_tuple(x + 1, y)] == '*' || + map[std::make_tuple(x, y + 1)] == '*' || + map[std::make_tuple(x, y - 1)] == '*') + return (true); + return (false); } - static void Map::createStairs(Vector3f coords, Vector3f size, WAL::Scene &scene) + void Map::createStairs(Vector3f coords, Vector3f size, WAL::Scene &scene) { WAL::Entity entity("Stairs Block"); @@ -96,44 +104,39 @@ namespace BBM scene.addEntity(entity); } - static BlockType Map::getRandomBlockType(int seed, int blockCreated) - { - return ((seed * blockCreated) % (END - 1)) + Map::BlockType Map::getRandomBlockType(int seed, int blockCreated) + { + return BlockType((seed * blockCreated) % (END - 1)); + //! @brief Which one is better ? + //return static_cast((seed * blockCreated) % (END - 1)); + } - static char Map::enumToChar(BlockType blockType) + void Map::generateMap(int width, int height, int seed, WAL::Scene &scene) { - switch (blockType) { - case BREAKABLE: - return 'X'; - case UNBREAKABLE: - return '#'; - case HOLE: - return 'O'; - case BUMPER: - return 'B'; - case STAIRS: - return 'S'; - default: - return ' '; - } - } - - static void Map::generateMap(int width, int height, int seed, WAL::Scene &scene) - { - std::map, char> map; + std::map, BlockType> map; for (int i = 0; i < width; i++) for (int j = 0; j < height; j++) - map[std::make_tuple(i, j)] = ' ' - for (int i = 0; i < 4; i++) - map[std::make_tuple((int)(std::pow(seed, i) % (width - 1) + 1),(int)(std::pow(seed * 0.7, i) % (height - 1) + 1)] = '*'; - for (int i = 1; i < width - 1; i++) - for (int j = 1; j < height - 1; j++) - if (isBlockCloseToPlayer(map[std::make_tuple(i, j)], i , j)) - map[std::make_tuple(i, j)] = ' '; - else - map[std::make_tuple(i, j)] = enumToChar(getRandomBlockType(seed, i * width + j) + map[std::make_tuple(i, j)] = NOTHING; + for (int i = 0; i < 4; i++) { + map[std::make_tuple(static_cast(std::pow(seed, i)) % (width - 1) + 1, \ + static_cast(std::pow(seed * 0.7, i)) % (height - 1) + 1)] = SPAWNER; + } + for (int i = 1; i < width - 1; i++) { + for (int j = 1; j < height - 1; j++) { + if (isBlockCloseToPlayer(map, i , j)) { + map[std::make_tuple(i, j)] = NOTHING; + } else { + map[std::make_tuple(i, j)] = getRandomBlockType(seed, i * width + j); + } + } + } + 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 \ No newline at end of file diff --git a/sources/Map/Map.hpp b/sources/Map/Map.hpp index 4563be8b..68b2c57f 100644 --- a/sources/Map/Map.hpp +++ b/sources/Map/Map.hpp @@ -3,9 +3,12 @@ // Edited by Benjamin Henry on 5/26/21. // +#pragma once + #include #include #include +#include #include #include #include @@ -18,15 +21,16 @@ namespace BBM { private: //! @brief Enum of the block available. - static enum BlockType { - NOTHING = 0, - BREAKABLE = 1, - UNBREAKABLE = 2, - HOLE = 3, - BUMPER = 4, - STAIRS = 5, - END = 6 - } + enum BlockType { + NOTHING, + BREAKABLE, + UNBREAKABLE, + HOLE, + BUMPER, + STAIRS, + SPAWNER, + END + }; //! @param seed seed to generate random block type //! @param blockCreated block created since the beginning @@ -36,7 +40,7 @@ namespace BBM //! @param map ASCII map //! @param x x index on the block //! @param y y index on the block - static bool isBlockCloseToPlayer(std::map, char> map, int x, int y) + static bool isBlockCloseToPlayer(std::map, BlockType> map, int x, int y); //! @param width Width of the map //! @param height Height of the map @@ -85,7 +89,6 @@ namespace BBM //! @param scene Scene where the map is instanced //! @brief Create stair of the map static void createStairs(Vector3f coords, Vector3f size, WAL::Scene &scene); - static char enumToChar(BlockType blockType); public: //! @param width Width of the map //! @param height Height of the map @@ -93,5 +96,5 @@ namespace BBM //! @brief Generate the map static void generateMap(int width, int height, int seed, WAL::Scene &scene); - } + }; } // namespace BBM