need to fix (crash)

This commit is contained in:
EternalRat
2021-06-14 18:21:45 +02:00
16 changed files with 442 additions and 54 deletions
+128 -45
View File
@@ -9,15 +9,41 @@
#include <iostream>
#include <Items/Bonus.hpp>
#include <Component/Levitate/LevitateComponent.hpp>
#include "Component/Movable/MovableComponent.hpp"
#include <Component/Timer/TimerComponent.hpp>
#include <Component/Tag/TagComponent.hpp>
#include <Component/BumperTimer/BumperTimerComponent.hpp>
namespace RAY3D = RAY::Drawables::Drawables3D;
using namespace std::chrono_literals;
namespace BBM
{
void MapGenerator::bumperCollide(WAL::Entity &entity,
const WAL::Entity &wall,
CollisionComponent::CollidedAxis collidedAxis)
{
auto *movable = entity.tryGetComponent<MovableComponent>();
auto *bumperTimer = entity.tryGetComponent<BumperTimerComponent>();
if (!movable || !bumperTimer)
return;
if (!bumperTimer->_isReseting) {
movable->_velocity.y = 1.5;
bumperTimer->_isReseting = true;
}
}
void MapGenerator::holeCollide(WAL::Entity &entity,
const WAL::Entity &wall,
CollisionComponent::CollidedAxis collidedAxis)
{
auto *health = entity.tryGetComponent<HealthComponent>();
if (!health)
return;
health->takeDmg(health->getHealthPoint());
}
void MapGenerator::wallCollided(WAL::Entity &entity,
const WAL::Entity &wall,
CollisionComponent::CollidedAxis collidedAxis)
@@ -125,7 +151,6 @@ namespace BBM
.addComponent<PositionComponent>(i, 0, height + 1)
.addComponent<TagComponent<Blowable>>();
}
scene->addEntity("Bottom Wall")
.addComponent<PositionComponent>(Vector3f((width + 1) / 2, 0, -1))
.addComponent<CollisionComponent>(
@@ -168,7 +193,7 @@ namespace BBM
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, -1, j)] != BUMPER)
scene->addEntity("Floor")
scene->addEntity("Ground")
.addComponent<PositionComponent>(Vector3f(i, -1, j))
.addComponent<Drawable3DComponent, RAY3D::Model>(floorObj, false,
std::make_pair(MAP_DIFFUSE, floorPng));
@@ -186,6 +211,7 @@ namespace BBM
{UPPERFLOOR, &createUpperFloor},
};
std::cout << "blockType: " << blockType << std::endl;
if (blockType == NOTHING || blockType == SPAWNER)
return;
auto element = elements.at(blockType);
@@ -207,17 +233,6 @@ namespace BBM
.addComponent<Drawable3DComponent, RAY3D::Model>(breakableObj, false, std::make_pair(MAP_DIFFUSE, breakablePng));
}
void MapGenerator::createFloor(Vector3f coords, std::shared_ptr<WAL::Scene> scene)
{
static const std::string floorObj = floorPath + objExtension;
static const std::string floorPng = floorPath + imageExtension;
scene->addEntity("Floor")
.addComponent<PositionComponent>(Vector3f(coords))
//.addComponent<CollisionComponent>(1)
.addComponent<Drawable3DComponent, RAY3D::Model>(floorObj, false, std::make_pair(MAP_DIFFUSE, floorPng));
}
void MapGenerator::createUpperFloor(Vector3f coords, std::shared_ptr<WAL::Scene> scene)
{
static const std::string floorObj = secondFloorPath + objExtension;
@@ -253,19 +268,16 @@ namespace BBM
WAL::Entity &holeEntity = scene->addEntity("Hole Block");
holeEntity.addComponent<PositionComponent>(Vector3f(coords.x, coords.y - 1, coords.z));
holeEntity.addComponent<PositionComponent>(Vector3f(coords.x, coords.y - 1, coords.z))
.addComponent<CollisionComponent>(
WAL::Callback<WAL::Entity &, const WAL::Entity &, CollisionComponent::CollidedAxis>(),
&MapGenerator::holeCollide, Vector3f(0.25, 0.25, 0.25),Vector3f(0.75, 1.75, 0.75));
if (coords.y == 0)
holeEntity.addComponent<Drawable3DComponent, RAY3D::Model>(holeObj, false, std::make_pair(MAP_DIFFUSE, holePng));
else
holeEntity.addComponent<Drawable3DComponent, RAY3D::Model>(secondFloorObj, false,
std::make_pair(MAP_DIFFUSE, secondFloorPng));
/*.addComponent<CollisionComponent>([](WAL::Entity &other, const WAL::Entity &entity) {
if (other.hasComponent<HealthComponent>()) {
auto &health = other.getComponent<HealthComponent>();
health.takeDmg(health.getHealthPoint());
}
}, [](WAL::Entity &other, const WAL::Entity &entity){}); */
}
void MapGenerator::createBumper(Vector3f coords, std::shared_ptr<WAL::Scene> scene)
@@ -275,13 +287,10 @@ namespace BBM
scene->addEntity("Bumper Block")
.addComponent<PositionComponent>(Vector3f(coords.x, coords.y, coords.z))
.addComponent<Drawable3DComponent, RAY3D::Model>(bumperObj, false, std::make_pair(MAP_DIFFUSE, bumperPng));
/* .addComponent<CollisionComponent>([](const WAL::Entity &entity, WAL::Entity &other) {
if (other.hasComponent<MovableComponent>()) {
auto &movable = other.getComponent<MovableComponent>();
movable.addForce(Vector3f(0, 5, 0));
}
}); */
.addComponent<Drawable3DComponent, RAY3D::Model>(bumperObj, false,std::make_pair(MAP_DIFFUSE, bumperPng))
.addComponent<CollisionComponent>(
WAL::Callback<WAL::Entity &, const WAL::Entity &, CollisionComponent::CollidedAxis>(),
&MapGenerator::bumperCollide, Vector3f(0.25, 0.25, 0.25),Vector3f(0.75, 0.75, 0.75));
}
bool MapGenerator::isCloseToBlockType(std::map<std::tuple<int, int, int>, BlockType> map, int x, int y, int z,
@@ -297,7 +306,7 @@ namespace BBM
{
double rnd = static_cast<double>(std::rand()) / RAND_MAX;
if (rnd > 0.95)
if (rnd > 0.98)
return HOLE;
if (rnd > 0.25)
return BREAKABLE;
@@ -308,7 +317,7 @@ namespace BBM
{
double rnd = static_cast<double>(std::rand()) / RAND_MAX;
if (rnd > 0.60) {
if (rnd > 0.01) {
for (int i = 0; i < width + 1; i++) {
map[std::make_tuple(i, 1, height)] = map[std::make_tuple(i, 0, height)];
map[std::make_tuple(i, 0, height)] = UPPERFLOOR;
@@ -321,12 +330,10 @@ namespace BBM
map[std::make_tuple(width, -1, 1)] = BUMPER;
map[std::make_tuple(width / 2, -1, height - 1)] = BUMPER;
map[std::make_tuple(width / 2, -1, 1)] = BUMPER;
}
if (rnd > 0.30) {
}
if (rnd > 0.01) {
for (int i = width / 2 - width / 4; i < width / 2 + width / 4 + 1; i++) {
for (int j = height / 2 - height / 4; j < height / 2 + height / 4 + 1; j++) {
if (map[std::make_tuple(i, 0, j)] == FLOOR)
continue;
map[std::make_tuple(i, 1, j)] = map[std::make_tuple(i, 0, j)];
map[std::make_tuple(i, 0, j)] = UPPERFLOOR;
}
@@ -355,26 +362,60 @@ namespace BBM
for (int j = 0; j < height; j++) {
if (map[std::make_tuple(i, 0, j)] == BREAKABLE && map[std::make_tuple(i, -1, j)] == BUMPER)
map[std::make_tuple(i, 0, j)] = NOTHING;
if (map[std::make_tuple(i, 1, j)] == BREAKABLE && isCloseToBlockType(map, i, -1, j, BUMPER))
map[std::make_tuple(i, 1, j)] = NOTHING;
}
return (map);
}
MapGenerator::MapBlock MapGenerator::createMap(int width, int height)
MapGenerator::MapBlock MapGenerator::createClassicUnbreakable(MapBlock map, int width, int height)
{
for (int i = 0; i < width + 1; i++) {
for (int j = 0; j < height + 1; j++) {
if (!((i + 1) % 2) && !((j + 1) % 2))
map[std::make_tuple(i, 0, j)] = UNBREAKABLE;
}
}
return (map);
}
MapGenerator::MapBlock MapGenerator::createLongClassicUnbreakable(MapBlock map, int width, int height)
{
int placedSpace = 0;
for (int i = 1; i < width; i++) {
placedSpace = 0;
for (int j = 1; j < height; j++) {
if (!(j % 2))
continue;
if (i < (width / 2 - width / 10) || i > (width / 2 + width / 10))
map[std::make_tuple(i, 0, j)] = UNBREAKABLE;
else
placedSpace++;
}
}
return (map);
}
MapGenerator::MapBlock MapGenerator::createMap(int width, int height, bool isHeight, bool isNotClassic)
{
MapBlock map;
width = width % 2 ? width + 1 : width;
height = height % 2 ? height + 1 : height;
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
for (int i = 0; i < width + 1; i++)
for (int j = 0; j < height + 1; j++) {
map[std::make_tuple(i, 0, j)] = NOTHING;
map[std::make_tuple(i, 1, j)] = NOTHING;
}
map = createSpawner(map, width, height);
for (int i = 0; i < width + 1; i++) {
for (int j = 0; j < height + 1; j++) {
if (map[std::make_tuple(i, 0, j)] == SPAWNER)
continue;
if (isCloseToBlockType(map, i, 0, j, SPAWNER)) {
map[std::make_tuple(i, 0, j)] = NOTHING;
map[std::make_tuple(i, isNotClassic ? -1 : 0, j)] = isNotClassic ? BUMPER : NOTHING;
} else {
map[std::make_tuple(i, 0, j)] = getRandomBlockType();
}
@@ -382,17 +423,59 @@ namespace BBM
map[std::make_tuple(i, 0, j)] = BREAKABLE;
}
}
for (int i = 0; i < width + 1; i++)
for (int j = 0; j < height + 1; j++)
if (!((i + 1) % 2) && !((j + 1) % 2))
map[std::make_tuple(i, 0, j)] = UNBREAKABLE;
map = createHeight(map, width, height);
if (!isNotClassic)
map = createClassicUnbreakable(map, width, height);
else
map = createLongClassicUnbreakable(map, width, height);
if (isHeight)
map = createHeight(map, width, height);
map = cleanBreakable(map, width, height);
return (map);
}
void MapGenerator::generateHeightCollision(MapBlock map, int width, int height, std::shared_ptr<WAL::Scene> scene)
{
int floor = 2;
for (int i = 0; i < width + 1; i++) {
if (map[std::make_tuple(i, 0, height)] == NOTHING && map[std::make_tuple(i, 0, 0)] == NOTHING) {
floor -= 1;
break;
}
}
for (int i = width / 2 - width / 4; i < width / 2 + width / 4 + 1; i++) {
for (int j = height / 2 - height / 4; j < height / 2 + height / 4 + 1; j++) {
if (map[std::make_tuple(i, 0, i)] == NOTHING) {
floor -= 1;
break;
}
}
if (floor <= 0)
break;
}
if (floor >= 1) {
scene->addEntity("FloorBot Hitbox")
.addComponent<PositionComponent>(Vector3f(0, 0, 0))
.addComponent<CollisionComponent>(
WAL::Callback<WAL::Entity &, const WAL::Entity &, CollisionComponent::CollidedAxis>(),
&MapGenerator::wallCollided, Vector3f(0.25, 0.25, 0.25), Vector3f(width, 0.75, 0.75));
scene->addEntity("FloorUp Hitbox")
.addComponent<PositionComponent>(Vector3f(0, 0, height))
.addComponent<CollisionComponent>(
WAL::Callback<WAL::Entity &, const WAL::Entity &, CollisionComponent::CollidedAxis>(),
&MapGenerator::wallCollided, Vector3f(0.25, 0.25, 0.25),Vector3f(width, 0.75, 0.75));
}
if (floor >= 2)
scene->addEntity("Middle Hitbox")
.addComponent<PositionComponent>(Vector3f(width / 2 - width / 4, 0, height / 2 - height / 4))
.addComponent<CollisionComponent>(
WAL::Callback<WAL::Entity &, const WAL::Entity &, CollisionComponent::CollidedAxis>(),
&MapGenerator::wallCollided, Vector3f(0.25, 0.25, 0.25),Vector3f(width, 0.75, height / 2 + height / 4));
}
void MapGenerator::loadMap(int width, int height, MapBlock map, const std::shared_ptr<WAL::Scene> &scene)
{
{
//generateHeightCollision(map, width, height, scene);
generateWall(width, height, scene);
generateFloor(map, width, height, scene);
for (int x = 0; x < width + 1; x++)