basic map with unbreakable & breakable block

This commit is contained in:
HENRY Benjamin
2021-05-31 17:32:30 +02:00
parent afcf15c91e
commit 38cbfa022e
11 changed files with 327 additions and 439 deletions

View File

@@ -5,133 +5,159 @@
#include "Map.hpp"
#include <cmath>
#include <Model/Model.hpp>
namespace RAY3D = RAY::Drawables::Drawables3D;
namespace BBM
{
void Map::generateWall(int width, int height, WAL::Scene &scene)
void Map::generateWall(int width, int height, std::shared_ptr<WAL::Scene> scene)
{
WAL::Entity entity("Unbreakable Block");
entity.addComponent<PositionComponent>(Vector3f(0,0,0));
scene.addEntity(entity);
for (int i = 0; i < width; i++) {
scene->addEntity("Width Wall")
.addComponent<PositionComponent>(Vector3f(i,0,height))
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png"));
scene->addEntity("Width Wall")
.addComponent<PositionComponent>(Vector3f(i,0,0))
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png"));
}
for (int i = 0; i < height; i++) {
scene->addEntity("Height Wall")
.addComponent<PositionComponent>(Vector3f(width,0,i))
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png"));
scene->addEntity("Height Wall")
.addComponent<PositionComponent>(Vector3f(0,0,i))
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png"));
}
scene->addEntity("Width Wall")
.addComponent<PositionComponent>(Vector3f(width, 0,height))
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png"));
}
void Map::generateFloor(int width, int height, WAL::Scene &scene)
void Map::generateFloor(int width, int height, std::shared_ptr<WAL::Scene> scene)
{
WAL::Entity entity("Unbreakable Block");
/* RAY3D::Model model = RAY3D::Model("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png"));
entity.addComponent<PositionComponent>(Vector3f(0,0,0));
scene.addEntity(entity);
model.setScale(RAY::Vector3(width, 0, height)); */
for (int i = 1; i < width; i++) {
for (int j = 1; j < height; j++) {
scene->addEntity("Floor")
.addComponent<PositionComponent>(Vector3f(i,-1,j))
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/wall/floor.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/floor.png"));
}
}
//.addComponent<Drawable3DComponent<RAY3D::Model>>(model);
}
void Map::createElement(Vector3f coords, Vector3f size, WAL::Scene &scene, BlockType blockType)
void Map::createElement(Vector3f coords, Vector3f size, std::shared_ptr<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}
if (blockType == BREAKABLE) {
createBreakable(coords, size, scene);
} else if (blockType == UNBREAKABLE) {
createUnbreakable(coords, size, scene);
}
/* std::map<BlockType, std::function<void (Vector3f coords, Vector3f size, std::shared_ptr<WAL::Scene> )>> elements = {
{BREAKABLE, &Map::createBreakable},
{UNBREAKABLE, &Map::createUnbreakable},
{HOLE, &Map::createHole},
{BUMPER, &Map::createBumper},
{STAIRS, &Map::createStairs}
};
auto element = std::find(elements.begin(), elements.end(), blockType);
if (element == elements.end())
return;
element->second(coords, size, scene);
element->second(coords, size, scene); */
}
void Map::createBreakable(Vector3f coords, Vector3f size, WAL::Scene &scene)
void Map::createBreakable(Vector3f coords, Vector3f size, std::shared_ptr<WAL::Scene> scene)
{
WAL::Entity entity("Breakable Block");
entity.addComponent<PositionComponent>(coords);
//entity.addComponent<HealthComponent>(1);
scene.addEntity(entity);
scene->addEntity("Breakable Block")
.addComponent<PositionComponent>(coords)
//.addComponent<HealthComponent>(1)
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/wall/breakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/breakable_wall.png"));
//.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/wall/breakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/breakable_wall.png"));
}
void Map::createUnbreakable(Vector3f coords, Vector3f size, WAL::Scene &scene)
void Map::createUnbreakable(Vector3f coords, Vector3f size, std::shared_ptr<WAL::Scene> scene)
{
WAL::Entity entity("Unbreakable Block");
entity.addComponent<PositionComponent>(coords);
scene.addEntity(entity);
scene->addEntity("Unbreakable Block")
.addComponent<PositionComponent>(coords)
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png"));
}
void Map::createHole(Vector3f coords, Vector3f size, WAL::Scene &scene)
void Map::createHole(Vector3f coords, Vector3f size, std::shared_ptr<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);
scene->addEntity("Hole Block")
.addComponent<PositionComponent>(coords)
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/wall/hole_block.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/hole_block.png"));
/* .addComponent<ColliderComponent>([](const WAL::Entity &entity, WAL::Entity &other) {
if (other.hasComponent<HealthComponent>()) {
auto &health = other.getComponent<HealthComponent>();
health.takeDmg(health.getHealthPoint());
}
}); */
WAL::Entity entity("");
}
void Map::createBumper(Vector3f coords, Vector3f size, WAL::Scene &scene)
void Map::createBumper(Vector3f coords, Vector3f size, std::shared_ptr<WAL::Scene> scene)
{
WAL::Entity entity("Bumper Block");
entity.addComponent<PositionComponent>(coords);
/* entity.addComponent<ColliderComponent>([](const WAL::Entity &entity, WAL::Entity &other) {
scene->addEntity("Bumper Block")
.addComponent<PositionComponent>(coords);
/* .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);
}
bool Map::isBlockCloseToPlayer(std::map<std::tuple<int, int>, BlockType> map, int x, int y)
void Map::createStairs(Vector3f coords, Vector3f size, std::shared_ptr<WAL::Scene> scene)
{
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)] == '*')
scene->addEntity("Stairs Block")
.addComponent<PositionComponent>(coords);
}
bool Map::isBlockCloseToBlockType(std::map<std::tuple<int, int>, BlockType> map, int x, int y, BlockType blockType)
{
if (map[std::make_tuple(x - 1, y)] == blockType ||
map[std::make_tuple(x + 1, y)] == blockType ||
map[std::make_tuple(x, y + 1)] == blockType ||
map[std::make_tuple(x, y - 1)] == blockType)
return (true);
return (false);
}
void Map::createStairs(Vector3f coords, Vector3f size, WAL::Scene &scene)
{
WAL::Entity entity("Stairs Block");
entity.addComponent<PositionComponent>(coords);
scene.addEntity(entity);
}
Map::BlockType Map::getRandomBlockType(int seed, int blockCreated)
{
return BlockType((seed * blockCreated) % (END - 1));
//! @brief Which one is better ?
//return static_cast<BlockType>((seed * blockCreated) % (END - 1));
return static_cast<BlockType>((seed * blockCreated * rand()) % (HOLE));
}
void Map::generateMap(int width, int height, int seed, WAL::Scene &scene)
void Map::generateMap(int width, int height, int seed, std::shared_ptr<WAL::Scene> scene)
{
std::map<std::tuple<int, int>, BlockType> map;
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
map[std::make_tuple(i, j)] = NOTHING;
for (int i = 0; i < 4; i++) {
map[std::make_tuple(static_cast<int>(std::pow(seed, i)) % (width - 1) + 1, \
static_cast<int>(std::pow(seed * 0.7, i)) % (height - 1) + 1)] = SPAWNER;
}
map[std::make_tuple(1, 1)] = SPAWNER;
map[std::make_tuple(width - 1, 1)] = SPAWNER;
map[std::make_tuple(1, height - 2)] = SPAWNER;
map[std::make_tuple(width - 1, height - 1)] = SPAWNER;
for (int i = 1; i < width - 1; i++) {
for (int j = 1; j < height - 1; j++) {
if (isBlockCloseToPlayer(map, i , j)) {
if (isBlockCloseToBlockType(map, i , j, SPAWNER)) {
map[std::make_tuple(i, j)] = NOTHING;
} else {
map[std::make_tuple(i, j)] = getRandomBlockType(seed, i * width + j);
}
if (isBlockCloseToBlockType(map, i , j, UNBREAKABLE) && !isBlockCloseToBlockType(map, i , j, SPAWNER)) {
map[std::make_tuple(i, j)] = BREAKABLE;
}
}
}
generateWall(width, height, scene);
generateFloor(width, height, scene);
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)]);