mirror of
https://github.com/zoriya/Bomberman.git
synced 2025-12-21 05:45:10 +00:00
Adding a position componentn and a vector3
This commit is contained in:
@@ -19,6 +19,20 @@ add_library(wal
|
||||
sources/Events/EventManager.hpp
|
||||
sources/Exception/WalError.cpp
|
||||
sources/Exception/WalError.hpp
|
||||
sources/Entity/Entity.cpp sources/Component/Component.cpp)
|
||||
sources/Entity/Entity.cpp
|
||||
sources/Component/Component.cpp
|
||||
sources/Component/Basics/PositionComponent.cpp
|
||||
sources/Component/Basics/PositionComponent.hpp
|
||||
sources/Models/Vector2.hpp
|
||||
sources/Models/Vector3.hpp
|
||||
)
|
||||
|
||||
target_include_directories(wal PUBLIC sources)
|
||||
|
||||
add_executable(wal_tests
|
||||
tests/EntityTests.cpp
|
||||
tests/MainTest.cpp)
|
||||
|
||||
target_link_libraries(wal_tests PRIVATE wal)
|
||||
find_package(Catch2 REQUIRED)
|
||||
target_link_libraries(wal_tests PRIVATE Catch2::Catch2)
|
||||
48
lib/wal/sources/Component/Basics/PositionComponent.cpp
Normal file
48
lib/wal/sources/Component/Basics/PositionComponent.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// Created by Zoe Roux on 5/17/21.
|
||||
//
|
||||
|
||||
#include "PositionComponent.hpp"
|
||||
|
||||
namespace WAL::Components
|
||||
{
|
||||
PositionComponent::PositionComponent(Entity &entity)
|
||||
: Component(entity),
|
||||
_position()
|
||||
{}
|
||||
|
||||
PositionComponent::PositionComponent(float x, float y, float z, Entity &entity)
|
||||
: Component(entity),
|
||||
_position(x, y, z)
|
||||
{}
|
||||
|
||||
Component *PositionComponent::clone(WAL::Entity &entity) const
|
||||
{
|
||||
return new PositionComponent(entity);
|
||||
}
|
||||
|
||||
Vector3f &PositionComponent::getPosition()
|
||||
{
|
||||
return this->_position;
|
||||
}
|
||||
|
||||
const Vector3f &PositionComponent::getPosition() const
|
||||
{
|
||||
return 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;
|
||||
}
|
||||
}
|
||||
45
lib/wal/sources/Component/Basics/PositionComponent.hpp
Normal file
45
lib/wal/sources/Component/Basics/PositionComponent.hpp
Normal file
@@ -0,0 +1,45 @@
|
||||
//
|
||||
// Created by Zoe Roux on 5/17/21.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Models/Vector3.hpp"
|
||||
#include "Component/Component.hpp"
|
||||
|
||||
namespace WAL::Components
|
||||
{
|
||||
//! @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;
|
||||
|
||||
//! @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
|
||||
Component *clone(Entity &entity) const override;
|
||||
|
||||
//! @brief Create a new PositionComponent linked to a specific entity
|
||||
explicit PositionComponent(Entity &entity);
|
||||
//! @brief Create a new PositionComponent at a certain position
|
||||
PositionComponent(float x, float y, float z, Entity &entity);
|
||||
//! @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;
|
||||
};
|
||||
}
|
||||
@@ -6,16 +6,10 @@
|
||||
|
||||
namespace WAL
|
||||
{
|
||||
Component::Component(std::string name, Entity &entity)
|
||||
: _name(std::move(name)),
|
||||
_entity(entity)
|
||||
Component::Component(Entity &entity)
|
||||
: _entity(entity)
|
||||
{ }
|
||||
|
||||
std::string Component::getName() const
|
||||
{
|
||||
return this->_name;
|
||||
}
|
||||
|
||||
bool Component::isDisabled() const
|
||||
{
|
||||
return this->_disabled;
|
||||
|
||||
@@ -16,8 +16,6 @@ namespace WAL
|
||||
class Component
|
||||
{
|
||||
private:
|
||||
//! @brief The name of this component
|
||||
std::string _name;
|
||||
//! @brief Is this component disabled?
|
||||
bool _disabled = false;
|
||||
protected:
|
||||
@@ -27,7 +25,7 @@ namespace WAL
|
||||
std::vector<std::type_index> _dependencies;
|
||||
|
||||
//! @brief A component can't be instantiated, it should be derived.
|
||||
explicit Component(std::string name, Entity &entity);
|
||||
explicit Component(Entity &entity);
|
||||
//! @brief A component can't be instantiated, it should be derived.
|
||||
Component(const Component &) = default;
|
||||
public:
|
||||
@@ -40,9 +38,6 @@ namespace WAL
|
||||
//! @param entity The entity that owns the ne component.
|
||||
virtual Component *clone(Entity &entity) const = 0;
|
||||
|
||||
//! @brief Get the name of this component
|
||||
std::string getName() const;
|
||||
|
||||
//! @brief Used if the component is disabled
|
||||
bool isDisabled() const;
|
||||
//! @brief Disable this component.
|
||||
|
||||
@@ -70,7 +70,7 @@ namespace WAL
|
||||
{
|
||||
if (this->hasComponent<T>())
|
||||
throw DuplicateError("A component of the type \"" + std::string(typeid(T).name()) + "\" already exists.");
|
||||
this->_components.push_back(std::make_unique(params...));
|
||||
this->_components.push_back(std::make_unique<T>(params...));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
157
lib/wal/sources/Models/Vector3.hpp
Normal file
157
lib/wal/sources/Models/Vector3.hpp
Normal file
@@ -0,0 +1,157 @@
|
||||
//
|
||||
// Created by Zoe Roux on 5/17/21.
|
||||
//
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
namespace WAL
|
||||
{
|
||||
//! @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<T>()
|
||||
: x(0), y(0), z(0)
|
||||
{}
|
||||
|
||||
//! @brief Create a new vector3 representing a specific coordinate.
|
||||
Vector3<T>(T x, T y, T z)
|
||||
: x(x), y(y), z(z)
|
||||
{}
|
||||
|
||||
//! @brief A default destructor
|
||||
~Vector3() = default;
|
||||
|
||||
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 Vector2<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 WAL::Vector3<T> &v)
|
||||
{
|
||||
s << v.x << " " << v.y << " " << v.z;
|
||||
return s;
|
||||
}
|
||||
16
lib/wal/tests/EntityTests.cpp
Normal file
16
lib/wal/tests/EntityTests.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// Created by Zoe Roux on 5/17/21.
|
||||
//
|
||||
|
||||
#include <catch2/catch.hpp>
|
||||
#include "Entity/Entity.hpp"
|
||||
#include "Component/Basics/PositionComponent.hpp"
|
||||
|
||||
using namespace WAL;
|
||||
using namespace WAL::Components;
|
||||
|
||||
TEST_CASE("Get component", "[Entity/getComponent]")
|
||||
{
|
||||
Entity entity("Bob");
|
||||
entity.addComponent<PositionComponent>();
|
||||
}
|
||||
6
lib/wal/tests/MainTest.cpp
Normal file
6
lib/wal/tests/MainTest.cpp
Normal file
@@ -0,0 +1,6 @@
|
||||
//
|
||||
// Created by Zoe Roux on 5/17/21.
|
||||
//
|
||||
|
||||
#define CATCH_CONFIG_MAIN
|
||||
#include <catch2/catch.hpp>
|
||||
Reference in New Issue
Block a user