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>
This commit is contained in:
TrueBabyChaise
2021-05-26 16:15:16 +02:00
parent 41dde4dc62
commit f79c6f46a3
4 changed files with 297 additions and 0 deletions
@@ -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<void (const WAL::Entity &, WAL::Entity &)> callback)
: Component(entity),
onCollide(callback)
{}
WAL::Component *ColliderComponent::clone(WAL::Entity &entity) const
{
return new ColliderComponent(entity);
}
} // namespace WAL
@@ -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<const WAL::Entity &, WAL::Entity &> 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<void (const WAL::Entity &, WAL::Entity &)> 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
+139
View File
@@ -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<BlockType, std::function<void (Vector3f coords, Vector3f size, WAL::Scene &)>> 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<PositionComponent>(coords);
//entity.addComponent<HealthComponent>(1);
scene.addEntity(entity);
}
static void Map::createUnbreakable(Vector3f coords, Vector3f size, WAL::Scene &scene)
{
WAL::Entity entity("Unbreakable Block");
entity.addComponent<PositionComponent>(coords);
scene.addEntity(entity);
}
static void Map::createHole(Vector3f coords, Vector3f size, WAL::Scene &scene)
{
WAL::Entity entity("Hole Block");
entity.addComponent<PositionComponent>(coords);
entity.addComponent<ColliderComponent>([](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<HealthComponent>()) {
auto &health = other.getComponent<HealthComponent>();
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<PositionComponent>(coords);
entity.addComponent<ColliderComponent>([](const WAL::Entity &entity, WAL::Entity &other) {
if (other.hasComponent<MovableComponent>()) {
auto &movable = other.getComponent<MovableComponent>();
movable.addForce(Vector3f(0, 0, 5));
}
}
scene.addEntity(entity);
}
static bool Map::isBlockCloseToPlayer(std::map<std::tuple<int, int>, 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<PositionComponent>(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<std::tuple<int, int>, 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
+97
View File
@@ -0,0 +1,97 @@
//
// Created by Tom Augier on 5/26/21.
// Edited by Benjamin Henry on 5/26/21.
//
#include <cmath>
#include <Component/Component.hpp>
#include <Component/Collider/ColliderComponent.hpp>
#include <Component/Movable/MovableComponent.hpp>
#include <Scene/Scene.hpp>
#include <map>
#include <tuple>
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<std::tuple<int, int>, 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