mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-06-01 09:45:42 +00:00
Making block solids
This commit is contained in:
@@ -79,6 +79,32 @@ namespace WAL
|
||||
return static_cast<T *>(existing->second.get());
|
||||
}
|
||||
|
||||
//! @brief Get a component of a specific type
|
||||
//! @tparam The type of the component
|
||||
//! @throw NotFoundError if the component could not be found
|
||||
//! @return The component of the requested type.
|
||||
template<typename T>
|
||||
const T &getComponent() const
|
||||
{
|
||||
const T *ret = this->tryGetComponent<T>();
|
||||
if (ret == nullptr)
|
||||
throw NotFoundError("No component could be found with the type \"" + std::string(typeid(T).name()) + "\".");
|
||||
return *ret;
|
||||
}
|
||||
|
||||
//! @brief Get a component of a specific type or null if not found.
|
||||
//! @tparam The type of the component
|
||||
//! @return The component or nullptr if not found.
|
||||
template<typename T>
|
||||
const T *tryGetComponent() const
|
||||
{
|
||||
const std::type_index &type = typeid(T);
|
||||
auto existing = this->_components.find(type);
|
||||
if (existing == this->_components.end())
|
||||
return nullptr;
|
||||
return static_cast<T *>(existing->second.get());
|
||||
}
|
||||
|
||||
//! @brief Check if this entity has a component.
|
||||
//! @tparam T The type of the component
|
||||
template<typename T>
|
||||
|
||||
@@ -64,4 +64,7 @@ namespace WAL
|
||||
this->addCallback(callback);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename ...Types>
|
||||
static constexpr Callback<Types...> EmptyCallback;
|
||||
} // namespace WAL
|
||||
@@ -20,11 +20,6 @@ namespace BBM
|
||||
this->_acceleration += force;
|
||||
}
|
||||
|
||||
void MovableComponent::resetVelocity(void)
|
||||
{
|
||||
this->_velocity = {0, 0, 0};
|
||||
}
|
||||
|
||||
const Vector3f &MovableComponent::getVelocity(void) const
|
||||
{
|
||||
return _velocity;
|
||||
|
||||
@@ -23,9 +23,6 @@ namespace BBM
|
||||
//! @param force The force to add to this entity's acceleration. The force is added instantly and in one go.
|
||||
void addForce(Vector3f force);
|
||||
|
||||
//! @brief Set velocity to 0
|
||||
void resetVelocity(void);
|
||||
|
||||
//! @brief Get velocity
|
||||
const Vector3f &getVelocity(void) const;
|
||||
|
||||
@@ -42,5 +39,6 @@ namespace BBM
|
||||
MovableComponent &operator=(const MovableComponent &) = delete;
|
||||
|
||||
friend class MovableSystem;
|
||||
friend class MapGenerator;
|
||||
};
|
||||
} // namespace WAL
|
||||
+29
-9
@@ -5,11 +5,31 @@
|
||||
|
||||
#include <Component/Collision/CollisionComponent.hpp>
|
||||
#include "Map.hpp"
|
||||
#include <iostream>
|
||||
|
||||
namespace RAY3D = RAY::Drawables::Drawables3D;
|
||||
|
||||
namespace BBM
|
||||
{
|
||||
{
|
||||
void MapGenerator::wallCollide(WAL::Entity &entity, const WAL::Entity &wall)
|
||||
{
|
||||
auto *mov = entity.tryGetComponent<MovableComponent>();
|
||||
if (!mov)
|
||||
return;
|
||||
auto &pos = entity.getComponent<PositionComponent>();
|
||||
const auto &wallPos = wall.getComponent<PositionComponent>();
|
||||
auto diff = pos.position + mov->getVelocity() - wallPos.position;
|
||||
std::cout << diff << std::endl;
|
||||
if (diff.x <= 0 && mov->_velocity.x < 0)
|
||||
mov->_velocity.x = 0;
|
||||
if (diff.x >= 0 && mov->_velocity.x > 0)
|
||||
mov->_velocity.x = 0;
|
||||
if (diff.z <= 0 && mov->_velocity.z < 0)
|
||||
mov->_velocity.z = 0;
|
||||
if (diff.z >= 0 && mov->_velocity.z > 0)
|
||||
mov->_velocity.z = 0;
|
||||
}
|
||||
|
||||
void MapGenerator::generateUnbreakableBlock(int width, int height, std::shared_ptr<WAL::Scene> scene)
|
||||
{
|
||||
std::string unbreakableObj = "assets/wall/unbreakable_wall.obj";
|
||||
@@ -20,7 +40,7 @@ namespace BBM
|
||||
if (!(i % 2) && !(j % 2)) {
|
||||
scene->addEntity("Unbreakable Wall")
|
||||
.addComponent<PositionComponent>(i, 0, j)
|
||||
.addComponent<CollisionComponent>(1)
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, .75)
|
||||
.addComponent<Drawable3DComponent, RAY3D::Model>(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj));
|
||||
}
|
||||
}
|
||||
@@ -34,19 +54,19 @@ namespace BBM
|
||||
|
||||
scene->addEntity("Bottom Wall")
|
||||
.addComponent<PositionComponent>(Vector3f((width + 1) / 2, 0, -1))
|
||||
.addComponent<CollisionComponent>(1)
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, .75)
|
||||
.addComponent<Drawable3DComponent, RAY3D::Model>(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1));
|
||||
scene->addEntity("Upper Wall")
|
||||
.addComponent<PositionComponent>(Vector3f((width + 1) / 2, 0, height + 1))
|
||||
.addComponent<CollisionComponent>(1)
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, .75)
|
||||
.addComponent<Drawable3DComponent, RAY3D::Model>(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1));
|
||||
scene->addEntity("Left Wall")
|
||||
.addComponent<PositionComponent>(Vector3f(width + 1, 0, (height + 1) / 2))
|
||||
.addComponent<CollisionComponent>(1)
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, .75)
|
||||
.addComponent<Drawable3DComponent, RAY3D::Model>(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 3));
|
||||
scene->addEntity("Right Wall")
|
||||
.addComponent<PositionComponent>(Vector3f(-1, 0, (height + 1) / 2))
|
||||
.addComponent<CollisionComponent>(1)
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, .75)
|
||||
.addComponent<Drawable3DComponent, RAY3D::Model>(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 3));
|
||||
}
|
||||
|
||||
@@ -81,7 +101,7 @@ namespace BBM
|
||||
scene->addEntity("Breakable Block")
|
||||
.addComponent<PositionComponent>(coords)
|
||||
.addComponent<HealthComponent>(1)
|
||||
.addComponent<CollisionComponent>(1)
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, .75)
|
||||
.addComponent<Drawable3DComponent, RAY3D::Model>("assets/wall/breakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/breakable_wall.png"));
|
||||
}
|
||||
|
||||
@@ -96,7 +116,7 @@ namespace BBM
|
||||
{
|
||||
scene->addEntity("Unbreakable Block")
|
||||
.addComponent<PositionComponent>(coords)
|
||||
.addComponent<CollisionComponent>(1)
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, .75)
|
||||
.addComponent<Drawable3DComponent, RAY3D::Model>("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png"));
|
||||
}
|
||||
|
||||
@@ -155,7 +175,7 @@ namespace BBM
|
||||
|
||||
MapGenerator::MapBlock MapGenerator::createHeight(MapBlock map, int width, int height)
|
||||
{
|
||||
double rnd = static_cast<double>(std::rand())/RAND_MAX;
|
||||
double rnd = static_cast<double>(std::rand()) / RAND_MAX;
|
||||
|
||||
if (rnd > 0.60) {
|
||||
for (int i = 0; i < width; i++) {
|
||||
|
||||
@@ -119,6 +119,8 @@ namespace BBM
|
||||
|
||||
|
||||
public:
|
||||
static void wallCollide(WAL::Entity &entity, const WAL::Entity &wall);
|
||||
|
||||
|
||||
//! @param width Width of the map
|
||||
//! @param height Height of the map
|
||||
|
||||
@@ -71,7 +71,13 @@ namespace BBM
|
||||
}
|
||||
|
||||
template<typename T2>
|
||||
Vector3<T> &operator*=(T2 d)
|
||||
Vector3<T> operator-(const Vector3<T2> &vec) const
|
||||
{
|
||||
return Vector3<T>(this->x - vec.x, this->y - vec.y, this->z - vec.z);
|
||||
}
|
||||
|
||||
template<typename T2>
|
||||
Vector3<T> &operator*=(const T2 d)
|
||||
{
|
||||
this->x *= d;
|
||||
this->y *= d;
|
||||
@@ -80,7 +86,7 @@ namespace BBM
|
||||
}
|
||||
|
||||
template<typename T2>
|
||||
Vector3<T> operator*(T2 d) const
|
||||
Vector3<T> operator*(const T2 d) const
|
||||
{
|
||||
return Vector3<T>(this->x * d, this->y * d, this->z * d);
|
||||
}
|
||||
|
||||
@@ -66,20 +66,8 @@ namespace BBM
|
||||
.addComponent<ControllableComponent>()
|
||||
.addComponent<KeyboardComponent>()
|
||||
.addComponent<AnimationsComponent>(RAY::ModelAnimations("assets/player/player.iqm"), 1)
|
||||
.addComponent<CollisionComponent>(2)
|
||||
.addComponent<CollisionComponent>(1)
|
||||
.addComponent<MovableComponent>();
|
||||
scene->addEntity("cube")
|
||||
.addComponent<PositionComponent>(-5, 0, -5)
|
||||
.addComponent<Drawable3DComponent, RAY3D::Cube>(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED)
|
||||
.addComponent<ControllableComponent>()
|
||||
.addComponent<KeyboardComponent>()
|
||||
.addComponent<CollisionComponent>([](WAL::Entity &, const WAL::Entity &){},
|
||||
[](WAL::Entity &actual, const WAL::Entity &) {
|
||||
try {
|
||||
auto &mov = actual.getComponent<MovableComponent>();
|
||||
mov.resetVelocity();
|
||||
} catch (std::exception &e) { }
|
||||
}, 3);
|
||||
|
||||
scene->addEntity("camera")
|
||||
.addComponent<PositionComponent>(8, 20, 7)
|
||||
|
||||
Reference in New Issue
Block a user