From f79c6f46a3bc376ef8d5c7026ac7bd56f48d8453 Mon Sep 17 00:00:00 2001 From: TrueBabyChaise Date: Wed, 26 May 2021 16:15:16 +0200 Subject: [PATCH] Map + ColliderComponent Basic map and ColliderComponent, system hasn't be done at the moment Co-Authored-By: Benjamin HENRY <44569175+EternalRat@users.noreply.github.com> --- .../Component/Collider/ColliderComponent.cpp | 23 +++ .../Component/Collider/ColliderComponent.hpp | 38 +++++ sources/Map/Map.cpp | 139 ++++++++++++++++++ sources/Map/Map.hpp | 97 ++++++++++++ 4 files changed, 297 insertions(+) create mode 100644 sources/Component/Collider/ColliderComponent.cpp create mode 100644 sources/Component/Collider/ColliderComponent.hpp create mode 100644 sources/Map/Map.cpp create mode 100644 sources/Map/Map.hpp diff --git a/sources/Component/Collider/ColliderComponent.cpp b/sources/Component/Collider/ColliderComponent.cpp new file mode 100644 index 00000000..f50d7b7f --- /dev/null +++ b/sources/Component/Collider/ColliderComponent.cpp @@ -0,0 +1,23 @@ +// +// 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 new file mode 100644 index 00000000..2a9a56cb --- /dev/null +++ b/sources/Component/Collider/ColliderComponent.hpp @@ -0,0 +1,38 @@ +// +// 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 movable component. + explicit ColliderComponent(WAL::Entity &entity); + + //! @brief Create a new movable component with a callback. + ColliderComponent::ColliderComponent(WAL::Entity &entity, std::function callback); + //! @brief A movable component is copy constructable. + ColliderComponent(const ColliderComponent &) = default; + //! @brief A default destructor + ~ColliderComponent() override = default; + //! @brief A movable component is not assignable. + ColliderComponent &operator=(const ColliderComponent &) = delete; + + friend class MovableSystem; + }; +} // namespace WAL \ No newline at end of file diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp new file mode 100644 index 00000000..2f18897e --- /dev/null +++ b/sources/Map/Map.cpp @@ -0,0 +1,139 @@ +// +// Created by Tom Augier on 5/26/21. +// Edited by Benjamin Henry on 5/26/21. +// + +#include "map.hpp" + +namespace BBM +{ + static void Map::generateWall(int width, int height, WAL::Scene &scene) + { + + } + + static void Map::generateFloor(int width, int height, WAL::Scene &scene) + { + + } + + static 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} + }; + + for (auto element : elements) + if (element.first == blockType) + element.second(coords, size, scene); + } + + static void Map::createBreakable(Vector3f coords, Vector3f size, WAL::Scene &scene) + { + WAL::Entity entity("Breakable Block"); + + entity.addComponent(coords); + //entity.addComponent(1); + scene.addEntity(entity); + } + + static void Map::createUnbreakable(Vector3f coords, Vector3f size, WAL::Scene &scene) + { + WAL::Entity entity("Unbreakable Block"); + + entity.addComponent(coords); + scene.addEntity(entity); + } + + static 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) { + //En commentaire car manque le HealthComponent sur la branche (pour pas gĂȘner au niveau des erreurs) + /* 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) + { + WAL::Entity entity("Bumper Block"); + + entity.addComponent(coords); + 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) + { + if (map[x - 1, y] == '*' || + map[x + 1, y] == '*' || + map[x, y + 1] == '*' || + map[x, y - 1] == '*' ||) + return (true) + return (false) + } + + static void Map::createStairs(Vector3f coords, Vector3f size, WAL::Scene &scene) + { + WAL::Entity entity("Stairs Block"); + + entity.addComponent(coords); + scene.addEntity(entity); + } + + static BlockType Map::getRandomBlockType(int seed, int blockCreated) + { + return ((seed * blockCreated) % (END - 1)) + } + + static char Map::enumToChar(BlockType blockType) + { + 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; + + 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) + } + +} // namespace BBM \ No newline at end of file diff --git a/sources/Map/Map.hpp b/sources/Map/Map.hpp new file mode 100644 index 00000000..4563be8b --- /dev/null +++ b/sources/Map/Map.hpp @@ -0,0 +1,97 @@ +// +// Created by Tom Augier on 5/26/21. +// Edited by Benjamin Henry on 5/26/21. +// + +#include +#include +#include +#include +#include +#include +#include + +namespace BBM +{ + + class Map + { + private: + //! @brief Enum of the block available. + static enum BlockType { + NOTHING = 0, + BREAKABLE = 1, + UNBREAKABLE = 2, + HOLE = 3, + BUMPER = 4, + STAIRS = 5, + END = 6 + } + + //! @param seed seed to generate random block type + //! @param blockCreated block created since the beginning + //! @brief Generate random block type + static BlockType getRandomBlockType(int seed, int blockCreated); + + //! @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) + + //! @param width Width of the map + //! @param height Height of the map + //! @param scene Scene where the map is instanced + //! @brief Generate the wall of the map + static void generateWall(int width, int height, WAL::Scene &scene); + + //! @param width Width of the map + //! @param height Height of the map + //! @param scene Scene where the map is instanced + //! @brief Generate the floor of the map + static void generateFloor(int width, int height, WAL::Scene &scene); + + //! @param coords coords of the element + //! @param size size of the element + //! @param scene Scene where the map is instanced + //! @brief Create element of the map + static void createElement(Vector3f coords, Vector3f size, WAL::Scene &scene, BlockType blockType); + + //! @param coords coords of the element + //! @param size size of the element + //! @param scene Scene where the map is instanced + //! @brief Create breakable of the map + static void createBreakable(Vector3f coords, Vector3f size, WAL::Scene &scene); + + //! @param coords coords of the element + //! @param size size of the element + //! @param scene Scene where the map is instanced + //! @brief Create unbreakable of the map + static void createUnbreakable(Vector3f coords, Vector3f size, WAL::Scene &scene); + + //! @param coords coords of the element + //! @param size size of the element + //! @param scene Scene where the map is instanced + //! @brief Create hole of the map + static void createHole(Vector3f coords, Vector3f size, WAL::Scene &scene); + + //! @param coords coords of the element + //! @param size size of the element + //! @param scene Scene where the map is instanced + //! @brief Create bumper of the map + static void createBumper(Vector3f coords, Vector3f size, WAL::Scene &scene); + + //! @param coords coords of the element + //! @param size size of the element + //! @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 + //! @param scene Scene where the map is instanced + //! @brief Generate the map + static void generateMap(int width, int height, int seed, WAL::Scene &scene); + + } +} // namespace BBM