Moving components and systems out of the ecs

This commit is contained in:
Zoe Roux
2021-05-24 15:23:20 +02:00
103 changed files with 7248 additions and 64 deletions
@@ -0,0 +1,22 @@
//
// Created by Zoe Roux on 5/17/21.
//
#include "MovableComponent.hpp"
namespace Bomberman
{
MovableComponent::MovableComponent(WAL::Entity &entity)
: Component(entity)
{}
WAL::Component *MovableComponent::clone(WAL::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 Bomberman
{
//! @brief A component to place on entities that can move or be moved.
class MovableComponent : public WAL::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
WAL::Component *clone(WAL::Entity &entity) const override;
//! @brief Create a new movable component.
explicit MovableComponent(WAL::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;
};
}
@@ -0,0 +1,43 @@
//
// Created by Zoe Roux on 5/17/21.
//
#include "PositionComponent.hpp"
namespace Bomberman
{
PositionComponent::PositionComponent(WAL::Entity &entity)
: Component(entity),
position()
{}
PositionComponent::PositionComponent(WAL::Entity &entity, Vector3f pos)
: Component(entity),
position(pos)
{}
PositionComponent::PositionComponent(WAL::Entity &entity, float x, float y, float z)
: Component(entity),
position(x, y, z)
{}
WAL::Component *PositionComponent::clone(WAL::Entity &entity) const
{
return new PositionComponent(entity, this->position);
}
float PositionComponent::getX() const
{
return this->position.x;
}
float PositionComponent::getY() const
{
return this->position.y;
}
float PositionComponent::getZ() const
{
return this->position.z;
}
}
@@ -0,0 +1,42 @@
//
// Created by Zoe Roux on 5/17/21.
//
#pragma once
#include "Models/Vector3.hpp"
#include "Component/Component.hpp"
namespace Bomberman
{
//! @brief A basic position component
class PositionComponent : public WAL::Component
{
public:
//! @brief Get the editable position of this entity
Vector3f position;
//! @brief Get the X position of this entity.
float getX() const;
//! @brief Get the Y position of this entity.
float getY() const;
//! @brief Get the Z position of this entity.
float getZ() const;
//! @inherit
WAL::Component *clone(WAL::Entity &entity) const override;
//! @brief Create a new PositionComponent linked to a specific entity
explicit PositionComponent(WAL::Entity &entity);
//! @brief Create a new PositionComponent at a certain position
PositionComponent(WAL::Entity &entity, Vector3f pos);
//! @brief Create a new PositionComponent at a certain position
PositionComponent(WAL::Entity &entity, float x, float y, float z);
//! @brief A position component is copy constructable
PositionComponent(const PositionComponent &) = default;
//! @brief A default destructor
~PositionComponent() override = default;
//! @brief A position component is not assignable
PositionComponent &operator=(const PositionComponent &) = delete;
};
}
+167
View File
@@ -0,0 +1,167 @@
//
// Created by Zoe Roux on 5/17/21.
//
#pragma once
#include <iostream>
#include <cmath>
namespace Bomberman
{
//! @brief A Vector3 data type. (templated to allow any kind of vector3)
template<typename T>
class Vector3
{
public:
//! @brief The x value of the vector
T x;
//! @brief The y value of the vector
T y;
//! @brief The y value of the vector
T z;
//! @brief Create a new nil vector3.
Vector3()
: x(0), y(0), z(0)
{}
//! @brief Create a new vector3 representing a specific coordinate.
Vector3(T x, T y, T z)
: x(x), y(y), z(z)
{}
//! @brief A default destructor
~Vector3() = default;
bool operator==(const Vector3<T> &other) const
{
return this->x == other.x && this->y == other.y && this->z == other.z;
}
bool operator!=(const Vector3<T> &other) const
{
return !this->operator==(other);
}
template<typename T2>
Vector3<T> &operator+=(const Vector3<T2> &vec)
{
this->x += vec.x;
this->y += vec.y;
this->z += vec.z;
return *this;
}
template<typename T2>
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 Vector3<T2> &vec)
{
this->x -= vec.x;
this->y -= vec.y;
this->z -= vec.z;
return *this;
}
template<typename T2>
Vector3<T> &operator*=(T2 d)
{
this->x *= d;
this->y *= d;
this->z *= d;
return *this;
}
template<typename T2>
Vector3<T> operator*(T2 d) const
{
return Vector3<T>(this->x * d, this->y * d, this->z * d);
}
template<typename T2>
Vector3<T> operator*(Vector3<T2> &b) const
{
return Vector3<T>(this->x * b.x, this->y * b.y, this->z * b.z);
}
template<typename T2>
Vector3<T> operator/=(Vector3<T2> &b)
{
this->x /= b.x;
this->y /= b.y;
this->z /= b.z;
return this;
}
template<typename T2>
Vector3<T> operator/(Vector3<T2> &b) const
{
return Vector3<T>(this->x / b.x, this->y / b.y, this->z / b.z);
}
template<typename T2>
Vector3<T> operator/=(T2 b)
{
this->x /= b;
this->y /= b;
this->z /= b;
return this;
}
template<typename T2>
Vector3<T> operator/(T2 b) const
{
return Vector3<T>(this->x / b, this->y / b, this->z / b);
}
template<typename T2>
double distance(const Vector3<T2> &o) const
{
return std::sqrt(std::pow(this->x - o.x, 2) + std::pow(this->y - o.y, 2) + std::pow(this->z - o.z, 2));
}
double magnitude() const
{
return (std::sqrt(std::pow(this->x, 2) + std::pow(this->y, 2), std::pow(this->z, 2)));
}
Vector3<T> normalize()
{
double mag = this->magnitude();
this->x /= mag;
this->y /= mag;
this->z /= mag;
return *this;
}
Vector3<T> normalized() const
{
T mag = this->magnitude();
return Vector3<T>(this->x / mag, this->y / mag, this->z / mag);
}
Vector3<T> projection(const Vector3<T> &point) const
{
return (point * this) / std::pow(this->magnitude(), 2) * this;
}
};
typedef Vector3<float> Vector3f;
typedef Vector3<unsigned> Vector3u;
typedef Vector3<int> Vector3i;
}
template<typename T>
std::ostream &operator<<(std::ostream &s, const Bomberman::Vector3<T> &v)
{
s << "Vector3<" << typeid(T).name() << ">("<< v.x << ", " << v.y << ", " << v.z << ")";
return s;
}
+28
View File
@@ -0,0 +1,28 @@
//
// Created by Zoe Roux on 5/17/21.
//
#include "Component/Position/PositionComponent.hpp"
#include "MovableSystem.hpp"
#include "Component/Movable/MovableComponent.hpp"
#include "Wal.hpp"
namespace Bomberman
{
MovableSystem::MovableSystem()
: System({
typeid(MovableComponent),
typeid(PositionComponent)
})
{}
void MovableSystem::onFixedUpdate(WAL::Entity &entity)
{
auto &movable = entity.getComponent<MovableComponent>();
auto &position = entity.getComponent<PositionComponent>();
position.position += movable._velocity * WAL::Wal::timestep.count();
movable._velocity = movable._acceleration * WAL::Wal::timestep.count();
movable._acceleration = Vector3f();
}
}
+28
View File
@@ -0,0 +1,28 @@
//
// Created by Zoe Roux on 5/17/21.
//
#pragma once
#include "System/System.hpp"
namespace Bomberman
{
//! @brief A system to handle movable entities. This system update velocity based on accelerations and positions based on velocity.
class MovableSystem : public WAL::System
{
public:
//! @inherit
void onFixedUpdate(WAL::Entity &entity) override;
//! @brief A default constructor
MovableSystem();
//! @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;
};
}
+14
View File
@@ -0,0 +1,14 @@
/*
** EPITECH PROJECT, 2021
** Bomberman
** File description:
** Vector
*/
#include "Vector/Vector3.hpp"
#include "Models/Vector3.hpp"
RAY::Vector3 toRAY(const WAL::Vector3f &wal)
{
return RAY::Vector3(wal.x, wal.y, wal.y);
}
+84
View File
@@ -9,6 +9,89 @@
#include <iostream>
#include "Runner/Runner.hpp"
// Dependencies of the demo
#include "Camera/Camera3D.hpp"
#include "Controllers/Keyboard.hpp"
#include "Drawables/2D/Text.hpp"
#include "Drawables/3D/Grid.hpp"
#include "Drawables/Texture.hpp"
#include "Model/Model.hpp"
#include "Model/ModelAnimations.hpp"
#include "Vector/Vector3.hpp"
#include "Window.hpp"
int demo()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
RAY::Window &window = RAY::Window::getInstance(screenWidth, screenHeight, "Bidibidibop", FLAG_WINDOW_RESIZABLE);
RAY::Camera::Camera3D camera(RAY::Vector3(10.0f, 10.0f, 10.0f),
RAY::Vector3(0.0f, 0.0f, 0.0f),
RAY::Vector3(0.0f, 1.0f, 0.0f),
45.0f, CAMERA_PERSPECTIVE
);
RAY::Model model("assets/guy.iqm");
RAY::Texture texture("assets/guytex.png");
RAY::ModelAnimations animations("assets/guy.iqm");
RAY::Drawables::Drawables3D::Grid grid(10, 1.0f);
RAY::Drawables::Drawables2D::Text instructionText("PRESS SPACE to PLAY MODEL ANIMATION", 10, {10, 20} , MAROON);
model.setTextureToMaterial(MAP_DIFFUSE, texture);
RAY::Vector3 position(0.0f, 0.0f, 0.0f); // Set model position
camera.setMode(CAMERA_FREE); // Set free camera mode
window.setFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!window.shouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
camera.update();
// Play animation when spacebar is held down
if (RAY::Controller::Keyboard::isDown(KEY_SPACE))
{
animations[0].incrementFrameCounter();
model.setAnimation(animations[0]);
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
window.setDrawingState(RAY::Window::DRAWING);
window.clear();
window.useCamera(camera);
window.draw(model, position, RAY::Vector3(1.0f, 0.0f, 0.0f), -90.0f, RAY::Vector3( 1.0f, 1.0f, 1.0f ));
window.draw(grid);
window.unuseCamera();
window.draw(instructionText);
window.setDrawingState(RAY::Window::IDLE);
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
window.close(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
void usage(const std::string &bin)
{
std::cout << "Bomberman." << std::endl
@@ -23,5 +106,6 @@ int main(int argc, char **argv)
usage(argv[0]);
return 1;
}
return demo();
return Bomberman::run();
}