Merge pull request #157 from AnonymusRaccoon/gravity_component

Gravity component
This commit is contained in:
Clément Le Bihan
2021-06-14 17:35:12 +02:00
committed by GitHub
14 changed files with 386 additions and 37 deletions
+8
View File
@@ -109,6 +109,14 @@ set(SOURCES
sources/System/Sound/PlayerSoundManagerSystem.hpp
sources/System/Music/MusicSystem.hpp
sources/System/Music/MusicSystem.cpp
sources/Component/Gravity/GravityComponent.hpp
sources/Component/Gravity/GravityComponent.cpp
sources/System/Gravity/GravitySystem.hpp
sources/System/Gravity/GravitySystem.cpp
sources/Component/BumperTimer/BumperTimerComponent.hpp
sources/Component/BumperTimer/BumperTimerComponent.cpp
sources/System/BumperTimer/BumperTimerSystem.hpp
sources/System/BumperTimer/BumperTimerSystem.cpp
sources/System/Bomb/BombSystem.cpp
sources/System/Bomb/BombSystem.hpp
sources/Component/IntroAnimation/IntroAnimationComponent.hpp
@@ -0,0 +1,17 @@
//
// Created by Tom Augier on 2021-05-20.
//
#include "BumperTimerComponent.hpp"
namespace BBM
{
BumperTimerComponent::BumperTimerComponent(WAL::Entity &entity)
: WAL::Component(entity)
{}
WAL::Component *BumperTimerComponent::clone(WAL::Entity &entity) const
{
return new BumperTimerComponent(entity);
}
}
@@ -0,0 +1,41 @@
//
// Created by Tom Augier on 2021-05-20.
//
#pragma once
#include <chrono>
#include "Component/Component.hpp"
#include "Entity/Entity.hpp"
using namespace std::chrono_literals;
namespace BBM
{
class BumperTimerComponent : public WAL::Component
{
public:
bool _isReseting = false;
//! @brief The number of seconds of each rest. This variable is used to reset the nextReset value.
std::chrono::nanoseconds resetRate = 1500ms;
//! @brief The number of nanosecond before the next bumper reset for the player.
std::chrono::nanoseconds nextReset = resetRate;
//! @inherit
WAL::Component *clone(WAL::Entity &entity) const override;
//! @brief Constructor
BumperTimerComponent(WAL::Entity &entity);
//! @brief A BumperTimer component can't be instantiated, it should be derived.
BumperTimerComponent(const BumperTimerComponent &) = default;
//! @brief default destructor
~BumperTimerComponent() override = default;
//! @brief A BumperTimer component can't be assigned
BumperTimerComponent &operator=(const BumperTimerComponent &) = delete;
};
}
@@ -0,0 +1,17 @@
//
// Created by Tom Augier on 2021-05-20.
//
#include "GravityComponent.hpp"
namespace BBM
{
GravityComponent::GravityComponent(WAL::Entity &entity)
: WAL::Component(entity)
{}
WAL::Component *GravityComponent::clone(WAL::Entity &entity) const
{
return new GravityComponent(entity);
}
}
@@ -0,0 +1,31 @@
//
// Created by Tom Augier on 2021-05-20.
//
#pragma once
#include "Component/Component.hpp"
#include "Entity/Entity.hpp"
namespace BBM
{
class GravityComponent : public WAL::Component
{
public:
//! @inherit
WAL::Component *clone(WAL::Entity &entity) const override;
//! @brief Constructor
GravityComponent(WAL::Entity &entity);
//! @brief A Gravity component can't be instantiated, it should be derived.
GravityComponent(const GravityComponent &) = default;
//! @brief default destructor
~GravityComponent() override = default;
//! @brief A Gravity component can't be assigned
GravityComponent &operator=(const GravityComponent &) = delete;
};
}
+127 -33
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)
@@ -168,7 +194,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));
@@ -254,19 +280,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)
@@ -276,13 +299,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,
@@ -298,7 +318,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;
@@ -309,7 +329,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;
@@ -322,12 +342,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;
}
@@ -356,26 +374,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();
}
@@ -383,17 +435,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++)
+30 -2
View File
@@ -31,6 +31,7 @@ namespace BBM
class MapGenerator
{
private:
//! @brief Enum of the block available.
enum BlockType
{
@@ -111,6 +112,25 @@ namespace BBM
//! @brief Create upper floor of the map
static void createUpperFloor(Vector3f coords, std::shared_ptr<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 height hitbox of the map
static void generateHeightCollision(MapBlock map, int width, int height,
std::shared_ptr<WAL::Scene> scene);
//! @param map Map to load with block declared inside
//! @param width Width of the map
//! @param height Height of the map
//! @brief Generate unbreakable block on the map
static MapBlock createClassicUnbreakable(MapBlock map, int width, int height);
//! @param map Map to load with block declared inside
//! @param width Width of the map
//! @param height Height of the map
//! @brief Generate unbreakable block on map
static MapBlock createLongClassicUnbreakable(MapBlock map, int width, int height);
//! @param map Map to load with block declared inside
//! @param width Width of the map
//! @param height Height of the map
@@ -160,11 +180,20 @@ namespace BBM
CollisionComponent::CollidedAxis collidedAxis);
static void wallDestroyed(WAL::Entity &entity, WAL::Wal &wal);
static void holeCollide(WAL::Entity &entity,
const WAL::Entity &wall,
CollisionComponent::CollidedAxis collidedAxis);
static void bumperCollide(WAL::Entity &entity,
const WAL::Entity &wall,
CollisionComponent::CollidedAxis collidedAxis);
//! @param width Width of the map
//! @param height Height of the map
//! @brief Generate map of block to be loaded
static MapBlock createMap(int width, int height);
static MapBlock createMap(int width, int height, bool isHeight = false, bool isNotClassic = false);
//! @param width Width of the map
//! @param height Height of the map
@@ -172,6 +201,5 @@ namespace BBM
//! @param scene Scene where the map is instanced
//! @brief Generate the map
static void loadMap(int width, int height, MapBlock map, const std::shared_ptr<WAL::Scene> &scene);
};
} // namespace BBM
+5 -1
View File
@@ -20,6 +20,8 @@
#include "Component/Renderer/Drawable3DComponent.hpp"
#include "Component/Button/ButtonComponent.hpp"
#include "Drawables/2D/Text.hpp"
#include "Component/Gravity/GravityComponent.hpp"
#include "Component/BumperTimer/BumperTimerComponent.hpp"
#include "Model/Model.hpp"
#include "Map/Map.hpp"
@@ -40,10 +42,12 @@ namespace BBM
//{SoundComponent::DEATH, "assets/sounds/death.ogg"}
};
scene->addEntity("player")
.addComponent<PositionComponent>()
.addComponent<PositionComponent>(0, 1.01, 0)
.addComponent<Drawable3DComponent, RAY3D::Model>("assets/player/player.iqm", true, std::make_pair(MAP_DIFFUSE, "assets/player/blue.png"))
.addComponent<ControllableComponent>()
.addComponent<AnimatorComponent>()
.addComponent<GravityComponent>()
.addComponent<BumperTimerComponent>()
.addComponent<KeyboardComponent>()
.addComponent<ShaderComponentModel>("assets/shaders/glsl330/predator.fs")
.addComponent<TagComponent<Blowable>>()
+4
View File
@@ -31,6 +31,8 @@
#include <System/Bomb/BombSystem.hpp>
#include "System/Sound/PlayerSoundManagerSystem.hpp"
#include "System/Sound/MenuSoundManagerSystem.hpp"
#include "System/Gravity/GravitySystem.hpp"
#include "System/BumperTimer/BumperTimerSystem.hpp"
#include "System/Music/MusicSystem.hpp"
namespace RAY3D = RAY::Drawables::Drawables3D;
@@ -80,6 +82,8 @@ namespace BBM
.addSystem<PlayerSoundManagerSystem>()
.addSystem<MenuSoundManagerSystem>()
.addSystem<IntroAnimationSystem>()
.addSystem<GravitySystem>()
.addSystem<BumperTimerSystem>()
.addSystem<MusicSystem>();
}
@@ -75,4 +75,4 @@ namespace BBM
//! @brief A bomb holder system is not assignable.
BombHolderSystem &operator=(const BombHolderSystem &) = delete;
};
}
}
@@ -0,0 +1,25 @@
//
// Created by Tom Augier on 2021-06-09.
//
#include "BumperTimerSystem.hpp"
namespace BBM
{
BumperTimerSystem::BumperTimerSystem(WAL::Wal &wal)
: System(wal)
{}
void BumperTimerSystem::onUpdate(WAL::ViewEntity<BumperTimerComponent> &entity, std::chrono::nanoseconds dtime)
{
auto &bumperTimer = entity.get<BumperTimerComponent>();
if (bumperTimer._isReseting) {
bumperTimer.nextReset -= dtime;
if (bumperTimer.nextReset <= 0ns) {
bumperTimer.nextReset = bumperTimer.resetRate;
bumperTimer._isReseting = false;
}
}
}
}
@@ -0,0 +1,29 @@
//
// Created by Tom Augier on 2021-06-09.
//
#pragma once
#include "Component/Movable/MovableComponent.hpp"
#include "Component/Position/PositionComponent.hpp"
#include "Component/BumperTimer/BumperTimerComponent.hpp"
#include "System/System.hpp"
namespace BBM
{
//! @brief A system to handle BumperTimer entities.
class BumperTimerSystem : public WAL::System<BumperTimerComponent>
{
public:
//! @inherit
void onUpdate(WAL::ViewEntity<BumperTimerComponent> &entity, std::chrono::nanoseconds dtime) override;
//! @brief A default constructor
explicit BumperTimerSystem(WAL::Wal &wal);
//! @brief A BumperTimer system is copy constructable
BumperTimerSystem(const BumperTimerSystem &) = default;
//! @brief A default destructor
~BumperTimerSystem() override = default;
//! @brief A system is not assignable.
BumperTimerSystem &operator=(const BumperTimerSystem &) = delete;
};
}
+21
View File
@@ -0,0 +1,21 @@
//
// Created by Tom Augier on 2021-06-09.
//
#include "GravitySystem.hpp"
namespace BBM
{
GravitySystem::GravitySystem(WAL::Wal &wal)
: System(wal)
{}
void GravitySystem::onFixedUpdate(WAL::ViewEntity<GravityComponent, MovableComponent, PositionComponent> &entity)
{
auto &movable = entity.get<MovableComponent>();
auto &position = entity.get<PositionComponent>();
if (position.getY() > 0)
movable.addForce(Vector3f(0, -0.1, 0));
}
}
+30
View File
@@ -0,0 +1,30 @@
//
// Created by Tom Augier on 2021-06-09.
//
#pragma once
#include "Component/Movable/MovableComponent.hpp"
#include "Component/Position/PositionComponent.hpp"
#include "Component/Gravity/GravityComponent.hpp"
#include "System/System.hpp"
namespace BBM
{
//! @brief A system to handle Gravity entities.
class GravitySystem : public WAL::System<GravityComponent, MovableComponent, PositionComponent>
{
public:
//! @inherit
void onFixedUpdate(WAL::ViewEntity<GravityComponent, MovableComponent, PositionComponent> &entity) override;
//! @brief A default constructor
explicit GravitySystem(WAL::Wal &wal);
//! @brief A Gravity system is copy constructable
GravitySystem(const GravitySystem &) = default;
//! @brief A default destructor
~GravitySystem() override = default;
//! @brief A system is not assignable.
GravitySystem &operator=(const GravitySystem &) = delete;
};
}