mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-06-12 05:10:37 +00:00
Adding a movable system/component
This commit is contained in:
@@ -21,10 +21,10 @@ add_library(wal
|
||||
sources/Exception/WalError.hpp
|
||||
sources/Entity/Entity.cpp
|
||||
sources/Component/Component.cpp
|
||||
sources/Component/Basics/PositionComponent.cpp
|
||||
sources/Component/Basics/PositionComponent.hpp
|
||||
sources/Component/Position/PositionComponent.cpp
|
||||
sources/Component/Position/PositionComponent.hpp
|
||||
sources/Models/Vector3.hpp
|
||||
)
|
||||
sources/Component/Movable/MovableComponent.cpp sources/Component/Movable/MovableComponent.hpp sources/System/Movable/MovableSystem.cpp sources/System/Movable/MovableSystem.hpp sources/System/System.cpp)
|
||||
|
||||
target_include_directories(wal PUBLIC sources)
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// Created by Zoe Roux on 5/17/21.
|
||||
//
|
||||
|
||||
#include "MovableComponent.hpp"
|
||||
|
||||
namespace WAL::Movable
|
||||
{
|
||||
MovableComponent::MovableComponent(Entity &entity)
|
||||
: Component(entity)
|
||||
{}
|
||||
|
||||
Component *MovableComponent::clone(Entity &entity) const
|
||||
{
|
||||
return new MovableComponent(entity);
|
||||
}
|
||||
|
||||
void MovableComponent::addForce(Vector3f force)
|
||||
{
|
||||
this->_acceleration += force;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
//
|
||||
// Created by Zoe Roux on 5/17/21.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Models/Vector3.hpp"
|
||||
#include "Entity/Entity.hpp"
|
||||
|
||||
namespace WAL
|
||||
{
|
||||
//! @brief A component to place on entities that can move or be moved.
|
||||
class MovableComponent : public Component
|
||||
{
|
||||
private:
|
||||
//! @brief The acceleration of this entity.
|
||||
Vector3f _acceleration;
|
||||
//! @brief The velocity of the entity.
|
||||
Vector3f _velocity;
|
||||
public:
|
||||
//! @brief Add an instant force to this entity.
|
||||
//! @param force The force to add to this entity's acceleration. The force is added instantly and in one go.
|
||||
void addForce(Vector3f force);
|
||||
|
||||
//! @inherit
|
||||
Component *clone(Entity &entity) const override;
|
||||
|
||||
//! @brief Create a new movable component.
|
||||
explicit MovableComponent(Entity &entity);
|
||||
//! @brief A movable component is copy constructable.
|
||||
MovableComponent(const MovableComponent &) = default;
|
||||
//! @brief A default destructor
|
||||
~MovableComponent() override = default;
|
||||
//! @brief A movable component is not assignable.
|
||||
MovableComponent &operator=(const MovableComponent &) = delete;
|
||||
|
||||
friend class MovableSystem;
|
||||
};
|
||||
}
|
||||
+8
-18
@@ -4,50 +4,40 @@
|
||||
|
||||
#include "PositionComponent.hpp"
|
||||
|
||||
namespace WAL::Components
|
||||
namespace WAL
|
||||
{
|
||||
PositionComponent::PositionComponent(Entity &entity)
|
||||
: Component(entity),
|
||||
_position()
|
||||
position()
|
||||
{}
|
||||
|
||||
PositionComponent::PositionComponent(Entity &entity, Vector3f pos)
|
||||
: Component(entity),
|
||||
_position(pos)
|
||||
position(pos)
|
||||
{}
|
||||
|
||||
PositionComponent::PositionComponent(Entity &entity, float x, float y, float z)
|
||||
: Component(entity),
|
||||
_position(x, y, z)
|
||||
position(x, y, z)
|
||||
{}
|
||||
|
||||
Component *PositionComponent::clone(WAL::Entity &entity) const
|
||||
{
|
||||
return new PositionComponent(entity, this->_position);
|
||||
}
|
||||
|
||||
Vector3f &PositionComponent::getPosition()
|
||||
{
|
||||
return this->_position;
|
||||
}
|
||||
|
||||
const Vector3f &PositionComponent::getPosition() const
|
||||
{
|
||||
return this->_position;
|
||||
return new PositionComponent(entity, this->position);
|
||||
}
|
||||
|
||||
float PositionComponent::getX() const
|
||||
{
|
||||
return this->_position.x;
|
||||
return this->position.x;
|
||||
}
|
||||
|
||||
float PositionComponent::getY() const
|
||||
{
|
||||
return this->_position.y;
|
||||
return this->position.y;
|
||||
}
|
||||
|
||||
float PositionComponent::getZ() const
|
||||
{
|
||||
return this->_position.z;
|
||||
return this->position.z;
|
||||
}
|
||||
}
|
||||
+2
-7
@@ -7,19 +7,14 @@
|
||||
#include "Models/Vector3.hpp"
|
||||
#include "Component/Component.hpp"
|
||||
|
||||
namespace WAL::Components
|
||||
namespace WAL
|
||||
{
|
||||
//! @brief A basic position component
|
||||
class PositionComponent : public Component
|
||||
{
|
||||
private:
|
||||
//! @brief The position of the entity as a vector3.
|
||||
Vector3f _position;
|
||||
public:
|
||||
//! @brief Get the editable position of this entity
|
||||
Vector3f &getPosition();
|
||||
//! @brief Get the const position of this entity
|
||||
const Vector3f &getPosition() const;
|
||||
Vector3f position;
|
||||
|
||||
//! @brief Get the X position of this entity.
|
||||
float getX() const;
|
||||
@@ -81,7 +81,7 @@ namespace WAL
|
||||
template<typename T2>
|
||||
Vector3<T> operator*(T2 d) const
|
||||
{
|
||||
return Vector2<T>(this->x * d, this->y * d, this->z * d);
|
||||
return Vector3<T>(this->x * d, this->y * d, this->z * d);
|
||||
}
|
||||
|
||||
template<typename T2>
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by Zoe Roux on 5/17/21.
|
||||
//
|
||||
|
||||
#include "Component/Position/PositionComponent.hpp"
|
||||
#include "System/Movable/MovableSystem.hpp"
|
||||
#include "Component/Movable/MovableComponent.hpp"
|
||||
#include "Wal.hpp"
|
||||
|
||||
namespace WAL
|
||||
{
|
||||
const std::type_info &MovableSystem::getComponent() const
|
||||
{
|
||||
return typeid(MovableComponent);
|
||||
}
|
||||
|
||||
void MovableSystem::onFixedUpdate(Entity &entity)
|
||||
{
|
||||
auto &movable = entity.getComponent<MovableComponent>();
|
||||
auto &position = entity.getComponent<PositionComponent>();
|
||||
|
||||
position.position += movable._velocity * WAL::timestep.count();
|
||||
movable._velocity = movable._acceleration * WAL::timestep.count();
|
||||
movable._acceleration = Vector3f();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
|
||||
//
|
||||
// Created by Zoe Roux on 5/17/21.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "System/System.hpp"
|
||||
|
||||
namespace WAL
|
||||
{
|
||||
//! @brief A system to handle movable entities. This system update velocity based on accelerations and positions based on velocity.
|
||||
class MovableSystem : public System
|
||||
{
|
||||
public:
|
||||
//! @inherit
|
||||
const std::type_info &getComponent() const override;
|
||||
//! @inherit
|
||||
void onFixedUpdate(Entity &entity) override;
|
||||
|
||||
//! @brief A default constructor
|
||||
MovableSystem() = default;
|
||||
//! @brief A movable system is copy constructable
|
||||
MovableSystem(const MovableSystem &) = default;
|
||||
//! @brief A default destructor
|
||||
~MovableSystem() override = default;
|
||||
//! @brief A movable system is assignable.
|
||||
MovableSystem &operator=(const MovableSystem &) = default;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// Created by Zoe Roux on 5/17/21.
|
||||
//
|
||||
|
||||
#include "System.hpp"
|
||||
|
||||
namespace WAL
|
||||
{
|
||||
|
||||
void System::onUpdate(Entity &entity, std::chrono::nanoseconds dtime)
|
||||
{}
|
||||
|
||||
void System::onFixedUpdate(Entity &entity)
|
||||
{}
|
||||
}
|
||||
@@ -17,17 +17,17 @@ namespace WAL
|
||||
System(System &&) = default;
|
||||
|
||||
//! @brief Get the name of the component corresponding to this system.
|
||||
virtual std::type_info &getComponent() const = 0;
|
||||
virtual const std::type_info &getComponent() const = 0;
|
||||
|
||||
//! @brief Update the corresponding component of the given entity
|
||||
//! @param entity The entity to update.
|
||||
//! @param dtime The delta time.
|
||||
virtual void onUpdate(Entity &entity, std::chrono::nanoseconds dtime) = 0;
|
||||
virtual void onUpdate(Entity &entity, std::chrono::nanoseconds dtime);
|
||||
|
||||
//! @brief An alternative of onUpdate that is called every 8ms (120 times per seconds). If the system slow down, it will try to catch up.
|
||||
//! @remark This should be used for Physics, AI and everything that could be imprecise due to float rounding.
|
||||
//! @param entity The entity to update.
|
||||
virtual void onFixedUpdate(Entity &entity) = 0;
|
||||
virtual void onFixedUpdate(Entity &entity);
|
||||
protected:
|
||||
//! @brief A system can't be instantiated, it should be derived.
|
||||
System() = default;
|
||||
|
||||
@@ -9,7 +9,7 @@ using namespace std::chrono_literals;
|
||||
|
||||
namespace WAL
|
||||
{
|
||||
std::chrono::nanoseconds WAL::_timestep = 8ms;
|
||||
std::chrono::nanoseconds WAL::timestep = 8ms;
|
||||
|
||||
SceneManager &WAL::getSceneManger()
|
||||
{
|
||||
@@ -27,8 +27,8 @@ namespace WAL
|
||||
lastTick = now;
|
||||
|
||||
this->_update(dtime);
|
||||
while (dtime > WAL::_timestep) {
|
||||
dtime -= WAL::_timestep;
|
||||
while (dtime > WAL::timestep) {
|
||||
dtime -= WAL::timestep;
|
||||
this->_fixedUpdate();
|
||||
}
|
||||
this->_renderer->render();
|
||||
@@ -41,7 +41,7 @@ namespace WAL
|
||||
|
||||
for (auto &system : this->_systems) {
|
||||
for (auto &entity : entities) {
|
||||
auto &cmp = system->getComponent();
|
||||
const auto &cmp = system->getComponent();
|
||||
if (!entity.hasComponent(cmp))
|
||||
continue;
|
||||
// TODO handle dependencies.
|
||||
|
||||
@@ -32,15 +32,15 @@ namespace WAL
|
||||
//! @brief True if the engine should close after the end of the current tick.
|
||||
bool _shouldClose = false;
|
||||
|
||||
//! @brief The time between each fixed update.
|
||||
static std::chrono::nanoseconds _timestep;
|
||||
|
||||
//! @brief Call the onUpdate of every system with every component
|
||||
void _update(std::chrono::nanoseconds dtime);
|
||||
|
||||
//! @brief Call the onFixedUpdate of every system with every component
|
||||
void _fixedUpdate();
|
||||
public:
|
||||
//! @brief The time between each fixed update.
|
||||
static std::chrono::nanoseconds timestep;
|
||||
|
||||
//! @brief Create a new system in place.
|
||||
//! @return The wal instance used to call this function is returned. This allow method chaining.
|
||||
template<typename T, class ...Types>
|
||||
|
||||
@@ -4,11 +4,10 @@
|
||||
|
||||
#include "tests.hpp"
|
||||
#include "Entity/Entity.hpp"
|
||||
#include "Component/Basics/PositionComponent.hpp"
|
||||
#include "Component/Position/PositionComponent.hpp"
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
using namespace WAL;
|
||||
using namespace WAL::Components;
|
||||
|
||||
TEST_CASE("Component", "[Entity]")
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user