diff options
Diffstat (limited to 'src/crepe/api')
28 files changed, 521 insertions, 121 deletions
diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 42add50..1e24ae8 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -10,7 +10,7 @@ namespace crepe { class Sound; //! Audio source component -class AudioSource : Component { +class AudioSource : public Component { public: AudioSource(std::unique_ptr<Asset> audio_clip); virtual ~AudioSource() = default; diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index 21638f4..6b1fec7 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -5,12 +5,9 @@ #include "../Component.h" namespace crepe { + class ScriptSystem; class ComponentManager; -} // namespace crepe - -namespace crepe { - class Script; class BehaviorScript : public Component { diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index f55bd9e..3b20142 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -4,7 +4,6 @@ target_sources(crepe PUBLIC Script.cpp GameObject.cpp Rigidbody.cpp - Force.cpp ParticleEmitter.cpp Transform.cpp Color.cpp @@ -13,6 +12,10 @@ target_sources(crepe PUBLIC Sprite.cpp SaveManager.cpp Config.cpp + Metadata.cpp + Scene.cpp + SceneManager.cpp + Vector2.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -25,10 +28,14 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES GameObject.hpp Rigidbody.h Sprite.h - Point.h + Vector2.h Color.h Texture.h AssetManager.h AssetManager.hpp SaveManager.h + Scene.h + Metadata.h + SceneManager.h + SceneManager.hpp ) diff --git a/src/crepe/api/CircleCollider.h b/src/crepe/api/CircleCollider.h index 931b012..caa7e43 100644 --- a/src/crepe/api/CircleCollider.h +++ b/src/crepe/api/CircleCollider.h @@ -5,7 +5,7 @@ namespace crepe { class CircleCollider : public Collider { public: - CircleCollider(uint32_t game_object_id, int radius) + CircleCollider(game_object_id_t game_object_id, int radius) : Collider(game_object_id), radius(radius) {} int radius; }; diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index bbcca87..56e3af5 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -47,6 +47,16 @@ public: */ std::string location = "save.crepe.db"; } savemgr; + + //! physics-related settings + struct { + /** + * \brief gravity value of physics system + * + * Gravity value of game. + */ + double gravity = 1; + } physics; }; } // namespace crepe diff --git a/src/crepe/api/Force.cpp b/src/crepe/api/Force.cpp deleted file mode 100644 index 3c33ad3..0000000 --- a/src/crepe/api/Force.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include <cmath> - -#include "Force.h" - -namespace crepe { - -Force::Force(uint32_t game_object_id, uint32_t magnitude, uint32_t direction) - : Component(game_object_id) { - // TODO: A standard angle unit should be established for the entire engine - // and assumed to be the default everywhere. Only conversion functions should - // explicitly contain the unit (i.e. `deg_to_rad()` & `rad_to_deg()`) - - // Convert direction from degrees to radians - float radian_direction = static_cast<float>(direction) * (M_PI / 180.0f); - force_x = static_cast<int32_t>( - std::round(magnitude * std::cos(radian_direction))); - force_y = static_cast<int32_t>( - std::round(magnitude * std::sin(radian_direction))); -} - -} // namespace crepe diff --git a/src/crepe/api/Force.h b/src/crepe/api/Force.h deleted file mode 100644 index c08a8b9..0000000 --- a/src/crepe/api/Force.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include <cstdint> - -#include "../Component.h" - -namespace crepe { - -class Force : public Component { -public: - Force(uint32_t game_object_id, uint32_t magnitude, uint32_t direction); - - int32_t force_x; - int32_t force_y; -}; - -} // namespace crepe diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index 445a60d..d252e77 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -1,7 +1,31 @@ +#include "api/Transform.h" + #include "GameObject.h" +#include "Metadata.h" using namespace crepe; using namespace std; -GameObject::GameObject(uint32_t id, string name, string tag, int layer) - : id(id), name(name), tag(tag), active(true), layer(layer) {} +GameObject::GameObject(game_object_id_t id, const std::string & name, + const std::string & tag, const Vector2 & position, + double rotation, double scale) + : id(id) { + // Add Transform and Metadata components + ComponentManager & mgr = ComponentManager::get_instance(); + mgr.add_component<Transform>(this->id, position, rotation, scale); + mgr.add_component<Metadata>(this->id, name, tag); +} + +void GameObject::set_parent(const GameObject & parent) { + ComponentManager & mgr = ComponentManager::get_instance(); + + // Set parent on own Metadata component + vector<reference_wrapper<Metadata>> this_metadata + = mgr.get_components_by_id<Metadata>(this->id); + this_metadata.at(0).get().parent = parent.id; + + // Add own id to children list of parent's Metadata component + vector<reference_wrapper<Metadata>> parent_metadata + = mgr.get_components_by_id<Metadata>(parent.id); + parent_metadata.at(0).get().children.push_back(this->id); +} diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index b5d6399..d703730 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -1,22 +1,63 @@ #pragma once -#include <cstdint> #include <string> +#include "types.h" + namespace crepe { +class Vector2; + +/** + * \brief Represents a GameObject + * + * This class represents a GameObject. The GameObject class is only used + * as an interface for the game programmer. The actual implementation is + * done in the ComponentManager. + */ class GameObject { public: - GameObject(uint32_t id, std::string name, std::string tag, int layer); - + /** + * This constructor creates a new GameObject. It creates a new + * Transform and Metadata component and adds them to the ComponentManager. + * + * \param id The id of the GameObject + * \param name The name of the GameObject + * \param tag The tag of the GameObject + * \param position The position of the GameObject + * \param rotation The rotation of the GameObject + * \param scale The scale of the GameObject + */ + GameObject(game_object_id_t id, const std::string & name, + const std::string & tag, const Vector2 & position, + double rotation, double scale); + /** + * \brief Set the parent of this GameObject + * + * This method sets the parent of this GameObject. It sets the parent + * in the Metadata component of this GameObject and adds this GameObject + * to the children list of the parent GameObject. + * + * \param parent The parent GameObject + */ + void set_parent(const GameObject & parent); + /** + * \brief Add a component to the GameObject + * + * This method adds a component to the GameObject. It forwards the + * arguments to the ComponentManager. + * + * \tparam T The type of the component + * \tparam Args The types of the arguments + * \param args The arguments to create the component + * \return The created component + */ template <typename T, typename... Args> T & add_component(Args &&... args); - uint32_t id; - std::string name; - std::string tag; - bool active; - int layer; +public: + //! The id of the GameObject + const game_object_id_t id; }; } // namespace crepe diff --git a/src/crepe/api/GameObject.hpp b/src/crepe/api/GameObject.hpp index 77cf40e..bfba7fe 100644 --- a/src/crepe/api/GameObject.hpp +++ b/src/crepe/api/GameObject.hpp @@ -8,7 +8,7 @@ namespace crepe { template <typename T, typename... Args> T & GameObject::add_component(Args &&... args) { - auto & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = ComponentManager::get_instance(); return mgr.add_component<T>(this->id, std::forward<Args>(args)...); } diff --git a/src/crepe/api/Metadata.cpp b/src/crepe/api/Metadata.cpp new file mode 100644 index 0000000..76f11d7 --- /dev/null +++ b/src/crepe/api/Metadata.cpp @@ -0,0 +1,7 @@ +#include "Metadata.h" + +using namespace crepe; +using namespace std; + +Metadata::Metadata(game_object_id_t id, const string & name, const string & tag) + : Component(id), name(name), tag(tag) {} diff --git a/src/crepe/api/Metadata.h b/src/crepe/api/Metadata.h new file mode 100644 index 0000000..c61e006 --- /dev/null +++ b/src/crepe/api/Metadata.h @@ -0,0 +1,43 @@ +#pragma once + +#include <string> +#include <vector> + +#include "../Component.h" + +namespace crepe { + +/** + * \brief Metadata component + * + * This class represents the Metadata component. It stores the name, tag, parent + * and children of a GameObject. + */ +class Metadata : public Component { +public: + /** + * \param game_object_id The id of the GameObject this component belongs to + * \param name The name of the GameObject + * \param tag The tag of the GameObject + */ + Metadata(game_object_id_t id, const std::string & name, + const std::string & tag); + /** + * \brief Get the maximum number of instances for this component + * + * \return The maximum number of instances for this component + */ + virtual int get_instances_max() const { return 1; } + +public: + //! The name of the GameObject + const std::string name; + //! The tag of the GameObject + const std::string tag; + //! The id of the parent GameObject (-1 if no parent) + game_object_id_t parent = -1; + //! The ids of the children GameObjects + std::vector<game_object_id_t> children; +}; + +} // namespace crepe diff --git a/src/crepe/api/ParticleEmitter.cpp b/src/crepe/api/ParticleEmitter.cpp index 0b3a9ee..3b2e2f2 100644 --- a/src/crepe/api/ParticleEmitter.cpp +++ b/src/crepe/api/ParticleEmitter.cpp @@ -6,15 +6,14 @@ using namespace crepe; -ParticleEmitter::ParticleEmitter(uint32_t game_object_id, - uint32_t max_particles, uint32_t emission_rate, - uint32_t speed, uint32_t speed_offset, - uint32_t angle, uint32_t angleOffset, - float begin_lifespan, float end_lifespan) - : Component(game_object_id), max_particles(max_particles), - emission_rate(emission_rate), speed(speed), speed_offset(speed_offset), - position{0, 0}, begin_lifespan(begin_lifespan), - end_lifespan(end_lifespan) { +ParticleEmitter::ParticleEmitter(game_object_id_t id, uint32_t max_particles, + uint32_t emission_rate, uint32_t speed, + uint32_t speed_offset, uint32_t angle, + uint32_t angleOffset, float begin_lifespan, + float end_lifespan) + : Component(id), max_particles(max_particles), emission_rate(emission_rate), + speed(speed), speed_offset(speed_offset), position{0, 0}, + begin_lifespan(begin_lifespan), end_lifespan(end_lifespan) { std::srand( static_cast<uint32_t>(std::time(nullptr))); // initialize random seed std::cout << "Create emitter" << std::endl; diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h index 2e2e95b..5939723 100644 --- a/src/crepe/api/ParticleEmitter.h +++ b/src/crepe/api/ParticleEmitter.h @@ -10,7 +10,7 @@ namespace crepe { class ParticleEmitter : public Component { public: - ParticleEmitter(uint32_t game_object_id, uint32_t max_particles, + ParticleEmitter(game_object_id_t id, uint32_t max_particles, uint32_t emission_rate, uint32_t speed, uint32_t speed_offset, uint32_t angle, uint32_t angleOffset, float begin_lifespan, float end_lifespan); diff --git a/src/crepe/api/Point.h b/src/crepe/api/Point.h deleted file mode 100644 index 575d624..0000000 --- a/src/crepe/api/Point.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -namespace crepe { - -class Point { -public: - double x; - double y; -}; - -} // namespace crepe diff --git a/src/crepe/api/Rigidbody.cpp b/src/crepe/api/Rigidbody.cpp index 0a6262a..cbf1325 100644 --- a/src/crepe/api/Rigidbody.cpp +++ b/src/crepe/api/Rigidbody.cpp @@ -2,7 +2,13 @@ using namespace crepe; -Rigidbody::Rigidbody(uint32_t game_object_id, int mass, int gravity_scale, - BodyType bodyType) - : Component(game_object_id), mass(mass), gravity_scale(gravity_scale), - body_type(bodyType) {} +crepe::Rigidbody::Rigidbody(uint32_t game_object_id, const Data & data) + : Component(game_object_id), data(data) {} + +void crepe::Rigidbody::add_force_linear(const Vector2 & force) { + this->data.linear_velocity += force; +} + +void crepe::Rigidbody::add_force_angular(double force) { + this->data.angular_velocity += force; +} diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h index 518ed94..68481f4 100644 --- a/src/crepe/api/Rigidbody.h +++ b/src/crepe/api/Rigidbody.h @@ -4,27 +4,101 @@ #include "../Component.h" -namespace crepe { +#include "Vector2.h" -// FIXME: can't this enum be defined inside the class declaration of Rigidbody? -enum class BodyType { - //! Does not move (e.g. walls, ground ...) - STATIC, - //! Moves and responds to forces (e.g. player, physics objects ...) - DYNAMIC, - //! Moves but does not respond to forces (e.g. moving platforms ...) - KINEMATIC, -}; +namespace crepe { +/** + * \brief Rigidbody class + * + * This class is used by the physics sytem and collision system. + * It configures how to system interact with the gameobject for movement and collisions. + */ class Rigidbody : public Component { public: - Rigidbody(uint32_t game_object_id, int mass, int gravity_scale, - BodyType body_type); - int32_t velocity_x; - int32_t velocity_y; - int mass; - int gravity_scale; - BodyType body_type; + /** + * \brief BodyType enum + * + * This enum provides three bodytypes the physics sytem and collision system use. + */ + enum class BodyType { + //! Does not move (e.g. walls, ground ...) + STATIC, + //! Moves and responds to forces (e.g. player, physics objects ...) + DYNAMIC, + //! Moves but does not respond to forces (e.g. moving platforms ...) + KINEMATIC, + }; + /** + * \brief PhysicsConstraints to constrain movement + * + * This struct configures the movement constraint for this object. + * If a constraint is enabled the systems will not move the object. + */ + struct PhysicsConstraints { + //! X constraint + bool x = false; + //! Y constraint + bool y = false; + //! rotation constraint + bool rotation = false; + }; + +public: + /** + * \brief struct for Rigidbody data + * + * This struct holds the data for the Rigidbody. + */ + struct Data { + //! objects mass + double mass = 0.0; + //! gravtiy scale + double gravity_scale = 0.0; + //! Changes if physics apply + BodyType body_type = BodyType::DYNAMIC; + //! linear velocity of object + Vector2 linear_velocity; + //! maximum linear velocity of object + Vector2 max_linear_velocity; + //! linear damping of object + Vector2 linear_damping; + //! angular velocity of object + double angular_velocity = 0.0; + //! max angular velocity of object + double max_angular_velocity = 0.0; + //! angular damping of object + double angular_damping = 0.0; + //! movements constraints of object + PhysicsConstraints constraints; + //! if gravity applies + bool use_gravity = true; + //! if object bounces + bool bounce = false; + }; + +public: + /** + * \param game_object_id id of the gameobject the rigibody is added to. + * \param data struct to configure the rigidbody. + */ + Rigidbody(uint32_t game_object_id, const Data & data); + //! struct to hold data of rigidbody + Data data; + +public: + /** + * \brief add a linear force to the Rigidbody. + * + * \param force Vector2 that is added to the linear force. + */ + void add_force_linear(const Vector2 & force); + /** + * \brief add a angular force to the Rigidbody. + * + * \param force Vector2 that is added to the angular force. + */ + void add_force_angular(double force); }; } // namespace crepe diff --git a/src/crepe/api/Scene.cpp b/src/crepe/api/Scene.cpp new file mode 100644 index 0000000..933edf4 --- /dev/null +++ b/src/crepe/api/Scene.cpp @@ -0,0 +1,5 @@ +#include "Scene.h" + +using namespace crepe; + +Scene::Scene(const std::string & name) : name(name) {} diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h new file mode 100644 index 0000000..f8bcc3d --- /dev/null +++ b/src/crepe/api/Scene.h @@ -0,0 +1,17 @@ +#pragma once + +#include <string> + +namespace crepe { + +class Scene { +public: + Scene(const std::string & name); + virtual ~Scene() = default; + virtual void load_scene() = 0; + +public: + std::string name; +}; + +} // namespace crepe diff --git a/src/crepe/api/SceneManager.cpp b/src/crepe/api/SceneManager.cpp new file mode 100644 index 0000000..dfed6ee --- /dev/null +++ b/src/crepe/api/SceneManager.cpp @@ -0,0 +1,38 @@ +#include <algorithm> +#include <memory> + +#include "../ComponentManager.h" + +#include "SceneManager.h" + +using namespace crepe; +using namespace std; + +SceneManager & SceneManager::get_instance() { + static SceneManager instance; + return instance; +} + +void SceneManager::set_next_scene(const string & name) { next_scene = name; } + +void SceneManager::load_next_scene() { + // next scene not set + if (this->next_scene.empty()) return; + + auto it + = find_if(this->scenes.begin(), this->scenes.end(), + [&next_scene = this->next_scene](unique_ptr<Scene> & scene) { + return scene->name == next_scene; + }); + + // next scene not found + if (it == this->scenes.end()) return; + unique_ptr<Scene> & scene = *it; + + // Delete all components of the current scene + ComponentManager & mgr = ComponentManager::get_instance(); + mgr.delete_all_components(); + + // Load the new scene + scene->load_scene(); +} diff --git a/src/crepe/api/SceneManager.h b/src/crepe/api/SceneManager.h new file mode 100644 index 0000000..1e0e670 --- /dev/null +++ b/src/crepe/api/SceneManager.h @@ -0,0 +1,50 @@ +#pragma once + +#include <memory> +#include <queue> +#include <vector> + +#include "Scene.h" + +namespace crepe { + +class SceneManager { +public: + // Singleton + static SceneManager & get_instance(); + SceneManager(const SceneManager &) = delete; + SceneManager(SceneManager &&) = delete; + SceneManager & operator=(const SceneManager &) = delete; + SceneManager & operator=(SceneManager &&) = delete; + +public: + /** + * \brief Add a new concrete scene to the scene manager + * + * \tparam T Type of concrete scene + * \param name Name of new scene + */ + template <typename T> + void add_scene(const std::string & name); + /** + * \brief Set the next scene + * + * This scene will be loaded at the end of the frame + * + * \param name Name of the next scene + */ + void set_next_scene(const std::string & name); + //! Load a new scene (if there is one) + void load_next_scene(); + +private: + SceneManager() = default; + +private: + std::vector<std::unique_ptr<Scene>> scenes; + std::string next_scene; +}; + +} // namespace crepe + +#include "SceneManager.hpp" diff --git a/src/crepe/api/SceneManager.hpp b/src/crepe/api/SceneManager.hpp new file mode 100644 index 0000000..8bad7b2 --- /dev/null +++ b/src/crepe/api/SceneManager.hpp @@ -0,0 +1,18 @@ +#include "SceneManager.h" + +namespace crepe { + +template <typename T> +void SceneManager::add_scene(const std::string & name) { + static_assert(std::is_base_of<Scene, T>::value, + "T must be derived from Scene"); + + scenes.emplace_back(make_unique<T>(name)); + + // The first scene added, is the one that will be loaded at the beginning + if (next_scene.empty()) { + next_scene = name; + } +} + +} // namespace crepe diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 3dd44f2..d3465c7 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -10,8 +10,8 @@ using namespace std; using namespace crepe; -Sprite::Sprite(uint32_t id, shared_ptr<Texture> image, const Color & color, - const FlipSettings & flip) +Sprite::Sprite(game_object_id_t id, shared_ptr<Texture> image, + const Color & color, const FlipSettings & flip) : Component(id), color(color), flip(flip), sprite_image(image) { dbg_trace(); } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index bdb4da9..00dcb27 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -12,14 +12,14 @@ namespace crepe { struct FlipSettings { - bool flip_x = 1; - bool flip_y = 1; + bool flip_x = true; + bool flip_y = true; }; class Sprite : public Component { public: - Sprite(uint32_t game_id, std::shared_ptr<Texture> image, + Sprite(game_object_id_t id, std::shared_ptr<Texture> image, const Color & color, const FlipSettings & flip); ~Sprite(); std::shared_ptr<Texture> sprite_image; diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index 70b16bd..a244bc5 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -1,16 +1,11 @@ -#include <cstdint> - -#include "api/Point.h" #include "util/log.h" -#include "Component.h" #include "Transform.h" using namespace crepe; -Transform::Transform(uint32_t game_id, const Point & point, double rot, - double scale) - : Component(game_id), position(point), rotation(rot), scale(scale) { +Transform::Transform(game_object_id_t id, const Vector2 & point, + double rotation, double scale) + : Component(id), position(point), rotation(rotation), scale(scale) { dbg_trace(); } -Transform::~Transform() { dbg_trace(); } diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index d416088..d7a5b8a 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -1,23 +1,37 @@ #pragma once -#include <cstdint> - -#include "api/Point.h" +#include "api/Vector2.h" #include "Component.h" namespace crepe { +/** + * \brief Transform component + * + * This class represents the Transform component. It stores the position, + * rotation and scale of a GameObject. + */ class Transform : public Component { - // FIXME: What's the difference between the `Point` and `Position` - // classes/structs? How about we replace both with a universal `Vec2` that - // works similar (or the same) as those found in GLSL? +public: + /** + * \param id The id of the GameObject this component belongs to + * \param point The position of the GameObject + * \param rotation The rotation of the GameObject + * \param scale The scale of the GameObject + */ + Transform(game_object_id_t id, const Vector2 & point, double rotation, + double scale); + /** + * \brief Get the maximum number of instances for this component + * + * \return The maximum number of instances for this component + */ + virtual int get_instances_max() const { return 1; } public: - Transform(uint32_t id, const Point &, double, double); - ~Transform(); //! Translation (shift) - Point position; + Vector2 position; //! Rotation, in radians double rotation; //! Multiplication factor diff --git a/src/crepe/api/Vector2.cpp b/src/crepe/api/Vector2.cpp new file mode 100644 index 0000000..09bb59b --- /dev/null +++ b/src/crepe/api/Vector2.cpp @@ -0,0 +1,57 @@ +#include "Vector2.h" + +namespace crepe { + +// Constructor with initial values +Vector2::Vector2(float x, float y) : x(x), y(y) {} + +// Subtracts another vector from this vector and returns the result. +Vector2 Vector2::operator-(const Vector2 & other) const { + return {x - other.x, y - other.y}; +} + +// Adds another vector to this vector and returns the result. +Vector2 Vector2::operator+(const Vector2 & other) const { + return {x + other.x, y + other.y}; +} + +// Multiplies this vector by a scalar and returns the result. +Vector2 Vector2::operator*(float scalar) const { + return {x * scalar, y * scalar}; +} + +// Multiplies this vector by another vector element-wise and updates this vector. +Vector2 & Vector2::operator*=(const Vector2 & other) { + x *= other.x; + y *= other.y; + return *this; +} + +// Adds another vector to this vector and updates this vector. +Vector2 & Vector2::operator+=(const Vector2 & other) { + x += other.x; + y += other.y; + return *this; +} + +// Adds a scalar value to both components of this vector and updates this vector. +Vector2 & Vector2::operator+=(float other) { + x += other; + y += other; + return *this; +} + +// Returns the negation of this vector. +Vector2 Vector2::operator-() const { return {-x, -y}; } + +// Checks if this vector is equal to another vector. +bool Vector2::operator==(const Vector2 & other) const { + return x == other.x && y == other.y; +} + +// Checks if this vector is not equal to another vector. +bool Vector2::operator!=(const Vector2 & other) const { + return !(*this == other); +} + +} // namespace crepe diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h new file mode 100644 index 0000000..741951b --- /dev/null +++ b/src/crepe/api/Vector2.h @@ -0,0 +1,47 @@ +#pragma once + +namespace crepe { + +//! Vector2 class +class Vector2 { +public: + //! X component of the vector + float x; + //! Y component of the vector + float y; + + //! Default constructor + Vector2() = default; + + //! Constructor with initial values + Vector2(float x, float y); + + //! Subtracts another vector from this vector and returns the result. + Vector2 operator-(const Vector2 & other) const; + + //! Adds another vector to this vector and returns the result. + Vector2 operator+(const Vector2 & other) const; + + //! Multiplies this vector by a scalar and returns the result. + Vector2 operator*(float scalar) const; + + //! Multiplies this vector by another vector element-wise and updates this vector. + Vector2 & operator*=(const Vector2 & other); + + //! Adds another vector to this vector and updates this vector. + Vector2 & operator+=(const Vector2 & other); + + //! Adds a scalar value to both components of this vector and updates this vector. + Vector2 & operator+=(float other); + + //! Returns the negation of this vector. + Vector2 operator-() const; + + //! Checks if this vector is equal to another vector. + bool operator==(const Vector2 & other) const; + + //! Checks if this vector is not equal to another vector. + bool operator!=(const Vector2 & other) const; +}; + +} // namespace crepe |